Skip to content

Commit 6ead6c6

Browse files
committed
Merge branch 'main' into qualifier-as-parameter-for-callee
2 parents c7efc91 + 14aa642 commit 6ead6c6

File tree

8 files changed

+232
-9
lines changed

8 files changed

+232
-9
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ class ElementBase extends @element {
6565
* which they belong; for example, `AddExpr` is a primary class, but
6666
* `BinaryOperation` is not.
6767
*
68-
* This predicate always has a result. If no primary class can be
69-
* determined, the result is `"???"`. If multiple primary classes match,
70-
* this predicate can have multiple results.
68+
* This predicate can have multiple results if multiple primary classes match.
69+
* For some elements, this predicate may not have a result.
7170
*/
72-
string getAPrimaryQlClass() { result = "???" }
71+
string getAPrimaryQlClass() { none() }
7372
}
7473

7574
/**

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ private newtype TPrintASTNode =
9191
TDeclarationEntryNode(DeclStmt stmt, DeclarationEntry entry) {
9292
// We create a unique node for each pair of (stmt, entry), to avoid having one node with
9393
// multiple parents due to extractor bug CPP-413.
94-
stmt.getADeclarationEntry() = entry
94+
stmt.getADeclarationEntry() = entry and
95+
shouldPrintFunction(stmt.getEnclosingFunction())
9596
} or
9697
TParametersNode(Function func) { shouldPrintFunction(func) } or
9798
TConstructorInitializersNode(Constructor ctor) {
@@ -234,11 +235,27 @@ class PrintASTNode extends TPrintASTNode {
234235
private Function getEnclosingFunction() { result = getParent*().(FunctionNode).getFunction() }
235236
}
236237

238+
/**
239+
* Class that restricts the elements that we compute `qlClass` for.
240+
*/
241+
private class PrintableElement extends Element {
242+
PrintableElement() {
243+
exists(TASTNode(this))
244+
or
245+
exists(TDeclarationEntryNode(_, this))
246+
or
247+
this instanceof Type
248+
}
249+
250+
pragma[noinline]
251+
string getAPrimaryQlClass0() { result = getAPrimaryQlClass() }
252+
}
253+
237254
/**
238255
* Retrieves the canonical QL class(es) for entity `el`
239256
*/
240-
private string qlClass(ElementBase el) {
241-
result = "[" + concat(el.getAPrimaryQlClass(), ",") + "] "
257+
private string qlClass(PrintableElement el) {
258+
result = "[" + concat(el.getAPrimaryQlClass0(), ",") + "] "
242259
// Alternative implementation -- do not delete. It is useful for QL class discovery.
243260
//result = "["+ concat(el.getAQlClass(), ",") + "] "
244261
}

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/AliasedSSA.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ private Overlap getVariableMemoryLocationOverlap(
566566
use.getEndBitOffset())
567567
}
568568

569+
bindingset[result, b]
570+
private boolean unbindBool(boolean b) { result != b.booleanNot() }
571+
569572
MemoryLocation getResultMemoryLocation(Instruction instr) {
570573
exists(MemoryAccessKind kind, boolean isMayAccess |
571574
kind = instr.getResultMemoryAccess() and
@@ -578,15 +581,16 @@ MemoryLocation getResultMemoryLocation(Instruction instr) {
578581
exists(Allocation var, IRType type, IntValue startBitOffset, IntValue endBitOffset |
579582
hasResultMemoryAccess(instr, var, type, _, startBitOffset, endBitOffset, isMayAccess) and
580583
result =
581-
TVariableMemoryLocation(var, type, _, startBitOffset, endBitOffset, isMayAccess)
584+
TVariableMemoryLocation(var, type, _, startBitOffset, endBitOffset,
585+
unbindBool(isMayAccess))
582586
)
583587
else result = TUnknownMemoryLocation(instr.getEnclosingIRFunction(), isMayAccess)
584588
)
585589
or
586590
kind instanceof EntireAllocationMemoryAccess and
587591
result =
588592
TEntireAllocationMemoryLocation(getAddressOperandAllocation(instr.getResultAddressOperand()),
589-
isMayAccess)
593+
unbindBool(isMayAccess))
590594
or
591595
kind instanceof EscapedMemoryAccess and
592596
result = TAllAliasedMemory(instr.getEnclosingIRFunction(), isMayAccess, false)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
public class Class1
4+
{
5+
public void M1(int j, uint k)
6+
{
7+
nint x = j;
8+
nint x0 = (nint)j;
9+
IntPtr x1 = (IntPtr)j;
10+
nuint y = k;
11+
12+
const nint i = (nint)42;
13+
}
14+
15+
public void M2()
16+
{
17+
nint x = 3;
18+
int y = 3;
19+
long v = 10;
20+
21+
var test3 = typeof(nint); // System.IntPtr
22+
var test4 = typeof(nuint); // System.UIntPtr
23+
var test5 = (x + 1).GetType(); // System.IntPtr
24+
var test6 = (x + y).GetType(); // System.IntPtr
25+
var test7 = (x + v).GetType(); // System.Int64
26+
}
27+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
NativeInt.cs:
2+
# 3| [Class] Class1
3+
# 5| 5: [Method] M1
4+
# 5| -1: [TypeMention] Void
5+
#-----| 2: (Parameters)
6+
# 5| 0: [Parameter] j
7+
# 5| -1: [TypeMention] int
8+
# 5| 1: [Parameter] k
9+
# 5| -1: [TypeMention] uint
10+
# 6| 4: [BlockStmt] {...}
11+
# 7| 0: [LocalVariableDeclStmt] ... ...;
12+
# 7| 0: [LocalVariableDeclAndInitExpr] IntPtr x = ...
13+
# 7| -1: [TypeMention] IntPtr
14+
# 7| 0: [LocalVariableAccess] access to local variable x
15+
# 7| 1: [CastExpr] (...) ...
16+
# 7| 1: [ParameterAccess] access to parameter j
17+
# 8| 1: [LocalVariableDeclStmt] ... ...;
18+
# 8| 0: [LocalVariableDeclAndInitExpr] IntPtr x0 = ...
19+
# 8| -1: [TypeMention] IntPtr
20+
# 8| 0: [LocalVariableAccess] access to local variable x0
21+
# 8| 1: [CastExpr] (...) ...
22+
# 8| 0: [TypeAccess] access to type IntPtr
23+
# 8| 0: [TypeMention] IntPtr
24+
# 8| 1: [ParameterAccess] access to parameter j
25+
# 9| 2: [LocalVariableDeclStmt] ... ...;
26+
# 9| 0: [LocalVariableDeclAndInitExpr] IntPtr x1 = ...
27+
# 9| -1: [TypeMention] IntPtr
28+
# 9| 0: [LocalVariableAccess] access to local variable x1
29+
# 9| 1: [CastExpr] (...) ...
30+
# 9| 0: [TypeAccess] access to type IntPtr
31+
# 9| 0: [TypeMention] IntPtr
32+
# 9| 1: [ParameterAccess] access to parameter j
33+
# 10| 3: [LocalVariableDeclStmt] ... ...;
34+
# 10| 0: [LocalVariableDeclAndInitExpr] UIntPtr y = ...
35+
# 10| -1: [TypeMention] UIntPtr
36+
# 10| 0: [LocalVariableAccess] access to local variable y
37+
# 10| 1: [CastExpr] (...) ...
38+
# 10| 1: [ParameterAccess] access to parameter k
39+
# 12| 4: [LocalConstantDeclStmt] const ... ...;
40+
# 12| 0: [LocalVariableDeclAndInitExpr] IntPtr i = ...
41+
# 12| -1: [TypeMention] IntPtr
42+
# 12| 0: [LocalVariableAccess] access to local variable i
43+
# 12| 1: [CastExpr] (...) ...
44+
# 12| 0: [TypeAccess] access to type IntPtr
45+
# 12| 0: [TypeMention] IntPtr
46+
# 12| 1: [IntLiteral] 42
47+
# 15| 6: [Method] M2
48+
# 15| -1: [TypeMention] Void
49+
# 16| 4: [BlockStmt] {...}
50+
# 17| 0: [LocalVariableDeclStmt] ... ...;
51+
# 17| 0: [LocalVariableDeclAndInitExpr] IntPtr x = ...
52+
# 17| -1: [TypeMention] IntPtr
53+
# 17| 0: [LocalVariableAccess] access to local variable x
54+
# 17| 1: [CastExpr] (...) ...
55+
# 17| 1: [IntLiteral] 3
56+
# 18| 1: [LocalVariableDeclStmt] ... ...;
57+
# 18| 0: [LocalVariableDeclAndInitExpr] Int32 y = ...
58+
# 18| -1: [TypeMention] int
59+
# 18| 0: [LocalVariableAccess] access to local variable y
60+
# 18| 1: [IntLiteral] 3
61+
# 19| 2: [LocalVariableDeclStmt] ... ...;
62+
# 19| 0: [LocalVariableDeclAndInitExpr] Int64 v = ...
63+
# 19| -1: [TypeMention] long
64+
# 19| 0: [LocalVariableAccess] access to local variable v
65+
# 19| 1: [CastExpr] (...) ...
66+
# 19| 1: [IntLiteral] 10
67+
# 21| 3: [LocalVariableDeclStmt] ... ...;
68+
# 21| 0: [LocalVariableDeclAndInitExpr] Type test3 = ...
69+
# 21| -1: [TypeMention] Type
70+
# 21| 0: [LocalVariableAccess] access to local variable test3
71+
# 21| 1: [TypeofExpr] typeof(...)
72+
# 21| 0: [TypeAccess] access to type IntPtr
73+
# 21| 0: [TypeMention] IntPtr
74+
# 22| 4: [LocalVariableDeclStmt] ... ...;
75+
# 22| 0: [LocalVariableDeclAndInitExpr] Type test4 = ...
76+
# 22| -1: [TypeMention] Type
77+
# 22| 0: [LocalVariableAccess] access to local variable test4
78+
# 22| 1: [TypeofExpr] typeof(...)
79+
# 22| 0: [TypeAccess] access to type UIntPtr
80+
# 22| 0: [TypeMention] UIntPtr
81+
# 23| 5: [LocalVariableDeclStmt] ... ...;
82+
# 23| 0: [LocalVariableDeclAndInitExpr] Type test5 = ...
83+
# 23| -1: [TypeMention] Type
84+
# 23| 0: [LocalVariableAccess] access to local variable test5
85+
# 23| 1: [MethodCall] call to method GetType
86+
# 23| -1: [AddExpr] ... + ...
87+
# 23| 0: [LocalVariableAccess] access to local variable x
88+
# 23| 1: [CastExpr] (...) ...
89+
# 23| 1: [IntLiteral] 1
90+
# 24| 6: [LocalVariableDeclStmt] ... ...;
91+
# 24| 0: [LocalVariableDeclAndInitExpr] Type test6 = ...
92+
# 24| -1: [TypeMention] Type
93+
# 24| 0: [LocalVariableAccess] access to local variable test6
94+
# 24| 1: [MethodCall] call to method GetType
95+
# 24| -1: [AddExpr] ... + ...
96+
# 24| 0: [LocalVariableAccess] access to local variable x
97+
# 24| 1: [CastExpr] (...) ...
98+
# 24| 1: [LocalVariableAccess] access to local variable y
99+
# 25| 7: [LocalVariableDeclStmt] ... ...;
100+
# 25| 0: [LocalVariableDeclAndInitExpr] Type test7 = ...
101+
# 25| -1: [TypeMention] Type
102+
# 25| 0: [LocalVariableAccess] access to local variable test7
103+
# 25| 1: [MethodCall] call to method GetType
104+
# 25| -1: [AddExpr] ... + ...
105+
# 25| 0: [CastExpr] (...) ...
106+
# 25| 1: [LocalVariableAccess] access to local variable x
107+
# 25| 1: [LocalVariableAccess] access to local variable v
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/csharp/PrintAst.ql
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
| NativeInt.cs:7:14:7:14 | access to local variable x | IntPtr |
2+
| NativeInt.cs:7:14:7:18 | IntPtr x = ... | IntPtr |
3+
| NativeInt.cs:7:18:7:18 | (...) ... | IntPtr |
4+
| NativeInt.cs:7:18:7:18 | access to parameter j | Int32 |
5+
| NativeInt.cs:8:14:8:15 | access to local variable x0 | IntPtr |
6+
| NativeInt.cs:8:14:8:25 | IntPtr x0 = ... | IntPtr |
7+
| NativeInt.cs:8:19:8:25 | (...) ... | IntPtr |
8+
| NativeInt.cs:8:20:8:23 | access to type IntPtr | IntPtr |
9+
| NativeInt.cs:8:25:8:25 | access to parameter j | Int32 |
10+
| NativeInt.cs:9:16:9:17 | access to local variable x1 | IntPtr |
11+
| NativeInt.cs:9:16:9:29 | IntPtr x1 = ... | IntPtr |
12+
| NativeInt.cs:9:21:9:29 | (...) ... | IntPtr |
13+
| NativeInt.cs:9:22:9:27 | access to type IntPtr | IntPtr |
14+
| NativeInt.cs:9:29:9:29 | access to parameter j | Int32 |
15+
| NativeInt.cs:10:15:10:15 | access to local variable y | UIntPtr |
16+
| NativeInt.cs:10:15:10:19 | UIntPtr y = ... | UIntPtr |
17+
| NativeInt.cs:10:19:10:19 | (...) ... | UIntPtr |
18+
| NativeInt.cs:10:19:10:19 | access to parameter k | UInt32 |
19+
| NativeInt.cs:12:20:12:20 | access to local variable i | IntPtr |
20+
| NativeInt.cs:12:20:12:31 | IntPtr i = ... | IntPtr |
21+
| NativeInt.cs:12:24:12:31 | (...) ... | IntPtr |
22+
| NativeInt.cs:12:25:12:28 | access to type IntPtr | IntPtr |
23+
| NativeInt.cs:12:30:12:31 | 42 | Int32 |
24+
| NativeInt.cs:17:14:17:14 | access to local variable x | IntPtr |
25+
| NativeInt.cs:17:14:17:18 | IntPtr x = ... | IntPtr |
26+
| NativeInt.cs:17:18:17:18 | 3 | Int32 |
27+
| NativeInt.cs:17:18:17:18 | (...) ... | IntPtr |
28+
| NativeInt.cs:18:13:18:13 | access to local variable y | Int32 |
29+
| NativeInt.cs:18:13:18:17 | Int32 y = ... | Int32 |
30+
| NativeInt.cs:18:17:18:17 | 3 | Int32 |
31+
| NativeInt.cs:19:14:19:14 | access to local variable v | Int64 |
32+
| NativeInt.cs:19:14:19:19 | Int64 v = ... | Int64 |
33+
| NativeInt.cs:19:18:19:19 | 10 | Int32 |
34+
| NativeInt.cs:19:18:19:19 | (...) ... | Int64 |
35+
| NativeInt.cs:21:13:21:17 | access to local variable test3 | Type |
36+
| NativeInt.cs:21:13:21:32 | Type test3 = ... | Type |
37+
| NativeInt.cs:21:21:21:32 | typeof(...) | Type |
38+
| NativeInt.cs:21:28:21:31 | access to type IntPtr | IntPtr |
39+
| NativeInt.cs:22:13:22:17 | access to local variable test4 | Type |
40+
| NativeInt.cs:22:13:22:33 | Type test4 = ... | Type |
41+
| NativeInt.cs:22:21:22:33 | typeof(...) | Type |
42+
| NativeInt.cs:22:28:22:32 | access to type UIntPtr | UIntPtr |
43+
| NativeInt.cs:23:13:23:17 | access to local variable test5 | Type |
44+
| NativeInt.cs:23:13:23:37 | Type test5 = ... | Type |
45+
| NativeInt.cs:23:21:23:37 | call to method GetType | Type |
46+
| NativeInt.cs:23:22:23:22 | access to local variable x | IntPtr |
47+
| NativeInt.cs:23:22:23:26 | ... + ... | IntPtr |
48+
| NativeInt.cs:23:26:23:26 | 1 | Int32 |
49+
| NativeInt.cs:23:26:23:26 | (...) ... | IntPtr |
50+
| NativeInt.cs:24:13:24:17 | access to local variable test6 | Type |
51+
| NativeInt.cs:24:13:24:37 | Type test6 = ... | Type |
52+
| NativeInt.cs:24:21:24:37 | call to method GetType | Type |
53+
| NativeInt.cs:24:22:24:22 | access to local variable x | IntPtr |
54+
| NativeInt.cs:24:22:24:26 | ... + ... | IntPtr |
55+
| NativeInt.cs:24:26:24:26 | (...) ... | IntPtr |
56+
| NativeInt.cs:24:26:24:26 | access to local variable y | Int32 |
57+
| NativeInt.cs:25:13:25:17 | access to local variable test7 | Type |
58+
| NativeInt.cs:25:13:25:37 | Type test7 = ... | Type |
59+
| NativeInt.cs:25:21:25:37 | call to method GetType | Type |
60+
| NativeInt.cs:25:22:25:22 | (...) ... | Int64 |
61+
| NativeInt.cs:25:22:25:22 | access to local variable x | IntPtr |
62+
| NativeInt.cs:25:22:25:26 | ... + ... | Int64 |
63+
| NativeInt.cs:25:26:25:26 | access to local variable v | Int64 |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from Expr e
4+
where e.fromSource()
5+
select e, e.getType().toString()

0 commit comments

Comments
 (0)