Guide
Code Property Graphs: What They Are and How to Use One
One graph that holds a program's syntax, its control flow and its data dependencies, so a single query can ask about all three. Here is how it is built, what it is good for, and where to start.
By Code Graph Review · Updated
A code property graph (CPG) is a single graph representation of source code that merges three classic program representations: the abstract syntax tree (AST), the control flow graph (CFG) and the program dependence graph (PDG). Nodes are program constructs such as methods, calls, identifiers and literals; labelled, directed edges say how they relate; and both carry key-value properties. Because everything lives in one graph, a single query can say "a value that comes from user input, reaches this call, and is never checked on the way", which none of the three representations can express alone.
The idea was introduced by Fabian Yamaguchi, Nico Golde, Daniel Arp and Konrad Rieck in the 2014 IEEE Symposium on Security and Privacy paper Modeling and Discovering Vulnerabilities with Code Property Graphs, where the authors modelled common vulnerability patterns as graph traversals and report finding 18 previously unknown vulnerabilities in the Linux kernel.
The three graphs a CPG merges
Each layer answers a different question about the same code. Take this small C function:
void handle(void) {
int x = read_input();
if (x < MAX) {
int y = 2 * x;
write_output(y);
}
}
- Abstract syntax tree: what the code says. The AST breaks the function into nested constructs: a declaration of
xinitialised by a call toread_input, anifwhose condition is the comparisonx < MAX, and so on. It is the layer you use to find a construct by its shape: every call to a given function, every comparison against a constant. - Control flow graph: in what order it runs. The CFG links statements in execution order, with two edges leaving the
ifcondition: one into the block whenx < MAXholds, one past it when it does not. It answers whether one statement can run before another, and which conditions guard it. - Program dependence graph: what depends on what. The PDG adds data dependence edges (the value of
xdefined on line two flows into the condition and intoy;yflows intowrite_output) and control dependence edges (whethery = 2 * xruns at all depends on the condition).
The CPG lays all three over the same statement and predicate nodes. From the call to write_output you can walk back along data dependence edges to read_input, then across control dependence edges to see that the flow is guarded by x < MAX, then into the AST to check what MAX is. That walk is a query, and it is the reason the representation exists.
How it differs from other code graphs
"Code graph" covers several structures with different jobs, and it helps to know which one a tool means:
- Call graphs and dependency graphs model relationships between functions, modules or packages. They are coarse and cheap, which suits impact analysis across a codebase, but they say nothing about what happens inside a function.
- Symbol and reference indexes such as LSIF and SCIP record definitions, references and hovers so an editor or code search can jump between them, across repositories too. They are built for navigation, not for reasoning about how data moves.
- Code property graphs go down to statements and expressions and keep control and data flow, which is what you need to ask whether a value can travel from one place to another. The price is size: a CPG of a large codebase is far bigger than its call graph.
In practice the layers are combined. Joern's CPG, for example, also carries method, type and call-graph information, and its specification describes "overlays" that add higher levels of abstraction on top of the low-level representations.
What code property graphs are used for
- Vulnerability discovery and variant analysis. The original use: write a pattern for a class of bug (an unchecked length reaching a copy, a format string built from input) and run it over a whole codebase, or take a bug you just fixed and look for its siblings.
- Taint tracking from sources to sinks. Mark where untrusted data enters (request parameters, file reads) and where it must not arrive unchecked (queries, shell commands, memory copies), and ask the graph for every path between them.
- Code review at scale. Rules written as graph queries can run on every change, so reviewers see a flagged data flow rather than having to trace it by hand across files.
- Program understanding and refactoring. The same queries answer non-security questions: where a value set in configuration ends up, which callers pass a null, what a change to one function's return value reaches.
- Machine learning on code. CPGs and their sub-graphs are a common input for graph neural networks trained to flag vulnerable functions, because they give a model structure that a flat token sequence lacks.
Open-source tools that build a code property graph
- Joern is the open-source platform that grew out of the original research. It generates CPGs from source code, bytecode and binaries, stores them in its own graph database, and queries them with a Scala-based language (CPGQL) from an interactive shell or scripts. Its GitHub description lists C/C++, Java, JavaScript, Python, Kotlin and binaries; the frontends page has the current list. The graph's schema is published as the Code Property Graph specification, open-sourced by Qwiet AI (formerly ShiftLeft), whose commercial analysis is built on the same representation.
- Fraunhofer AISEC CPG is a Kotlin library that extracts CPGs from C/C++, Java, Go, Python, TypeScript and LLVM IR, among others, with analysis passes on top. Its documentation describes the graph as a supergraph of the AST, an evaluation order graph, data flow and control dependence graphs. It is a good fit when you want the graph inside your own JVM tooling rather than in a separate shell.
Language support, licences and maintenance status change; check each project's repository and documentation before you commit to one.
Querying a CPG with Joern: a first session
After installing Joern, start its shell with joern and import a project. This builds the graph and stores it in a workspace:
joern> importCode(inputPath = "./src", projectName = "my-service")
Queries start from cpg and chain steps. List the call sites of a function you want to review:
joern> cpg.method("gets").callIn.l
Then ask a data flow question: can anything that enters through main's parameters reach the arguments of strcpy?
joern> def source = cpg.method.name("main").parameter
joern> def sink = cpg.call.name("strcpy").argument
joern> sink.reachableByFlows(source).p
The last line prints each flow it finds as the path of statements the data takes, which is exactly the trail a reviewer would otherwise reconstruct by hand. For a ready-made set of checks, joern-scan builds the graph for a path and runs the queries in Joern's query database against it; see the Joern Scan docs. The quickstart and the CPGQL reference card cover the rest of the language.
Limits to plan for
- Static, so approximate. Like any static analysis, a CPG over-approximates some paths and misses others: dynamic dispatch, reflection, callbacks and code generated at build time are where flows go missing or appear that cannot happen. Treat a reported flow as a lead to verify.
- Frontends differ. How much type, call and data flow detail you get depends on the language frontend; a query that works well on C may need adjusting for a dynamically typed language.
- Size and build time. Graphs of large monorepos take time and memory to build. Scope the import to the code you are reviewing, and rebuild when the code has changed significantly.
- Queries are code. Rules need owners, tests against known-good and known-bad samples, and review, or they drift into noise that reviewers learn to ignore.
Frequently asked questions
- What is a code property graph in one sentence?
- A single property graph that merges a program's abstract syntax tree, control flow graph and program dependence graph, so one query can reason about syntax, execution order and data flow together.
- Who invented the code property graph?
- Fabian Yamaguchi, Nico Golde, Daniel Arp and Konrad Rieck introduced it in the paper "Modeling and Discovering Vulnerabilities with Code Property Graphs" at the 2014 IEEE Symposium on Security and Privacy.
- Is a code property graph the same as a call graph?
- No. A call graph only records which functions call which. A code property graph goes down to individual statements and expressions and keeps control and data flow, though tools such as Joern also include call graph edges in it.
- What tools build a code property graph?
- The best-known open-source ones are Joern, which builds CPGs from source, bytecode and binaries and queries them with a Scala-based language, and the Fraunhofer AISEC CPG library for Kotlin and Java tooling. Check each project's documentation for the languages it currently supports.
- Do I need to compile my project to build a CPG with Joern?
- Joern's frontends parse code directly rather than observing a build, so it can be run on code that does not compile in your environment. How complete the graph is still depends on the language frontend; see the Joern frontends documentation for each language's options.
Sources
Read next on Code Graph Review
- Graph-Based Impact Analysis for Large-Scale API Changes
Dependency graphs map what API changes touch across millions of client applications.
- Code Graphs as Context Windows for LLM Code Assistants
Graph-based retrieval beats flat file search on accuracy and token cost.
- Language Server Protocol Index Format and Cross-Repo Navigation
SCIP replaces LSIF's graph model to simplify cross-repository code intelligence at scale.