Skip to content

Commit

Permalink
[c2cpg] Added typefullnames to all calls (#4731)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Jul 4, 2024
1 parent dbdb02f commit 59b5ada
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.{ExpressionNew, NewCall, N
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
import io.joern.x2cpg.{Ast, SourceFiles, ValidationMode}
import io.joern.x2cpg.utils.NodeBuilders.newDependencyNode
import io.joern.x2cpg.Defines as X2CpgDefines
import io.shiftleft.codepropertygraph.generated.EdgeTypes
import io.shiftleft.utils.IOUtils
import org.apache.commons.lang3.StringUtils
Expand Down Expand Up @@ -456,7 +457,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As

private def astForDecltypeSpecifier(decl: ICPPASTDecltypeSpecifier): Ast = {
val op = Defines.OperatorTypeOf
val cpgUnary = callNode(decl, code(decl), op, op, DispatchTypes.STATIC_DISPATCH)
val cpgUnary = callNode(decl, code(decl), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val operand = nullSafeAst(decl.getDecltypeExpression)
callAst(cpgUnary, List(operand))
}
Expand All @@ -467,7 +468,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
val op = Operators.assignment
val calls = withIndex(d.getDesignators) { (des, o) =>
val callNode_ =
callNode(d, code(d), op, op, DispatchTypes.STATIC_DISPATCH)
callNode(d, code(d), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
.argumentIndex(o)
val left = astForNode(des)
val right = astForNode(d.getOperand)
Expand All @@ -483,7 +484,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
val op = Operators.assignment
val calls = withIndex(d.getDesignators) { (des, o) =>
val callNode_ =
callNode(d, code(d), op, op, DispatchTypes.STATIC_DISPATCH)
callNode(d, code(d), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
.argumentIndex(o)
val left = astForNode(des)
val right = astForNode(d.getOperand)
Expand All @@ -495,22 +496,22 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As

private def astForCPPASTConstructorInitializer(c: ICPPASTConstructorInitializer): Ast = {
val name = Defines.OperatorConstructorInitializer
val callNode_ = callNode(c, code(c), name, name, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(c, code(c), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val args = c.getArguments.toList.map(a => astForNode(a))
callAst(callNode_, args)
}

private def astForCASTArrayRangeDesignator(des: CASTArrayRangeDesignator): Ast = {
val op = Operators.arrayInitializer
val callNode_ = callNode(des, code(des), op, op, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(des, code(des), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val floorAst = nullSafeAst(des.getRangeFloor)
val ceilingAst = nullSafeAst(des.getRangeCeiling)
callAst(callNode_, List(floorAst, ceilingAst))
}

private def astForCPPASTArrayRangeDesignator(des: CPPASTArrayRangeDesignator): Ast = {
val op = Operators.arrayInitializer
val callNode_ = callNode(des, code(des), op, op, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(des, code(des), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val floorAst = nullSafeAst(des.getRangeFloor)
val ceilingAst = nullSafeAst(des.getRangeCeiling)
callAst(callNode_, List(floorAst, ceilingAst))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
case _ => Defines.OperatorUnknown
}

val callNode_ = callNode(bin, code(bin), op, op, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(bin, code(bin), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val left = nullSafeAst(bin.getOperand1)
val right = nullSafeAst(bin.getOperand2)
callAst(callNode_, List(left, right))
}

private def astForExpressionList(exprList: IASTExpressionList): Ast = {
val name = Defines.OperatorExpressionList
val callNode_ = callNode(exprList, code(exprList), name, name, DispatchTypes.STATIC_DISPATCH)
val name = Defines.OperatorExpressionList
val callNode_ =
callNode(exprList, code(exprList), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val childAsts = exprList.getExpressions.map(nullSafeAst)
callAst(callNode_, childAsts.toIndexedSeq)
}
Expand Down Expand Up @@ -353,8 +354,16 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
) {
nullSafeAst(unary.getOperand)
} else {
val cpgUnary = callNode(unary, code(unary), operatorMethod, operatorMethod, DispatchTypes.STATIC_DISPATCH)
val operand = nullSafeAst(unary.getOperand)
val cpgUnary = callNode(
unary,
code(unary),
operatorMethod,
operatorMethod,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val operand = nullSafeAst(unary.getOperand)
callAst(cpgUnary, List(operand))
}
}
Expand All @@ -368,7 +377,15 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
op == IASTTypeIdExpression.op_alignof ||
op == IASTTypeIdExpression.op_typeof =>
val call =
callNode(typeId, code(typeId), Operators.sizeOf, Operators.sizeOf, DispatchTypes.STATIC_DISPATCH)
callNode(
typeId,
code(typeId),
Operators.sizeOf,
Operators.sizeOf,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val arg = astForNode(typeId.getTypeId.getDeclSpecifier)
callAst(call, List(arg))
case _ => notHandledYet(typeId)
Expand All @@ -377,7 +394,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

private def astForConditionalExpression(expr: IASTConditionalExpression): Ast = {
val name = Operators.conditional
val call = callNode(expr, code(expr), name, name, DispatchTypes.STATIC_DISPATCH)
val call = callNode(expr, code(expr), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))

val condAst = nullSafeAst(expr.getLogicalConditionExpression)
val posAst = nullSafeAst(expr.getPositiveResultExpression)
Expand All @@ -390,7 +407,15 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
private def astForArrayIndexExpression(arrayIndexExpression: IASTArraySubscriptExpression): Ast = {
val name = Operators.indirectIndexAccess
val cpgArrayIndexing =
callNode(arrayIndexExpression, code(arrayIndexExpression), name, name, DispatchTypes.STATIC_DISPATCH)
callNode(
arrayIndexExpression,
code(arrayIndexExpression),
name,
name,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)

val expr = astForExpression(arrayIndexExpression.getArrayExpression)
val arg = astForNode(arrayIndexExpression.getArgument)
Expand All @@ -399,7 +424,15 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

private def astForCastExpression(castExpression: IASTCastExpression): Ast = {
val cpgCastExpression =
callNode(castExpression, code(castExpression), Operators.cast, Operators.cast, DispatchTypes.STATIC_DISPATCH)
callNode(
castExpression,
code(castExpression),
Operators.cast,
Operators.cast,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)

val expr = astForExpression(castExpression.getOperand)
val argNode = castExpression.getTypeId
Expand All @@ -421,8 +454,16 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
}

private def astForNewExpression(newExpression: ICPPASTNewExpression): Ast = {
val name = Defines.OperatorNew
val cpgNewExpression = callNode(newExpression, code(newExpression), name, name, DispatchTypes.STATIC_DISPATCH)
val name = Defines.OperatorNew
val cpgNewExpression = callNode(
newExpression,
code(newExpression),
name,
name,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)

val typeId = newExpression.getTypeId
if (newExpression.isArrayAllocation) {
Expand All @@ -439,15 +480,23 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
private def astForDeleteExpression(delExpression: ICPPASTDeleteExpression): Ast = {
val name = Operators.delete
val cpgDeleteNode =
callNode(delExpression, code(delExpression), name, name, DispatchTypes.STATIC_DISPATCH)
callNode(
delExpression,
code(delExpression),
name,
name,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val arg = astForExpression(delExpression.getOperand)
callAst(cpgDeleteNode, List(arg))
}

private def astForTypeIdInitExpression(typeIdInit: IASTTypeIdInitializerExpression): Ast = {
val name = Operators.cast
val cpgCastExpression =
callNode(typeIdInit, code(typeIdInit), name, name, DispatchTypes.STATIC_DISPATCH)
callNode(typeIdInit, code(typeIdInit), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))

val typeAst = unknownNode(typeIdInit.getTypeId, code(typeIdInit.getTypeId))
val expr = astForNode(typeIdInit.getInitializer)
Expand All @@ -456,7 +505,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

private def astForConstructorExpression(c: ICPPASTSimpleTypeConstructorExpression): Ast = {
val name = c.getDeclSpecifier.toString
val callNode_ = callNode(c, code(c), name, name, DispatchTypes.STATIC_DISPATCH)
val callNode_ = callNode(c, code(c), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val arg = astForNode(c.getInitializer)
callAst(callNode_, List(arg))
}
Expand Down Expand Up @@ -499,7 +548,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {

protected def astForStaticAssert(a: ICPPASTStaticAssertDeclaration): Ast = {
val name = "static_assert"
val call = callNode(a, code(a), name, name, DispatchTypes.STATIC_DISPATCH)
val call = callNode(a, code(a), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val cond = nullSafeAst(a.getCondition)
val message = nullSafeAst(a.getMessage)
callAst(call, List(cond, message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.joern.c2cpg.astcreation

import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
import io.joern.x2cpg.{Ast, ValidationMode}
import io.joern.x2cpg.Defines as X2CpgDefines
import io.shiftleft.codepropertygraph.generated.nodes.NewMethodRef
import org.eclipse.cdt.core.dom.ast.*
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding
Expand Down Expand Up @@ -90,7 +91,7 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t

protected def astForFieldReference(fieldRef: IASTFieldReference): Ast = {
val op = if (fieldRef.isPointerDereference) Operators.indirectFieldAccess else Operators.fieldAccess
val ma = callNode(fieldRef, code(fieldRef), op, op, DispatchTypes.STATIC_DISPATCH)
val ma = callNode(fieldRef, code(fieldRef), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val owner = astForExpression(fieldRef.getFieldOwner)
val member = fieldIdentifierNode(fieldRef, fieldRef.getFieldName.toString, fieldRef.getFieldName.toString)
callAst(ma, List(owner, Ast(member)))
Expand All @@ -101,7 +102,7 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t

protected def astForInitializerList(l: IASTInitializerList): Ast = {
val op = Operators.arrayInitializer
val initCallNode = callNode(l, code(l), op, op, DispatchTypes.STATIC_DISPATCH)
val initCallNode = callNode(l, code(l), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))

val MAX_INITIALIZERS = 1000
val clauses = l.getClauses.slice(0, MAX_INITIALIZERS)
Expand All @@ -120,7 +121,7 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t

protected def astForQualifiedName(qualId: CPPASTQualifiedName): Ast = {
val op = Operators.fieldAccess
val ma = callNode(qualId, code(qualId), op, op, DispatchTypes.STATIC_DISPATCH)
val ma = callNode(qualId, code(qualId), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))

def fieldAccesses(names: List[IASTNode], argIndex: Int = -1): Ast = names match {
case Nil => Ast()
Expand All @@ -129,7 +130,7 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t
case head :: tail =>
val codeString = s"${code(head)}::${tail.map(code).mkString("::")}"
val callNode_ =
callNode(head, code(head), op, op, DispatchTypes.STATIC_DISPATCH)
callNode(head, code(head), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
.argumentIndex(argIndex)
callNode_.code = codeString
val arg1 = astForNode(head)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.joern.c2cpg.astcreation
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
import io.joern.x2cpg.{Ast, ValidationMode}
import io.joern.x2cpg.Defines as X2CpgDefines
import org.eclipse.cdt.core.dom.ast.*
import org.eclipse.cdt.core.dom.ast.cpp.*
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTAliasDeclaration
Expand Down Expand Up @@ -99,19 +100,36 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
case i: IASTEqualsInitializer =>
val operatorName = Operators.assignment
val callNode_ =
callNode(declarator, code(declarator), operatorName, operatorName, DispatchTypes.STATIC_DISPATCH)
callNode(
declarator,
code(declarator),
operatorName,
operatorName,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val left = astForNode(declarator.getName)
val right = astForNode(i.getInitializerClause)
callAst(callNode_, List(left, right))
case i: ICPPASTConstructorInitializer =>
val name = ASTStringUtil.getSimpleName(declarator.getName)
val callNode_ = callNode(declarator, code(declarator), name, name, DispatchTypes.STATIC_DISPATCH)
val args = i.getArguments.toList.map(x => astForNode(x))
val name = ASTStringUtil.getSimpleName(declarator.getName)
val callNode_ =
callNode(declarator, code(declarator), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val args = i.getArguments.toList.map(x => astForNode(x))
callAst(callNode_, args)
case i: IASTInitializerList =>
val operatorName = Operators.assignment
val callNode_ =
callNode(declarator, code(declarator), operatorName, operatorName, DispatchTypes.STATIC_DISPATCH)
callNode(
declarator,
code(declarator),
operatorName,
operatorName,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val left = astForNode(declarator.getName)
val right = astForNode(i)
callAst(callNode_, List(left, right))
Expand Down Expand Up @@ -212,8 +230,9 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
case d: IASTDeclarator if d.getInitializer != null =>
astForInitializer(d, d.getInitializer)
case arrayDecl: IASTArrayDeclarator =>
val op = Operators.arrayInitializer
val initCallNode = callNode(arrayDecl, code(arrayDecl), op, op, DispatchTypes.STATIC_DISPATCH)
val op = Operators.arrayInitializer
val initCallNode =
callNode(arrayDecl, code(arrayDecl), op, op, DispatchTypes.STATIC_DISPATCH, None, Some(X2CpgDefines.Any))
val initArgs =
arrayDecl.getArrayModifiers.toList.filter(m => m.getConstantExpression != null).map(astForNode)
callAst(initCallNode, initArgs)
Expand Down Expand Up @@ -318,7 +337,15 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
if (enumerator.getValue != null) {
val operatorName = Operators.assignment
val callNode_ =
callNode(enumerator, code(enumerator), operatorName, operatorName, DispatchTypes.STATIC_DISPATCH)
callNode(
enumerator,
code(enumerator),
operatorName,
operatorName,
DispatchTypes.STATIC_DISPATCH,
None,
Some(X2CpgDefines.Any)
)
val left = astForNode(enumerator.getName)
val right = astForNode(enumerator.getValue)
val ast = callAst(callNode_, List(left, right))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ trait MacroHandler(implicit withSchemaValidation: ValidationMode) { this: AstCre

val callName = StringUtils.normalizeSpace(name)
val callFullName = StringUtils.normalizeSpace(fullName(macroDef, argAsts))
val typeFullName = registerType(cleanType(typeFor(node)))
val callNode =
NewCall()
.name(callName)
.dispatchType(DispatchTypes.INLINED)
.methodFullName(callFullName)
.code(code)
.typeFullName(typeFor(node))
.typeFullName(typeFullName)
.lineNumber(line(node))
.columnNumber(column(node))
callAst(callNode, argAsts)
Expand Down

0 comments on commit 59b5ada

Please sign in to comment.