Skip to content

all of rust codebase: This codebase fails even the most basic miri checks, allows for UB in safe rust #30719

@AwesomeQubic

Description

@AwesomeQubic
error: Undefined Behavior: constructing invalid value of type &[u8]: encountered a dangling reference (0x20933[noalloc] has no provenance)
  --> src/main.rs:97:18
   |
97 |         unsafe { core::slice::from_raw_parts(ptr as *const u8, self.len()) }
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
   |
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
   = note: stack backtrace:
           0: PathString::slice
               at src/main.rs:97:18: 97:75
           1: main
               at src/main.rs:130:22: 130:34

code:

fn main() {
    let test = Box::new(*b"Hello World");
    let init = PathString::init(&*test);
    drop(test);

    println!("{:?}", init.slice());
}

Please consider not vibe coding rust as AIs are not good at writing Rust and also hire a real rust dev

Activity

changed the title [-]all of rust codebase: This codebase fails even the most basic miri checks, and allows for UB in safe rust example[/-] [+]all of rust codebase: This codebase fails even the most basic miri checks, and allows for UB in safe rust[/+] on May 15, 2026
robobun

robobun commented on May 15, 2026

@robobun
Collaborator

✅ Reproduced — PathString::init is safe but erases the slice lifetime, so the reporter's example compiles and produces a dangling &[u8]. Fix in #30728 marks PathString::init (and the parallel hole in dir_iterator::next()) as unsafe fn with documented outlives contracts, audits ~70 in-tree call sites with per-site SAFETY comments naming the backing allocation, adds compile_fail doctests enforcing the unsafe keyword on all three signatures, and closes the readdir-error fd-leak in the resolver.

Diff is green. CI is red on the pre-existing v8-heap-snapshot.test.ts SIGKILL flake (ubuntu-x64 shard 11/20, hit on three separate builds — unrelated to this PR). Needs a maintainer to merge or retrigger.

AwesomeQubic

AwesomeQubic commented on May 15, 2026

@AwesomeQubic
Author

also PathString::init erases provenance and fails with MIRIFLAGS=-Zmiri-strict-provenance

changed the title [-]all of rust codebase: This codebase fails even the most basic miri checks, and allows for UB in safe rust[/-] [+]all of rust codebase: This codebase fails even the most basic miri checks, allows for UB in safe rust[/+] on May 15, 2026
JavaDerg

JavaDerg commented on May 15, 2026

@JavaDerg

Maybe to explain the issue a bit further, looking at https://github.com/oven-sh/bun/blob/main/src/bun_core/string/PathString.rs#L111-L119, we can see that init takes &[u8] a reference to a u8 slice. This references has a lifetime, which is implicit. The unsafe operation erases the lifetime and returns Self which is 'static. This is incredibly bad beacuse it allows for use after free, invalid aliasing, etc.

This is just one example from the codebase, debugging this will be a horrible experience, far worse than what is typical in c/c++ code, especially if your codebase is loaded with such quite horrible mistakes. Rust makes many strong assumtions on it's strong safety model and UB can causes issues in places where you just wont expect them. You are disabeling all of the advantages you claim you get by moving to Rust.

It is highly adviced that every usage of unsafe is properly reviewed in the codebase. Rust is not suited for a 1:1 translation from other languages with different memory managment schemes. Beyond that it's also simply not a good idea.

There is a lot more to say about why all of this is not a good idea, but just to elaborate on the issue it self.

added a commit that references this issue on May 15, 2026
0db8c59
added a commit that references this issue on May 15, 2026
rkben

rkben commented on May 15, 2026

@rkben

Zig to Rust can be directly translated and Bun isn't a dead project we should stop using. Right?

39 remaining items

lc-nyovchev

lc-nyovchev commented on May 16, 2026

@lc-nyovchev
lessless

lessless commented on May 16, 2026

@lessless
donuts-are-good

donuts-are-good commented on May 16, 2026

@donuts-are-good
IsaacGemal

IsaacGemal commented on May 16, 2026

@IsaacGemal
HeyBanditoz

HeyBanditoz commented on May 16, 2026

@HeyBanditoz
DMessham

DMessham commented on May 16, 2026

@DMessham
pluto-0

pluto-0 commented on May 16, 2026

@pluto-0
bejminliou

bejminliou commented on May 16, 2026

@bejminliou
defidude

defidude commented on May 16, 2026

@defidude
SimonReiff

SimonReiff commented on May 16, 2026

@SimonReiff

architector4@AGOGUS:/tmp$ git clone --depth=1 'https://github.com/oven-sh/bun'
Cloning into 'bun'...

architector4@AGOGUS:/tmp$ cd bun/
architector4@AGOGUS:/tmp/bun$ find -type f -name '*.rs' -exec grep unsafe {} ; | grep -v '//' | wc -l
13255

This actually makes me mad for reasons I'm still trying to articulate. Maybe it's because I work so hard to take care not to do sloppy things, especially anywhere around using AI agents, and then you see things like this.

I suggest that the maintainers promptly revert, then pause, think, deliberate, and discuss thoroughly with the community, before implementing new policies and procedures surrounding the proper usage of AI code going forward. It just isn't respectful to the 90k+ users who gave a star, and the countless more who adopted Bun or are generally supportive of the project, and reflects poorly on anyone involved in or adjacent to the project. It's bad enough that this was shipped. Every minute this isn't retracted is a new mistake.

Kevin-Illu

Kevin-Illu commented on May 16, 2026

@Kevin-Illu
VictorMMorG

VictorMMorG commented on May 16, 2026

@VictorMMorG
locked as off topic and limited conversation to collaborators on May 16, 2026
Jarred-Sumner

Jarred-Sumner commented on May 16, 2026

@Jarred-Sumner
Collaborator

Please continue to point out any and all issues in the Rust code. Today, the Rust port is intended to be as close as possible to a 1:1 mapping of the original Zig code - which is the starting point. We are making it better.

This particular issue got a little out of hand with memes, but please do continue to file new issues about any bugs or unsound behavior you find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @joeyguerra@Jarred-Sumner@DanielRuf@lc-nyovchev@lessless

      Issue actions