Skip to content

Commit

Permalink
Merge branch 'main' into kw/wikiToSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto authored Nov 19, 2024
2 parents 98725ce + 3a430fc commit a294866
Show file tree
Hide file tree
Showing 18 changed files with 1,619 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
package de.fraunhofer.aisec.cpg.graph.statements.expressions

import de.fraunhofer.aisec.cpg.frontends.TranslationException
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.ArgumentHolder
import de.fraunhofer.aisec.cpg.graph.HasOverloadedOperation
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgeOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
import de.fraunhofer.aisec.cpg.graph.types.HasType
Expand Down Expand Up @@ -81,6 +82,7 @@ open class BinaryOperator :
.append("lhs", lhs.name)
.append("rhs", rhs.name)
.append("operatorCode", operatorCode)
.append("location", location)
.toString()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ExpressionHandler(lang: LLVMIRLanguageFrontend) :
LLVMPoisonValueValueKind -> {
newReference("poison", frontend.typeOf(value), rawNode = value)
}
LLVMConstantTargetNoneValueKind -> newLiteral(null, unknownType(), rawNode = value)
LLVMConstantTokenNoneValueKind -> newLiteral(null, unknownType(), rawNode = value)
LLVMUndefValueValueKind -> initializeAsUndef(frontend.typeOf(value), value)
LLVMConstantAggregateZeroValueKind -> initializeAsZero(frontend.typeOf(value), value)
Expand All @@ -88,45 +89,28 @@ class ExpressionHandler(lang: LLVMIRLanguageFrontend) :
)
}
else -> {
log.info(
"Not handling value kind {} in handleValue yet. Falling back to the legacy way. Please change",
kind
)
val cpgType = frontend.typeOf(value)

// old stuff from getOperandValue, needs to be refactored to the when above
// TODO also move the other stuff to the expression handler
return when {
LLVMIsConstant(value) != 1 -> {
val operandName: String =
if (
LLVMIsAGlobalAlias(value) != null ||
LLVMIsGlobalConstant(value) == 1
) {
val aliasee = LLVMAliasGetAliasee(value)
LLVMPrintValueToString(aliasee)
.string // Already resolve the aliasee of the constant
} else {
// TODO This does not return the actual constant but only a string
// representation
LLVMPrintValueToString(value).string
}
newLiteral(operandName, cpgType, rawNode = value)
}
LLVMIsUndef(value) == 1 -> {
newReference("undef", cpgType, rawNode = value)
}
LLVMIsPoison(value) == 1 -> {
newReference("poison", cpgType, rawNode = value)
}
else -> {
log.error("Unknown expression {}", kind)
newProblemExpression(
"Unknown expression $kind",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
}
// old stuff from getOperandValue, needs to be refactored to the `when` above
return if (LLVMIsConstant(value) != 1) {
log.info("Update handling value kind {} to the new way", kind)
var printVal =
if (LLVMIsAGlobalAlias(value) != null || LLVMIsGlobalConstant(value) == 1) {
// Already resolve the aliasee of the constant
LLVMAliasGetAliasee(value)
} else {
value
}
newLiteral(
LLVMPrintValueToString(printVal).string,
frontend.typeOf(value),
rawNode = value
)
} else {
log.error("Unknown expression {}", kind)
newProblemExpression(
"Unknown expression $kind",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
}
}
}
Expand Down Expand Up @@ -210,65 +194,42 @@ class ExpressionHandler(lang: LLVMIRLanguageFrontend) :
* regular expression.
*/
private fun handleConstantExprValueKind(value: LLVMValueRef): Expression {
val expr =
when (val kind = LLVMGetConstOpcode(value)) {
LLVMGetElementPtr -> handleGetElementPtr(value)
LLVMSelect -> handleSelect(value)
LLVMTrunc,
LLVMZExt,
LLVMSExt,
LLVMFPToUI,
LLVMFPToSI,
LLVMUIToFP,
LLVMSIToFP,
LLVMFPTrunc,
LLVMFPExt,
LLVMPtrToInt,
LLVMIntToPtr,
LLVMBitCast,
LLVMAddrSpaceCast -> handleCastInstruction(value)
LLVMAdd,
LLVMFAdd ->
frontend.statementHandler.handleBinaryOperator(value, "+", false) as? Expression
?: newProblemExpression(
"Wrong type of constant binary operation +",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
LLVMSub,
LLVMFSub ->
frontend.statementHandler.handleBinaryOperator(value, "-", false) as? Expression
?: newProblemExpression(
"Wrong type of constant binary operation -",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
LLVMAShr ->
frontend.statementHandler.handleBinaryOperator(value, ">>", false)
as? Expression
?: newProblemExpression(
"Wrong type of constant binary operation >>",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
LLVMICmp ->
frontend.statementHandler.handleIntegerComparison(value) as? Expression
?: newProblemExpression(
"Wrong type of constant comparison",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
else -> {
log.error("Not handling constant expression of opcode {} yet", kind)
newProblemExpression(
"Not handling constant expression of opcode $kind yet",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
}
return when (val kind = LLVMGetConstOpcode(value)) {
LLVMGetElementPtr -> handleGetElementPtr(value)
LLVMSelect -> handleSelect(value)
LLVMTrunc,
LLVMZExt,
LLVMSExt,
LLVMFPToUI,
LLVMFPToSI,
LLVMUIToFP,
LLVMSIToFP,
LLVMFPTrunc,
LLVMFPExt,
LLVMPtrToInt,
LLVMIntToPtr,
LLVMBitCast,
LLVMAddrSpaceCast -> handleCastInstruction(value)
LLVMAdd,
LLVMFAdd -> frontend.statementHandler.handleBinaryOperator(value, "+", false)
LLVMSub,
LLVMFSub -> frontend.statementHandler.handleBinaryOperator(value, "-", false)
LLVMMul,
LLVMFMul -> frontend.statementHandler.handleBinaryOperator(value, "*", false)
LLVMShl -> frontend.statementHandler.handleBinaryOperator(value, "<<", false)
LLVMLShr,
LLVMAShr -> frontend.statementHandler.handleBinaryOperator(value, ">>", false)
LLVMXor -> frontend.statementHandler.handleBinaryOperator(value, "^", false)
LLVMICmp -> frontend.statementHandler.handleIntegerComparison(value)
else -> {
log.error("Not handling constant expression of opcode {} yet", kind)
newProblemExpression(
"Not handling constant expression of opcode $kind yet",
ProblemNode.ProblemType.TRANSLATION,
rawNode = value
)
}

return expr
}
}

/**
Expand Down
Loading

0 comments on commit a294866

Please sign in to comment.