Skip to content

Commit 72e7235

Browse files
C++: Use glval<Unknown> as type of call target
Also shared some code between `TranslatedFunctionCall` and `TranslatedAllocatorCall`, and fixed dumps of glval<Unknown> to not print the size.
1 parent bba7f16 commit 72e7235

File tree

7 files changed

+770
-780
lines changed

7 files changed

+770
-780
lines changed

cpp/ql/src/semmle/code/cpp/ir/internal/Instruction.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class Instruction extends Construction::TInstruction {
195195
private string getResultTypeString() {
196196
exists(string valcat |
197197
valcat = getValueCategoryString(resultType.toString()) and
198-
if resultType instanceof UnknownType and exists(getResultSize()) then
198+
if (resultType instanceof UnknownType and
199+
not isGLValue() and
200+
exists(getResultSize())) then (
199201
result = valcat + "[" + getResultSize().toString() + "]"
202+
)
200203
else
201204
result = valcat
202205
)

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

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,26 +2088,11 @@ class TranslatedNonConstantAllocationSize extends TranslatedAllocationSize {
20882088
}
20892089

20902090
/**
2091-
* The IR translation of a call to `operator new` as part of a `new` or `new[]`
2092-
* expression.
2091+
* IR translation of a direct call to a specific function. Used for both
2092+
* explicit calls (`TranslatedFunctionCall`) and implicit calls
2093+
* (`TranslatedAllocatorCall`).
20932094
*/
2094-
class TranslatedAllocatorCall extends TTranslatedAllocatorCall,
2095-
TranslatedCall {
2096-
NewOrNewArrayExpr newExpr;
2097-
2098-
TranslatedAllocatorCall() {
2099-
this = TTranslatedAllocatorCall(newExpr) and
2100-
expr = newExpr
2101-
}
2102-
2103-
override final string toString() {
2104-
result = "Allocator call for " + newExpr.toString()
2105-
}
2106-
2107-
override final predicate producesExprResult() {
2108-
none()
2109-
}
2110-
2095+
abstract class TranslatedDirectCall extends TranslatedCall {
21112096
override final Instruction getFirstCallTargetInstruction() {
21122097
result = getInstruction(CallTargetTag())
21132098
}
@@ -2122,8 +2107,11 @@ class TranslatedAllocatorCall extends TTranslatedAllocatorCall,
21222107
(
21232108
tag = CallTargetTag() and
21242109
opcode instanceof Opcode::FunctionAddress and
2125-
resultType instanceof BoolType and //HACK
2126-
isGLValue = false
2110+
// The database does not contain a `FunctionType` for a function unless
2111+
// its address was taken, so we'll just use glval<Unknown> instead of
2112+
// glval<FunctionType>.
2113+
resultType instanceof UnknownType and
2114+
isGLValue = true
21272115
)
21282116
}
21292117

@@ -2136,6 +2124,28 @@ class TranslatedAllocatorCall extends TTranslatedAllocatorCall,
21362124
result = getFirstArgumentOrCallInstruction()
21372125
)
21382126
}
2127+
}
2128+
2129+
/**
2130+
* The IR translation of a call to `operator new` as part of a `new` or `new[]`
2131+
* expression.
2132+
*/
2133+
class TranslatedAllocatorCall extends TTranslatedAllocatorCall,
2134+
TranslatedDirectCall {
2135+
NewOrNewArrayExpr newExpr;
2136+
2137+
TranslatedAllocatorCall() {
2138+
this = TTranslatedAllocatorCall(newExpr) and
2139+
expr = newExpr
2140+
}
2141+
2142+
override final string toString() {
2143+
result = "Allocator call for " + newExpr.toString()
2144+
}
2145+
2146+
override final predicate producesExprResult() {
2147+
none()
2148+
}
21392149

21402150
override Function getInstructionFunction(InstructionTag tag) {
21412151
tag = CallTargetTag() and result = newExpr.getAllocator()
@@ -2220,42 +2230,13 @@ class TranslatedExprCall extends TranslatedCallExpr {
22202230
/**
22212231
* Represents the IR translation of a direct function call.
22222232
*/
2223-
class TranslatedFunctionCall extends TranslatedCallExpr {
2233+
class TranslatedFunctionCall extends TranslatedCallExpr, TranslatedDirectCall {
22242234
FunctionCall funcCall;
22252235

22262236
TranslatedFunctionCall() {
22272237
expr = funcCall
22282238
}
22292239

2230-
override final Instruction getFirstCallTargetInstruction() {
2231-
result = getInstruction(CallTargetTag())
2232-
}
2233-
2234-
override final Instruction getCallTargetResult() {
2235-
result = getInstruction(CallTargetTag())
2236-
}
2237-
2238-
override predicate hasInstruction(Opcode opcode, InstructionTag tag,
2239-
Type resultType, boolean isGLValue) {
2240-
super.hasInstruction(opcode, tag, resultType, isGLValue) or
2241-
(
2242-
tag = CallTargetTag() and
2243-
opcode instanceof Opcode::FunctionAddress and
2244-
resultType instanceof BoolType and //HACK
2245-
isGLValue = false
2246-
)
2247-
}
2248-
2249-
override Instruction getInstructionSuccessor(InstructionTag tag,
2250-
EdgeKind kind) {
2251-
result = super.getInstructionSuccessor(tag, kind) or
2252-
(
2253-
tag = CallTargetTag() and
2254-
kind instanceof GotoEdge and
2255-
result = getFirstArgumentOrCallInstruction()
2256-
)
2257-
}
2258-
22592240
override Function getInstructionFunction(InstructionTag tag) {
22602241
tag = CallTargetTag() and result = funcCall.getTarget()
22612242
}

cpp/ql/src/semmle/code/cpp/ssa/internal/aliased_ssa/Instruction.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class Instruction extends Construction::TInstruction {
195195
private string getResultTypeString() {
196196
exists(string valcat |
197197
valcat = getValueCategoryString(resultType.toString()) and
198-
if resultType instanceof UnknownType and exists(getResultSize()) then
198+
if (resultType instanceof UnknownType and
199+
not isGLValue() and
200+
exists(getResultSize())) then (
199201
result = valcat + "[" + getResultSize().toString() + "]"
202+
)
200203
else
201204
result = valcat
202205
)

cpp/ql/src/semmle/code/cpp/ssa/internal/ssa/Instruction.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class Instruction extends Construction::TInstruction {
195195
private string getResultTypeString() {
196196
exists(string valcat |
197197
valcat = getValueCategoryString(resultType.toString()) and
198-
if resultType instanceof UnknownType and exists(getResultSize()) then
198+
if (resultType instanceof UnknownType and
199+
not isGLValue() and
200+
exists(getResultSize())) then (
199201
result = valcat + "[" + getResultSize().toString() + "]"
202+
)
200203
else
201204
result = valcat
202205
)

0 commit comments

Comments
 (0)