Skip to content

Commit da66136

Browse files
authored
Merge pull request github#13911 from MathiasVP/fix-taint-for-frontend-upgrade
C++: Fix taint-flow in preparation for frontend upgrade
2 parents c0dec21 + 499b6f3 commit da66136

File tree

2 files changed

+40
-51
lines changed

2 files changed

+40
-51
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ predicate hasRawIndirectInstruction(Instruction instr, int indirectionIndex) {
9494

9595
cached
9696
private newtype TDefOrUseImpl =
97-
TDefImpl(Operand address, int indirectionIndex) {
98-
exists(Instruction base | isDef(_, _, address, base, _, indirectionIndex) |
97+
TDefImpl(BaseSourceVariableInstruction base, Operand address, int indirectionIndex) {
98+
isDef(_, _, address, base, _, indirectionIndex) and
99+
(
99100
// We only include the definition if the SSA pruning stage
100101
// concluded that the definition is live after the write.
101102
any(SsaInternals0::Def def).getAddressOperand() = address
@@ -105,8 +106,8 @@ private newtype TDefOrUseImpl =
105106
base.(VariableAddressInstruction).getAstVariable() instanceof GlobalLikeVariable
106107
)
107108
} or
108-
TUseImpl(Operand operand, int indirectionIndex) {
109-
isUse(_, operand, _, _, indirectionIndex) and
109+
TUseImpl(BaseSourceVariableInstruction base, Operand operand, int indirectionIndex) {
110+
isUse(_, operand, base, _, indirectionIndex) and
110111
not isDef(_, _, operand, _, _, _)
111112
} or
112113
TGlobalUse(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
@@ -193,7 +194,7 @@ abstract private class DefOrUseImpl extends TDefOrUseImpl {
193194

194195
/**
195196
* Gets the instruction that computes the base of this definition or use.
196-
* This is always a `VariableAddressInstruction` or an `AllocationInstruction`.
197+
* This is always a `VariableAddressInstruction` or an `CallInstruction`.
197198
*/
198199
abstract BaseSourceVariableInstruction getBase();
199200

@@ -265,15 +266,17 @@ abstract class DefImpl extends DefOrUseImpl {
265266
}
266267

267268
private class DirectDef extends DefImpl, TDefImpl {
268-
DirectDef() { this = TDefImpl(address, ind) }
269+
BaseSourceVariableInstruction base;
270+
271+
DirectDef() { this = TDefImpl(base, address, ind) }
269272

270-
override BaseSourceVariableInstruction getBase() { isDef(_, _, address, result, _, _) }
273+
override BaseSourceVariableInstruction getBase() { result = base }
271274

272-
override int getIndirection() { isDef(_, _, address, _, result, ind) }
275+
override int getIndirection() { isDef(_, _, address, base, result, ind) }
273276

274-
override Node0Impl getValue() { isDef(_, result, address, _, _, _) }
277+
override Node0Impl getValue() { isDef(_, result, address, base, _, _) }
275278

276-
override predicate isCertain() { isDef(true, _, address, _, _, ind) }
279+
override predicate isCertain() { isDef(true, _, address, base, _, ind) }
277280
}
278281

279282
private class IteratorDef extends DefImpl, TIteratorDef {
@@ -316,57 +319,52 @@ abstract class UseImpl extends DefOrUseImpl {
316319

317320
abstract private class OperandBasedUse extends UseImpl {
318321
Operand operand;
322+
BaseSourceVariableInstruction base;
319323

320324
bindingset[ind]
321325
OperandBasedUse() { any() }
322326

323327
final override predicate hasIndexInBlock(IRBlock block, int index) {
324328
// See the comment in `ssa0`'s `OperandBasedUse` for an explanation of this
325329
// predicate's implementation.
326-
exists(BaseSourceVariableInstruction base | base = this.getBase() |
327-
if base.getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
328-
then
329-
exists(Operand op, int indirectionIndex, int indirection |
330-
indirectionIndex = this.getIndirectionIndex() and
331-
indirection = this.getIndirection() and
332-
op =
333-
min(Operand cand, int i |
334-
isUse(_, cand, base, indirection, indirectionIndex) and
335-
block.getInstruction(i) = cand.getUse()
336-
|
337-
cand order by i
338-
) and
339-
block.getInstruction(index) = op.getUse()
340-
)
341-
else operand.getUse() = block.getInstruction(index)
342-
)
330+
if base.getAst() = any(Cpp::PostfixCrementOperation c).getOperand()
331+
then
332+
exists(Operand op, int indirectionIndex, int indirection |
333+
indirectionIndex = this.getIndirectionIndex() and
334+
indirection = this.getIndirection() and
335+
op =
336+
min(Operand cand, int i |
337+
isUse(_, cand, base, indirection, indirectionIndex) and
338+
block.getInstruction(i) = cand.getUse()
339+
|
340+
cand order by i
341+
) and
342+
block.getInstruction(index) = op.getUse()
343+
)
344+
else operand.getUse() = block.getInstruction(index)
343345
}
344346

347+
final override BaseSourceVariableInstruction getBase() { result = base }
348+
345349
final Operand getOperand() { result = operand }
346350

347351
final override Cpp::Location getLocation() { result = operand.getLocation() }
348352
}
349353

350354
private class DirectUse extends OperandBasedUse, TUseImpl {
351-
DirectUse() { this = TUseImpl(operand, ind) }
352-
353-
override int getIndirection() { isUse(_, operand, _, result, ind) }
355+
DirectUse() { this = TUseImpl(base, operand, ind) }
354356

355-
override BaseSourceVariableInstruction getBase() { isUse(_, operand, result, _, ind) }
357+
override int getIndirection() { isUse(_, operand, base, result, ind) }
356358

357-
override predicate isCertain() { isUse(true, operand, _, _, ind) }
359+
override predicate isCertain() { isUse(true, operand, base, _, ind) }
358360

359361
override Node getNode() { nodeHasOperand(result, operand, ind) }
360362
}
361363

362364
private class IteratorUse extends OperandBasedUse, TIteratorUse {
363-
BaseSourceVariableInstruction container;
364-
365-
IteratorUse() { this = TIteratorUse(operand, container, ind) }
365+
IteratorUse() { this = TIteratorUse(operand, base, ind) }
366366

367-
override int getIndirection() { isIteratorUse(container, operand, result, ind) }
368-
369-
override BaseSourceVariableInstruction getBase() { result = container }
367+
override int getIndirection() { isIteratorUse(base, operand, result, ind) }
370368

371369
override predicate isCertain() { none() }
372370

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,6 @@ int countIndirectionsForCppType(LanguageType langType) {
146146
)
147147
}
148148

149-
/**
150-
* A `CallInstruction` that calls an allocation function such
151-
* as `malloc` or `operator new`.
152-
*/
153-
class AllocationInstruction extends CallInstruction {
154-
AllocationInstruction() { this.getStaticCallTarget() instanceof Cpp::AllocationFunction }
155-
}
156-
157149
private predicate isIndirectionType(Type t) { t instanceof Indirection }
158150

159151
private predicate hasUnspecifiedBaseType(Indirection t, Type base) {
@@ -368,7 +360,7 @@ newtype TBaseSourceVariable =
368360
// Each IR variable gets its own source variable
369361
TBaseIRVariable(IRVariable var) or
370362
// Each allocation gets its own source variable
371-
TBaseCallVariable(AllocationInstruction call)
363+
TBaseCallVariable(CallInstruction call) { not call.getResultIRType() instanceof IRVoidType }
372364

373365
abstract private class AbstractBaseSourceVariable extends TBaseSourceVariable {
374366
/** Gets a textual representation of this element. */
@@ -396,11 +388,11 @@ class BaseIRVariable extends AbstractBaseSourceVariable, TBaseIRVariable {
396388
}
397389

398390
class BaseCallVariable extends AbstractBaseSourceVariable, TBaseCallVariable {
399-
AllocationInstruction call;
391+
CallInstruction call;
400392

401393
BaseCallVariable() { this = TBaseCallVariable(call) }
402394

403-
AllocationInstruction getCallInstruction() { result = call }
395+
CallInstruction getCallInstruction() { result = call }
404396

405397
override string toString() { result = call.toString() }
406398

@@ -504,8 +496,7 @@ private class BaseIRVariableInstruction extends BaseSourceVariableInstruction,
504496
override BaseIRVariable getBaseSourceVariable() { result.getIRVariable() = this.getIRVariable() }
505497
}
506498

507-
private class BaseAllocationInstruction extends BaseSourceVariableInstruction, AllocationInstruction
508-
{
499+
private class BaseCallInstruction extends BaseSourceVariableInstruction, CallInstruction {
509500
override BaseCallVariable getBaseSourceVariable() { result.getCallInstruction() = this }
510501
}
511502

0 commit comments

Comments
 (0)