Skip to content

Commit abdfd22

Browse files
committed
C++: Fix IR variable reuse for global var inits
1 parent 259580a commit abdfd22

File tree

12 files changed

+97
-94
lines changed

12 files changed

+97
-94
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private import Imports::IRType
1818
* by the AST-to-IR translation (`IRTempVariable`).
1919
*/
2020
class IRVariable extends TIRVariable {
21-
Language::Function func;
21+
Language::Declaration func;
2222

2323
IRVariable() {
2424
this = TIRUserVariable(_, _, func) or
@@ -79,7 +79,7 @@ class IRVariable extends TIRVariable {
7979
/**
8080
* Gets the function that references this variable.
8181
*/
82-
final Language::Function getEnclosingFunction() { result = func }
82+
final Language::Declaration getEnclosingFunction() { result = func }
8383
}
8484

8585
/**
@@ -246,7 +246,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
246246

247247
final override string toString() { result = "#ellipsis" }
248248

249-
final override int getIndex() { result = func.getNumberOfParameters() }
249+
final override int getIndex() { result = func.(Language::Function).getNumberOfParameters() }
250250
}
251251

252252
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/internal/TIRVariable.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ private import TIRVariableInternal
22
private import Imports::TempVariableTag
33

44
newtype TIRVariable =
5-
TIRUserVariable(Language::Variable var, Language::LanguageType type, Language::Function func) {
5+
TIRUserVariable(Language::Variable var, Language::LanguageType type, Language::Declaration func) {
66
Construction::hasUserVariable(func, var, type)
77
} or
88
TIRTempVariable(
9-
Language::Function func, Language::AST ast, TempVariableTag tag, Language::LanguageType type
9+
Language::Declaration func, Language::AST ast, TempVariableTag tag, Language::LanguageType type
1010
) {
1111
Construction::hasTempVariable(func, ast, tag, type)
1212
} or
1313
TIRDynamicInitializationFlag(
14-
Language::Function func, Language::Variable var, Language::LanguageType type
14+
Language::Declaration func, Language::Variable var, Language::LanguageType type
1515
) {
1616
Construction::hasDynamicInitializationFlag(func, var, type)
1717
} or
1818
TIRStringLiteral(
19-
Language::Function func, Language::AST ast, Language::LanguageType type,
19+
Language::Declaration func, Language::AST ast, Language::LanguageType type,
2020
Language::StringLiteral literal
2121
) {
2222
Construction::hasStringLiteral(func, ast, type, literal)

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRVariable.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private import Imports::IRType
1818
* by the AST-to-IR translation (`IRTempVariable`).
1919
*/
2020
class IRVariable extends TIRVariable {
21-
Language::Function func;
21+
Language::Declaration func;
2222

2323
IRVariable() {
2424
this = TIRUserVariable(_, _, func) or
@@ -79,7 +79,7 @@ class IRVariable extends TIRVariable {
7979
/**
8080
* Gets the function that references this variable.
8181
*/
82-
final Language::Function getEnclosingFunction() { result = func }
82+
final Language::Declaration getEnclosingFunction() { result = func }
8383
}
8484

8585
/**
@@ -246,7 +246,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
246246

247247
final override string toString() { result = "#ellipsis" }
248248

249-
final override int getIndex() { result = func.getNumberOfParameters() }
249+
final override int getIndex() { result = func.(Language::Function).getNumberOfParameters() }
250250
}
251251

252252
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class TranslatedGlobalOrNamespaceVarInit extends TranslatedRootElement,
8989

9090
override IRUserVariable getInstructionVariable(InstructionTag tag) {
9191
tag = InitializerVariableAddressTag() and
92-
result.getVariable() = var
92+
result.getVariable() = var and
93+
result.getEnclosingFunction() = var
9394
}
9495

9596
override Instruction getTargetAddress() {

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/IRVariable.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private import Imports::IRType
1818
* by the AST-to-IR translation (`IRTempVariable`).
1919
*/
2020
class IRVariable extends TIRVariable {
21-
Language::Function func;
21+
Language::Declaration func;
2222

2323
IRVariable() {
2424
this = TIRUserVariable(_, _, func) or
@@ -79,7 +79,7 @@ class IRVariable extends TIRVariable {
7979
/**
8080
* Gets the function that references this variable.
8181
*/
82-
final Language::Function getEnclosingFunction() { result = func }
82+
final Language::Declaration getEnclosingFunction() { result = func }
8383
}
8484

8585
/**
@@ -246,7 +246,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
246246

247247
final override string toString() { result = "#ellipsis" }
248248

249-
final override int getIndex() { result = func.getNumberOfParameters() }
249+
final override int getIndex() { result = func.(Language::Function).getNumberOfParameters() }
250250
}
251251

252252
/**

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-ir-consistency.expected

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
uniqueEnclosingCallable
22
| globals.cpp:9:5:9:19 | Address | Node should have one enclosing callable but has 0. |
3+
| globals.cpp:9:5:9:19 | AliasedDefinition | Node should have one enclosing callable but has 0. |
34
| globals.cpp:9:5:9:19 | VariableAddress | Node should have one enclosing callable but has 0. |
45
| globals.cpp:9:5:9:19 | VariableAddress [post update] | Node should have one enclosing callable but has 0. |
56
| globals.cpp:9:23:9:23 | 0 | Node should have one enclosing callable but has 0. |
67
| globals.cpp:9:23:9:23 | ChiPartial | Node should have one enclosing callable but has 0. |
8+
| globals.cpp:9:23:9:23 | ChiTotal | Node should have one enclosing callable but has 0. |
79
| globals.cpp:9:23:9:23 | Store | Node should have one enclosing callable but has 0. |
810
| globals.cpp:9:23:9:23 | StoreValue | Node should have one enclosing callable but has 0. |
911
| globals.cpp:16:12:16:26 | Address | Node should have one enclosing callable but has 0. |
12+
| globals.cpp:16:12:16:26 | AliasedDefinition | Node should have one enclosing callable but has 0. |
1013
| globals.cpp:16:12:16:26 | VariableAddress | Node should have one enclosing callable but has 0. |
1114
| globals.cpp:16:12:16:26 | VariableAddress [post update] | Node should have one enclosing callable but has 0. |
1215
| globals.cpp:16:30:16:30 | 0 | Node should have one enclosing callable but has 0. |
1316
| globals.cpp:16:30:16:30 | ChiPartial | Node should have one enclosing callable but has 0. |
17+
| globals.cpp:16:30:16:30 | ChiTotal | Node should have one enclosing callable but has 0. |
1418
| globals.cpp:16:30:16:30 | Store | Node should have one enclosing callable but has 0. |
1519
| globals.cpp:16:30:16:30 | StoreValue | Node should have one enclosing callable but has 0. |
1620
uniqueType
@@ -234,10 +238,10 @@ postWithInFlow
234238
| lambdas.cpp:20:11:20:11 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
235239
| lambdas.cpp:20:11:20:11 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
236240
| lambdas.cpp:20:11:20:11 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
241+
| lambdas.cpp:23:3:23:3 | (reference dereference) [post update] | PostUpdateNode should not be the target of local flow. |
237242
| lambdas.cpp:23:3:23:14 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
238243
| lambdas.cpp:23:3:23:14 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
239244
| lambdas.cpp:23:3:23:14 | v [post update] | PostUpdateNode should not be the target of local flow. |
240-
| lambdas.cpp:23:15:23:15 | (reference dereference) [post update] | PostUpdateNode should not be the target of local flow. |
241245
| lambdas.cpp:28:7:28:7 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
242246
| lambdas.cpp:28:10:31:2 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
243247
| lambdas.cpp:28:10:31:2 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4744,15 +4744,13 @@
47444744
| ir.cpp:1034:6:1034:20 | SideEffect | m1034_3 |
47454745
| ir.cpp:1035:15:1035:15 | Address | &:r1035_1 |
47464746
| ir.cpp:1038:6:1038:8 | Address | &:r1038_3 |
4747-
| ir.cpp:1038:6:1038:8 | SideEffect | ~m1038_9 |
4747+
| ir.cpp:1038:6:1038:8 | SideEffect | ~m1038_8 |
47484748
| ir.cpp:1038:12:1038:18 | Address | &:r1038_4 |
47494749
| ir.cpp:1038:12:1038:18 | Address | &:r1038_4 |
4750-
| ir.cpp:1038:12:1038:18 | ChiPartial | partial:m1038_5 |
4751-
| ir.cpp:1038:12:1038:18 | ChiPartial | partial:m1038_8 |
4750+
| ir.cpp:1038:12:1038:18 | ChiPartial | partial:m1038_7 |
47524751
| ir.cpp:1038:12:1038:18 | ChiTotal | total:m1038_2 |
4753-
| ir.cpp:1038:12:1038:18 | ChiTotal | total:m1038_6 |
4754-
| ir.cpp:1038:12:1038:18 | Load | ~m1038_6 |
4755-
| ir.cpp:1038:12:1038:18 | StoreValue | r1038_7 |
4752+
| ir.cpp:1038:12:1038:18 | Load | m1038_5 |
4753+
| ir.cpp:1038:12:1038:18 | StoreValue | r1038_6 |
47564754
| ir.cpp:1038:14:1038:14 | Address | &:r1038_5 |
47574755
| ir.cpp:1038:14:1038:14 | Address | &:r1038_5 |
47584756
| ir.cpp:1038:14:1038:14 | Address | &:r1038_5 |
@@ -4843,6 +4841,9 @@
48434841
| ir.cpp:1043:24:1043:24 | SideEffect | ~m1043_20 |
48444842
| ir.cpp:1043:31:1043:31 | Address | &:r1043_9 |
48454843
| ir.cpp:1043:36:1043:55 | Address | &:r1043_11 |
4844+
| ir.cpp:1043:43:1043:43 | Address | &:r1043_16 |
4845+
| ir.cpp:1043:43:1043:43 | Arg(this) | this:r1043_16 |
4846+
| ir.cpp:1043:43:1043:43 | SideEffect | ~m1043_20 |
48464847
| ir.cpp:1043:43:1043:54 | Address | &:r1043_22 |
48474848
| ir.cpp:1043:43:1043:54 | Address | &:r1043_24 |
48484849
| ir.cpp:1043:43:1043:54 | Address | &:r1043_25 |
@@ -4863,11 +4864,8 @@
48634864
| ir.cpp:1043:45:1043:49 | SideEffect | ~m1043_4 |
48644865
| ir.cpp:1043:45:1043:49 | Unary | r1043_13 |
48654866
| ir.cpp:1043:45:1043:49 | Unary | r1043_15 |
4866-
| ir.cpp:1043:52:1043:52 | Address | &:r1043_16 |
4867-
| ir.cpp:1043:52:1043:52 | Arg(this) | this:r1043_16 |
4868-
| ir.cpp:1043:52:1043:52 | SideEffect | ~m1043_20 |
4869-
| ir.cpp:1043:54:1043:54 | Load | ~m1043_20 |
4870-
| ir.cpp:1043:54:1043:54 | Right | r1043_26 |
4867+
| ir.cpp:1043:53:1043:53 | Load | ~m1043_20 |
4868+
| ir.cpp:1043:53:1043:53 | Right | r1043_26 |
48714869
| ir.cpp:1043:58:1043:58 | ChiPartial | partial:m1043_9 |
48724870
| ir.cpp:1043:58:1043:58 | ChiTotal | total:m1043_3 |
48734871
| ir.cpp:1043:58:1043:58 | StoreValue | r1043_8 |
@@ -4982,6 +4980,9 @@
49824980
| ir.cpp:1047:34:1047:34 | SideEffect | ~m1047_20 |
49834981
| ir.cpp:1047:41:1047:41 | Address | &:r1047_9 |
49844982
| ir.cpp:1047:46:1047:65 | Address | &:r1047_11 |
4983+
| ir.cpp:1047:53:1047:53 | Address | &:r1047_16 |
4984+
| ir.cpp:1047:53:1047:53 | Arg(this) | this:r1047_16 |
4985+
| ir.cpp:1047:53:1047:53 | SideEffect | ~m1047_20 |
49854986
| ir.cpp:1047:53:1047:64 | Address | &:r1047_23 |
49864987
| ir.cpp:1047:53:1047:64 | Load | ~m1047_20 |
49874988
| ir.cpp:1047:53:1047:64 | StoreValue | r1047_24 |
@@ -4996,9 +4997,6 @@
49964997
| ir.cpp:1047:55:1047:59 | SideEffect | ~m1047_4 |
49974998
| ir.cpp:1047:55:1047:59 | Unary | r1047_13 |
49984999
| ir.cpp:1047:55:1047:59 | Unary | r1047_15 |
4999-
| ir.cpp:1047:62:1047:62 | Address | &:r1047_16 |
5000-
| ir.cpp:1047:62:1047:62 | Arg(this) | this:r1047_16 |
5001-
| ir.cpp:1047:62:1047:62 | SideEffect | ~m1047_20 |
50025000
| ir.cpp:1047:63:1047:63 | Right | r1047_22 |
50035001
| ir.cpp:1047:68:1047:68 | StoreValue | r1047_8 |
50045002
| ir.cpp:1047:68:1047:68 | Unary | r1047_7 |
@@ -5107,6 +5105,9 @@
51075105
| ir.cpp:1051:39:1051:39 | SideEffect | ~m1051_20 |
51085106
| ir.cpp:1051:46:1051:46 | Address | &:r1051_9 |
51095107
| ir.cpp:1051:51:1051:70 | Address | &:r1051_11 |
5108+
| ir.cpp:1051:58:1051:58 | Address | &:r1051_16 |
5109+
| ir.cpp:1051:58:1051:58 | Arg(this) | this:r1051_16 |
5110+
| ir.cpp:1051:58:1051:58 | SideEffect | ~m1051_20 |
51105111
| ir.cpp:1051:58:1051:69 | Address | &:r1051_22 |
51115112
| ir.cpp:1051:58:1051:69 | Address | &:r1051_24 |
51125113
| ir.cpp:1051:58:1051:69 | Address | &:r1051_26 |
@@ -5127,9 +5128,6 @@
51275128
| ir.cpp:1051:60:1051:64 | SideEffect | ~m1051_4 |
51285129
| ir.cpp:1051:60:1051:64 | Unary | r1051_13 |
51295130
| ir.cpp:1051:60:1051:64 | Unary | r1051_15 |
5130-
| ir.cpp:1051:67:1051:67 | Address | &:r1051_16 |
5131-
| ir.cpp:1051:67:1051:67 | Arg(this) | this:r1051_16 |
5132-
| ir.cpp:1051:67:1051:67 | SideEffect | ~m1051_20 |
51335131
| ir.cpp:1051:73:1051:73 | ChiPartial | partial:m1051_9 |
51345132
| ir.cpp:1051:73:1051:73 | ChiTotal | total:m1051_3 |
51355133
| ir.cpp:1051:73:1051:73 | StoreValue | r1051_8 |
@@ -5194,6 +5192,9 @@
51945192
| ir.cpp:1054:49:1054:49 | SideEffect | ~m1054_20 |
51955193
| ir.cpp:1054:56:1054:56 | Address | &:r1054_9 |
51965194
| ir.cpp:1054:61:1054:88 | Address | &:r1054_11 |
5195+
| ir.cpp:1054:68:1054:68 | Address | &:r1054_16 |
5196+
| ir.cpp:1054:68:1054:68 | Arg(this) | this:r1054_16 |
5197+
| ir.cpp:1054:68:1054:68 | SideEffect | ~m1054_20 |
51975198
| ir.cpp:1054:68:1054:87 | Address | &:r1054_37 |
51985199
| ir.cpp:1054:68:1054:87 | Load | ~m1054_20 |
51995200
| ir.cpp:1054:68:1054:87 | StoreValue | r1054_38 |
@@ -5208,9 +5209,6 @@
52085209
| ir.cpp:1054:70:1054:74 | SideEffect | ~m1054_4 |
52095210
| ir.cpp:1054:70:1054:74 | Unary | r1054_13 |
52105211
| ir.cpp:1054:70:1054:74 | Unary | r1054_15 |
5211-
| ir.cpp:1054:77:1054:77 | Address | &:r1054_16 |
5212-
| ir.cpp:1054:77:1054:77 | Arg(this) | this:r1054_16 |
5213-
| ir.cpp:1054:77:1054:77 | SideEffect | ~m1054_20 |
52145212
| ir.cpp:1054:78:1054:82 | Address | &:r1054_22 |
52155213
| ir.cpp:1054:78:1054:82 | Address | &:r1054_24 |
52165214
| ir.cpp:1054:78:1054:82 | Left | r1054_25 |

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5652,16 +5652,16 @@ ir.cpp:
56525652

56535653
# 1038| (lambda [] type at line 1038, col. 12) lam
56545654
# 1038| Block 0
5655-
# 1038| v1038_1(void) = EnterFunction :
5656-
# 1038| mu1038_2(unknown) = AliasedDefinition :
5657-
# 1038| r1038_3(glval<decltype([...](...){...})>) = VariableAddress :
5658-
# 1038| r1038_4(glval<decltype([...](...){...})>) = VariableAddress :
5659-
# 1038| mu1038_5(decltype([...](...){...})) = Uninitialized : &:r1038_4
5660-
# 1038| r1038_6(decltype([...](...){...})) = Load[?] : &:r1038_4, ~m?
5661-
# 1038| mu1038_7(decltype([...](...){...})) = Store[?] : &:r1038_3, r1038_6
5662-
# 1038| v1038_8(void) = ReturnVoid :
5663-
# 1038| v1038_9(void) = AliasedUse : ~m?
5664-
# 1038| v1038_10(void) = ExitFunction :
5655+
# 1038| v1038_1(void) = EnterFunction :
5656+
# 1038| mu1038_2(unknown) = AliasedDefinition :
5657+
# 1038| r1038_3(glval<decltype([...](...){...})>) = VariableAddress :
5658+
# 1038| r1038_4(glval<decltype([...](...){...})>) = VariableAddress[#temp1038:12] :
5659+
# 1038| mu1038_5(decltype([...](...){...})) = Uninitialized[#temp1038:12] : &:r1038_4
5660+
# 1038| r1038_6(decltype([...](...){...})) = Load[#temp1038:12] : &:r1038_4, ~m?
5661+
# 1038| mu1038_7(decltype([...](...){...})) = Store[?] : &:r1038_3, r1038_6
5662+
# 1038| v1038_8(void) = ReturnVoid :
5663+
# 1038| v1038_9(void) = AliasedUse : ~m?
5664+
# 1038| v1038_10(void) = ExitFunction :
56655665

56665666
# 1038| void (lambda [] type at line 1038, col. 12)::operator()() const
56675667
# 1038| Block 0
@@ -9483,16 +9483,16 @@ ir.cpp:
94839483

94849484
# 1767| char* global_string
94859485
# 1767| Block 0
9486-
# 1767| v1767_1(void) = EnterFunction :
9487-
# 1767| mu1767_2(unknown) = AliasedDefinition :
9488-
# 1767| r1767_3(glval<char *>) = VariableAddress :
9489-
# 1767| r1767_4(glval<char[14]>) = StringConstant :
9490-
# 1767| r1767_5(char *) = Convert : r1767_4
9491-
# 1767| r1767_6(char *) = Convert : r1767_5
9492-
# 1767| mu1767_7(char *) = Store[?] : &:r1767_3, r1767_6
9493-
# 1767| v1767_8(void) = ReturnVoid :
9494-
# 1767| v1767_9(void) = AliasedUse : ~m?
9495-
# 1767| v1767_10(void) = ExitFunction :
9486+
# 1767| v1767_1(void) = EnterFunction :
9487+
# 1767| mu1767_2(unknown) = AliasedDefinition :
9488+
# 1767| r1767_3(glval<char *>) = VariableAddress :
9489+
# 1767| r1767_4(glval<char[14]>) = StringConstant["global string"] :
9490+
# 1767| r1767_5(char *) = Convert : r1767_4
9491+
# 1767| r1767_6(char *) = Convert : r1767_5
9492+
# 1767| mu1767_7(char *) = Store[?] : &:r1767_3, r1767_6
9493+
# 1767| v1767_8(void) = ReturnVoid :
9494+
# 1767| v1767_9(void) = AliasedUse : ~m?
9495+
# 1767| v1767_10(void) = ExitFunction :
94969496

94979497
perf-regression.cpp:
94989498
# 6| void Big::Big()
@@ -9723,7 +9723,7 @@ struct_init.cpp:
97239723
# 9| r9_4(int) = Constant[0] :
97249724
# 9| r9_5(glval<Info>) = PointerAdd[16] : r9_3, r9_4
97259725
# 10| r10_1(glval<char *>) = FieldAddress[name] : r9_5
9726-
# 10| r10_2(glval<char[2]>) = StringConstant :
9726+
# 10| r10_2(glval<char[2]>) = StringConstant["1"] :
97279727
# 10| r10_3(char *) = Convert : r10_2
97289728
# 10| mu10_4(char *) = Store[?] : &:r10_1, r10_3
97299729
# 10| r10_5(glval<..(*)(..)>) = FieldAddress[handler] : r9_5
@@ -9732,7 +9732,7 @@ struct_init.cpp:
97329732
# 9| r9_6(int) = Constant[1] :
97339733
# 9| r9_7(glval<Info>) = PointerAdd[16] : r9_3, r9_6
97349734
# 11| r11_1(glval<char *>) = FieldAddress[name] : r9_7
9735-
# 11| r11_2(glval<char[2]>) = StringConstant :
9735+
# 11| r11_2(glval<char[2]>) = StringConstant["3"] :
97369736
# 11| r11_3(char *) = Convert : r11_2
97379737
# 11| mu11_4(char *) = Store[?] : &:r11_1, r11_3
97389738
# 11| r11_5(glval<..(*)(..)>) = FieldAddress[handler] : r9_7

0 commit comments

Comments
 (0)