LLMs struggle with unsafe Rust: six months of testing revealed critical errors

For six months, the author had LLMs write unsafe Rust code in real projects and analyzed the results with Miri. The models make the same mistakes consistently: aliasing, provenance, layout in memory management, 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 revealed 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 — violation of the non-overlapping references rule
Key metric #2Provenance — attempts to use improperly obtained pointers
Key metric #3Layout in alloc/dealloc — size/alignment mismatch during allocation and deallocation

The author had LLM write unsafe Rust code in production projects for six months and verified each block with Miri and sanitizers. The result was disappointing: models made the same errors predictably and consistently.

Seven categories of LLM errors in unsafe Rust

Models consistently make errors in the same locations:

  • Aliasing — violation of the non-overlapping references rule
  • Provenance — attempts to use improperly obtained pointers
  • Layout in alloc/dealloc — size/alignment mismatch during allocation and deallocation
  • ManuallyDrop — forget to call drop or access the object after drop
  • Race conditions in FFI callbacks — race conditions when calling C functions from threads
  • Manual Send/Sync — incorrectly implement thread safety traits
  • Uninit memory — treat uninitialized data as initialized

Why Miri revealed the problems

Miri is a Rust interpreter that tracks undefined behavior. When LLM generates code with subtle violations at first glance (for example, a temporary pointer with incorrect provenance), the regular compiler misses it, but Miri catches it. Under sanitizers (AddressSanitizer, ThreadSanitizer), race conditions and use-after-free were uncovered, which may not appear in regular tests.

"Each category comes with a minimal example and 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 errors in memory management and multithreading. For critical code (system software, system libraries), unsafe Rust from models must be checked with Miri.

Key Questions Answered

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

Models consistently make errors in the same locations: Aliasing — violation of the non-overlapping references rule Provenance — attempts to use improperly obtained pointers Layout in alloc/dealloc — size/alignment mismatch during allocation and deallocation ManuallyDrop — forget to call drop or access the object after drop Race conditions in FFI callbacks — race conditions when calling C functions from threads Manual Send/Sync — incorrectly implement thread safety traits Uninit memory — treat uninitialized data as initialized

Why Miri revealed the problems?

Miri is a Rust interpreter that tracks undefined behavior. When LLM generates code with subtle violations at first glance (for example, a temporary pointer with incorrect provenance), the regular compiler misses it, but Miri catches it. Under sanitizers (AddressSanitizer, ThreadSanitizer), race conditions and use-after-free were uncovered, which may not appear in regular tests. "Each category comes with a minimal example and 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 errors in memory management and multithreading. For critical code (system software, system libraries), unsafe Rust from models must be checked with 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.
✦ Hands-On AI Engineering · Hamidun Academy

Build & Ship Production AI Agents Without Boilerplate Code

Start your free 72-hour trial at Hamidun Academy (academy.hamidun.com) — interactive lessons on Claude Code & Cursor, live Tech Radar, and production agent templates.

Start 72h Free Trial →Vibe-Coding Intensive
What do you think?
Loading comments…