Most real software mixes a safe, high-level language (Python, Java, JavaScript) with fast, unsafe native code (C, C++), joined by a foreign function interface, the glue layer that lets one language call functions written in another. Program analyzers handle each side well but fail at this join. Java and JavaScript analyzers treat a native call as a dead end. Native analyzers see pointers and registers but not the high-level types or permissions the caller assumed. The tools that do cross the boundary depend on hand-written rules and hand-curated lists of API names. As a result they miss custom permission checks and stop wherever an extra native layer sits in the middle.
This note proposes learning a representation of code on each side of a boundary, built so that every function is embedded near functions with the same contract. A contract here means what the function expects and guarantees at the boundary: which argument types it checks, who must free the memory it returns, and which privilege level it enforces. A bug is then a disagreement between the caller's assumed contract and the callee's actual contract, rather than a pattern a rule must match. The shaping technique comes from scientific design. There, an autoencoder (a network that compresses inputs to a vector and reconstructs them) is trained alongside small "side predictors" of properties, so that nearness in the vector space reflects function rather than surface syntax. We bring that technique to cross-language analysis, and we test it where rules and plain classifiers should break: renamed or custom checks, single-line edits that flip safety, and call chains with more than one hop.
Core Claim
The claim is this. An encoder that embeds managed-side and native-side functions separately, trained on reconstruction plus per-side contract prediction heads, gives contract estimates that can be compared and chained. A detector built on such embeddings has three properties. It generalizes to permission and type checks whose names are absent from any rule list. It separates code that differs by one missing check, which is the analogue of an "activity cliff" in chemistry, where a small structural change flips a property. And when trained only on single-hop boundaries, it can flag mismatches in multi-hop chains such as JavaScript → C++ → Java. A model that classifies (caller, callee) pairs jointly cannot do the third without chain-level labels. The per-side split is what makes the third possible, and the reconstruction term is what we expect to drive the first.
Relation to Prior Work
The method comes from the property-structured latent space literature. Continuous Representation of Molecules using Graph Variational Autoencoder attached a side predictor to a graph autoencoder's latent vector. The space then organized by chemical property, well enough that a model trained with a druglikeness head predicted solubility at 91.0% accuracy. Property-guided generation of complex polymer topologies using variational autoencoders combined reconstruction, regression and classification heads on one latent. Molecular Graph Generation via Geometric Scattering added an explicit smoothness penalty. The design choice we borrow most directly is from Predicting thermoelectric properties from chemical formula with explicitly identifying dopant effects. That work kept the base-material and dopant representations separate, so property effects became readable and transferred out of distribution. Our caller/callee split plays the same role.
All of these works optimize a latent space to generate new structures, and they assume properties vary smoothly. We deliberately do not do latent optimization of glue code. Memory safety is a step function: one missing type check is the bug. CrossPL also suggests that the hard part of foreign-function code is multi-step type reasoning, not search. We therefore use the property heads for analysis, and we treat cliffs as something the geometry must represent sharply rather than smooth away.
The motivation comes from the native-analysis literature. Uncovering Cross-Context Inconsistent Access Control Enforcement in Android (IAceFinder) bridges Java and C++ through Soot and LLVM and normalizes privileges into four levels. It reaches 89.5% Java and 94.0% native precision, but it relies on curated function names such as checkPermission. Bilingual Problems built cross-language data-flow graphs over 6,432 npm packages. It found that about 30% of native addons perform missing or insufficient type checks, and it identified 33 exploitable libraries. Automated Detection of Inter-Language Design Smells (CPSMELL) detects Python/C++ smells with hand-written rules at 98.17% precision. ReuNify lifts Hermes bytecode into Jimple, Soot's intermediate representation. It explicitly leaves React Native's intermediate C++ layer (the JavaScript Interface, or JSI) as a black box.
We reuse these tools as label sources and input pipelines rather than replacing them. That means our detector is bounded by their labels in-distribution. The question is whether it extends past their hand-written lists and past their single-hop scope.
The closest representation-learning work is CLaSCoRe. It embeds single-language snippets with clustered Word2Vec tokens and transfers vulnerability detection to C# at F1 0.80. It never models two languages interacting in one process, and its space is not shaped by any property.
Research Program
We would build four components.
The first is a corpus of boundary crossings. Each crossing is a managed-side function (the caller and its argument handling) paired with the native-side function it reaches. The sources are:
- the npm native-extension packages from Bilingual Problems;
- Java/JNI service pairs from Android open-source releases, following IAceFinder's setup;
- Python/C API, pybind11 and ctypes bindings in the deep learning framework versions used by CPSMELL;
- React Native apps lifted with ReuNify.
Each function becomes a graph combining its syntax tree and data flow.
The second component is the contract labels. For each side we record:
- per-parameter type-check presence;
- whether the function null-checks its arguments;
- ownership of returned buffers;
- the enforced privilege level on IAceFinder's four-level scale.
Labels come from reimplementations of the cited rules, plus a manually audited subset for evaluation.
The third component is the encoder, which has three parts. A shared graph encoder maps each side's function $x$ to a latent $z$. A decoder reconstructs masked graph nodes. Contract heads $h_k$ predict each contract attribute $k$. Training minimizes
$$ \mathcal{L} = \mathcal{L}_{\text{rec}}(x, z) + \beta\,\mathrm{KL}(q(z\mid x)\,\|\,p(z)) + \sum_k \lambda_k \mathcal{L}_k(h_k(z), y_k) + \gamma \sum_{(x, x')} \max\!\big(0,\, m - \lVert z - z' \rVert\big). $$
The terms are as follows:
- $\mathcal{L}_{\text{rec}}$ is masked-node reconstruction loss.
- The KL term, weighted by $\beta$, keeps the latent close to a standard prior $p(z)$.
- $\mathcal{L}_k$ is the loss for contract label $y_k$, weighted by $\lambda_k$.
- The last term is a cliff margin. It pushes each function $x$ at least distance $m$ from its mutant $x'$, where the mutant has had one check inserted or deleted, scaled by weight $\gamma$.
The fourth component is the boundary score. For a caller $a$ and callee $b$, the mismatch score is
$$ s(a,b) = \sum_k w_k\, \mathrm{dis}_k\big(h_k^{\text{assumed}}(z_a),\, h_k^{\text{enforced}}(z_b)\big), $$
Here $h^{\text{assumed}}$ is what the caller's handling implies the callee will check, $h^{\text{enforced}}$ is what the callee actually checks, $\mathrm{dis}_k$ measures disagreement on attribute $k$, and $w_k$ is calibrated on validation data. For a chain $a \to b \to c$, we score each adjacent hop and propagate the strongest enforced contract along the path. This uses only single-hop training.
Baselines
The naive reference point is the label-source rule tools run as-is: IAceFinder-style privilege matching on the Android pairs, the Bilingual Problems type-check analysis on npm, and CPSMELL rules on the frameworks. In-distribution, these define the label set, so we expect to roughly tie them at best. IAceFinder reports 89.5% Java and 94.0% native precision. CPSMELL reports 98.17% precision. Beating them means equal precision at higher recall on the held-out custom-check set described below, where their name lists do not apply. For fairness, we also run a strengthened rule variant that matches any function calling a known check transitively. This stops the comparison from rewarding us for a weakness that is trivially fixable.
The strongest learned competitor, and the main threat, is a joint pair classifier. It is the same graph encoder with the same data and the same labels, fed the concatenated caller and callee and predicting "mismatch". It has no reconstruction term and no per-side contract heads. No reported result for this setup was found on Lacuna, so we build it ourselves under an identical tuning budget and identical encoder size. On single-hop data we must at least match it. The proposal's distinctive claims succeed only if we clearly beat it on custom-check generalization and on multi-hop chains. On chains the pair classifier receives the concatenated chain, since it has no other way to handle them.
We also run CLaSCoRe-style token-cluster features with a support vector machine. This checks whether syntax-abstracted token embeddings already suffice. Its reported number is zero-shot F1 0.80 on synthetic SARD, which is single-language, not boundary detection.
For prompted frontier models, no reported detection result was found on Lacuna. The nearest figure is generation, not detection: 19.54% Pass@1 for GPT-4o on CrossPL foreign-function tasks. We would prompt a strong model with both sides of each crossing, with and without step-by-step reasoning, on the same test sets.
Three ablations isolate the ingredients:
- removing $\mathcal{L}_{\text{rec}}$ tests whether reconstruction drives generalization;
- merging the two sides into one latent tests whether the split drives compositionality;
- removing the cliff margin tests one-edit separation.
Why not just…?
Why not just lift everything into one intermediate representation, as ReuNify does? We do lift, and we use ReuNify's Jimple output as input. Lifting gives a unified call graph but does not supply the contracts. An analyzer on the lifted code still needs someone to say what counts as a check, and the C++ JSI layer remains unlifted (ReuNify). The multi-hop experiment is where learned per-side contracts must earn their keep over lifting alone.
Why not just write better rules? On canonical APIs, rules will likely win or tie, and we say so. IAceFinder's dependence on curated names is a documented limitation (IAceFinder). That limitation is exactly what the custom-check test probes. If the strengthened transitive-rule variant closes the gap there, the learned approach is not justified.
Why not just train a pair classifier, or fine-tune a code model, on the same labels? This is the real threat. The paired baseline above is built specifically to settle it. If it matches us on custom checks and on chains, then contract structuring adds nothing and we would report that.
Why not just prompt a frontier model? CrossPL shows weak foreign-function reasoning for generation (CrossPL), but detection is easier than generation. We run this baseline rather than assume it fails.
Proposed Experiments
The first experiment is single-hop detection. We evaluate missing type checks on the npm addons and privilege inconsistencies on the Android Java/JNI pairs, using precision and recall against manually audited labels. Splits are by package or repository, so the same project never appears in both training and test.
The second experiment tests generalization to custom checks. We build a held-out set where checks are renamed, wrapped in project-specific helpers, or replaced by real custom validation routines found during manual audit. The decisive check is recall at fixed precision against the rule tools and the pair classifier.
The third experiment is a cliff test. We apply single-edit mutations that insert or delete one check and measure the fraction whose predicted contract flips correctly. We compare full training against the no-margin and reconstruction-only variants. A reconstruction-dominated space should fail this test, as the molecular literature would predict.
The fourth experiment is multi-hop composition. We use React Native apps whose JavaScript → C++ JSI → Java chains we label manually; this is a small but audited set. Training uses single hops only. The decisive check is whether chained per-side scoring beats the pair classifier given the whole chain.
The fifth experiment is transfer across frameworks. We train on Python/C bindings in three of the CPSMELL frameworks and test on the remaining two.
The proposal stands if two things hold: experiments two and four show clear gains over the pair classifier, and the reconstruction and split ablations each account for part of those gains. It falls if the pair classifier or the transitive-rule variant matches us on those two experiments.
References
- Continuous Representation of Molecules using Graph Variational Autoencoder
- Property-guided generation of complex polymer topologies using variational autoencoders
- Molecular Graph Generation via Geometric Scattering
- Predicting thermoelectric properties from chemical formula with explicitly identifying dopant effects
- CrossPL: Systematic Evaluation of Large Language Models for Cross Programming Language Interoperating Code Generation
- Uncovering Cross-Context Inconsistent Access Control Enforcement in Android
- Bilingual Problems: Studying the Security Risks Incurred by Native Extensions in Scripting Languages
- Automated detection of inter-language design smells in multi-language deep learning frameworks
- ReuNify: A Step Towards Whole Program Analysis for React Native Android Apps
- Detection of Software Security Weaknesses Using Cross-Language Source Code Representation (CLaSCoRe)