Skip to content

Commit cd574e8

Browse files
authored
Merge pull request github#3589 from rdmarsh2/ir-placement-new-consistency
C++: fix IR control flow for cast in placement new
2 parents 91da0d5 + e17adf1 commit cd574e8

File tree

7 files changed

+62
-10
lines changed

7 files changed

+62
-10
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,8 @@ class TranslatedAllocatorCall extends TTranslatedAllocatorCall, TranslatedDirect
16981698
else
16991699
if index = 1 and expr.hasAlignedAllocation()
17001700
then result = getTranslatedExpr(expr.getAlignmentArgument())
1701-
else result = getTranslatedExpr(expr.getAllocatorCall().getArgument(index))
1701+
else
1702+
result = getTranslatedExpr(expr.getAllocatorCall().getArgument(index).getFullyConverted())
17021703
}
17031704
}
17041705

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10483,6 +10483,35 @@ ir.cpp:
1048310483
# 1315| 2: [VariableAccess] y
1048410484
# 1315| Type = [IntType] int
1048510485
# 1315| ValueCategory = prvalue(load)
10486+
# 1318| [Operator,TopLevelFunction] void* operator new(size_t, void*)
10487+
# 1318| params:
10488+
# 1318| 0: [Parameter] p#0
10489+
# 1318| Type = [CTypedefType,Size_t] size_t
10490+
# 1318| 1: [Parameter] p#1
10491+
# 1318| Type = [VoidPointerType] void *
10492+
# 1320| [TopLevelFunction] void f(int*)
10493+
# 1320| params:
10494+
# 1320| 0: [Parameter] p
10495+
# 1320| Type = [IntPointerType] int *
10496+
# 1321| body: [Block] { ... }
10497+
# 1322| 0: [ExprStmt] ExprStmt
10498+
# 1322| 0: [NewExpr] new
10499+
# 1322| Type = [IntPointerType] int *
10500+
# 1322| ValueCategory = prvalue
10501+
# 1322| 0: [FunctionCall] call to operator new
10502+
# 1322| Type = [VoidPointerType] void *
10503+
# 1322| ValueCategory = prvalue
10504+
# 1322| 0: [ErrorExpr] <error expr>
10505+
# 1322| Type = [LongType] unsigned long
10506+
# 1322| ValueCategory = prvalue
10507+
# 1322| 1: [CStyleCast] (void *)...
10508+
# 1322| Conversion = [PointerConversion] pointer conversion
10509+
# 1322| Type = [VoidPointerType] void *
10510+
# 1322| ValueCategory = prvalue
10511+
# 1322| expr: [VariableAccess] p
10512+
# 1322| Type = [IntPointerType] int *
10513+
# 1322| ValueCategory = prvalue(load)
10514+
# 1323| 1: [ReturnStmt] return ...
1048610515
perf-regression.cpp:
1048710516
# 4| [CopyAssignmentOperator] Big& Big::operator=(Big const&)
1048810517
# 4| params:

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,4 +1315,11 @@ int shortCircuitConditional(int x, int y) {
13151315
return predicateA() && predicateB() ? x : y;
13161316
}
13171317

1318+
void *operator new(size_t, void *) noexcept;
1319+
1320+
void f(int* p)
1321+
{
1322+
new (p) int;
1323+
}
1324+
13181325
// semmle-extractor-options: -std=c++17 --clang

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7369,6 +7369,30 @@ ir.cpp:
73697369
# 1314| v1314_10(void) = AliasedUse : ~m?
73707370
# 1314| v1314_11(void) = ExitFunction :
73717371

7372+
# 1320| void f(int*)
7373+
# 1320| Block 0
7374+
# 1320| v1320_1(void) = EnterFunction :
7375+
# 1320| mu1320_2(unknown) = AliasedDefinition :
7376+
# 1320| mu1320_3(unknown) = InitializeNonLocal :
7377+
# 1320| r1320_4(glval<int *>) = VariableAddress[p] :
7378+
# 1320| mu1320_5(int *) = InitializeParameter[p] : &:r1320_4
7379+
# 1320| r1320_6(int *) = Load : &:r1320_4, ~m?
7380+
# 1320| mu1320_7(unknown) = InitializeIndirection[p] : &:r1320_6
7381+
# 1322| r1322_1(glval<unknown>) = FunctionAddress[operator new] :
7382+
# 1322| r1322_2(unsigned long) = Constant[4] :
7383+
# 1322| r1322_3(glval<int *>) = VariableAddress[p] :
7384+
# 1322| r1322_4(int *) = Load : &:r1322_3, ~m?
7385+
# 1322| r1322_5(void *) = Convert : r1322_4
7386+
# 1322| r1322_6(void *) = Call : func:r1322_1, 0:r1322_2, 1:r1322_5
7387+
# 1322| mu1322_7(unknown) = ^CallSideEffect : ~m?
7388+
# 1322| mu1322_8(unknown) = ^InitializeDynamicAllocation : &:r1322_6
7389+
# 1322| r1322_9(int *) = Convert : r1322_6
7390+
# 1323| v1323_1(void) = NoOp :
7391+
# 1320| v1320_8(void) = ReturnIndirection[p] : &:r1320_6, ~m?
7392+
# 1320| v1320_9(void) = ReturnVoid :
7393+
# 1320| v1320_10(void) = AliasedUse : ~m?
7394+
# 1320| v1320_11(void) = ExitFunction :
7395+
73727396
perf-regression.cpp:
73737397
# 6| void Big::Big()
73747398
# 6| Block 0

cpp/ql/test/library-tests/syntax-zoo/aliased_ssa_consistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ instructionWithoutSuccessor
3939
| condition_decls.cpp:26:23:26:24 | Chi: call to BoxedInt |
4040
| condition_decls.cpp:41:22:41:23 | Chi: call to BoxedInt |
4141
| condition_decls.cpp:48:52:48:53 | Chi: call to BoxedInt |
42-
| cpp17.cpp:15:11:15:21 | Convert: (void *)... |
4342
| misc.c:171:10:171:13 | Uninitialized: definition of str2 |
4443
| misc.c:219:47:219:48 | InitializeIndirection: sp |
4544
| ms_try_except.cpp:3:9:3:9 | Uninitialized: definition of x |
@@ -156,8 +155,6 @@ ambiguousSuccessors
156155
| constructorinitializer.cpp:6:6:6:6 | Chi: f | Goto | 14 | pmcallexpr.cpp:7:5:7:5 | VariableAddress: definition of c |
157156
| constructorinitializer.cpp:6:6:6:6 | Chi: f | Goto | 14 | staticmembercallexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
158157
| constructorinitializer.cpp:6:6:6:6 | Chi: f | Goto | 14 | staticmembercallexpr_args.cpp:8:6:8:6 | VariableAddress: definition of i |
159-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:5:15:45 | Call: new |
160-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:11:15:21 | Convert: (void *)... |
161158
| defconstructornewexpr.cpp:3:6:3:6 | Chi: f | Goto | 14 | array_delete.cpp:6:12:6:24 | Constant: (Foo *)... |
162159
| defconstructornewexpr.cpp:3:6:3:6 | Chi: f | Goto | 14 | assignexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
163160
| defconstructornewexpr.cpp:3:6:3:6 | Chi: f | Goto | 14 | constmemberaccess.cpp:7:5:7:5 | VariableAddress: definition of c |

cpp/ql/test/library-tests/syntax-zoo/raw_consistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ instructionWithoutSuccessor
5252
| condition_decls.cpp:41:22:41:23 | IndirectMayWriteSideEffect: call to BoxedInt |
5353
| condition_decls.cpp:48:52:48:53 | IndirectMayWriteSideEffect: call to BoxedInt |
5454
| cpp17.cpp:15:5:15:45 | InitializeDynamicAllocation: new |
55-
| cpp17.cpp:15:11:15:21 | Convert: (void *)... |
5655
| enum.c:6:9:6:9 | Constant: (int)... |
5756
| file://:0:0:0:0 | CompareNE: (bool)... |
5857
| file://:0:0:0:0 | CompareNE: (bool)... |
@@ -208,8 +207,6 @@ ambiguousSuccessors
208207
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | pmcallexpr.cpp:7:5:7:5 | VariableAddress: definition of c |
209208
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | staticmembercallexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
210209
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | staticmembercallexpr_args.cpp:8:6:8:6 | VariableAddress: definition of i |
211-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:5:15:45 | Call: new |
212-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:11:15:21 | Convert: (void *)... |
213210
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | array_delete.cpp:6:12:6:24 | Constant: (Foo *)... |
214211
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | assignexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
215212
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | constmemberaccess.cpp:7:5:7:5 | VariableAddress: definition of c |

cpp/ql/test/library-tests/syntax-zoo/unaliased_ssa_consistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ instructionWithoutSuccessor
3939
| condition_decls.cpp:26:23:26:24 | IndirectMayWriteSideEffect: call to BoxedInt |
4040
| condition_decls.cpp:41:22:41:23 | IndirectMayWriteSideEffect: call to BoxedInt |
4141
| condition_decls.cpp:48:52:48:53 | IndirectMayWriteSideEffect: call to BoxedInt |
42-
| cpp17.cpp:15:11:15:21 | Convert: (void *)... |
4342
| misc.c:171:10:171:13 | Uninitialized: definition of str2 |
4443
| misc.c:219:47:219:48 | InitializeIndirection: sp |
4544
| ms_try_except.cpp:3:9:3:9 | Uninitialized: definition of x |
@@ -156,8 +155,6 @@ ambiguousSuccessors
156155
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | pmcallexpr.cpp:7:5:7:5 | VariableAddress: definition of c |
157156
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | staticmembercallexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
158157
| constructorinitializer.cpp:6:6:6:6 | InitializeNonLocal: f | Goto | 14 | staticmembercallexpr_args.cpp:8:6:8:6 | VariableAddress: definition of i |
159-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:5:15:45 | Call: new |
160-
| cpp17.cpp:15:19:15:21 | Load: ptr | Goto | 2 | cpp17.cpp:15:11:15:21 | Convert: (void *)... |
161158
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | array_delete.cpp:6:12:6:24 | Constant: (Foo *)... |
162159
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | assignexpr.cpp:7:4:7:4 | VariableAddress: definition of c |
163160
| defconstructornewexpr.cpp:3:6:3:6 | InitializeNonLocal: f | Goto | 14 | constmemberaccess.cpp:7:5:7:5 | VariableAddress: definition of c |

0 commit comments

Comments
 (0)