Skip to content

Commit 9a45c55

Browse files
committed
C++: Move Load from AssignmentOperation to its LHS
This is analogous to what was done for `CrementOperation`.
1 parent 53b1068 commit 9a45c55

File tree

6 files changed

+43
-50
lines changed

6 files changed

+43
-50
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ newtype TInstructionTag =
1212
ZeroPadStringElementIndexTag() or
1313
ZeroPadStringElementAddressTag() or
1414
ZeroPadStringStoreTag() or
15-
AssignOperationLoadTag() or
1615
AssignOperationConvertLeftTag() or
1716
AssignOperationOpTag() or
1817
AssignOperationConvertResultTag() or
@@ -93,8 +92,6 @@ string getInstructionTagId(TInstructionTag tag) {
9392
or
9493
tag = ZeroPadStringStoreTag() and result = "ZeroPadStore"
9594
or
96-
tag = AssignOperationLoadTag() and result = "AssignOpLoad"
97-
or
9895
tag = AssignOperationConvertLeftTag() and result = "AssignOpConvLeft"
9996
or
10097
tag = AssignOperationOpTag() and result = "AssignOpOp"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ private predicate ignoreLoad(Expr expr) {
229229
*/
230230
private predicate needsLoadForParentExpr(Expr expr) {
231231
exists(CrementOperation crement | expr = crement.getOperand().getFullyConverted())
232+
or
233+
exists(AssignOperation ao | expr = ao.getLValue().getFullyConverted())
232234
}
233235

234236
/**

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

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
12991299
override AssignOperation expr;
13001300

13011301
final override TranslatedElement getChild(int id) {
1302-
id = 0 and result = getLeftOperand()
1302+
id = 0 and result = getLoadedLeftOperand()
13031303
or
13041304
id = 1 and result = getRightOperand()
13051305
}
@@ -1320,27 +1320,30 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
13201320
else
13211321
// This is C++, where the result is an lvalue for the left operand,
13221322
// and that lvalue is not being loaded as part of this expression.
1323-
result = getLeftOperand().getResult()
1323+
result = getUnloadedLeftOperand().getResult()
13241324
}
13251325

1326-
final TranslatedExpr getLeftOperand() {
1326+
final TranslatedExpr getUnloadedLeftOperand() { result = getLoadedLeftOperand().getOperand() }
1327+
1328+
/**
1329+
* Gets the `TranslatedLoad` on the `e` in this `e += ...` which is the
1330+
* element that holds the value to be cremented. It's guaranteed that there's
1331+
* a load on `e` because of the `needsLoadForParentExpr` predicate.
1332+
*/
1333+
final TranslatedLoad getLoadedLeftOperand() {
13271334
result = getTranslatedExpr(expr.getLValue().getFullyConverted())
13281335
}
13291336

1337+
/**
1338+
* Gets the address to which the result of this operation will be stored.
1339+
*/
13301340
final TranslatedExpr getRightOperand() {
13311341
result = getTranslatedExpr(expr.getRValue().getFullyConverted())
13321342
}
13331343

13341344
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
13351345
kind instanceof GotoEdge and
13361346
(
1337-
(
1338-
tag = AssignOperationLoadTag() and
1339-
if leftOperandNeedsConversion()
1340-
then result = getInstruction(AssignOperationConvertLeftTag())
1341-
else result = getInstruction(AssignOperationOpTag())
1342-
)
1343-
or
13441347
tag = AssignOperationConvertLeftTag() and
13451348
result = getInstruction(AssignOperationOpTag())
13461349
or
@@ -1362,10 +1365,12 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
13621365
override Instruction getChildSuccessor(TranslatedElement child) {
13631366
// Operands are evaluated right-to-left.
13641367
child = getRightOperand() and
1365-
result = getLeftOperand().getFirstInstruction()
1368+
result = getLoadedLeftOperand().getFirstInstruction()
13661369
or
1367-
child = getLeftOperand() and
1368-
result = getInstruction(AssignOperationLoadTag())
1370+
child = getLoadedLeftOperand() and
1371+
if leftOperandNeedsConversion()
1372+
then result = getInstruction(AssignOperationConvertLeftTag())
1373+
else result = getInstruction(AssignOperationOpTag())
13691374
}
13701375

13711376
private Instruction getStoredValue() {
@@ -1388,14 +1393,14 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
13881393
// anyway. If we really want to model this case perfectly, we'll need the
13891394
// extractor to tell us what the promoted type of the left operand would
13901395
// be.
1391-
result = getLeftOperand().getExpr().getType()
1396+
result = getLoadedLeftOperand().getExpr().getType()
13921397
else
13931398
// The right operand has already been converted to the type of the op.
13941399
result = getRightOperand().getExpr().getType()
13951400
}
13961401

13971402
private predicate leftOperandNeedsConversion() {
1398-
getConvertedLeftOperandType().getUnspecifiedType() != getLeftOperand()
1403+
getConvertedLeftOperandType().getUnspecifiedType() != getLoadedLeftOperand()
13991404
.getExpr()
14001405
.getUnspecifiedType()
14011406
}
@@ -1427,10 +1432,6 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
14271432
}
14281433

14291434
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
1430-
tag = AssignOperationLoadTag() and
1431-
opcode instanceof Opcode::Load and
1432-
resultType = getTypeForPRValue(getLeftOperand().getExpr().getType())
1433-
or
14341435
tag = AssignOperationOpTag() and
14351436
opcode = getOpcode() and
14361437
resultType = getTypeForPRValue(getConvertedLeftOperandType())
@@ -1446,7 +1447,7 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
14461447
resultType = getTypeForPRValue(getConvertedLeftOperandType())
14471448
or
14481449
tag = AssignOperationConvertResultTag() and
1449-
resultType = getTypeForPRValue(getLeftOperand().getExpr().getType())
1450+
resultType = getTypeForPRValue(getLoadedLeftOperand().getExpr().getType())
14501451
)
14511452
}
14521453

@@ -1460,27 +1461,18 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
14601461
}
14611462

14621463
override Instruction getInstructionOperand(InstructionTag tag, OperandTag operandTag) {
1463-
tag = AssignOperationLoadTag() and
1464-
(
1465-
operandTag instanceof AddressOperandTag and
1466-
result = getLeftOperand().getResult()
1467-
or
1468-
operandTag instanceof LoadOperandTag and
1469-
result = getEnclosingFunction().getUnmodeledDefinitionInstruction()
1470-
)
1471-
or
14721464
leftOperandNeedsConversion() and
14731465
tag = AssignOperationConvertLeftTag() and
14741466
operandTag instanceof UnaryOperandTag and
1475-
result = getInstruction(AssignOperationLoadTag())
1467+
result = getLoadedLeftOperand().getResult()
14761468
or
14771469
tag = AssignOperationOpTag() and
14781470
(
14791471
(
14801472
operandTag instanceof LeftOperandTag and
14811473
if leftOperandNeedsConversion()
14821474
then result = getInstruction(AssignOperationConvertLeftTag())
1483-
else result = getInstruction(AssignOperationLoadTag())
1475+
else result = getLoadedLeftOperand().getResult()
14841476
)
14851477
or
14861478
operandTag instanceof RightOperandTag and
@@ -1495,7 +1487,7 @@ class TranslatedAssignOperation extends TranslatedNonConstantExpr {
14951487
tag = AssignmentStoreTag() and
14961488
(
14971489
operandTag instanceof AddressOperandTag and
1498-
result = getLeftOperand().getResult()
1490+
result = getUnloadedLeftOperand().getResult()
14991491
or
15001492
operandTag instanceof StoreValueOperandTag and
15011493
result = getStoredValue()

cpp/ql/test/library-tests/dataflow/crement/crements.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ void test_crement() {
66
x2++;
77

88
int x3 = 0;
9-
x3 -= 1; // flow [NOT DETECTED]
9+
x3 -= 1; // flow
1010

1111
int x4 = 0;
12-
x4 |= 1; // flow [NOT DETECTED]
12+
x4 |= 1; // flow
1313

1414
int x5 = 0;
1515
x5 = x5 - 1; // flow (to RHS)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| crements.cpp:3:5:3:6 | x1 |
22
| crements.cpp:6:3:6:4 | x2 |
3+
| crements.cpp:9:3:9:4 | x3 |
4+
| crements.cpp:12:3:12:4 | x4 |
35
| crements.cpp:15:8:15:9 | x5 |

cpp/ql/test/library-tests/rangeanalysis/signanalysis/SignAnalysis.expected

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
| test.c:33:26:33:26 | Load: i | positive |
7272
| test.c:33:26:33:28 | Add: ... + ... | positive strictlyPositive |
7373
| test.c:33:28:33:28 | Constant: 1 | positive strictlyPositive |
74+
| test.c:34:5:34:9 | Load: total | positive |
7475
| test.c:34:5:34:14 | Add: ... += ... | positive |
75-
| test.c:34:5:34:14 | Load: ... += ... | positive |
7676
| test.c:34:5:34:14 | Store: ... += ... | positive |
7777
| test.c:34:14:34:14 | Load: i | positive |
7878
| test.c:36:10:36:14 | Load: total | positive |
@@ -87,8 +87,8 @@
8787
| test.c:42:22:42:24 | Add: ... ++ | positive strictlyPositive |
8888
| test.c:42:22:42:24 | Constant: ... ++ | positive strictlyPositive |
8989
| test.c:42:22:42:24 | Store: ... ++ | positive strictlyPositive |
90+
| test.c:43:5:43:9 | Load: total | positive |
9091
| test.c:43:5:43:14 | Add: ... += ... | positive |
91-
| test.c:43:5:43:14 | Load: ... += ... | positive |
9292
| test.c:43:5:43:14 | Store: ... += ... | positive |
9393
| test.c:43:14:43:14 | Load: i | positive |
9494
| test.c:45:10:45:14 | Load: total | positive |
@@ -105,8 +105,8 @@
105105
| test.c:51:28:51:28 | Load: i | positive |
106106
| test.c:51:28:51:30 | Add: ... + ... | positive strictlyPositive |
107107
| test.c:51:30:51:30 | Constant: 1 | positive strictlyPositive |
108+
| test.c:52:5:52:9 | Load: total | positive |
108109
| test.c:52:5:52:14 | Add: ... += ... | positive |
109-
| test.c:52:5:52:14 | Load: ... += ... | positive |
110110
| test.c:52:5:52:14 | Store: ... += ... | positive |
111111
| test.c:52:14:52:14 | Load: i | positive |
112112
| test.c:54:10:54:14 | Load: total | positive |
@@ -148,8 +148,8 @@
148148
| test.c:124:36:124:36 | Constant: (unsigned long long)... | positive strictlyPositive |
149149
| test.c:126:31:126:43 | Call: call to test12_helper | positive |
150150
| test.c:126:31:126:43 | Store: call to test12_helper | positive |
151+
| test.c:127:6:127:10 | Load: Start | positive |
151152
| test.c:127:6:127:24 | Add: ... += ... | positive strictlyPositive |
152-
| test.c:127:6:127:24 | Load: ... += ... | positive |
153153
| test.c:127:6:127:24 | Store: ... += ... | positive strictlyPositive |
154154
| test.c:127:15:127:20 | Load: Length | positive |
155155
| test.c:127:15:127:24 | Add: ... + ... | positive strictlyPositive |
@@ -253,8 +253,8 @@
253253
| test.c:205:13:205:15 | Mul: ... * ... | positive |
254254
| test.c:205:13:205:15 | Store: ... * ... | positive |
255255
| test.c:205:15:205:15 | Load: b | positive |
256+
| test.c:206:5:206:9 | Load: total | positive |
256257
| test.c:206:5:206:14 | Add: ... += ... | positive |
257-
| test.c:206:5:206:14 | Load: ... += ... | positive |
258258
| test.c:206:5:206:14 | Store: ... += ... | positive |
259259
| test.c:206:14:206:14 | Load: r | positive |
260260
| test.c:208:7:208:7 | Constant: 3 | positive strictlyPositive |
@@ -264,7 +264,7 @@
264264
| test.c:208:28:208:30 | Constant: - ... | negative strictlyNegative |
265265
| test.c:208:45:208:46 | Constant: 23 | positive strictlyPositive |
266266
| test.c:209:13:209:13 | Load: a | positive strictlyPositive |
267-
| test.c:210:5:210:14 | Load: ... += ... | positive |
267+
| test.c:210:5:210:9 | Load: total | positive |
268268
| test.c:212:7:212:7 | Constant: 3 | positive strictlyPositive |
269269
| test.c:212:17:212:17 | Load: a | positive strictlyPositive |
270270
| test.c:212:22:212:23 | Constant: 11 | positive strictlyPositive |
@@ -305,8 +305,8 @@
305305
| test.c:233:13:233:15 | Mul: ... * ... | positive |
306306
| test.c:233:13:233:15 | Store: ... * ... | positive |
307307
| test.c:233:15:233:15 | Load: b | positive |
308+
| test.c:234:5:234:9 | Load: total | positive |
308309
| test.c:234:5:234:14 | Add: ... += ... | positive |
309-
| test.c:234:5:234:14 | Load: ... += ... | positive |
310310
| test.c:234:5:234:14 | Store: ... += ... | positive |
311311
| test.c:234:14:234:14 | Load: r | positive |
312312
| test.c:236:7:236:7 | Phi: 0 | positive |
@@ -315,7 +315,7 @@
315315
| test.c:236:28:236:30 | Constant: - ... | negative strictlyNegative |
316316
| test.c:236:45:236:46 | Constant: 23 | positive strictlyPositive |
317317
| test.c:237:13:237:13 | Load: a | positive |
318-
| test.c:238:5:238:14 | Load: ... += ... | positive |
318+
| test.c:238:5:238:9 | Load: total | positive |
319319
| test.c:240:17:240:17 | Load: a | positive |
320320
| test.c:240:22:240:23 | Constant: 11 | positive strictlyPositive |
321321
| test.c:240:28:240:30 | Constant: - ... | negative strictlyNegative |
@@ -376,16 +376,16 @@
376376
| test.c:289:13:289:15 | Mul: ... * ... | negative |
377377
| test.c:289:13:289:15 | Store: ... * ... | negative |
378378
| test.c:289:15:289:15 | Load: b | positive |
379+
| test.c:290:5:290:9 | Load: total | negative |
379380
| test.c:290:5:290:14 | Add: ... += ... | negative |
380-
| test.c:290:5:290:14 | Load: ... += ... | negative |
381381
| test.c:290:5:290:14 | Store: ... += ... | negative |
382382
| test.c:290:14:290:14 | Load: r | negative |
383383
| test.c:292:7:292:9 | Constant: - ... | negative strictlyNegative |
384384
| test.c:292:7:292:9 | Phi: - ... | negative |
385385
| test.c:292:29:292:31 | Constant: - ... | negative strictlyNegative |
386386
| test.c:292:46:292:47 | Constant: 23 | positive strictlyPositive |
387387
| test.c:293:13:293:13 | Load: a | negative |
388-
| test.c:294:5:294:14 | Load: ... += ... | negative |
388+
| test.c:294:5:294:9 | Load: total | negative |
389389
| test.c:296:7:296:9 | Constant: - ... | negative strictlyNegative |
390390
| test.c:296:29:296:31 | Constant: - ... | negative strictlyNegative |
391391
| test.c:297:13:297:13 | Load: a | negative |
@@ -422,8 +422,8 @@
422422
| test.c:317:13:317:15 | Mul: ... * ... | negative |
423423
| test.c:317:13:317:15 | Store: ... * ... | negative |
424424
| test.c:317:15:317:15 | Load: b | positive |
425+
| test.c:318:5:318:9 | Load: total | negative |
425426
| test.c:318:5:318:14 | Add: ... += ... | negative |
426-
| test.c:318:5:318:14 | Load: ... += ... | negative |
427427
| test.c:318:5:318:14 | Store: ... += ... | negative |
428428
| test.c:318:14:318:14 | Load: r | negative |
429429
| test.c:320:7:320:9 | Constant: - ... | negative strictlyNegative |
@@ -432,7 +432,7 @@
432432
| test.c:320:30:320:32 | Constant: - ... | negative strictlyNegative |
433433
| test.c:320:47:320:48 | Constant: 23 | positive strictlyPositive |
434434
| test.c:321:13:321:13 | Load: a | negative strictlyNegative |
435-
| test.c:322:5:322:14 | Load: ... += ... | negative |
435+
| test.c:322:5:322:9 | Load: total | negative |
436436
| test.c:324:7:324:9 | Constant: - ... | negative strictlyNegative |
437437
| test.c:324:24:324:25 | Constant: - ... | negative strictlyNegative |
438438
| test.c:324:30:324:32 | Constant: - ... | negative strictlyNegative |
@@ -657,8 +657,8 @@
657657
| test.c:398:9:398:11 | Constant: ... ++ | positive strictlyPositive |
658658
| test.c:398:9:398:11 | Store: ... ++ | positive strictlyPositive |
659659
| test.c:398:9:398:22 | CopyValue: ... , ... | positive strictlyPositive |
660+
| test.c:398:14:398:14 | Load: y | positive strictlyPositive |
660661
| test.c:398:14:398:19 | Add: ... += ... | positive strictlyPositive |
661-
| test.c:398:14:398:19 | Load: ... += ... | positive strictlyPositive |
662662
| test.c:398:14:398:19 | Store: ... += ... | positive strictlyPositive |
663663
| test.c:398:19:398:19 | Constant: (unsigned int)... | positive strictlyPositive |
664664
| test.c:398:22:398:22 | Load: y | positive strictlyPositive |

0 commit comments

Comments
 (0)