Where Quantization Breaks Ownership Reasoning in C-to-Rust Translation

Rewriting old C code in Rust removes whole classes of memory bugs. The best-known is use-after-free: a program reads memory it has already released. Rust removes these bugs only if the translation recovers what each C pointer actually means. That means deciding whether a char* owns its buffer, borrows someone else's, may be null, or points to an array, and then choosing the matching safe Rust type (Box, &, Option, a slice). Large language models are now the strongest tools for this "semantic lifting." They are also expensive, and the usual way to make them cheaper is quantization: storing weights in 4, 3 or 2 bits instead of 16. Quantization studies consistently find that low bit widths damage multi-step reasoning well before they damage fluent-looking output. Ownership inference is exactly that kind of reasoning.

This proposal claims that the usual score for transpilation, whether the output compiles and passes tests, hides a specific failure. A compressed model can still produce working Rust while quietly giving up on ownership. It may wrap raw pointers in unsafe, clone data everywhere, or reach for reference-counted cells, so the use-after-free risk is kept or merely hidden rather than removed. We propose to measure this "ownership fidelity" directly, to find the bit width where it collapses, and to test one cheap fix. The fix changes how quantization decides which rounding errors matter: during calibration, it gives extra weight to the few tokens where the model commits to an ownership decision.

Core Claim

We make two linked, falsifiable claims. The first is diagnostic. On C-to-Rust lifting, ownership fidelity declines at a higher bit width than test pass rate does. Ownership fidelity here means agreement with statically inferred ownership, absence of unsafe, and correct handling of seeded use-after-free code. If so, a quantized translator can look healthy on standard benchmarks while reintroducing the memory unsafety the migration was meant to remove.

The second claim is about method. Standard post-training quantization rounds each layer's weights to keep that layer's outputs close to the original on a small calibration set, weighting every token equally. In translated Rust, ownership is decided at a small fraction of positions: type annotations, &/&mut, Box::new, Option, .clone(), unsafe. Reweighting the calibration objective toward those positions should protect ownership fidelity at 3 bits much better than uniform weighting on the same data. Concretely, GPTQ-style and QuIP-style rounding (the LDLQ step in QuIP) minimize a quadratic proxy built from the layer-input statistic $H=\sum_t x_t x_t^\top$, where $x_t$ is the layer input at token $t$. We replace it with

$$H_w=\sum_t w_t\, x_t x_t^\top,\qquad w_t = 1+\lambda\,\mathbb{1}[t\in\mathcal{D}],$$

where $\mathcal{D}$ is the set of ownership-decision tokens in teacher-forced reference Rust and $\lambda\ge 0$ sets how much they count. This changes only the calibration statistic. The quantizer, kernels and model are otherwise unchanged, so the method can be deployed wherever the base method can.

Relation to Prior Work

The research file reports no paper that quantizes models for C-to-safe-Rust translation. That absence alone does not make the work worthwhile. What does is that each side has evidence pointing at the other.

On the quantization side, two studies motivate the diagnostic claim. Evaluating Quantized Large Language Models surveys post-training quantization across 11 model families. It finds weight-only 4-bit near-lossless, and finds that mathematical reasoning degrades before surface fluency. An empirical study of LLaMA3 quantization finds a similar cliff for visual reasoning at 2 bits. Neither tests compiler-checked code or ownership. We ask whether borrow and lifetime reasoning follows the same pattern, and we add a measurement that catches failures a test pass rate would miss.

QuIP supplies the method we build on. It rotates weights with random orthogonal matrices so that no single weight is unusually large, then applies adaptive rounding. It reports WikiText-2 perplexity of 6.33 for Llama 2 70B at 2 bits, against 123.91 for OPTQ. QuIP, like OPTQ, optimizes each layer against an unweighted proxy. Our change targets exactly that choice.

Mixed-precision methods protect sensitive parts of the network with generic sensitivity scores rather than task-specific ones. SliM-LLM and AlphaQ allocate bits by weight or expert salience, and SpinQuant learns the rotations. Our weighting is independent of all of them and can be layered on top.

On the translation side, several papers supply the evaluation and the ground truth. Ownership guided C to Rust translation introduces Crown, which statically infers whether each C pointer owns or borrows. We use its output as a reference label for ownership fidelity, not as a translator. SACTOR splits translation into a literal C-compatible step and an idiomatic refinement step guided by Crown hints. It reports 93% end-to-end success with DeepSeek-R1 on TransCoder-IR and notes that reasoning models handle pointer layout far better than instruction-tuned ones. That suggests the idiomatic refinement step is where precision loss should bite, and we evaluate inside this pipeline.

Other papers set expectations. LLM-Based Rust Code Generation with On-the-Fly Compiler Feedback finds that larger models use compiler feedback better than smaller ones. This is a warning that quantized models may also lose the ability to act on rustc diagnostics, and we test it directly. CRUST-Bench and C2RUST-BENCH provide the benchmarks. Fine-Tuning Qwen3-27B for C-to-Rust reports 87.3% success on 200 CodeNet-derived C programs, evaluated only at full precision, and supplies the C2Rust-Moxin paired data we draw calibration samples from. The LoRA finding in the LLaMA3 study cuts against us in one respect: specialized fine-tuning can make quantized models more fragile. We therefore quantize specialized and general code models separately rather than assume the results transfer.

Research Program

The work has three parts.

Measurement. Ownership fidelity combines three quantities, each computed on outputs that compile. The first is the Crown agreement rate: the fraction of pointer-typed C parameters and locals whose lifted Rust type falls in the ownership class Crown infers (owned, borrowed immutable, borrowed mutable, nullable). The second is the unsafe-and-escape rate: unsafe blocks, Box::leak, raw-pointer casts, and Rc<RefCell<_>> introduced where Crown found a single owner. The third is a use-after-free laundering rate. For this we build a probe set of a few hundred C functions from C2RUST-BENCH and CRUST-Bench sources. Some have seeded temporal bugs (use after free, double free, a freed pointer returned through an out-parameter). Others are matched correct versions with tricky conditional ownership transfer. A faithful safe lift of a buggy function either cannot compile or must restructure the code so the stale access becomes impossible. "Laundering" means the output compiles and still performs the stale access through unsafe or a leaked or raw pointer. The matched correct functions keep this metric from simply rewarding compile failures.

Weighted calibration. We tag ownership-decision tokens with a Rust parser on reference translations from C2Rust-Moxin: tokens in type positions, borrow operators, and ownership-changing calls. We compute $H_w$ and run otherwise-standard GPTQ and QuIP at 4, 3 and 2 bits. $\lambda$ is chosen on a held-out calibration split, never on the test benchmarks.

Feedback sensitivity. For each bit width we measure the recovery curve under SACTOR-style post-hoc rustc feedback and under on-the-fly feedback. This shows whether a quantized model still turns borrow-checker errors such as E0382 (use after move) into correct fixes, or has stopped responding to diagnostics.

The models are Qwen2.5-coder 14B and 32B, which C2RUST-BENCH already evaluates at full precision, and base Qwen3.5-27B. If the curriculum-trained Qwen3.5-27B weights from the fine-tuning paper are available, we add them to test whether curriculum skills survive quantization. Everything runs on open weights with fixed decoding.

Baselines

The naive reference is the same model at 16 bits in the same SACTOR pipeline with the same repair budget. It sets the ceiling for every metric. The closest published full-precision figure is 72.3% success for base Qwen3.5-27B on the 200-program CodeNet benchmark, reported in Fine-Tuning Qwen3-27B for C-to-Rust. C2RUST-BENCH reports 97.6% transpilation success for Qwen2.5-coder 32B, but that metric does not require safe code, which is why we add ownership fidelity. We found no reported result on Lacuna for any quantized model on a C-to-Rust benchmark, so the quantized baselines are produced here.

The strongest existing method is QuIP at matched bit width. It is the published method that survives 2 bits (perplexity 6.33 against 123.91 for OPTQ on Llama 2 70B, per QuIP). We also run GPTQ, and AWQ at 3 bits, since Evaluating Quantized Large Language Models reports that AWQ rescues 3-bit weights but fails at 2 bits. Each baseline gets the same number of calibration sequences and the same group size. Baselines are calibrated on their default generic data and, separately, on the same Rust data we use.

The ablation that isolates the key ingredient is $\lambda=0$ on identical C2Rust-Moxin calibration data. This is uniform-weight calibration on in-domain code, and it answers whether token weighting matters or only the choice of domain.

A memory-matched smaller model at 16 bits is also a baseline: Qwen2.5-coder 14B at FP16 against 32B at 3–4 bits. The proposal succeeds if, at 3 bits, weighted calibration recovers at least half of the gap in Crown agreement and use-after-free laundering rate between uniform in-domain calibration and FP16, with test pass rate no worse. The comparison must hold on at least two of the three models, with differences outside seed-to-seed variation across calibration subsets.

Why not just…?

Why not just stay at 4 bits, which is already near-lossless? This is the strongest objection, and it may be right. Both quantization studies report 4-bit weights within about 2% of FP16 on general benchmarks. Those benchmarks do not measure ownership, and our diagnostic claim is precisely that test-style scores miss this loss. If ownership fidelity at 4 bits also stays within noise of FP16, we will report that 4 bits is safe for migration and shift the method to 3 bits, where AWQ is already reported to be marginal. If weighting cannot help at 3 bits either, the method claim fails.

Why not just run a smaller model at full precision in the same memory? The file's evidence is suggestive but indirect. QuIP shows larger models tolerate compression better, and the on-the-fly feedback paper shows larger models use compiler feedback better. Neither compares memory-matched configurations on translation. The 14B-FP16 versus 32B-quantized comparison settles it for this task. If the small model wins on ownership fidelity, deployment should use it, and we will say so.

Why not just calibrate on Rust code, without special token weights? This is a real threat. Most of any gain may come from domain match rather than from weighting. The $\lambda=0$ ablation on identical data answers it directly. The idea has value only if weighting adds measurable ownership fidelity beyond in-domain calibration.

Why not just forbid unsafe and let the compiler loop fix everything? Adding #![forbid(unsafe_code)] removes one escape route, and we will run that setting. It does not stop the model from falling back on Rc<RefCell<_>>, cloning, or leaking memory to satisfy the borrow checker. It also does not tell us whether a quantized model can still use diagnostics. CRUST-Bench already shows repair loops plateauing at 48% for o3 at full precision. The feedback-sensitivity curves test whether low-bit models fall below that plateau.

Decisive Checks

The diagnostic claim fails if, across bit widths, the drop in ownership fidelity tracks the drop in test pass rate. The standard metric would then be enough. It holds if at some bit width, most likely 3, pass rate stays within a few points of FP16 while Crown agreement falls or laundering rises clearly.

The method claim fails if the weighted calibration does not beat $\lambda=0$ on the same data at 3 bits. A secondary check is whether $\lambda>0$ lowers perplexity on general Rust text. That would be expected: the weighting should trade a little average accuracy for accuracy at ownership decisions. If general perplexity instead improves at the same time, the weighting is probably acting as extra regularization rather than protecting ownership, and we will say so.

Evaluation covers CRUST-Bench (safe interfaces and tests), the C2RUST-BENCH functions, the 200 CodeNet programs, libogg within SACTOR, and the use-after-free probe set, which we will release.