Skip to content

Add the BY_COPY evaluation strategy - #1876

Closed
royalbert wants to merge 1 commit into
ShiftLeftSecurity:masterfrom
royalbert:evaluation-strategy-by-copy
Closed

royalbert wants to merge 1 commit into
ShiftLeftSecurity:masterfrom
royalbert:evaluation-strategy-by-copy

Conversation

@royalbert

Copy link
Copy Markdown

This PR adds BY_COPY to the EvaluationStrategies constants: a parameter passed by value whose copy shares nothing with the argument, so the callee cannot change the argument. A consumer such as Joern's reaching-definition pass can then tell, from the callee's parameter alone, that a call does not write a variable it is given.

Problem

No existing value says that a call cannot change an argument:

  • BY_VALUE is defined as "passed by value which means a flat copy is used". A flat copy can still share: for void f(struct Node n) with struct Node { struct Node *next; }, f gets a copy of the pointer and can change the nodes it reaches. n is a correct BY_VALUE by that definition, and f can change the argument.
  • BY_REFERENCE and BY_SHARING say that the callee can.

In practice BY_VALUE is wider still. On Joern master, the frontends set these on method parameters:

frontend ordinary parameters this / self
c2cpg (C, C++) BY_VALUE, pointers, arrays and references included; ... and macro parameters too BY_SHARING
javasrc2cpg BY_VALUE for primitives, BY_SHARING otherwise BY_SHARING
jimple2cpg BY_VALUE for primitive, void and null types, BY_SHARING otherwise BY_SHARING
kotlin2cpg BY_VALUE; the implicit lambda parameter it BY_REFERENCE BY_SHARING
jssrc2cpg BY_VALUE BY_VALUE
swiftsrc2cpg BY_VALUE, inout included BY_SHARING
gosrc2cpg BY_SHARING for a pointer type, BY_VALUE otherwise, slices, maps, channels and interfaces included by the same rule
php2cpg BY_REFERENCE for &$x, BY_VALUE otherwise BY_SHARING
abap2cpg BY_VALUE for VALUE(...), BY_REFERENCE otherwise none
pysrc2cpg BY_SHARING BY_SHARING
rubysrc2cpg BY_REFERENCE BY_SHARING
rust2cpg BY_SHARING BY_VALUE for self, BY_SHARING for &self
csharpsrc2cpg BY_SHARING (marked TODO), ref and out included BY_SHARING
ghidra2cpg not set not set
x2cpg method stubs BY_VALUE BY_VALUE

So BY_VALUE is set on parameters that share their argument with the caller: pointers in C and C++, objects in Kotlin, JavaScript, Swift and PHP, and slices, maps and interfaces in Go. Apart from the code that builds the graph, which sets the strategy and copies it to output parameters and closure bindings, no code reads EVALUATION_STRATEGY, so none of this has mattered: Joern's reaching-definition pass treats every argument of a call as defined by the call.

So the existing labels cannot be trusted to say that a callee cannot change an argument. Making them trustworthy means reviewing every frontend against its language; that is each frontend's work, and this change does not take it on. A new value of the existing property lets a frontend mark what it has decided, without changing any other frontend's graphs: a frontend sets BY_COPY only where it knows the copy shares nothing, and every other parameter keeps the strategy it has now.

Change

CallGraph.scala adds the constant, and EVALUATION_STRATEGY's description lists it:

Constant(
  name = "BY_COPY",
  value = "BY_COPY",
  valueType = ValueType.String,
  comment =
    "A parameter of a function passed by value whose copy shares nothing with the argument: it holds no pointer or reference, so the callee cannot change the argument"
).protoId(ProtoIds.ByCopy)

ProtoIds.ByCopy is 4, the next free ID among the strategies. The domain classes are regenerated. Nothing else changes: no frontend sets BY_COPY yet. Each language decides whether it needs handling different from what Joern does now, with value(s) of its own.

EvaluationStrategies.BY_COPY is a new public name.

Testing

A parameter passed by value whose copy shares nothing with the argument, so the callee cannot change the argument. BY_VALUE does not say this: frontends set it on parameters that receive an object, slice or map the callee can change.
@ml86

ml86 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@royalbert

  • I do not yet see the point of this additional value. It sounds to me like it should represent a deep copy right? If that is the case for which language would we need that?
  • Additionally you bring up that the currently labels cannot be trusted. Well then we will need to review them but using a new label to encode "We have looked at it and it is a (deep?) copy" does not sound right to me.

@royalbert

Copy link
Copy Markdown
Author

Thanks, Markus. The real problem is the existing labels: across the frontends they are wrong far more often than right. On Joern master, 9 of the 13 frontends that set EVALUATION_STRATEGY on parameters set it in a way that contradicts their language:

frontend what it sets what the language does
c2cpg BY_VALUE on every parameter, pointers, arrays and C++ references included, and on macro parameters pointers and arrays share; references alias; a macro can assign its argument
gosrc2cpg BY_VALUE for slices, maps, channels, interfaces and function values all of these share
kotlin2cpg BY_VALUE for every parameter, objects included objects are shared, as in Java, where javasrc2cpg says BY_SHARING
jssrc2cpg BY_VALUE for every parameter and for this objects are shared
swiftsrc2cpg BY_VALUE for every parameter, class instances and inout included class instances are shared; inout writes back to the caller
php2cpg BY_VALUE unless &$x, objects included objects are passed as handles
csharpsrc2cpg BY_SHARING for every parameter (marked TODO), value types and ref/out included value types are copied; ref and out are references
rust2cpg BY_SHARING for every parameter but self Copy types and moves are by value; &mut is a reference
rubysrc2cpg BY_REFERENCE for every parameter a callee cannot rebind the caller's variable

Only javasrc2cpg, jimple2cpg, abap2cpg and pysrc2cpg match their language. On top of that, x2cpg's method stubs set BY_VALUE on every parameter in every language. None of this has shown up because nothing after graph construction reads the strategy. Any consumer that trusts it today, as the reaching-definition pass would, gets wrong flows in most languages.

Even a BY_VALUE that is correct does not mean the callee cannot change the argument. The schema defines it as "a flat copy", and a flat copy can still share. For void f(struct Node n) with struct Node { struct Node *next; }, n is a correct BY_VALUE by that definition, yet f can change the nodes it reaches through next. The same holds for a struct or class with a pointer or reference member, or a C++ class whose copy constructor writes its source.

That is why I asked for a new value: using BY_VALUE directly would be catastrophic to the graphs. However, I found a different way to isolate the work I was doing for C and C++, by using a system property that defaults to off. That lets me apply the work only to C and C++, and opens the door for other languages to follow suit once they fix their handling of parameters. In joernio/joern#6529:

  • c2cpg labels a parameter BY_VALUE only when its copy shares nothing with the argument, and gives everything else, those flat copies included, BY_SHARING or BY_REFERENCE.
  • The reaching-definition pass reads the labels only in C and C++ graphs.

For more information, see joernio/joern#6529, including its comments. So this PR is no longer needed, and I'm closing it.

@royalbert royalbert closed this Nov 18, 2026
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.

2 participants