shekohex / allo-isolate

Run Multithreaded Rust along with Dart VM (in isolate) 🌀
Apache License 2.0
120 stars 18 forks source link

Program seems to panic when dropping vectors #59

Closed fzyzcjy closed 4 months ago

fzyzcjy commented 5 months ago

Hi thanks for the lib! It seems that it panics under some scenarios.

Reproduction:

Firstly, use Rust 1.79.0

cargo --version
cargo 1.79.0 (ffa9cf99a 2024-06-03)

Then add these lines to tests/containers.rs (or whatever other places) and run RUST_BACKTRACE=1 cargo run --features="anyhow backtrace chrono uuid" --example containers:

    let a: ZeroCopyBuffer<Vec<u64>> = ZeroCopyBuffer(vec![]);
    let b = a.into_dart();
    drop(b);

And see

thread 'main' panicked at library/core/src/panicking.rs:220:5:
unsafe precondition(s) violated: NonNull::new_unchecked requires that the pointer is non-null
stack backtrace:
   0: rust_begin_unwind
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:120:5
   3: core::panicking::panic_nounwind
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:220:5
   4: core::ptr::non_null::NonNull<T>::new_unchecked::precondition_check
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ub_checks.rs:66:21
   5: core::ptr::non_null::NonNull<T>::new_unchecked
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ub_checks.rs:73:17
   6: core::ptr::unique::Unique<T>::new_unchecked
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ptr/unique.rs:89:36
   7: alloc::raw_vec::RawVec<T,A>::from_raw_parts_in
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/raw_vec.rs:259:30
   8: alloc::vec::Vec<T,A>::from_raw_parts_in
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/vec/mod.rs:831:29
   9: alloc::vec::Vec<T>::from_raw_parts
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/vec/mod.rs:604:18
  10: <<allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop::MyVisitor as allo_isolate::into_dart::DartTypedDataTypeVisitor>::visit
             at ./src/ffi.rs:186:29
  11: allo_isolate::into_dart::visit_dart_typed_data_type
             at ./src/into_dart.rs:268:35
  12: <allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop
             at ./src/ffi.rs:196:17
  13: core::ptr::drop_in_place<allo_isolate::ffi::DartCObject>
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ptr/mod.rs:514:1
  14: core::mem::drop
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/mem/mod.rs:938:24
  15: containers::main
             at ./tests/containers.rs:9:5
  16: core::ops::function::FnOnce::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
[1]    29935 abort      RUST_BACKTRACE=1 cargo run --features="anyhow backtrace chrono uuid" --exampl

EDIT: Very briefly glanced at the code, and I guess this line may be related: https://github.com/shekohex/allo-isolate/blob/0ae80ab08ba788776e5585aaa34238dfa6111aba/src/into_dart.rs#L171

Hack it by changing to such a not-allocated pointer (definitely should not be the final solution, just a quick test), and the error disappears.

            values: vec![].as_mut_ptr(),

P.S. the empty non-allocated vec looks like to have address like the following.

image

shekohex commented 4 months ago

Another way, is to take into account a null ptr when the vec is empty before we go and dealloc it: https://github.com/shekohex/allo-isolate/blob/0ae80ab08ba788776e5585aaa34238dfa6111aba/src/ffi.rs#L184-L191

You can see from the following frames:

10: <<allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop::MyVisitor as allo_isolate::into_dart::DartTypedDataTypeVisitor>::visit
             at ./src/ffi.rs:186:29
  11: allo_isolate::into_dart::visit_dart_typed_data_type
             at ./src/into_dart.rs:268:35