Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , bug of apple's bag of tools) #1252

Open
peitalin opened this issue Nov 4, 2024 · 10 comments

Comments

@peitalin
Copy link

peitalin commented Nov 4, 2024

I'm running cargo test on this example: https://docs.near.org/build/smart-contracts/quickstart and getting panics on the set_then_get_greeting() test:

thread 'tests::set_then_get_greeting' panicked at core/src/panicking.rs:221:5:
unsafe precondition(s) violated: ptr::replace requires that the pointer argument is aligned and non-null

Commenting out the log!() line resolves the issue.
I'm running:

rustc 1.82.0 (f6e511eec 2024-10-15)
cargo-near-near 0.11.0
near-cli-rs 0.15.1

Reverting back to rustc 1.79-aarch64-apple-darwin fixes the issues, seems like a similar issue to this:
https://stackoverflow.com/questions/78952340/unsafe-preconditions-violated-ptrreplace-requires-that-the-pointer-argument

Rust versions after v1.79 panic:

stable: 1.80, 1.81, 1.82 panics
beta 1.83 panics
nightly 1.84 panics
@peitalin
Copy link
Author

peitalin commented Nov 4, 2024

Adding some additional info:

$ RUST_BACKTRACE=full cargo test

    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.17s
     Running unittests src/lib.rs (target/debug/deps/hello2-9da8e95d65f72eb1)

running 2 tests
test tests::get_default_greeting ... ok
Saving greeting: howdy
thread 'tests::set_then_get_greeting' panicked at core/src/panicking.rs:221:5:
unsafe precondition(s) violated: ptr::replace requires that the pointer argument is aligned and non-null
stack backtrace:
   0:        0x100a4c2e4 - std::backtrace_rs::backtrace::libunwind::trace::h1c3451b342542f10
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:        0x100a4c2e4 - std::backtrace_rs::backtrace::trace_unsynchronized::h3743cad8cc2d54de
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x100a4c2e4 - std::sys::backtrace::_print_fmt::h31ad576fee12d4ee
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/backtrace.rs:66:9
   3:        0x100a4c2e4 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::habbf9c4f641febb1
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/backtrace.rs:39:26
   4:        0x100a67f1c - core::fmt::rt::Argument::fmt::h54965d9b1ee82264
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/fmt/rt.rs:177:76
   5:        0x100a67f1c - core::fmt::write::ha36a8060c13608ea
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/fmt/mod.rs:1178:21
   6:        0x100a49bd0 - std::io::Write::write_fmt::h431832c8ebcc85c9
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/io/mod.rs:1823:15
   7:        0x100a4d8a4 - std::sys::backtrace::BacktraceLock::print::hd085ca0c3911e184
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/backtrace.rs:42:9
   8:        0x100a4d8a4 - std::panicking::default_hook::{{closure}}::h4aa1f60327dfff6a
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:266:22
   9:        0x100a4d454 - std::panicking::default_hook::h4ebc6eb4ae179807
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:293:9
  10:        0x10091028c - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h53c41d20c10901e7
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/boxed.rs:2245:9
  11:        0x10091028c - test::test_main::{{closure}}::h4995b77ab7caefb9
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:132:21
  12:        0x100a4e434 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hc542ab67a612d649
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/boxed.rs:2245:9
  13:        0x100a4e434 - std::panicking::rust_panic_with_hook::h6a84efe4dcab239c
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:805:13
  14:        0x100a4dd24 - std::panicking::begin_panic_handler::{{closure}}::h5eef292190467fef
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:664:13
  15:        0x100a4c7a8 - std::sys::backtrace::__rust_end_short_backtrace::hd7e7925203f20af9
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/backtrace.rs:170:18
  16:        0x100a4da14 - rust_begin_unwind
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:662:5
  17:        0x100a7482c - core::panicking::panic_nounwind_fmt::runtime::hed7cc75ebcf8d970
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:112:18
  18:        0x100a7482c - core::panicking::panic_nounwind_fmt::hb2df5b6c3d6ab15b
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:122:5
  19:        0x100a748a4 - core::panicking::panic_nounwind::h6b53a509f430e184
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panicking.rs:221:5
  20:        0x10091c618 - core::ptr::replace::precondition_check::hd2a53dc2849d57c6
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ub_checks.rs:70:21
  21:        0x100a70f6c - core::ptr::replace::h6e71c02d7d982086
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ub_checks.rs:77:17
  22:        0x100a70f6c - core::ptr::mut_ptr::<impl *mut T>::replace::h3e08361adbedb861
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ptr/mut_ptr.rs:1462:18
  23:        0x100a70f6c - std::sys::thread_local::native::lazy::Storage<T,D>::initialize::h77e0ec5dabb9a15f
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/thread_local/native/lazy.rs:66:45
  24:        0x100920b6c - std::sys::thread_local::native::lazy::Storage<T,D>::get_or_init::h3e7331068e4509c8
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/thread_local/native/lazy.rs:56:40
  25:        0x10091f070 - near_sdk::environment::mock::BLOCKCHAIN_INTERFACE::{{constant}}::{{closure}}::ha7d1483e997156ec
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/thread_local/native/mod.rs:93:25
  26:        0x10091f358 - core::ops::function::FnOnce::call_once::hb46014094badbdb2
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
  27:        0x100918550 - std::thread::local::LocalKey<T>::try_with::he8cbf70eaf6aa230
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/thread/local.rs:282:37
  28:        0x1009184f0 - std::thread::local::LocalKey<T>::with::h775eb4470ea9f3ed
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/thread/local.rs:260:9
  29:        0x10091ef08 - near_sdk::environment::mock::with_mocked_blockchain::h3d2088172d5388b6
                               at /Users/peita/.cargo/registry/src/index.crates.io-6f17d22bba15001f/near-sdk-5.5.0/src/environment/mock/mod.rs:24:5
  30:        0x10091ba80 - near_sdk::environment::mock::mocked_blockchain::mock_chain::with_mock_interface::hf6b2c01c6b8f12bf
                               at /Users/peita/.cargo/registry/src/index.crates.io-6f17d22bba15001f/near-sdk-5.5.0/src/environment/mock/mocked_blockchain.rs:196:9
  31:        0x10091bc3c - log_utf8
                               at /Users/peita/.cargo/registry/src/index.crates.io-6f17d22bba15001f/near-sdk-5.5.0/src/environment/mock/mocked_blockchain.rs:333:9
  32:        0x10091eec8 - near_sdk::environment::env::log_str::hb044490e6998aef1
                               at /Users/peita/.cargo/registry/src/index.crates.io-6f17d22bba15001f/near-sdk-5.5.0/src/environment/env.rs:887:14
  33:        0x1008e1bf4 - hello2::Contract::set_greeting::h8482d803f4b18bea
                               at /Users/peita/Dev/near/hello2/src/lib.rs:29:9
  34:        0x1008e18ec - hello2::tests::set_then_get_greeting::hbbd22229a720bff2
                               at /Users/peita/Dev/near/hello2/src/lib.rs:52:9
  35:        0x1008e1e50 - hello2::tests::set_then_get_greeting::{{closure}}::h6f8da75ab8128a33
                               at /Users/peita/Dev/near/hello2/src/lib.rs:50:31
  36:        0x1008e1d18 - core::ops::function::FnOnce::call_once::h124e4f2e396dade9
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
  37:        0x100913f28 - core::ops::function::FnOnce::call_once::h2dc2ae3d5deab128
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
  38:        0x100913f28 - test::__rust_begin_short_backtrace::ha74869ac477f01d4
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:620:18
  39:        0x10091379c - test::run_test_in_process::{{closure}}::h480e4aa17f895fab
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:643:60
  40:        0x10091379c - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::he972b835bfbe1a74
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panic/unwind_safe.rs:272:9
  41:        0x10091379c - std::panicking::try::do_call::h6d242d7a00c26634
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:554:40
  42:        0x10091379c - std::panicking::try::hb95de8c02276d3a3
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:518:19
  43:        0x10091379c - std::panic::catch_unwind::ha1de2aaaa7ebc727
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panic.rs:345:14
  44:        0x10091379c - test::run_test_in_process::he8812b09a718bc7e
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:643:27
  45:        0x10091379c - test::run_test::{{closure}}::h2a5b46f746d71bd1
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:564:43
  46:        0x1008e382c - test::run_test::{{closure}}::h61d08600987824eb
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/test/src/lib.rs:594:41
  47:        0x1008e382c - std::sys::backtrace::__rust_begin_short_backtrace::h8712ad22d62bd1c4
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/backtrace.rs:154:18
  48:        0x1008e6994 - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::h4a31c94c016920f8
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/thread/mod.rs:522:17
  49:        0x1008e6994 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hb12f0f3e20fc7e49
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/panic/unwind_safe.rs:272:9
  50:        0x1008e6994 - std::panicking::try::do_call::hc89f2e3a18dc8949
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:554:40
  51:        0x1008e6994 - std::panicking::try::h155ce7ca424a9125
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panicking.rs:518:19
  52:        0x1008e6994 - std::panic::catch_unwind::h0fce6398764430bd
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/panic.rs:345:14
  53:        0x1008e6994 - std::thread::Builder::spawn_unchecked_::{{closure}}::h38c8d2c04ffb7937
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/thread/mod.rs:521:30
  54:        0x1008e6994 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h28859a09daa84c0f
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/ops/function.rs:250:5
  55:        0x100a51a18 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2fa7f2d055a8b06a
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/boxed.rs:2231:9
  56:        0x100a51a18 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h53b33f8374f8384c
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/boxed.rs:2231:9
  57:        0x100a51a18 - std::sys::pal::unix::thread::Thread::new::thread_start::hd88bc8e95f2ca709
                               at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/sys/pal/unix/thread.rs:105:17
  58:        0x19d27bfa8 - __pthread_joiner_wake
thread caused non-unwinding panic. aborting.
error: test failed, to rerun pass `--lib`

Caused by:
  process didn't exit successfully: `/Users/peita/Dev/near/hello2/target/debug/deps/hello2-9da8e95d65f72eb1` (signal: 6, SIGABRT: process abort signal)

@peitalin
Copy link
Author

peitalin commented Nov 5, 2024

FYI, running cargo test --release works on rust stable, and beta.

The log_str() function is where it panics:

#[cfg(all(debug_assertions, not(target_arch = "wasm32")))]

If I turn off debug assertions: RUSTFLAGS='-C debug_assertions=no' cargo test then cargo test works fine.

The core::ptr::replace::precondition_check failure seems to triggered somewhere around this line in the near-sdk to do with threadlocal!():

@frol
Copy link
Collaborator

frol commented Nov 5, 2024

@peitalin thank you for the detailed report! @dj8yfo and I looking into it now. I saw something similar before, but didn't have time to troubleshoot it, and it got resolved for me on its own. If you have the capacity to dig into it and find the root cause, I'm happy to allocate $500 USDC bounty reward

@dj8yfo
Copy link
Collaborator

dj8yfo commented Nov 5, 2024

887 is sys::log_utf8 https://docs.rs/crate/near-sdk/5.5.0/source/src/environment/env.rs#887 in published version of code

@peitalin
Copy link
Author

peitalin commented Nov 6, 2024

Adding Rc<Refcell<MockedBlockchain>> resolves my issues ...although I'm not entirely sure what causes the panic.

Running cargo test through rust-lldb on rustc 1.82 for a more detailed backtrace here.

It seems that the panic occurs when thread_local!(..) first lazily initializes Refcell<MockedBlockchain> via with_mock_interface(..) when running local tests here.

Here it fails some pointer alignment checks in prt::replace(dst=0x0000000101808228, src=State<RefCell<MockedBlockchain>>):

    frame #10: 0x0000000100197fec hello3-55b931dd32c44b6a`core::panicking::panic_nounwind::h6b53a509f430e184 at panicking.rs:221:5 [opt]
    
    frame #11: 0x000000010003e9c4 hello3-55b931dd32c44b6a`core::ptr::replace::precondition_check::h70eae1b4d5638254(addr=0x0000000101808228, align=32) at ub_checks.rs:70:21
    
    frame #12: 0x000000010019471c hello3-55b931dd32c44b6a`std::sys::thread_local::native::lazy::Storage<T,D>::initialize::h0b9f75b9dd77b574 [inlined] core::ptr::replace::hb9b1fc681700d126(dst=0x0000000101808228, src=State<core::cell::RefCell<near_sdk::environment::mock::mocked_blockchain::MockedBlockchain<near_sdk::environment::mock::mocked_memory::MockedMemory>>, ()> @ 0x000000017020efe0) at ub_checks.rs:77:17
    
    frame #13: 0x0000000100194708 hello3-55b931dd32c44b6a`std::sys::thread_local::native::lazy::Storage<T,D>::initialize::h0b9f75b9dd77b574 [inlined] core::ptr::mut_ptr::<impl &mut T>::replace::h7e1551e201d1a8bd(self=0x0000000101808228, src=State<core::cell::RefCell<near_sdk::environment::mock::mocked_blockchain::MockedBlockchain<near_sdk::environment::mock::mocked_memory::MockedMemory>>, ()> @ 0x000000017020efe0) at mut_ptr.rs:1462:18
    
    frame #14: 0x0000000100194708 hello3-55b931dd32c44b6a`std::sys::thread_local::native::lazy::Storage<T,D>::initialize::h0b9f75b9dd77b574(self=0x0000000101808228, i=Option<&mut core::option::Option<core::cell::RefCell<near_sdk::environment::mock::mocked_blockchain::MockedBlockchain<near_sdk::environment::mock::mocked_memory::MockedMemory>>>> @ 0x000000017020d190, f=0x6000017080a00000) at lazy.rs:66:45

Somehow adding the extra Rc<Refcell<MockedBlockchain>> passes these pointer alignment / precondition checks.

@dj8yfo
Copy link
Collaborator

dj8yfo commented Nov 6, 2024

@peitalin i've linked a duplicating issue from rustc.
If it sparks anyone's interest, and they ask you to share more details about your specific env/setup,
please do to help the investigation go on.

@aegroto
Copy link

aegroto commented Nov 7, 2024

cargo test does not panic on my Linux system with rustc 1.81.0:

[lorenzo@ivo test-project]$ cargo test
   Compiling test-project v0.1.0 (/home/lorenzo/repository/near-quickstart/test-project)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 9.22s
     Running unittests src/lib.rs (target/debug/deps/test_project-82076a5f905019d4)

running 2 tests
test tests::get_default_greeting ... ok
test tests::set_then_get_greeting ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

     Running tests/test_basics.rs (target/debug/deps/test_basics-12c4894e7d02d9aa)

running 1 test
test test_contract_is_operational ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 6.08s

   Doc-tests test_project

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

[lorenzo@ivo test-project]$ rustc --version
rustc 1.81.0 (eeb90cda1 2024-09-04)

The project seems to work with 1.82.0 as well:

[lorenzo@ivo test-project]$ rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15)
[lorenzo@ivo test-project]$ cargo test
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.29s
     Running unittests src/lib.rs (target/debug/deps/test_project-7bff64842ede34ea)

running 2 tests
test tests::get_default_greeting ... ok
test tests::set_then_get_greeting ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

     Running tests/test_basics.rs (target/debug/deps/test_basics-bcac249d1b92ad5d)

running 1 test
test test_contract_is_operational ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 18.37s

   Doc-tests test_project

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

[lorenzo@ivo test-project]$ rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15)

Am I missing something? Otherwise, it seems like the issue is OS-dependent.

@g4titanx
Copy link
Contributor

is this still open?

@dj8yfo
Copy link
Collaborator

dj8yfo commented Nov 11, 2024

@peitalin can you provide more details about your system into rustc issue and also try minimal reproducing snippet, suggested there?

@peitalin
Copy link
Author

@dj8yfo sure, just added a reproduction in the other thread: rust-lang/rust#132704 (comment)

@dj8yfo dj8yfo changed the title Quickstart example panics on rust v1.80+ Quickstart example panics on rust v1.80+ (aarch64-apple-darwing , one of flavours compiler problem) Nov 12, 2024
@dj8yfo dj8yfo changed the title Quickstart example panics on rust v1.80+ (aarch64-apple-darwing , one of flavours compiler problem) Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , one of flavours compiler problem) Nov 12, 2024
@dj8yfo dj8yfo changed the title Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , one of flavours compiler problem) Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , compiler problem on one of flavours) Nov 12, 2024
@dj8yfo dj8yfo changed the title Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , compiler problem on one of flavours) Quickstart example panics on rust v1.80+ (aarch64-apple-darwin , bug of apple's bag of tools) Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants