Skip to content

Conversation

@peckto
Copy link
Collaborator

@peckto peckto commented Jul 17, 2025

Transition from the Neo4j OGM to the cpg internal OGM to persist cpg as json, analog to the Neo4j persistence.

One problem I'm currently facing with this approach is, that in some cases nodes with duplicate id (uuid) occur.
Not sure if those nodes are actually the same, or if there is a collision with the uuid.

Example:
https://github.com/pq-crystals/dilithium

    {
      "id": "ffffffff-ad71-abff-ffff-ffffcb38da74",
      "labels": [
        "Node",
        "Declaration",
        "ValueDeclaration",
        "VariableDeclaration"
      ],
      "properties": {
        "isArray": false,
        "isGlobal": true,
        "isImplicitInitializerAllowed": false,
        "argumentIndex": 0,
        "isImplicit": false,
        "isInferred": true,
        "fullName": "pqcrystals_dilithium2_ref_idxlut",
        "localName": "pqcrystals_dilithium2_ref_idxlut",
        "nameDelimiter": "::",
        "name": "pqcrystals_dilithium2_ref_idxlut"
      }
    },
    {
      "id": "ffffffff-ad71-abff-ffff-ffffcb38da74",
      "labels": [
        "Node",
        "Declaration",
        "ValueDeclaration",
        "VariableDeclaration"
      ],
      "properties": {
        "isArray": false,
        "isGlobal": true,
        "isImplicitInitializerAllowed": false,
        "argumentIndex": 0,
        "isImplicit": false,
        "isInferred": true,
        "fullName": "pqcrystals_dilithium2_ref_idxlut",
        "localName": "pqcrystals_dilithium2_ref_idxlut",
        "nameDelimiter": "::",
        "name": "pqcrystals_dilithium2_ref_idxlut"
      }
    },
    {
      "id": "ffffffff-ad71-abff-ffff-ffffcb38da74",
      "labels": [
        "Node",
        "Declaration",
        "ValueDeclaration",
        "VariableDeclaration"
      ],
      "properties": {
        "isArray": false,
        "isGlobal": true,
        "isImplicitInitializerAllowed": false,
        "argumentIndex": 0,
        "isImplicit": false,
        "isInferred": true,
        "fullName": "pqcrystals_dilithium2_ref_idxlut",
        "localName": "pqcrystals_dilithium2_ref_idxlut",
        "nameDelimiter": "::",
        "name": "pqcrystals_dilithium2_ref_idxlut"
      }
    },
[...]

@peckto peckto force-pushed the json-export-new-ogm branch from 0ea2193 to bb2eefa Compare July 17, 2025 07:31
@codecov
Copy link

codecov bot commented Jul 17, 2025

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
851 2 849 12
View the top 2 failed test(s) by shortest run time
de.fraunhofer.aisec.cpg.IntegrationTest::testExportToJson()
Stack Traces | 0.058s run time
java.lang.NullPointerException
	at de.fraunhofer.aisec.cpg.persistence.JsonKt.createJsonGraph(Json.kt:69)
	at de.fraunhofer.aisec.cpg.persistence.JsonKt.persistJson(Json.kt:113)
	at de.fraunhofer.aisec.cpg.IntegrationTest.testExportToJson(IntegrationTest.kt:80)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
de.fraunhofer.aisec.cpg.IntegrationTest::testBuildJsonGraph()
Stack Traces | 11.3s run time
java.lang.NullPointerException
	at de.fraunhofer.aisec.cpg.persistence.JsonKt.createJsonGraph(Json.kt:69)
	at de.fraunhofer.aisec.cpg.IntegrationTest.testBuildJsonGraph(IntegrationTest.kt:51)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link
Member

@oxisto oxisto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! the implementation even looks simpler than before. Next step would be to use the same functions in the schema export thingie

@@ -0,0 +1,116 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can even move this file to cog-core? Should not have any dependencies on neo4j anymore

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, good point, moved it to core

import de.fraunhofer.aisec.cpg.helpers.Benchmark
import java.io.File

data class JsonNode(val id: String, val labels: Set<String>, val properties: Map<String, Any?>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some doc here would be nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


data class JsonNode(val id: String, val labels: Set<String>, val properties: Map<String, Any?>)

data class JsonEdge(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some doc here would be nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

val properties: Map<String, Any?>,
)

data class JsonGraph(val nodes: List<JsonNode>, val edges: List<JsonEdge>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some doc here would be nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@peckto peckto force-pushed the json-export-new-ogm branch from bb2eefa to 176b407 Compare August 7, 2025 06:28
@peckto peckto force-pushed the json-export-new-ogm branch 2 times, most recently from e36f3a4 to a5f56cb Compare August 7, 2025 06:42
@peckto
Copy link
Collaborator Author

peckto commented Aug 7, 2025

I noticed, the duplicate node UUID problem is not specific to inferred nodes.
Here is another example, the node is in the same file, but in a different location:

    [
        {
            "id": "ffffffff-b571-ae40-ffff-ffff8b131677",
            "labels": [
                "Node",
                "AstNode",
                "Statement",
                "Expression",
                "Reference"
            ],
            "properties": {
                "access": "READ",
                "isStaticAccess": false,
                "referenceTag": -1839066876,
                "argumentIndex": 0,
                "code": "a",
                "isImplicit": false,
                "isInferred": false,
                "artifact": "file:///dilithium/ref/poly.c",
                "endLine": 253,
                "endColumn": 30,
                "startColumn": 29,
                "startLine": 253,
                "fullName": "a",
                "localName": "a",
                "nameDelimiter": "::",
                "name": "a"
            }
        },
        {
            "id": "ffffffff-b571-ae40-ffff-ffff8b131677",
            "labels": [
                "Node",
                "AstNode",
                "Statement",
                "Expression",
                "Reference"
            ],
            "properties": {
                "access": "READ",
                "isStaticAccess": false,
                "referenceTag": -1497655690,
                "argumentIndex": 0,
                "code": "a",
                "isImplicit": false,
                "isInferred": false,
                "artifact": "file:///dilithium/ref/poly.c",
                "endLine": 625,
                "endColumn": 18,
                "startColumn": 17,
                "startLine": 625,
                "fullName": "a",
                "localName": "a",
                "nameDelimiter": "::",
                "name": "a"
            }
        }
    ]

import picocli.CommandLine

fun createTranslationResult(file: String = "client.cpp"): Pair<Application, TranslationResult> {
fun createTranslationResult(file: String = "client.cpp"): TranslationResult {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No being in the cpg-core we probably cannot access the cxx language frontend anymore ^^
Is there an alternative way to create a graph that we can export for the test-case?

@peckto peckto force-pushed the json-export-new-ogm branch from 2454689 to 13d76da Compare August 7, 2025 09:13
@peckto peckto force-pushed the json-export-new-ogm branch from 13d76da to 464b4be Compare August 7, 2025 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants