LLMs Struggle With Unsafe Rust: Six Months of Testing Reveal Critical Errors

The author spent six months having LLMs write unsafe Rust in real projects and analyzed the results with Miri. The models make the same mistakes consistently: aliasing, provenance, layout in memory management, a forgotten ManuallyDrop, races in FFI, incorrect Send/Sync and uninitialized memory. Every error can be reproduced and fixed.

AI-processed from Habr AI; edited by Hamidun News
LLMs Struggle With Unsafe Rust: Six Months of Testing Reveal Critical Errors
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Key metric #1Aliasing — violating the rule on non-overlapping references
Key metric #2Provenance — trying to use pointers obtained incorrectly
Key metric #3Layout in alloc/dealloc — a mismatch in size/alignment between allocation and deallocation

The author had LLMs write unsafe Rust in production projects for six months and checked every block with Miri and sanitizers. The result was disappointing: the models make the same errors predictably and consistently.

Seven categories of LLM errors in unsafe Rust

The models keep failing in the same places:

  • Aliasing — violating the rule on non-overlapping references
  • Provenance — trying to use pointers obtained incorrectly
  • Layout in alloc/dealloc — a mismatch in size/alignment between allocation and deallocation
  • ManuallyDrop — forgetting to call drop, or using an object after drop
  • Races in FFI callbacks — race conditions when C functions are called from threads
  • Manual Send/Sync — incorrect implementations of thread-safety traits
  • Uninit memory — treating uninitialized data as initialized

Why Miri exposed the problems

Miri is a Rust interpreter that tracks undefined behavior. When an LLM generates code with violations that are not obvious at first glance (for example, a temporary pointer with incorrect provenance), the regular compiler lets it through, while Miri catches it. Under sanitizers (AddressSanitizer, ThreadSanitizer), races and use-after-free bugs surfaced that may not show up in ordinary tests.

"Each category comes with a minimal example and a fix" — the author

shows that these are not just observations but reproducible error patterns.

What this means

LLMs are not yet ready to write unsafe Rust without human oversight. They generate syntax and general logic well, but systematically make mistakes when dealing with memory and multithreading. For critical code (system software, system libraries), unsafe Rust written by models must be checked under Miri.

Key Questions Answered

What are the key details on Seven categories of LLM errors in unsafe Rust?

The models keep failing in the same places: Aliasing — violating the rule on non-overlapping references Provenance — trying to use pointers obtained incorrectly Layout in alloc/dealloc — a mismatch in size/alignment between allocation and deallocation ManuallyDrop — forgetting to call drop, or using an object after drop Races in FFI callbacks — race conditions when C functions are called from threads Manual Send/Sync — incorrect implementations of thread-safety traits Uninit memory — treating uninitialized data as initialized

Why Miri exposed the problems?

Miri is a Rust interpreter that tracks undefined behavior. When an LLM generates code with violations that are not obvious at first glance (for example, a temporary pointer with incorrect provenance), the regular compiler lets it through, while Miri catches it. Under sanitizers (AddressSanitizer, ThreadSanitizer), races and use-after-free bugs surfaced that may not show up in ordinary tests. "Each category comes with a minimal example and a fix" — the author shows that these are not just observations but reproducible error patterns.

What this means?

LLMs are not yet ready to write unsafe Rust without human oversight. They generate syntax and general logic well, but systematically make mistakes when dealing with memory and multithreading. For critical code (system software, system libraries), unsafe Rust written by models must be checked under Miri.

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…