Skip to content

Commit 47e16b0

Browse files
author
Dave Bartolomeo
committed
Move logic for determining CallSideEffect opcode out of TranslatedCall.
This is the first step to fixing the order of side effects on call instructions. The goal is to move all side effects (argument side effects, allocation side effects, and conservative call side effects) to be treated as elements in a single sequence of side effects, which will then be handled in a single place similar to how we already handle argument side effects.
1 parent d22620f commit 47e16b0

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ private predicate hasDefaultSideEffect(Call call, ParameterIndex i, boolean buff
111111
)
112112
}
113113

114+
/**
115+
* Gets the `SideEffectFunction` called by the specified expression.class, if known.
116+
*
117+
* Thie will return a result only for `Call`, in which case it returns the target of the call, or
118+
* for `NewExpr` and `NewArrayExpr`, in which case it returns the allocator function called by the
119+
* expression.
120+
*/
121+
private SideEffectFunction getCallOrAllocationSideEffectFunction(Expr expr) {
122+
result = expr.(Call).getTarget()
123+
or
124+
result = expr.(NewOrNewArrayExpr).getAllocator()
125+
}
126+
127+
/**
128+
* Returns the side effect opcode, if any, that represents any side effects not specifically modeled
129+
* by an argument side effect.
130+
*/
131+
Opcode getCallSideEffectOpcode(Expr expr) {
132+
if not getCallOrAllocationSideEffectFunction(expr).hasOnlySpecificWriteSideEffects()
133+
then result instanceof Opcode::CallSideEffect
134+
else (
135+
not getCallOrAllocationSideEffectFunction(expr).hasOnlySpecificReadSideEffects() and
136+
result instanceof Opcode::CallReadSideEffect
137+
)
138+
}
139+
114140
/**
115141
* Returns a side effect opcode for parameter index `i` of the specified call.
116142
*

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@ abstract class TranslatedCall extends TranslatedExpr {
5050
opcode instanceof Opcode::Call and
5151
resultType = getTypeForPRValue(getCallResultType())
5252
or
53-
hasSideEffect() and
5453
tag = CallSideEffectTag() and
54+
opcode = getCallSideEffectOpcode(expr) and
5555
(
56-
if hasWriteSideEffect()
57-
then (
58-
opcode instanceof Opcode::CallSideEffect and
59-
resultType = getUnknownType()
60-
) else (
61-
opcode instanceof Opcode::CallReadSideEffect and
62-
resultType = getVoidType()
63-
)
56+
opcode instanceof Opcode::CallSideEffect and
57+
resultType = getUnknownType()
58+
or
59+
opcode instanceof Opcode::CallReadSideEffect and
60+
resultType = getVoidType()
6461
)
6562
}
6663

@@ -200,11 +197,7 @@ abstract class TranslatedCall extends TranslatedExpr {
200197
*/
201198
abstract predicate hasArguments();
202199

203-
predicate hasReadSideEffect() { any() }
204-
205-
predicate hasWriteSideEffect() { any() }
206-
207-
private predicate hasSideEffect() { hasReadSideEffect() or hasWriteSideEffect() }
200+
final private predicate hasSideEffect() { exists(getCallSideEffectOpcode(expr)) }
208201

209202
override Instruction getPrimaryInstructionForSideEffect(InstructionTag tag) {
210203
hasSideEffect() and
@@ -325,14 +318,6 @@ class TranslatedFunctionCall extends TranslatedCallExpr, TranslatedDirectCall {
325318
tag = CallTargetTag() and result = expr.getTarget()
326319
}
327320

328-
override predicate hasReadSideEffect() {
329-
not expr.getTarget().(SideEffectFunction).hasOnlySpecificReadSideEffects()
330-
}
331-
332-
override predicate hasWriteSideEffect() {
333-
not expr.getTarget().(SideEffectFunction).hasOnlySpecificWriteSideEffects()
334-
}
335-
336321
override Instruction getQualifierResult() {
337322
hasQualifier() and
338323
result = getQualifier().getResult()

0 commit comments

Comments
 (0)