From 33910a85b920a697f048caa21688d3eacecd3b35 Mon Sep 17 00:00:00 2001 From: Robert Marsh Date: Tue, 3 May 2022 16:50:53 -0400 Subject: [PATCH] C++: restrict global variable IR generation --- .../ir/implementation/raw/internal/IRConstruction.qll | 9 ++++++++- .../implementation/raw/internal/TranslatedElement.qll | 4 ++-- .../library-tests/ir/ir/operand_locations.expected | 5 ----- cpp/ql/test/library-tests/ir/ir/raw_ir.expected | 11 ----------- .../test/library-tests/ir/ssa/aliased_ssa_ir.expected | 2 -- .../ir/ssa/aliased_ssa_ir_unsound.expected | 2 -- .../library-tests/ir/ssa/unaliased_ssa_ir.expected | 2 -- .../ir/ssa/unaliased_ssa_ir_unsound.expected | 2 -- .../GlobalValueNumbering/ir_gvn.expected | 4 ---- 9 files changed, 10 insertions(+), 31 deletions(-) diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll index c798f33c045..44e9ecbfe5e 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll @@ -37,7 +37,14 @@ module Raw { predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) } cached - predicate varHasIRFunc(GlobalOrNamespaceVariable var) { any() } // TODO: restrict? + predicate varHasIRFunc(GlobalOrNamespaceVariable var) { + var.hasInitializer() and + ( + not var.getType().isDeeplyConst() + or + var.getInitializer().getExpr() instanceof StringLiteral + ) + } cached predicate hasInstruction(TranslatedElement element, InstructionTag tag) { diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll index 82663e7b125..103b5424197 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll @@ -119,7 +119,7 @@ private predicate ignoreExprOnly(Expr expr) { exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0)) or not translateFunction(expr.getEnclosingFunction()) and - not expr.getEnclosingVariable() instanceof GlobalOrNamespaceVariable + not Raw::varHasIRFunc(expr.getEnclosingVariable()) or // We do not yet translate destructors properly, so for now we ignore the // destructor call. We do, however, translate the expression being @@ -665,7 +665,7 @@ newtype TTranslatedElement = } or // The side effect that initializes newly-allocated memory. TTranslatedAllocationSideEffect(AllocationExpr expr) { not ignoreSideEffects(expr) } or - TTranslatedGlobalOrNamespaceVarInit(GlobalOrNamespaceVariable var) { var.hasInitializer() } + TTranslatedGlobalOrNamespaceVarInit(GlobalOrNamespaceVariable var) { Raw::varHasIRFunc(var) } /** * Gets the index of the first explicitly initialized element in `initList` diff --git a/cpp/ql/test/library-tests/ir/ir/operand_locations.expected b/cpp/ql/test/library-tests/ir/ir/operand_locations.expected index 7e3252978ab..29d5e6cdfcb 100644 --- a/cpp/ql/test/library-tests/ir/ir/operand_locations.expected +++ b/cpp/ql/test/library-tests/ir/ir/operand_locations.expected @@ -8470,11 +8470,6 @@ | ir.cpp:1821:16:1821:16 | ChiPartial | partial:m1821_5 | | ir.cpp:1821:16:1821:16 | ChiTotal | total:m1821_2 | | ir.cpp:1821:16:1821:16 | StoreValue | r1821_4 | -| ir.cpp:1823:11:1823:18 | Address | &:r1823_3 | -| ir.cpp:1823:11:1823:18 | SideEffect | ~m1823_6 | -| ir.cpp:1823:22:1823:22 | ChiPartial | partial:m1823_5 | -| ir.cpp:1823:22:1823:22 | ChiTotal | total:m1823_2 | -| ir.cpp:1823:22:1823:22 | StoreValue | r1823_4 | | ir.cpp:1825:18:1825:25 | Address | &:r1825_3 | | ir.cpp:1825:18:1825:25 | Arg(this) | this:r1825_3 | | ir.cpp:1825:18:1825:25 | SideEffect | ~m1825_10 | diff --git a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected index defcc9fbc06..8f67435f3c1 100644 --- a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected +++ b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected @@ -9744,17 +9744,6 @@ ir.cpp: # 1821| v1821_7(void) = AliasedUse : ~m? # 1821| v1821_8(void) = ExitFunction : -# 1823| int const global_3 -# 1823| Block 0 -# 1823| v1823_1(void) = EnterFunction : -# 1823| mu1823_2(unknown) = AliasedDefinition : -# 1823| r1823_3(glval) = VariableAddress[global_3] : -# 1823| r1823_4(int) = Constant[2] : -# 1823| mu1823_5(int) = Store[global_3] : &:r1823_3, r1823_4 -# 1823| v1823_6(void) = ReturnVoid : -# 1823| v1823_7(void) = AliasedUse : ~m? -# 1823| v1823_8(void) = ExitFunction : - # 1825| constructor_only global_4 # 1825| Block 0 # 1825| v1825_1(void) = EnterFunction : diff --git a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected index 1893ab5c0d5..147c10b7c7f 100644 --- a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected +++ b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected @@ -1234,8 +1234,6 @@ ssa.cpp: # 268| v268_14(void) = AliasedUse : ~m269_7 # 268| v268_15(void) = ExitFunction : -# 274| Point* pp - # 275| void EscapedButNotConflated(bool, Point, int) # 275| Block 0 # 275| v275_1(void) = EnterFunction : diff --git a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected index faedd418ed2..396b7532d68 100644 --- a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected +++ b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected @@ -1229,8 +1229,6 @@ ssa.cpp: # 268| v268_14(void) = AliasedUse : ~m269_7 # 268| v268_15(void) = ExitFunction : -# 274| Point* pp - # 275| void EscapedButNotConflated(bool, Point, int) # 275| Block 0 # 275| v275_1(void) = EnterFunction : diff --git a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected index 6d1e8f4d03d..3fc07bf6950 100644 --- a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected +++ b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected @@ -1140,8 +1140,6 @@ ssa.cpp: # 268| v268_13(void) = AliasedUse : ~m? # 268| v268_14(void) = ExitFunction : -# 274| Point* pp - # 275| void EscapedButNotConflated(bool, Point, int) # 275| Block 0 # 275| v275_1(void) = EnterFunction : diff --git a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected index 6d1e8f4d03d..3fc07bf6950 100644 --- a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected +++ b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected @@ -1140,8 +1140,6 @@ ssa.cpp: # 268| v268_13(void) = AliasedUse : ~m? # 268| v268_14(void) = ExitFunction : -# 274| Point* pp - # 275| void EscapedButNotConflated(bool, Point, int) # 275| Block 0 # 275| v275_1(void) = EnterFunction : diff --git a/cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected b/cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected index 5ef74834a60..88e365023a1 100644 --- a/cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected +++ b/cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected @@ -941,10 +941,6 @@ test.cpp: # 124| v124_13(void) = AliasedUse : m124_3 # 124| v124_14(void) = ExitFunction : -# 132| A* global_a - -# 133| int global_n - # 135| void test_read_global_same() # 135| Block 0 # 135| v135_1(void) = EnterFunction :