Bun Rust rewrite: "codebase fails basic miri checks, allows for UB in safe rust"
TL;DR Highlight
Miri found UB reachable from safe APIs in Bun's Rust port, leading to Miri in CI
Who Should Read
A backend or runtime developer about to merge a large AI-generated code translation into the main branch
Core Mechanics
- PathString::init is safe yet causes UB
- Initializing from a Boxed slice, dropping it, then calling slice() makes miri report a dangling reference.
- The issue author posted this repro on May 14, 2026.
- The cause is an erased lifetime
- A comment explained that init takes &[u8], erases its lifetime, and returns a 'static Self.
- That allows use after free and invalid aliasing.
- A second UB showed up
- Calling init_owned then deinit_owned triggered a Stacked Borrows violation in miri.
- The author said they found it within minutes.
- robobun opened a PR making them unsafe fn
- It marked PathString::init and dir_iterator::next() as unsafe fn and added SAFETY comments at about 70 call sites.
- It also added compile_fail doctests to keep the unsafe keyword on three signatures.
- Maintainer says the port is 1:1 as a starting point
- Jarred-Sumner said the Rust port is meant to map as closely as possible to the original Zig code.
- He asked people to keep reporting bugs and unsound behavior.
- The result was cargo-miri support
- #30876 added cargo-miri support and fixed a HiveArray aliasing UB, closing the issue.
- The issue was retitled 'PathString::slice dangling reference UB - add Miri to CI'.
Community Reactions
- Opinion that the safe API is the real problem
- One comment said the issue is not UB miri would catch but an API exposing UB from safe code.
- It argued this is a temporary porting-stage flaw to fix before an actual release.
- Doubt about the translation method
- One comment asked why they used an LLM instead of building a deterministic translation tool.
- It argued the output is less trustworthy because no human has looked at it.
- Verification should be stronger
- One comment said that when code can be generated this fast, verification should be 100x more robust.
- Another said they should have run tests under miri from the start.
- Experience of moving back to Node
- One commenter said their company decided to migrate everything from Bun back to Node after the merge.
- Several comments worried that a roughly 1M-line diff was not properly reviewed by humans.
How to Apply
- Run miri in your Rust CI
- Today, run cargo +nightly miri test on a crate that contains unsafe.
- Collect failing tests and UB reports into an issue.
- Audit every safe wrapper around unsafe
- Grep for safe functions that erase lifetimes or hand out raw pointers.
- If callers must uphold conditions, make it unsafe fn and document the reasoning in a SAFETY comment.
- Add compile_fail tests against unsafe regressions
- Write a doctest per risky signature that fails to compile when called as safe.
- CI then catches anyone removing the unsafe keyword.
- Gate large AI translations before merge
- Require miri and an unsafe review checklist beyond passing existing tests.
- State the release criteria in the PR description.
Try It
# Run in terminal (check the pattern from the issue with miri)
rustup toolchain install nightly --component miri
cargo +nightly miri setup
# Run tests under miri from the project root
cargo +nightly miri test
# Also check pointer provenance strictly
MIRIFLAGS=-Zmiri-strict-provenance cargo +nightly miri testTerminology
Related Papers
Gemini 4 Neon is the new #1 on the Models Engineering Index
Gemini 4 Neon ranks first on the Models Engineering Index with 68.0, combining multiple benchmarks.
LLMs can read and write executables byte by byte from a hex dump
Given only a hex dump and no tools, an LLM analyzed, patched and wrote working executables.
Claude was trained on protein sequences at scale
Next-amino-acid tests on obscure proteins show only the latest Claude models predict them
Remembered as Brief: Stored Preferences SilentlyRemove Decision-Critical Information
A stored 'keep it short' preference silently drops warnings; listing must-know points first restores content.
All AI-generated code should be Open Source
A blog argues AI-generated code should be open source; most HN commenters disagreed.
What thousands of conversations look like inside a language model
Mapping Qwen3-1.7B states showed Chinese sitting apart from English, while German and Croatian overlapped it.
AI, activity, and outcomes in software engineering
AI sped up writing code, but shipping speed did not rise as much, says a talk
LLMs Play Zachtronics' TIS-100
16 LLMs played TIS-100 and set 133 new Pareto frontier points across 18 puzzles