mirror of
https://github.com/github/codeql.git
synced 2025-12-23 12:16:33 +01:00
C++: Fix enclosing functions for static locals.
This commit is contained in:
@@ -180,7 +180,7 @@ abstract class TranslatedSideEffects extends TranslatedElement {
|
|||||||
/** DEPRECATED: Alias for getAst */
|
/** DEPRECATED: Alias for getAst */
|
||||||
deprecated override Locatable getAST() { result = getAst() }
|
deprecated override Locatable getAST() { result = getAst() }
|
||||||
|
|
||||||
final override Declaration getFunction() { result = getExpr().getEnclosingDeclaration() }
|
final override Declaration getFunction() { result = getEnclosingDeclaration(getExpr()) }
|
||||||
|
|
||||||
final override TranslatedElement getChild(int i) {
|
final override TranslatedElement getChild(int i) {
|
||||||
result =
|
result =
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ abstract class TranslatedCondition extends TranslatedElement {
|
|||||||
|
|
||||||
final Expr getExpr() { result = expr }
|
final Expr getExpr() { result = expr }
|
||||||
|
|
||||||
final override Function getFunction() { result = expr.getEnclosingFunction() }
|
final override Function getFunction() { result = getEnclosingFunction(expr) }
|
||||||
|
|
||||||
final Type getResultType() { result = expr.getUnspecifiedType() }
|
final Type getResultType() { result = expr.getUnspecifiedType() }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,9 +28,14 @@ abstract class TranslatedDeclarationEntry extends TranslatedElement, TTranslated
|
|||||||
|
|
||||||
TranslatedDeclarationEntry() { this = TTranslatedDeclarationEntry(entry) }
|
TranslatedDeclarationEntry() { this = TTranslatedDeclarationEntry(entry) }
|
||||||
|
|
||||||
final override Function getFunction() {
|
final override Declaration getFunction() {
|
||||||
exists(DeclStmt stmt |
|
exists(DeclStmt stmt | stmt = entry.getStmt() |
|
||||||
stmt = entry.getStmt() and
|
result = entry.getDeclaration().(StaticInitializedStaticLocalVariable)
|
||||||
|
or
|
||||||
|
result = entry.getDeclaration().(GlobalOrNamespaceVariable)
|
||||||
|
or
|
||||||
|
not entry.getDeclaration() instanceof StaticInitializedStaticLocalVariable and
|
||||||
|
not entry.getDeclaration() instanceof GlobalOrNamespaceVariable and
|
||||||
result = stmt.getEnclosingFunction()
|
result = stmt.getEnclosingFunction()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -237,7 +242,7 @@ class TranslatedStaticLocalVariableInitialization extends TranslatedElement,
|
|||||||
|
|
||||||
final override LocalVariable getVariable() { result = var }
|
final override LocalVariable getVariable() { result = var }
|
||||||
|
|
||||||
final override Function getFunction() { result = var.getFunction() }
|
final override Declaration getFunction() { result = var.getFunction() }
|
||||||
}
|
}
|
||||||
|
|
||||||
TranslatedConditionDecl getTranslatedConditionDecl(ConditionDeclExpr expr) {
|
TranslatedConditionDecl getTranslatedConditionDecl(ConditionDeclExpr expr) {
|
||||||
@@ -264,7 +269,7 @@ class TranslatedConditionDecl extends TranslatedLocalVariableDeclaration, TTrans
|
|||||||
/** DEPRECATED: Alias for getAst */
|
/** DEPRECATED: Alias for getAst */
|
||||||
deprecated override Locatable getAST() { result = getAst() }
|
deprecated override Locatable getAST() { result = getAst() }
|
||||||
|
|
||||||
override Function getFunction() { result = conditionDeclExpr.getEnclosingFunction() }
|
override Declaration getFunction() { result = getEnclosingFunction(conditionDeclExpr) }
|
||||||
|
|
||||||
override LocalVariable getVariable() { result = conditionDeclExpr.getVariable() }
|
override LocalVariable getVariable() { result = conditionDeclExpr.getVariable() }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,8 +109,8 @@ private predicate ignoreExprOnly(Expr expr) {
|
|||||||
// should not be translated.
|
// should not be translated.
|
||||||
exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0))
|
exists(NewOrNewArrayExpr new | expr = new.getAllocatorCall().getArgument(0))
|
||||||
or
|
or
|
||||||
not translateFunction(expr.getEnclosingFunction()) and
|
not translateFunction(getEnclosingFunction(expr)) and
|
||||||
not Raw::varHasIRFunc(expr.getEnclosingVariable())
|
not Raw::varHasIRFunc(getEnclosingVariable(expr))
|
||||||
or
|
or
|
||||||
// We do not yet translate destructors properly, so for now we ignore the
|
// We do not yet translate destructors properly, so for now we ignore the
|
||||||
// destructor call. We do, however, translate the expression being
|
// destructor call. We do, however, translate the expression being
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ abstract class TranslatedExpr extends TranslatedElement {
|
|||||||
/** DEPRECATED: Alias for getAst */
|
/** DEPRECATED: Alias for getAst */
|
||||||
deprecated override Locatable getAST() { result = this.getAst() }
|
deprecated override Locatable getAST() { result = this.getAst() }
|
||||||
|
|
||||||
final override Declaration getFunction() { result = expr.getEnclosingDeclaration() }
|
final override Declaration getFunction() { result = getEnclosingDeclaration(expr) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the expression from which this `TranslatedExpr` is generated.
|
* Gets the expression from which this `TranslatedExpr` is generated.
|
||||||
@@ -90,12 +90,57 @@ abstract class TranslatedExpr extends TranslatedElement {
|
|||||||
* Gets the `TranslatedFunction` containing this expression.
|
* Gets the `TranslatedFunction` containing this expression.
|
||||||
*/
|
*/
|
||||||
final TranslatedRootElement getEnclosingFunction() {
|
final TranslatedRootElement getEnclosingFunction() {
|
||||||
result = getTranslatedFunction(expr.getEnclosingFunction())
|
result = getTranslatedFunction(getEnclosingFunction(expr))
|
||||||
or
|
or
|
||||||
result = getTranslatedVarInit(expr.getEnclosingVariable())
|
result = getTranslatedVarInit(getEnclosingVariable(expr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function getEnclosingFunction(Expr e) {
|
||||||
|
not exists(getEnclosingVariable(e)) and
|
||||||
|
result = e.getEnclosingFunction()
|
||||||
|
}
|
||||||
|
|
||||||
|
Declaration getEnclosingDeclaration0(Expr e) {
|
||||||
|
result = getEnclosingDeclaration0(e.getParentWithConversions())
|
||||||
|
or
|
||||||
|
exists(Initializer i, Variable v |
|
||||||
|
i.getExpr().getFullyConverted() = e and
|
||||||
|
v = i.getDeclaration()
|
||||||
|
|
|
||||||
|
if v instanceof StaticInitializedStaticLocalVariable or v instanceof GlobalOrNamespaceVariable
|
||||||
|
then result = v
|
||||||
|
else result = e.getEnclosingDeclaration()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Declaration getEnclosingDeclaration(Expr e) {
|
||||||
|
result = getEnclosingDeclaration0(e)
|
||||||
|
or
|
||||||
|
not exists(getEnclosingDeclaration0(e)) and
|
||||||
|
result = e.getEnclosingDeclaration()
|
||||||
|
}
|
||||||
|
|
||||||
|
Variable getEnclosingVariable0(Expr e) {
|
||||||
|
result = getEnclosingVariable0(e.getParentWithConversions())
|
||||||
|
or
|
||||||
|
exists(Initializer i, Variable v |
|
||||||
|
i.getExpr().getFullyConverted() = e and
|
||||||
|
v = i.getDeclaration()
|
||||||
|
|
|
||||||
|
if v instanceof StaticInitializedStaticLocalVariable or v instanceof GlobalOrNamespaceVariable
|
||||||
|
then result = v
|
||||||
|
else result = e.getEnclosingVariable()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Variable getEnclosingVariable(Expr e) {
|
||||||
|
result = getEnclosingVariable0(e)
|
||||||
|
or
|
||||||
|
not exists(getEnclosingVariable0(e)) and
|
||||||
|
result = e.getEnclosingVariable()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The IR translation of the "core" part of an expression. This is the part of
|
* The IR translation of the "core" part of an expression. This is the part of
|
||||||
* the expression that produces the result value of the expression, before any
|
* the expression that produces the result value of the expression, before any
|
||||||
@@ -843,7 +888,7 @@ class TranslatedNonFieldVariableAccess extends TranslatedVariableAccess {
|
|||||||
|
|
||||||
override IRVariable getInstructionVariable(InstructionTag tag) {
|
override IRVariable getInstructionVariable(InstructionTag tag) {
|
||||||
tag = OnlyInstructionTag() and
|
tag = OnlyInstructionTag() and
|
||||||
result = getIRUserVariable(expr.getEnclosingDeclaration(), expr.getTarget())
|
result = getIRUserVariable(getEnclosingDeclaration(expr), expr.getTarget())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2000,7 +2045,7 @@ class TranslatedDestructorFieldDestruction extends TranslatedNonConstantExpr, St
|
|||||||
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
|
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
|
||||||
tag = OnlyInstructionTag() and
|
tag = OnlyInstructionTag() and
|
||||||
operandTag instanceof UnaryOperandTag and
|
operandTag instanceof UnaryOperandTag and
|
||||||
result = getTranslatedFunction(expr.getEnclosingFunction()).getInitializeThisInstruction()
|
result = getTranslatedFunction(getEnclosingFunction(expr)).getInitializeThisInstruction()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override Field getInstructionField(InstructionTag tag) {
|
final override Field getInstructionField(InstructionTag tag) {
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ class TranslatedFunction extends TranslatedRootElement, TTranslatedFunction {
|
|||||||
) and
|
) and
|
||||||
exists(VariableAccess access |
|
exists(VariableAccess access |
|
||||||
access.getTarget() = var and
|
access.getTarget() = var and
|
||||||
access.getEnclosingFunction() = func
|
getEnclosingFunction(access) = func
|
||||||
)
|
)
|
||||||
or
|
or
|
||||||
var.(LocalScopeVariable).getFunction() = func
|
var.(LocalScopeVariable).getFunction() = func
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import semmle.code.cpp.ir.implementation.raw.internal.TranslatedElement
|
import semmle.code.cpp.ir.implementation.raw.internal.TranslatedElement
|
||||||
|
private import TranslatedExpr
|
||||||
private import cpp
|
private import cpp
|
||||||
private import semmle.code.cpp.ir.implementation.IRType
|
private import semmle.code.cpp.ir.implementation.IRType
|
||||||
private import semmle.code.cpp.ir.implementation.Opcode
|
private import semmle.code.cpp.ir.implementation.Opcode
|
||||||
@@ -117,7 +118,7 @@ class TranslatedStaticStorageDurationVarInit extends TranslatedRootElement,
|
|||||||
) and
|
) and
|
||||||
exists(VariableAccess access |
|
exists(VariableAccess access |
|
||||||
access.getTarget() = varUsed and
|
access.getTarget() = varUsed and
|
||||||
access.getEnclosingVariable() = var
|
getEnclosingVariable(access) = var
|
||||||
)
|
)
|
||||||
or
|
or
|
||||||
var = varUsed
|
var = varUsed
|
||||||
|
|||||||
@@ -138,9 +138,9 @@ abstract class TranslatedInitialization extends TranslatedElement, TTranslatedIn
|
|||||||
final override string toString() { result = "init: " + expr.toString() }
|
final override string toString() { result = "init: " + expr.toString() }
|
||||||
|
|
||||||
final override Declaration getFunction() {
|
final override Declaration getFunction() {
|
||||||
result = expr.getEnclosingFunction() or
|
result = getEnclosingFunction(expr) or
|
||||||
result = expr.getEnclosingVariable().(GlobalOrNamespaceVariable) or
|
result = getEnclosingVariable(expr).(GlobalOrNamespaceVariable) or
|
||||||
result = expr.getEnclosingVariable().(StaticInitializedStaticLocalVariable)
|
result = getEnclosingVariable(expr).(StaticInitializedStaticLocalVariable)
|
||||||
}
|
}
|
||||||
|
|
||||||
final override Locatable getAst() { result = expr }
|
final override Locatable getAst() { result = expr }
|
||||||
@@ -160,7 +160,7 @@ abstract class TranslatedInitialization extends TranslatedElement, TTranslatedIn
|
|||||||
final InitializationContext getContext() { result = getParent() }
|
final InitializationContext getContext() { result = getParent() }
|
||||||
|
|
||||||
final TranslatedFunction getEnclosingFunction() {
|
final TranslatedFunction getEnclosingFunction() {
|
||||||
result = getTranslatedFunction(expr.getEnclosingFunction())
|
result = getTranslatedFunction(this.getFunction())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,8 +494,9 @@ abstract class TranslatedFieldInitialization extends TranslatedElement {
|
|||||||
deprecated override Locatable getAST() { result = getAst() }
|
deprecated override Locatable getAST() { result = getAst() }
|
||||||
|
|
||||||
final override Declaration getFunction() {
|
final override Declaration getFunction() {
|
||||||
result = ast.getEnclosingFunction() or
|
result = getEnclosingFunction(ast) or
|
||||||
result = ast.getEnclosingVariable().(GlobalOrNamespaceVariable)
|
result = getEnclosingVariable(ast).(GlobalOrNamespaceVariable) or
|
||||||
|
result = getEnclosingVariable(ast).(StaticInitializedStaticLocalVariable)
|
||||||
}
|
}
|
||||||
|
|
||||||
final override Instruction getFirstInstruction() { result = getInstruction(getFieldAddressTag()) }
|
final override Instruction getFirstInstruction() { result = getInstruction(getFieldAddressTag()) }
|
||||||
@@ -652,11 +653,11 @@ abstract class TranslatedElementInitialization extends TranslatedElement {
|
|||||||
deprecated override Locatable getAST() { result = getAst() }
|
deprecated override Locatable getAST() { result = getAst() }
|
||||||
|
|
||||||
final override Declaration getFunction() {
|
final override Declaration getFunction() {
|
||||||
result = initList.getEnclosingFunction()
|
result = getEnclosingFunction(initList)
|
||||||
or
|
or
|
||||||
result = initList.getEnclosingVariable().(GlobalOrNamespaceVariable)
|
result = getEnclosingVariable(initList).(GlobalOrNamespaceVariable)
|
||||||
or
|
or
|
||||||
result = initList.getEnclosingVariable().(StaticInitializedStaticLocalVariable)
|
result = getEnclosingVariable(initList).(StaticInitializedStaticLocalVariable)
|
||||||
}
|
}
|
||||||
|
|
||||||
final override Instruction getFirstInstruction() { result = getInstruction(getElementIndexTag()) }
|
final override Instruction getFirstInstruction() { result = getInstruction(getElementIndexTag()) }
|
||||||
@@ -855,7 +856,7 @@ abstract class TranslatedStructorCallFromStructor extends TranslatedElement, Str
|
|||||||
result = getStructorCall()
|
result = getStructorCall()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override Function getFunction() { result = call.getEnclosingFunction() }
|
final override Function getFunction() { result = getEnclosingFunction(call) }
|
||||||
|
|
||||||
final override Instruction getChildSuccessor(TranslatedElement child) {
|
final override Instruction getChildSuccessor(TranslatedElement child) {
|
||||||
child = getStructorCall() and
|
child = getStructorCall() and
|
||||||
|
|||||||
Reference in New Issue
Block a user