Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0878d7

Browse files
author
Robert Marsh
committed
C++: Fix IR variable reuse for global var inits
1 parent 767b0cf commit e0878d7

12 files changed

Lines changed: 83 additions & 80 deletions

File tree

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: 4 additions & 6 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 |

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
@@ -9785,16 +9785,16 @@ ir.cpp:
97859785

97869786
# 1829| char* global_string
97879787
# 1829| Block 0
9788-
# 1829| v1829_1(void) = EnterFunction :
9789-
# 1829| mu1829_2(unknown) = AliasedDefinition :
9790-
# 1829| r1829_3(glval<char *>) = VariableAddress :
9791-
# 1829| r1829_4(glval<char[14]>) = StringConstant :
9792-
# 1829| r1829_5(char *) = Convert : r1829_4
9793-
# 1829| r1829_6(char *) = Convert : r1829_5
9794-
# 1829| mu1829_7(char *) = Store[?] : &:r1829_3, r1829_6
9795-
# 1829| v1829_8(void) = ReturnVoid :
9796-
# 1829| v1829_9(void) = AliasedUse : ~m?
9797-
# 1829| v1829_10(void) = ExitFunction :
9788+
# 1829| v1829_1(void) = EnterFunction :
9789+
# 1829| mu1829_2(unknown) = AliasedDefinition :
9790+
# 1829| r1829_3(glval<char *>) = VariableAddress :
9791+
# 1829| r1829_4(glval<char[14]>) = StringConstant["global string"] :
9792+
# 1829| r1829_5(char *) = Convert : r1829_4
9793+
# 1829| r1829_6(char *) = Convert : r1829_5
9794+
# 1829| mu1829_7(char *) = Store[?] : &:r1829_3, r1829_6
9795+
# 1829| v1829_8(void) = ReturnVoid :
9796+
# 1829| v1829_9(void) = AliasedUse : ~m?
9797+
# 1829| v1829_10(void) = ExitFunction :
97989798

97999799
perf-regression.cpp:
98009800
# 6| void Big::Big()
@@ -10025,7 +10025,7 @@ struct_init.cpp:
1002510025
# 9| r9_4(int) = Constant[0] :
1002610026
# 9| r9_5(glval<Info>) = PointerAdd[16] : r9_3, r9_4
1002710027
# 10| r10_1(glval<char *>) = FieldAddress[name] : r9_5
10028-
# 10| r10_2(glval<char[2]>) = StringConstant :
10028+
# 10| r10_2(glval<char[2]>) = StringConstant["1"] :
1002910029
# 10| r10_3(char *) = Convert : r10_2
1003010030
# 10| mu10_4(char *) = Store[?] : &:r10_1, r10_3
1003110031
# 10| r10_5(glval<..(*)(..)>) = FieldAddress[handler] : r9_5
@@ -10034,7 +10034,7 @@ struct_init.cpp:
1003410034
# 9| r9_6(int) = Constant[1] :
1003510035
# 9| r9_7(glval<Info>) = PointerAdd[16] : r9_3, r9_6
1003610036
# 11| r11_1(glval<char *>) = FieldAddress[name] : r9_7
10037-
# 11| r11_2(glval<char[2]>) = StringConstant :
10037+
# 11| r11_2(glval<char[2]>) = StringConstant["3"] :
1003810038
# 11| r11_3(char *) = Convert : r11_2
1003910039
# 11| mu11_4(char *) = Store[?] : &:r11_1, r11_3
1004010040
# 11| r11_5(glval<..(*)(..)>) = FieldAddress[handler] : r9_7

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ test.cpp:
7171

7272
# 10| int global01
7373
# 10| Block 0
74-
# 10| v10_1(void) = EnterFunction :
75-
# 10| m10_2(unknown) = AliasedDefinition :
74+
# 10| v10_1(void) = EnterFunction :
75+
# 10| m10_2(unknown) = AliasedDefinition :
7676
# 10| valnum = unique
77-
# 10| r10_3(glval<int>) = VariableAddress[global01] :
77+
# 10| r10_3(glval<int>) = VariableAddress :
7878
# 10| valnum = unique
79-
# 10| r10_4(int) = Constant[1] :
79+
# 10| r10_4(int) = Constant[1] :
8080
# 10| valnum = m10_5, r10_4
81-
# 10| m10_5(int) = Store[global01] : &:r10_3, r10_4
81+
# 10| m10_5(int) = Store[?] : &:r10_3, r10_4
8282
# 10| valnum = m10_5, r10_4
83-
# 10| m10_6(unknown) = Chi : total:~m?, partial:m10_5
83+
# 10| m10_6(unknown) = Chi : total:m10_2, partial:m10_5
8484
# 10| valnum = unique
85-
# 10| v10_7(void) = ReturnVoid :
86-
# 10| v10_8(void) = AliasedUse : ~m10_2
87-
# 10| v10_9(void) = ExitFunction :
85+
# 10| v10_7(void) = ReturnVoid :
86+
# 10| v10_8(void) = AliasedUse : ~m10_6
87+
# 10| v10_9(void) = ExitFunction :
8888

8989
# 12| void test01(int, int)
9090
# 12| Block 0
@@ -170,20 +170,20 @@ test.cpp:
170170

171171
# 21| int global02
172172
# 21| Block 0
173-
# 21| v21_1(void) = EnterFunction :
174-
# 21| m21_2(unknown) = AliasedDefinition :
173+
# 21| v21_1(void) = EnterFunction :
174+
# 21| m21_2(unknown) = AliasedDefinition :
175175
# 21| valnum = unique
176-
# 21| r21_3(glval<int>) = VariableAddress[global02] :
176+
# 21| r21_3(glval<int>) = VariableAddress :
177177
# 21| valnum = unique
178-
# 21| r21_4(int) = Constant[2] :
178+
# 21| r21_4(int) = Constant[2] :
179179
# 21| valnum = m21_5, r21_4
180-
# 21| m21_5(int) = Store[global02] : &:r21_3, r21_4
180+
# 21| m21_5(int) = Store[?] : &:r21_3, r21_4
181181
# 21| valnum = m21_5, r21_4
182-
# 21| m21_6(unknown) = Chi : total:~m?, partial:m21_5
182+
# 21| m21_6(unknown) = Chi : total:m21_2, partial:m21_5
183183
# 21| valnum = unique
184-
# 21| v21_7(void) = ReturnVoid :
185-
# 21| v21_8(void) = AliasedUse : ~m21_2
186-
# 21| v21_9(void) = ExitFunction :
184+
# 21| v21_7(void) = ReturnVoid :
185+
# 21| v21_8(void) = AliasedUse : ~m21_6
186+
# 21| v21_9(void) = ExitFunction :
187187

188188
# 25| void test02(int, int)
189189
# 25| Block 0
@@ -276,20 +276,20 @@ test.cpp:
276276

277277
# 35| int global03
278278
# 35| Block 0
279-
# 35| v35_1(void) = EnterFunction :
280-
# 35| m35_2(unknown) = AliasedDefinition :
279+
# 35| v35_1(void) = EnterFunction :
280+
# 35| m35_2(unknown) = AliasedDefinition :
281281
# 35| valnum = unique
282-
# 35| r35_3(glval<int>) = VariableAddress[global03] :
282+
# 35| r35_3(glval<int>) = VariableAddress :
283283
# 35| valnum = unique
284-
# 35| r35_4(int) = Constant[3] :
284+
# 35| r35_4(int) = Constant[3] :
285285
# 35| valnum = m35_5, r35_4
286-
# 35| m35_5(int) = Store[global03] : &:r35_3, r35_4
286+
# 35| m35_5(int) = Store[?] : &:r35_3, r35_4
287287
# 35| valnum = m35_5, r35_4
288-
# 35| m35_6(unknown) = Chi : total:~m?, partial:m35_5
288+
# 35| m35_6(unknown) = Chi : total:m35_2, partial:m35_5
289289
# 35| valnum = unique
290-
# 35| v35_7(void) = ReturnVoid :
291-
# 35| v35_8(void) = AliasedUse : ~m35_2
292-
# 35| v35_9(void) = ExitFunction :
290+
# 35| v35_7(void) = ReturnVoid :
291+
# 35| v35_8(void) = AliasedUse : ~m35_6
292+
# 35| v35_9(void) = ExitFunction :
293293

294294
# 39| void test03(int, int, int*)
295295
# 39| Block 0

csharp/ql/src/experimental/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)

0 commit comments

Comments
 (0)