From 59b5ada8d72cc43b6b5bfb937eede06495204275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Leuth=C3=A4user?= <1417198+max-leuthaeuser@users.noreply.github.com> Date: Thu, 4 Jul 2024 10:36:36 +0200 Subject: [PATCH] [c2cpg] Added typefullnames to all calls (#4731) --- .../c2cpg/astcreation/AstCreatorHelper.scala | 13 +-- .../AstForExpressionsCreator.scala | 79 +++++++++++++++---- .../astcreation/AstForPrimitivesCreator.scala | 9 ++- .../astcreation/AstForTypesCreator.scala | 43 ++++++++-- .../c2cpg/astcreation/MacroHandler.scala | 3 +- 5 files changed, 113 insertions(+), 34 deletions(-) diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala index e091fe3c417f..bb974d216bb2 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala @@ -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 @@ -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)) } @@ -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) @@ -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) @@ -495,14 +496,14 @@ 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)) @@ -510,7 +511,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As 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)) diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala index 352285d82f16..a3fb534a1f70 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForExpressionsCreator.scala @@ -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) } @@ -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)) } } @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) { @@ -439,7 +480,15 @@ 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)) } @@ -447,7 +496,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) { 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) @@ -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)) } @@ -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)) diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala index e90e3901394e..f5006827ae4c 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForPrimitivesCreator.scala @@ -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 @@ -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))) @@ -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) @@ -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() @@ -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) diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala index a5756948d2a7..f71914cc07c6 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala @@ -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 @@ -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)) @@ -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) @@ -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)) diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/MacroHandler.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/MacroHandler.scala index bceefb567a94..6bef1b5c44ad 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/MacroHandler.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/MacroHandler.scala @@ -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)