Skip to content

Commit 6034eb0

Browse files
committed
C++: Change the API for indirect operands and indirection instructions to not allow pointer conflation.
1 parent 9e9c811 commit 6034eb0

File tree

1 file changed

+67
-64
lines changed

1 file changed

+67
-64
lines changed

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

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -193,86 +193,89 @@ private class SingleUseOperandNode0 extends OperandNode0, TSingleUseOperandNode0
193193
SingleUseOperandNode0() { this = TSingleUseOperandNode0(op) }
194194
}
195195

196-
/**
197-
* INTERNAL: Do not use.
198-
*
199-
* A node that represents the indirect value of an operand in the IR
200-
* after `index` number of loads.
201-
*
202-
* Note: Unlike `RawIndirectOperand`, a value of type `IndirectOperand` may
203-
* be an `OperandNode`.
204-
*/
205-
class IndirectOperand extends Node {
206-
Operand operand;
207-
int indirectionIndex;
196+
private module IndirectOperands {
197+
/**
198+
* INTERNAL: Do not use.
199+
*
200+
* A node that represents the indirect value of an operand in the IR
201+
* after `index` number of loads.
202+
*
203+
* Note: Unlike `RawIndirectOperand`, a value of type `IndirectOperand` may
204+
* be an `OperandNode`.
205+
*/
206+
abstract class IndirectOperand extends Node {
207+
/** Gets the underlying operand. */
208+
abstract predicate hasOperandAndIndirectionIndex(Operand operand, int indirectionIndex);
209+
}
208210

209-
IndirectOperand() {
210-
this.(RawIndirectOperand).getOperand() = operand and
211-
this.(RawIndirectOperand).getIndirectionIndex() = indirectionIndex
212-
or
213-
nodeHasOperand(this, Ssa::getIRRepresentationOfIndirectOperand(operand, indirectionIndex),
214-
indirectionIndex - 1)
211+
private class IndirectOperandFromRaw extends IndirectOperand instanceof RawIndirectOperand {
212+
override predicate hasOperandAndIndirectionIndex(Operand operand, int indirectionIndex) {
213+
operand = RawIndirectOperand.super.getOperand() and
214+
indirectionIndex = RawIndirectOperand.super.getIndirectionIndex()
215+
}
215216
}
216217

217-
/** Gets the underlying operand. */
218-
Operand getOperand() { result = operand }
218+
private class IndirectOperandFromIRRepr extends IndirectOperand {
219+
Operand operand;
220+
int indirectionIndex;
219221

220-
/** Gets the underlying indirection index. */
221-
int getIndirectionIndex() { result = indirectionIndex }
222+
IndirectOperandFromIRRepr() {
223+
exists(Operand repr |
224+
repr = Ssa::getIRRepresentationOfIndirectOperand(operand, indirectionIndex) and
225+
nodeHasOperand(this, repr, indirectionIndex - 1)
226+
)
227+
}
222228

223-
/**
224-
* Holds if this `IndirectOperand` is represented directly in the IR instead of
225-
* a `RawIndirectionOperand` with operand `op` and indirection index `index`.
226-
*/
227-
predicate isIRRepresentationOf(Operand op, int index) {
228-
this instanceof OperandNode and
229-
(
230-
op = operand and
231-
index = indirectionIndex
232-
)
229+
override predicate hasOperandAndIndirectionIndex(Operand op, int index) {
230+
op = operand and index = indirectionIndex
231+
}
233232
}
234233
}
235234

236-
/**
237-
* INTERNAL: Do not use.
238-
*
239-
* A node that represents the indirect value of an instruction in the IR
240-
* after `index` number of loads.
241-
*
242-
* Note: Unlike `RawIndirectInstruction`, a value of type `IndirectInstruction` may
243-
* be an `InstructionNode`.
244-
*/
245-
class IndirectInstruction extends Node {
246-
Instruction instr;
247-
int indirectionIndex;
235+
import IndirectOperands
248236

249-
IndirectInstruction() {
250-
this.(RawIndirectInstruction).getInstruction() = instr and
251-
this.(RawIndirectInstruction).getIndirectionIndex() = indirectionIndex
252-
or
253-
nodeHasInstruction(this, Ssa::getIRRepresentationOfIndirectInstruction(instr, indirectionIndex),
254-
indirectionIndex - 1)
237+
private module IndirectInstructions {
238+
/**
239+
* INTERNAL: Do not use.
240+
*
241+
* A node that represents the indirect value of an instruction in the IR
242+
* after `index` number of loads.
243+
*
244+
* Note: Unlike `RawIndirectInstruction`, a value of type `IndirectInstruction` may
245+
* be an `InstructionNode`.
246+
*/
247+
abstract class IndirectInstruction extends Node {
248+
/** Gets the underlying instruction. */
249+
abstract predicate hasInstructionAndIndirectionIndex(Instruction instr, int index);
255250
}
256251

257-
/** Gets the underlying instruction. */
258-
Instruction getInstruction() { result = instr }
252+
private class IndirectInstructionFromRaw extends IndirectInstruction instanceof RawIndirectInstruction
253+
{
254+
override predicate hasInstructionAndIndirectionIndex(Instruction instr, int index) {
255+
instr = RawIndirectInstruction.super.getInstruction() and
256+
index = RawIndirectInstruction.super.getIndirectionIndex()
257+
}
258+
}
259259

260-
/** Gets the underlying indirection index. */
261-
int getIndirectionIndex() { result = indirectionIndex }
260+
private class IndirectInstructionFromIRRepr extends IndirectInstruction {
261+
Instruction instr;
262+
int indirectionIndex;
262263

263-
/**
264-
* Holds if this `IndirectInstruction` is represented directly in the IR instead of
265-
* a `RawIndirectionInstruction` with instruction `i` and indirection index `index`.
266-
*/
267-
predicate isIRRepresentationOf(Instruction i, int index) {
268-
this instanceof InstructionNode and
269-
(
270-
i = instr and
271-
index = indirectionIndex
272-
)
264+
IndirectInstructionFromIRRepr() {
265+
exists(Instruction repr |
266+
repr = Ssa::getIRRepresentationOfIndirectInstruction(instr, indirectionIndex) and
267+
nodeHasInstruction(this, repr, indirectionIndex - 1)
268+
)
269+
}
270+
271+
override predicate hasInstructionAndIndirectionIndex(Instruction i, int index) {
272+
i = instr and index = indirectionIndex
273+
}
273274
}
274275
}
275276

277+
import IndirectInstructions
278+
276279
/** Gets the callable in which this node occurs. */
277280
DataFlowCallable nodeGetEnclosingCallable(Node n) { result = n.getEnclosingCallable() }
278281

0 commit comments

Comments
 (0)