Skip to content

Commit 4a5299e

Browse files
authored
Merge pull request github#3843 from geoffw0/qldoc6
C++: Bit more QLDoc
2 parents 224289c + 3bdfab8 commit 4a5299e

File tree

5 files changed

+98
-25
lines changed

5 files changed

+98
-25
lines changed

cpp/ql/src/semmle/code/cpp/commons/Synchronization.qll

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ abstract class MutexType extends Type {
2323
abstract predicate trylockAccess(FunctionCall fc, Expr arg);
2424

2525
/**
26-
* Holds if `fc` is a call that unlocks mutex `arg`
27-
* of this type.
26+
* Holds if `fc` is a call that unlocks mutex `arg` of this type.
2827
*/
2928
abstract predicate unlockAccess(FunctionCall fc, Expr arg);
3029

@@ -38,53 +37,52 @@ abstract class MutexType extends Type {
3837
}
3938

4039
/**
41-
* Holds if `fc` is a call that locks or tries to lock any
42-
* mutex of this type.
40+
* Gets a call that locks or tries to lock any mutex of this type.
4341
*/
4442
FunctionCall getLockAccess() {
4543
result = getMustlockAccess() or
4644
result = getTrylockAccess()
4745
}
4846

4947
/**
50-
* Holds if `fc` is a call that always locks any mutex of this type.
48+
* Gets a call that always locks any mutex of this type.
5149
*/
5250
FunctionCall getMustlockAccess() { this.mustlockAccess(result, _) }
5351

5452
/**
55-
* Holds if `fc` is a call that tries to lock any mutex of this type,
53+
* Gets a call that tries to lock any mutex of this type,
5654
* by may return without success.
5755
*/
5856
FunctionCall getTrylockAccess() { this.trylockAccess(result, _) }
5957

6058
/**
61-
* Holds if `fc` is a call that unlocks any mutex of this type.
59+
* Gets a call that unlocks any mutex of this type.
6260
*/
6361
FunctionCall getUnlockAccess() { this.unlockAccess(result, _) }
6462

6563
/**
66-
* DEPRECATED: use mustlockAccess(fc, arg) instead
64+
* DEPRECATED: use mustlockAccess(fc, arg) instead.
6765
*/
6866
deprecated Function getMustlockFunction() { result = getMustlockAccess().getTarget() }
6967

7068
/**
71-
* DEPRECATED: use trylockAccess(fc, arg) instead
69+
* DEPRECATED: use trylockAccess(fc, arg) instead.
7270
*/
7371
deprecated Function getTrylockFunction() { result = getTrylockAccess().getTarget() }
7472

7573
/**
76-
* DEPRECATED: use lockAccess(fc, arg) instead
74+
* DEPRECATED: use lockAccess(fc, arg) instead.
7775
*/
7876
deprecated Function getLockFunction() { result = getLockAccess().getTarget() }
7977

8078
/**
81-
* DEPRECATED: use unlockAccess(fc, arg) instead
79+
* DEPRECATED: use unlockAccess(fc, arg) instead.
8280
*/
8381
deprecated Function getUnlockFunction() { result = getUnlockAccess().getTarget() }
8482
}
8583

8684
/**
87-
* A function that looks like a lock function.
85+
* Gets a function that looks like a lock function.
8886
*/
8987
private Function mustlockCandidate() {
9088
exists(string name | name = result.getName() |
@@ -94,7 +92,7 @@ private Function mustlockCandidate() {
9492
}
9593

9694
/**
97-
* A function that looks like a try-lock function.
95+
* Gets a function that looks like a try-lock function.
9896
*/
9997
private Function trylockCandidate() {
10098
exists(string name | name = result.getName() |
@@ -104,7 +102,7 @@ private Function trylockCandidate() {
104102
}
105103

106104
/**
107-
* A function that looks like an unlock function.
105+
* Gets a function that looks like an unlock function.
108106
*/
109107
private Function unlockCandidate() {
110108
exists(string name | name = result.getName() |
@@ -171,7 +169,10 @@ class DefaultMutexType extends MutexType {
171169
}
172170
}
173171

174-
/** Get the mutex argument of a call to lock or unlock. */
172+
/**
173+
* Holds if `arg` is the mutex argument of a call to lock or unlock and
174+
* `argType` is the type of the mutex.
175+
*/
175176
private predicate lockArg(Expr arg, MutexType argType, FunctionCall call) {
176177
argType = arg.getUnderlyingType().stripType() and
177178
(
@@ -184,18 +185,31 @@ private predicate lockArg(Expr arg, MutexType argType, FunctionCall call) {
184185
// `MutexType.mustlockAccess`.
185186
}
186187

188+
/**
189+
* Holds if `call` is a call that locks or tries to lock its argument `arg`.
190+
*/
187191
predicate lockCall(Expr arg, FunctionCall call) {
188192
exists(MutexType t | lockArg(arg, t, call) and call = t.getLockAccess())
189193
}
190194

195+
/**
196+
* Holds if `call` is a call that always locks its argument `arg`.
197+
*/
191198
predicate mustlockCall(Expr arg, FunctionCall call) {
192199
exists(MutexType t | lockArg(arg, t, call) and call = t.getMustlockAccess())
193200
}
194201

202+
/**
203+
* Holds if `call` is a call that tries to lock its argument `arg`, but may
204+
* return without success.
205+
*/
195206
predicate trylockCall(Expr arg, FunctionCall call) {
196207
exists(MutexType t | lockArg(arg, t, call) and call = t.getTrylockAccess())
197208
}
198209

210+
/**
211+
* Holds if `call` is a call that unlocks its argument `arg`.
212+
*/
199213
predicate unlockCall(Expr arg, FunctionCall call) {
200214
exists(MutexType t | lockArg(arg, t, call) and call = t.getUnlockAccess())
201215
}

cpp/ql/src/semmle/code/cpp/exprs/Literal.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Provides classes for modeling literals in the source code such as `0`, `'c'`
3+
* or `"string"`.
4+
*/
5+
16
import semmle.code.cpp.exprs.Expr
27

38
/**

cpp/ql/src/semmle/code/cpp/security/FunctionWithWrappers.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Provides predicates for identifying functions that wrap other functions,
3+
* passing the same arguments from the outer call into the inner call. In the
4+
* following example `MyMalloc` wraps a call to `malloc`, passing in the `size`
5+
* parameter:
6+
* ```
7+
* void *MyMalloc(size_t size)
8+
* {
9+
* void *ptr = malloc(size);
10+
*
11+
* // ... additional logic?
12+
*
13+
* return ptr;
14+
* }
15+
* ```
16+
*/
17+
118
import cpp
219
import PrintfLike
320
private import TaintTracking
@@ -152,6 +169,9 @@ abstract class FunctionWithWrappers extends Function {
152169
}
153170
}
154171

172+
/**
173+
* A `printf`-like formatting function.
174+
*/
155175
class PrintfLikeFunction extends FunctionWithWrappers {
156176
PrintfLikeFunction() { printfLikeFunction(this, _) }
157177

cpp/ql/src/semmle/code/cpp/security/Overflow.qll

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
/**
2+
* Provides predicates for reasoning about when the value of an expression is
3+
* guarded by an operation such as `<`, which confines its range.
4+
*/
5+
16
import cpp
27
import semmle.code.cpp.controlflow.Dominance
38

4-
/*
5-
* Guarding
9+
/**
10+
* Holds if the value of `use` is guarded using `abs`.
611
*/
7-
8-
/** is the size of this use guarded using 'abs'? */
912
predicate guardedAbs(Operation e, Expr use) {
1013
exists(FunctionCall fc | fc.getTarget().getName() = "abs" |
1114
fc.getArgument(0).getAChild*() = use and
1215
guardedLesser(e, fc)
1316
)
1417
}
1518

16-
/** This is `BasicBlock.getNode`, restricted to `Stmt` for performance. */
19+
/**
20+
* Gets the position of `stmt` in basic block `block` (this is a thin layer
21+
* over `BasicBlock.getNode`, intended to improve performance).
22+
*/
1723
pragma[noinline]
1824
private int getStmtIndexInBlock(BasicBlock block, Stmt stmt) { block.getNode(result) = stmt }
1925

@@ -30,7 +36,9 @@ private predicate stmtDominates(Stmt dominator, Stmt dominated) {
3036
bbStrictlyDominates(dominator.getBasicBlock(), dominated.getBasicBlock())
3137
}
3238

33-
/** is the size of this use guarded to be less than something? */
39+
/**
40+
* Holds if the value of `use` is guarded to be less than something.
41+
*/
3442
pragma[nomagic]
3543
predicate guardedLesser(Operation e, Expr use) {
3644
exists(IfStmt c, RelationalOperation guard |
@@ -54,7 +62,9 @@ predicate guardedLesser(Operation e, Expr use) {
5462
guardedAbs(e, use)
5563
}
5664

57-
/** is the size of this use guarded to be greater than something? */
65+
/**
66+
* Holds if the value of `use` is guarded to be greater than something.
67+
*/
5868
pragma[nomagic]
5969
predicate guardedGreater(Operation e, Expr use) {
6070
exists(IfStmt c, RelationalOperation guard |
@@ -78,10 +88,14 @@ predicate guardedGreater(Operation e, Expr use) {
7888
guardedAbs(e, use)
7989
}
8090

81-
/** a use of a given variable */
91+
/**
92+
* Gets a use of a given variable `v`.
93+
*/
8294
VariableAccess varUse(LocalScopeVariable v) { result = v.getAnAccess() }
8395

84-
/** is e not guarded against overflow by use? */
96+
/**
97+
* Holds if `e` is not guarded against overflow by `use`.
98+
*/
8599
predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
86100
use = e.getAnOperand() and
87101
exists(LocalScopeVariable v | use.getTarget() = v |
@@ -100,7 +114,9 @@ predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
100114
)
101115
}
102116

103-
/** is e not guarded against underflow by use? */
117+
/**
118+
* Holds if `e` is not guarded against underflow by `use`.
119+
*/
104120
predicate missingGuardAgainstUnderflow(Operation e, VariableAccess use) {
105121
use = e.getAnOperand() and
106122
exists(LocalScopeVariable v | use.getTarget() = v |

cpp/ql/src/semmle/code/cpp/security/SensitiveExprs.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
/**
2+
* Provides classes for heuristically identifying variables and functions that
3+
* might contain or return a password or other sensitive information.
4+
*/
5+
16
import cpp
27

8+
/**
9+
* Holds if the name `s` suggests something might contain or return a password
10+
* or other sensitive information.
11+
*/
312
bindingset[s]
413
private predicate suspicious(string s) {
514
(
@@ -16,14 +25,23 @@ private predicate suspicious(string s) {
1625
)
1726
}
1827

28+
/**
29+
* A variable that might contain a password or other sensitive information.
30+
*/
1931
class SensitiveVariable extends Variable {
2032
SensitiveVariable() { suspicious(getName().toLowerCase()) }
2133
}
2234

35+
/**
36+
* A function that might return a password or other sensitive information.
37+
*/
2338
class SensitiveFunction extends Function {
2439
SensitiveFunction() { suspicious(getName().toLowerCase()) }
2540
}
2641

42+
/**
43+
* An expression whose value might be a password or other sensitive information.
44+
*/
2745
class SensitiveExpr extends Expr {
2846
SensitiveExpr() {
2947
this.(VariableAccess).getTarget() instanceof SensitiveVariable or

0 commit comments

Comments
 (0)