Skip to content

Commit de68107

Browse files
committed
C++: restrict global variable IR generation
1 parent ffd2cd7 commit de68107

File tree

9 files changed

+10
-31
lines changed

9 files changed

+10
-31
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ module Raw {
3737
predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) }
3838

3939
cached
40-
predicate varHasIRFunc(GlobalOrNamespaceVariable var) { any() } // TODO: restrict?
40+
predicate varHasIRFunc(GlobalOrNamespaceVariable var) {
41+
var.hasInitializer() and
42+
(
43+
not var.getType().isDeeplyConst()
44+
or
45+
var.getInitializer().getExpr() instanceof StringLiteral
46+
)
47+
}
4148

4249
cached
4350
predicate hasInstruction(TranslatedElement element, InstructionTag tag) {

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private predicate ignoreExprOnly(Expr expr) {
119119
exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0))
120120
or
121121
not translateFunction(expr.getEnclosingFunction()) and
122-
not expr.getEnclosingVariable() instanceof GlobalOrNamespaceVariable
122+
not Raw::varHasIRFunc(expr.getEnclosingVariable())
123123
or
124124
// We do not yet translate destructors properly, so for now we ignore the
125125
// destructor call. We do, however, translate the expression being
@@ -665,7 +665,7 @@ newtype TTranslatedElement =
665665
} or
666666
// The side effect that initializes newly-allocated memory.
667667
TTranslatedAllocationSideEffect(AllocationExpr expr) { not ignoreSideEffects(expr) } or
668-
TTranslatedGlobalOrNamespaceVarInit(GlobalOrNamespaceVariable var) { var.hasInitializer() }
668+
TTranslatedGlobalOrNamespaceVarInit(GlobalOrNamespaceVariable var) { Raw::varHasIRFunc(var) }
669669

670670
/**
671671
* Gets the index of the first explicitly initialized element in `initList`

cpp/ql/test/library-tests/ir/ir/operand_locations.expected

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8228,11 +8228,6 @@
82288228
| ir.cpp:1759:16:1759:16 | ChiPartial | partial:m1759_5 |
82298229
| ir.cpp:1759:16:1759:16 | ChiTotal | total:m1759_2 |
82308230
| ir.cpp:1759:16:1759:16 | StoreValue | r1759_4 |
8231-
| ir.cpp:1761:11:1761:18 | Address | &:r1761_3 |
8232-
| ir.cpp:1761:11:1761:18 | SideEffect | ~m1761_6 |
8233-
| ir.cpp:1761:22:1761:22 | ChiPartial | partial:m1761_5 |
8234-
| ir.cpp:1761:22:1761:22 | ChiTotal | total:m1761_2 |
8235-
| ir.cpp:1761:22:1761:22 | StoreValue | r1761_4 |
82368231
| ir.cpp:1763:18:1763:25 | Address | &:r1763_3 |
82378232
| ir.cpp:1763:18:1763:25 | Arg(this) | this:r1763_3 |
82388233
| ir.cpp:1763:18:1763:25 | SideEffect | ~m1763_10 |

cpp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9442,17 +9442,6 @@ ir.cpp:
94429442
# 1759| v1759_7(void) = AliasedUse : ~m?
94439443
# 1759| v1759_8(void) = ExitFunction :
94449444

9445-
# 1761| int const global_3
9446-
# 1761| Block 0
9447-
# 1761| v1761_1(void) = EnterFunction :
9448-
# 1761| mu1761_2(unknown) = AliasedDefinition :
9449-
# 1761| r1761_3(glval<int>) = VariableAddress[global_3] :
9450-
# 1761| r1761_4(int) = Constant[2] :
9451-
# 1761| mu1761_5(int) = Store[global_3] : &:r1761_3, r1761_4
9452-
# 1761| v1761_6(void) = ReturnVoid :
9453-
# 1761| v1761_7(void) = AliasedUse : ~m?
9454-
# 1761| v1761_8(void) = ExitFunction :
9455-
94569445
# 1763| constructor_only global_4
94579446
# 1763| Block 0
94589447
# 1763| v1763_1(void) = EnterFunction :

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,8 +1234,6 @@ ssa.cpp:
12341234
# 268| v268_14(void) = AliasedUse : ~m269_7
12351235
# 268| v268_15(void) = ExitFunction :
12361236

1237-
# 274| Point* pp
1238-
12391237
# 275| void EscapedButNotConflated(bool, Point, int)
12401238
# 275| Block 0
12411239
# 275| v275_1(void) = EnterFunction :

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,6 @@ ssa.cpp:
12291229
# 268| v268_14(void) = AliasedUse : ~m269_7
12301230
# 268| v268_15(void) = ExitFunction :
12311231

1232-
# 274| Point* pp
1233-
12341232
# 275| void EscapedButNotConflated(bool, Point, int)
12351233
# 275| Block 0
12361234
# 275| v275_1(void) = EnterFunction :

cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,6 @@ ssa.cpp:
11401140
# 268| v268_13(void) = AliasedUse : ~m?
11411141
# 268| v268_14(void) = ExitFunction :
11421142

1143-
# 274| Point* pp
1144-
11451143
# 275| void EscapedButNotConflated(bool, Point, int)
11461144
# 275| Block 0
11471145
# 275| v275_1(void) = EnterFunction :

cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,6 @@ ssa.cpp:
11401140
# 268| v268_13(void) = AliasedUse : ~m?
11411141
# 268| v268_14(void) = ExitFunction :
11421142

1143-
# 274| Point* pp
1144-
11451143
# 275| void EscapedButNotConflated(bool, Point, int)
11461144
# 275| Block 0
11471145
# 275| v275_1(void) = EnterFunction :

cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,6 @@ test.cpp:
941941
# 124| v124_13(void) = AliasedUse : m124_3
942942
# 124| v124_14(void) = ExitFunction :
943943

944-
# 132| A* global_a
945-
946-
# 133| int global_n
947-
948944
# 135| void test_read_global_same()
949945
# 135| Block 0
950946
# 135| v135_1(void) = EnterFunction :

0 commit comments

Comments
 (0)