Skip to content

Commit bb3254d

Browse files
committed
Merge branch 'main' into alternative-instruction-operand-flow
2 parents 2e9c0fc + 7dd2677 commit bb3254d

File tree

122 files changed

+6238
-2814
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+6238
-2814
lines changed

cpp/autobuilder/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
obj/
2+
TestResults/
3+
*.manifest
4+
*.pdb
5+
*.suo
6+
*.mdb
7+
*.vsmdi
8+
csharp.log
9+
**/bin/Debug
10+
**/bin/Release
11+
*.tlog
12+
.vs
13+
*.user

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/Bound.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ private newtype TBound =
88
exists(Instruction i |
99
vn.getAnInstruction() = i and
1010
(
11-
i.getResultType() instanceof IntegralType or
12-
i.getResultType() instanceof PointerType
11+
i.getResultIRType() instanceof IRIntegerType or
12+
i.getResultIRType() instanceof IRAddressType
1313
) and
1414
not vn.getAnInstruction() instanceof ConstantInstruction
1515
|

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/RangeAnalysis.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ class CondReason extends Reason, TCondReason {
244244
/**
245245
* Holds if `typ` is a small integral type with the given lower and upper bounds.
246246
*/
247-
private predicate typeBound(IntegralType typ, int lowerbound, int upperbound) {
248-
typ.isSigned() and typ.getSize() = 1 and lowerbound = -128 and upperbound = 127
247+
private predicate typeBound(IRIntegerType typ, int lowerbound, int upperbound) {
248+
typ.isSigned() and typ.getByteSize() = 1 and lowerbound = -128 and upperbound = 127
249249
or
250-
typ.isUnsigned() and typ.getSize() = 1 and lowerbound = 0 and upperbound = 255
250+
typ.isUnsigned() and typ.getByteSize() = 1 and lowerbound = 0 and upperbound = 255
251251
or
252-
typ.isSigned() and typ.getSize() = 2 and lowerbound = -32768 and upperbound = 32767
252+
typ.isSigned() and typ.getByteSize() = 2 and lowerbound = -32768 and upperbound = 32767
253253
or
254-
typ.isUnsigned() and typ.getSize() = 2 and lowerbound = 0 and upperbound = 65535
254+
typ.isUnsigned() and typ.getByteSize() = 2 and lowerbound = 0 and upperbound = 65535
255255
}
256256

257257
/**
@@ -260,14 +260,14 @@ private predicate typeBound(IntegralType typ, int lowerbound, int upperbound) {
260260
private class NarrowingCastInstruction extends ConvertInstruction {
261261
NarrowingCastInstruction() {
262262
not this instanceof SafeCastInstruction and
263-
typeBound(getResultType(), _, _)
263+
typeBound(getResultIRType(), _, _)
264264
}
265265

266266
/** Gets the lower bound of the resulting type. */
267-
int getLowerBound() { typeBound(getResultType(), result, _) }
267+
int getLowerBound() { typeBound(getResultIRType(), result, _) }
268268

269269
/** Gets the upper bound of the resulting type. */
270-
int getUpperBound() { typeBound(getResultType(), _, result) }
270+
int getUpperBound() { typeBound(getResultIRType(), _, result) }
271271
}
272272

273273
/**

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/RangeUtils.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ predicate backEdge(PhiInstruction phi, PhiInputOperand op) {
8686
* range analysis.
8787
*/
8888
pragma[inline]
89-
private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
90-
fromtyp.getSize() < totyp.getSize() and
89+
private predicate safeCast(IRIntegerType fromtyp, IRIntegerType totyp) {
90+
fromtyp.getByteSize() < totyp.getByteSize() and
9191
(
9292
fromtyp.isUnsigned()
9393
or
9494
totyp.isSigned()
9595
)
9696
or
97-
fromtyp.getSize() <= totyp.getSize() and
97+
fromtyp.getByteSize() <= totyp.getByteSize() and
9898
(
9999
fromtyp.isSigned() and
100100
totyp.isSigned()
@@ -109,8 +109,8 @@ private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
109109
*/
110110
class PtrToPtrCastInstruction extends ConvertInstruction {
111111
PtrToPtrCastInstruction() {
112-
getResultType() instanceof PointerType and
113-
getUnary().getResultType() instanceof PointerType
112+
getResultIRType() instanceof IRAddressType and
113+
getUnary().getResultIRType() instanceof IRAddressType
114114
}
115115
}
116116

@@ -119,7 +119,7 @@ class PtrToPtrCastInstruction extends ConvertInstruction {
119119
* that cannot overflow or underflow.
120120
*/
121121
class SafeIntCastInstruction extends ConvertInstruction {
122-
SafeIntCastInstruction() { safeCast(getUnary().getResultType(), getResultType()) }
122+
SafeIntCastInstruction() { safeCast(getUnary().getResultIRType(), getResultIRType()) }
123123
}
124124

125125
/**

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/SignAnalysis.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,21 @@ module SignAnalysisCached {
469469
not exists(certainInstructionSign(i)) and
470470
not (
471471
result = TNeg() and
472-
i.getResultType().(IntegralType).isUnsigned()
472+
i.getResultIRType().(IRIntegerType).isUnsigned()
473473
) and
474474
(
475475
unknownSign(i)
476476
or
477477
exists(ConvertInstruction ci, Instruction prior, boolean fromSigned, boolean toSigned |
478478
i = ci and
479479
prior = ci.getUnary() and
480-
(if ci.getResultType().(IntegralType).isSigned() then toSigned = true else toSigned = false) and
481480
(
482-
if prior.getResultType().(IntegralType).isSigned()
481+
if ci.getResultIRType().(IRIntegerType).isSigned()
482+
then toSigned = true
483+
else toSigned = false
484+
) and
485+
(
486+
if prior.getResultIRType().(IRIntegerType).isSigned()
483487
then fromSigned = true
484488
else fromSigned = false
485489
) and
@@ -512,11 +516,11 @@ module SignAnalysisCached {
512516
i instanceof ShiftLeftInstruction and result = s1.lshift(s2)
513517
or
514518
i instanceof ShiftRightInstruction and
515-
i.getResultType().(IntegralType).isSigned() and
519+
i.getResultIRType().(IRIntegerType).isSigned() and
516520
result = s1.rshift(s2)
517521
or
518522
i instanceof ShiftRightInstruction and
519-
not i.getResultType().(IntegralType).isSigned() and
523+
not i.getResultIRType().(IRIntegerType).isSigned() and
520524
result = s1.urshift(s2)
521525
)
522526
or

cpp/ql/src/printAst.ql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @name Print AST
3+
* @description Outputs a representation of a file's Abstract Syntax Tree. This
4+
* query is used by the VS Code extension.
5+
* @id cpp/print-ast
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-ast
8+
*/
9+
10+
import cpp
11+
import semmle.code.cpp.PrintAST
12+
import definitions
13+
14+
/**
15+
* The source file to generate an AST from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
class Cfg extends PrintASTConfiguration {
20+
/**
21+
* Holds if the AST for `func` should be printed.
22+
* Print All functions from the selected file.
23+
*/
24+
override predicate shouldPrintFunction(Function func) {
25+
func.getFile() = getEncodedFile(selectedSourceFile())
26+
}
27+
}

cpp/ql/src/semmle/code/cpp/Preprocessor.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ class PreprocessorDirective extends Locatable, @preprocdirect {
3333
}
3434
}
3535

36+
private class TPreprocessorBranchDirective = @ppd_branch or @ppd_else or @ppd_endif;
37+
3638
/**
3739
* A C/C++ preprocessor branch related directive: `#if`, `#ifdef`,
3840
* `#ifndef`, `#elif`, `#else` or `#endif`.
3941
*/
40-
abstract class PreprocessorBranchDirective extends PreprocessorDirective {
42+
class PreprocessorBranchDirective extends PreprocessorDirective, TPreprocessorBranchDirective {
4143
/**
4244
* Gets the `#if`, `#ifdef` or `#ifndef` directive which matches this
4345
* branching directive.

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,20 @@ predicate clearsContent(Node n, Content c) {
234234
}
235235

236236
/** Gets the type of `n` used for type pruning. */
237-
Type getNodeType(Node n) {
237+
IRType getNodeType(Node n) {
238238
suppressUnusedNode(n) and
239-
result instanceof VoidType // stub implementation
239+
result instanceof IRVoidType // stub implementation
240240
}
241241

242242
/** Gets a string representation of a type returned by `getNodeType`. */
243-
string ppReprType(Type t) { none() } // stub implementation
243+
string ppReprType(IRType t) { none() } // stub implementation
244244

245245
/**
246246
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from
247247
* a node of type `t1` to a node of type `t2`.
248248
*/
249249
pragma[inline]
250-
predicate compatibleTypes(Type t1, Type t2) {
250+
predicate compatibleTypes(IRType t1, IRType t2) {
251251
any() // stub implementation
252252
}
253253

@@ -271,7 +271,7 @@ class DataFlowCallable = Declaration;
271271

272272
class DataFlowExpr = Expr;
273273

274-
class DataFlowType = Type;
274+
class DataFlowType = IRType;
275275

276276
/** A function call relevant for data flow. */
277277
class DataFlowCall extends CallInstruction {

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Node extends TIRDataFlowNode {
3434
Function getFunction() { none() } // overridden in subclasses
3535

3636
/** Gets the type of this node. */
37-
Type getType() { none() } // overridden in subclasses
37+
IRType getType() { none() } // overridden in subclasses
3838

3939
/** Gets the instruction corresponding to this node, if any. */
4040
Instruction asInstruction() { result = this.(InstructionNode).getInstruction() }
@@ -89,7 +89,7 @@ class Node extends TIRDataFlowNode {
8989
/**
9090
* Gets an upper bound on the type of this node.
9191
*/
92-
Type getTypeBound() { result = getType() }
92+
IRType getTypeBound() { result = getType() }
9393

9494
/** Gets the location of this element. */
9595
Location getLocation() { none() } // overridden by subclasses
@@ -126,7 +126,7 @@ class InstructionNode extends Node, TInstructionNode {
126126

127127
override Function getFunction() { result = instr.getEnclosingFunction() }
128128

129-
override Type getType() { result = instr.getResultType() }
129+
override IRType getType() { result = instr.getResultIRType() }
130130

131131
override Location getLocation() { result = instr.getLocation() }
132132

@@ -152,7 +152,7 @@ class OperandNode extends Node, TOperandNode {
152152

153153
override Function getFunction() { result = op.getUse().getEnclosingFunction() }
154154

155-
override Type getType() { result = op.getType() }
155+
override IRType getType() { result = op.getIRType() }
156156

157157
override Location getLocation() { result = op.getLocation() }
158158

@@ -450,7 +450,7 @@ class VariableNode extends Node, TVariableNode {
450450
result = v
451451
}
452452

453-
override Type getType() { result = v.getType() }
453+
override IRType getType() { result.getCanonicalLanguageType().hasUnspecifiedType(v.getType(), _) }
454454

455455
override Location getLocation() { result = v.getLocation() }
456456

cpp/ql/src/semmle/code/cpp/ir/implementation/IRType.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class IRIntegerType extends IRNumericType {
152152
this = TIRSignedIntegerType(byteSize) or
153153
this = TIRUnsignedIntegerType(byteSize)
154154
}
155+
156+
/** Holds if this integer type is signed. */
157+
predicate isSigned() { none() }
158+
159+
/** Holds if this integer type is unsigned. */
160+
predicate isUnsigned() { none() }
155161
// Don't override `getByteSize()` here. The optimizer seems to generate better code when this is
156162
// overridden only in the leaf classes.
157163
}
@@ -169,6 +175,8 @@ class IRSignedIntegerType extends IRIntegerType, TIRSignedIntegerType {
169175

170176
pragma[noinline]
171177
final override int getByteSize() { result = byteSize }
178+
179+
override predicate isSigned() { any() }
172180
}
173181

174182
/**
@@ -184,6 +192,8 @@ class IRUnsignedIntegerType extends IRIntegerType, TIRUnsignedIntegerType {
184192

185193
pragma[noinline]
186194
final override int getByteSize() { result = byteSize }
195+
196+
override predicate isUnsigned() { any() }
187197
}
188198

189199
/**

0 commit comments

Comments
 (0)