Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ExprNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,49 @@ private module Cached {
result = any(InitializeDynamicAllocationInstruction init).getAllocationAddressOperand()
}

/** Holds if `instr` computes the result of a `ReuseExpr`. */
private predicate isReuse(Instruction instr) {
instr.getUnconvertedResultExpression() instanceof ReuseExpr
}

/**
* Holds if `op` is the operand of an instruction representing a
* `ReuseExpr`, and the reused value is also accessed elsewhere.
*/
private predicate isReuseOfAccessedValue(Operand op) {
isReuse(op.getUse()) and
exists(Operand other |
other = getAUse(op.getDef()) and
other != op and
not other = any(StoreInstruction store).getDestinationAddressOperand()
)
}

/**
* Holds if `opFrom` is the source operand of the `StoreInstruction` that
* initializes a temporary object, and `instrTo` computes the address of the
* temporary.
*/
private predicate initializesTemporaryObject(Operand opFrom, Instruction instrTo) {
exists(StoreInstruction store |
opFrom = store.getSourceValueOperand() and
instrTo = store.getDestinationAddress() and
instrTo.getConvertedResultExpression() =
IRConstruction::Raw::getTemporaryObjectInitializationStoreExpr(store)
)
}

/**
* Holds if `opFrom` flows to `instrTo` through a step that corresponds to a
* conversion in the AST.
*/
private predicate exprConversionFlow(Operand opFrom, Instruction instrTo) {
conversionFlow(opFrom, instrTo, false, false) and
not isReuseOfAccessedValue(opFrom)
or
initializesTemporaryObject(opFrom, instrTo)
}

/**
* Gets the expression that should be returned as the result expression from `instr`.
*
Expand All @@ -23,7 +66,7 @@ private module Cached {
*/
private Expr getConvertedResultExpression(Instruction instr, int n) {
// Only fully converted instructions have a result for `asConvertedExpr`
not conversionFlow(unique(Operand op |
not exprConversionFlow(unique(Operand op |
// The address operand of a `InitializeDynamicAllocationInstruction` is
// special: we need to handle it during dataflow (since it's
// effectively a store to an indirection), but it doesn't appear in
Expand All @@ -32,12 +75,12 @@ private module Cached {
op = getAUse(instr) and not op = getAnInitializeDynamicAllocationInstructionAddress()
|
op
), _, false, false) and
), _) and
result = getConvertedResultExpressionImpl(instr) and
n = 0
or
// If the conversion also has a result then we return multiple results
exists(Operand operand | conversionFlow(operand, instr, false, false) |
exists(Operand operand | exprConversionFlow(operand, instr) |
n = 1 and
result = getConvertedResultExpressionImpl(operand.getDef())
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ module Raw {
)
}

/**
* Gets the `TemporaryObjectExpr` whose temporary object is initialized by
* `store`.
*/
cached
TemporaryObjectExpr getTemporaryObjectInitializationStoreExpr(StoreInstruction store) {
exists(TranslatedTemporaryObjectExpr ttoe |
store = ttoe.getInitialization().getInstruction(InitializerStoreTag()) and
result = ttoe.getExpr()
)
}

/** Gets the instruction that computes the address used to initialize `v`. */
cached
Instruction getInitializationTargetAddress(IRVariable v) {
Expand Down
19 changes: 19 additions & 0 deletions cpp/ql/test/library-tests/dataflow/asExpr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,22 @@ void test_parameters(
int *p, // $ asParameter=p parameterNode=p
int *&rp) { // $ asParameter=rp(*rp) parameterNode=rp(*rp)
}

struct NoDestructor {
void f();
};

NoDestructor get_no_destructor();

struct HasDestructor {
~HasDestructor();
void f();
};

HasDestructor get_has_destructor();

void test_temporaries() {
get_no_destructor().f(); // $ asExpr="call to get_no_destructor" asIndirectExpr="call to get_no_destructor"
get_has_destructor().f(); // $ asExpr="call to get_has_destructor" asExpr="reuse of temporary object" asIndirectExpr="call to get_has_destructor" asIndirectExpr="reuse of temporary object"
get_has_destructor(); // $ asExpr="call to get_has_destructor(reuse of temporary object)" asExpr="reuse of temporary object" asIndirectExpr="call to get_has_destructor(reuse of temporary object)" asIndirectExpr="reuse of temporary object"
}
Loading