A trusted execution environment, or enclave, is a hardware-isolated region of a processor that runs code on sensitive data while hiding it from the host operating system. Enclaves have two well-documented weaknesses. First, their protected memory is small: on early Intel hardware, going past it triggers encrypted paging that slows programs by 18x to 27x. Second, they leak secrets through which memory addresses and branches a program touches, even though the data itself stays encrypted. This note proposes one feedback signal that addresses both problems. A language model repeatedly rewrites enclave code until the program's memory-address and branch trace is identical for every input. Once the trace is fixed, the program no longer leaks through access patterns. It also becomes fully predictable, so an offline planner can assign every allocation a static location and schedule page swaps ahead of time, the way MAGE does for secure computation.
The opportunity comes from how two separate lines of work fit together. MAGE shows that when a program's access trace is known in advance, planned paging with Belady's optimal eviction runs out-of-core workloads within 15% of in-memory speed. Its limitation is that the program must already be data-oblivious, meaning its accesses cannot depend on secret values, and it must be hand-written in an unrolled domain-specific language. LLM porting systems such as AUTOTEE can move sensitive functions into enclave-ready Rust automatically, but they check only compilation and input/output equivalence. They pay no attention to memory footprint or to leakage. The missing piece is an automated way to turn ordinary code into code with a fixed trace. We propose using the trace itself as the feedback that drives the rewriting.
Core Claim
Consider an iterative refinement loop whose checker runs the candidate program on pairs of inputs chosen to differ, compares the resulting address and branch traces, and reports the first point where they diverge, mapped back to a source line. We claim such a loop can convert a substantial fraction of real enclave-bound functions into trace-invariant form while keeping them functionally correct. For the converted functions, the fixed trace makes two things possible at once. The first is resistance to access-pattern and branch side channels, measured empirically. The second is fully deterministic memory: static offsets for every allocation, plus a precomputed swap schedule that keeps performance close to native even when the enclave's memory budget is well below the program's footprint.
This differs from each closest system. AUTOTEE's loop never observes memory behavior. POLO refines code against wall-clock profiles, and its rewrites, such as preallocated buffers and vectors in place of linked lists, reach 1.34x to 21.5x over -O3 without any notion of leakage or enclave budgets. MAGE plans memory but cannot produce plannable programs. Truong et al. hand-derive a static tiling for convolutional networks that cuts VGG-16 latency in a 28MB enclave from 84.639s to 3.468s, but the tiling is specific to those layer topologies.
The leakage metric is simple. For a function $f$ and a set $P$ of input pairs, define trace divergence
$$D(f) = \frac{1}{|P|}\sum_{(x,x') \in P} \mathbf{1}\left[\tau(f,x) \neq \tau(f,x')\right],$$
where $\tau(f,x)$ is the sequence of cache-line addresses and branch outcomes produced when $f$ runs on input $x$, restricted to inputs of equal public length. The loop's goal is $D(f)=0$ on adversarially chosen pairs, with every functional test still passing. This is an empirical guarantee, not a proof, and we will report it as such.
Research Program
The system has a refinement stage and a planning stage.
The refinement stage starts from AUTOTEE-style Rust ports of sensitive leaf functions. A generator model proposes a rewrite. A checker then runs three kinds of test. The first is the functional test suite. The second is an instrumented differential execution that records cache-line and branch traces on fuzzed input pairs. The pair generator is itself optimized to maximize divergence, so the model is not graded only against easy inputs. The third is a report of resource bounds: peak footprint and whether every allocation has a statically bounded size.
Feedback to the model is specific. It names the diverging source line, the kind of divergence (a branch taken differently, or an address that depends on secret data), and whether a remaining allocation has no size bound. Search follows AlphaVerus's tree search rather than greedy repair (AlphaVerus), with rollback to the last functionally correct checkpoint when an edit breaks tests, following Fang et al.. This choice responds to the plateau and thrashing reported by Yan et al. and in VERINA, where 64 rounds of linear feedback lifted proof success only to about 20%.
The target transformations are standard oblivious ones: conditional moves in place of secret-dependent branches, linear scans or fixed sorting networks in place of secret-indexed lookups (Baldimtsi et al.), and fixed-capacity buffers sized by public parameters in place of growable collections. The last transformation is Deca's static and runtime fixed-size type discipline (Deca), applied by rewriting rather than by analysis. Deca's own failure mode is variable-sized types, which it has to leave to the garbage-collected heap. Bounding such types by public input length is exactly the refactoring an LLM can attempt and the checker can verify.
Reward hacking is a real risk here, as it was for AlphaVerus's assume(false) exploits. The cheapest way to make a trace invariant is to stop computing on the input. Functional tests catch most such cases. We add a critic model that flags rewrites which hard-code outputs or skip the secret-dependent computation.
The planning stage takes the single canonical trace of an accepted function. It computes Belady MIN evictions for a given enclave budget, emits a static allocation map and a prefetch schedule, and links them into the enclave runtime. The result needs no in-enclave dynamic allocator, which also shrinks the trusted code base, a secondary concern raised by SecureCloud. Because trace invariance holds only for inputs of equal public length, the planner produces one plan per length class, or a padded plan for a maximum length. The cost of that padding is measured directly.
Experiments run on SGX1-class hardware with a small protected memory. Where budgets must be varied, we cap enclave memory artificially.
Baselines
The naive reference point is the AUTOTEE-ported function or project run unchanged in the enclave, with dynamic allocation and reactive encrypted paging. This is the configuration that produced the 18x cliff in SecureCloud and the 26.73x overhead (84.639s for VGG-16 at 28MB) in Truong et al.. We measure it at the same memory budgets as every other system.
The strongest existing porting method is the full AUTOTEE pipeline. Its reported transformation success is 91.8% on Java and 84.3% on Python with GPT-4o. We rerun it with the same model, the same token budget per function, and the same test suites. Our functional-success rate must not fall more than a few points below AUTOTEE's. Our trace divergence must be near zero where AUTOTEE's is not, which we expect for most crypto and serialization functions.
The strongest memory-performance competitor is a POLO-style generator and decision loop given the same model and budget, but with its decision criterion changed to in-enclave latency under the memory budget. This baseline tests whether optimizing footprint and speed directly works as well as enforcing a fixed trace. We beat it if our planned programs match or exceed its latency at budgets below the program's footprint while also reaching zero divergence.
For the convolutional workloads, the handcrafted y-plane partitioning of Truong et al. is the expert reference, with its reported 3.468s (1.09x) on VGG-16 at 28MB. Convolutional inference is already largely oblivious, so this is a sanity check that automatic planning recovers expert tiling. It is not where we expect to win.
Two ablations isolate the key ingredient. The first runs the same loop with functional feedback only, which removes the trace signal. The second runs the planner on traces recorded from non-invariant code using a single input. When other inputs are then executed, the gap between planned and actual behavior shows directly why the fixed trace is the precondition for planning.
Why not just…?
Why not just apply a rule-based if-conversion or constant-time compiler pass? For small, branch-heavy crypto kernels this is a real threat, and it may match the LLM loop. We include a rule-based rewriter that converts secret-dependent branches to conditional selects and secret-indexed loads to full scans, and we compare function by function. Our expectation is that rules fail wherever obliviousness requires changing data structures or algorithms: bounding variable-sized collections, or replacing a hash lookup with a sorting network. Deca's inability to handle variable-sized types by static analysis alone supports that expectation. If the rule pass matches us on most of the AUTOTEE dataset, the contribution narrows to the planning stage, and we will say so.
Why not just prompt the model to "write constant-time code" once? Unverified model output on security code is often confidently wrong. Perry et al. found AI-assisted users produced secure encryption code 24% of the time versus 43% for the control group. We run zero-shot and few-shot constant-time prompting as a baseline and measure $D(f)$ on the outputs, so the size of this gap is settled empirically rather than assumed.
Why not just shrink the footprint to fit the enclave, or wait for larger enclaves? Fitting in memory removes the paging cliff but not the leakage. Branch shadowing recovers branch decisions regardless of footprint. The POLO-with-footprint baseline measures exactly this alternative. On hardware with large protected memory, the performance half of our claim weakens, and we expect the benefit to concentrate on small-memory settings and out-of-core workloads. The security half is unaffected, because the side channel does not depend on memory size.
Proposed Experiments
The primary benchmark is the AUTOTEE dataset of 385 sensitive leaf functions from 68 repositories. We report functional success, trace divergence, slowdown from obliviousness, and refinement cost in tokens. We found no reported result on Lacuna for automated oblivious rewriting success on this or any other benchmark, so this number is new. For project-scale code with nontrivial data structures, we use POLO's AES, SkipList, KGraph, and MiniSQL, running sensitive entry points in the enclave at budgets of 100%, 50%, and 25% of measured footprint. For convolutional inference, we use VGG-16 and AlexNet at 28MB, following Truong et al.
Four checks decide whether the proposal succeeds. First, trace-feedback refinement must reach zero measured divergence on a clear majority of functions, and on markedly more of them than both the rule-based pass and the functional-only ablation. Second, planned execution at a 25% budget must stay within a small factor of in-enclave execution with ample memory, in the spirit of MAGE's 15% gap, while the naive baseline shows the cliff reported in the literature. Third, the oblivious rewrites must not cost more than the planner saves on workloads that exceed the enclave's memory. We report both the oblivious-only slowdown and the planned-execution speedup separately, so the tradeoff between obliviousness and efficiency is visible rather than hidden. Fourth, a held-out attacker must fail to separate inputs from recorded traces of accepted functions. For this check we use fresh fuzzed inputs not seen by the loop's pair generator, plus a simulated branch-shadowing observer.
References
- MAGE: Nearly Zero-Cost Virtual Memory for Secure Computation
- Automated TEE Adaptation With LLMs: Identifying, Transforming, and Porting Sensitive Functions in Programs
- POLO: An LLM-Powered Project-Level Code Performance Optimization Framework
- Memory-Efficient Deep Learning Inference in Trusted Execution Environments
- AlphaVerus: Bootstrapping Formally Verified Code Generation through Self-Improving Translation and Treefinement
- MEIC: Re-thinking RTL Debug Automation using LLMs
- CodeTransOcean: A Comprehensive Multilingual Benchmark for Code Translation
- VERINA: Benchmarking Verifiable Code Generation
- Sorting and Searching Behind the Curtain: Private Outsourced Sort and Frequency-Based Ranking of Search Results Over Encrypted Data
- Lifetime-Based Memory Management for Distributed Data Processing Systems
- SecureCloud: Secure big data processing in untrusted clouds
- Do Users Write More Insecure Code with AI Assistants?
- Inferring Fine-grained Control Flow Inside SGX Enclaves with Branch Shadowing