MPK: Segfault running an `Engine` on a preexisting thread

Đang mở
#14,581 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend

Hướng nghiên cứu

Two reproducers are given in the issue: one segfaults when a Store (with memory_protection_keys(Enabled::Yes) and linear_memory_keep_resident) is dropped on a different thread than where it ran, the other when an Engine's memory is read from a preexisting thread. Start by reproducing both under a debugger to see where the fault happens, then trace wasmtime's MPK/PKRU handling in the pooling allocator — where per-thread key state is registered, saved/restored across threads, and cleaned up on thread exit and Store drop. Done means both snippets run to completion without a segfault, ideally with a regression test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug wasmtime:pooling-allocator

This input:

use std::sync::mpsc;
use wasmtime::*;

const WAT: &str = r#"(module
    (memory (export "m") 1)
    (data (i32.const 0) "hello")
    (func (export "f") (i32.store (i32.const 100) (i32.const 1))))"#;

fn engine() -> Result<Engine> {
    let mut pool = PoolingAllocationConfig::default();
    pool.memory_protection_keys(Enabled::Yes);
    pool.total_memories(32);
    pool.linear_memory_keep_resident(65536);
    let mut config = Config::new();
    config.allocation_strategy(InstanceAllocationStrategy::Pooling(pool));
    Engine::new(&config)
}

fn main() -> Result<()> {
    let (tx, rx) = mpsc::channel::<Store<()>>();
    let pre = std::thread::spawn(move || {
        let store = rx.recv().unwrap();
        eprintln!("dropping store on other thread...");
        drop(store);
        eprintln!("dropped");
    });
    let engine = engine()?;
    let module = Module::new(&engine, WAT)?;
    let mut store = Store::new(&engine, ());
    let instance = Instance::new(&mut store, &module, &[])?;
    instance
        .get_typed_func::<(), ()>(&mut store, "f")?
        .call(&mut store, ())?;
    tx.send(store).unwrap();
    pre.join().unwrap();

    Ok(())
}

fails with:

$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/mpk-thread`
dropping store on other thread...
zsh: segmentation fault (core dumped)  cargo run

and this input:

use std::sync::mpsc;
use wasmtime::*;

const WAT: &str = r#"(module
    (memory (export "m") 1)
    (data (i32.const 0) "hello")
    (func (export "f") (i32.store (i32.const 100) (i32.const 1))))"#;

fn engine() -> Result<Engine> {
    let mut pool = PoolingAllocationConfig::default();
    pool.memory_protection_keys(Enabled::Yes);
    pool.total_memories(32);
    pool.linear_memory_keep_resident(65536);
    let mut config = Config::new();
    config.allocation_strategy(InstanceAllocationStrategy::Pooling(pool));
    Engine::new(&config)
}

fn main() -> Result<()> {
    let (tx, rx) = mpsc::channel::<Engine>();
    let worker = std::thread::spawn(move || -> Result<()> {
        let engine = rx.recv().unwrap();
        let module = Module::new(&engine, WAT)?;
        let mut store = Store::new(&engine, ());
        let instance = Instance::new(&mut store, &module, &[])?;
        let mem = instance.get_memory(&mut store, "m").unwrap();
        eprintln!("reading memory from pre-existing thread...");
        eprintln!("data = {:?}", &mem.data(&store)[..5]);
        Ok(())
    });
    tx.send(engine()?).unwrap();
    worker.join().unwrap()?;
    Ok(())
}

fails with:

$ cargo run
   Compiling mpk-thread v0.1.0 (/home/alex/code/wasmtime2/reports/001-mpk-pkru-preexisting-thread-segv)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/mpk-thread`
reading memory from pre-existing thread...
data = [zsh: segmentation fault (core dumped)  cargo run
Ngôn ngữ chính
Rust
Star
18.9k
Fork
2k
Merge trung bình
1 ngày 6 giờ
Pull request đã merge (30 ngày)
238

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bytecodealliance/wasmtime

Tất cả issue của bytecodealliance/wasmtime

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.