gfx-rs / metal-rs

Rust bindings for Metal
Apache License 2.0
567 stars 112 forks source link

new_library_with_data is broken #308

Closed K0bin closed 3 months ago

K0bin commented 4 months ago

There appears to be a memory corruption bug with new_library_with_data.

Calling it results in all kinds of weird errors. Here are a few I've run into today while trying to figure out my problem:

Writing the exact same data to disk and using new_library_with_file instead works perfectly fine.

use metal;

const BROKEN: bool = true;

fn main() {
    println!("Hello, world!");

    let device = metal::Device::system_default().unwrap();
    let data = std::fs::read("test.metallib").unwrap();
    let data_box = data.into_boxed_slice();
    let lib = if BROKEN {
        device.new_library_with_data(&data_box).unwrap()
    } else {
        device.new_library_with_file("test.metallib").unwrap()
    };
    std::mem::drop(data_box);
    let function = lib.get_function("main0", None).unwrap();
    let _pipeline = device.new_compute_pipeline_state_with_function(&function).unwrap();
    println!("Done");
}

Minimal reproducer that produces the failed assert inside the Metal driver with _with_data but not when using _with_file

madsmtm commented 4 months ago

Thanks for the reproducer, have posted https://github.com/gfx-rs/metal-rs/pull/309 to fix this.