Azure / azure-sphere-gallery

A collection of inspirational, unmaintained Azure Sphere software and hardware designs ready to be reused.
37 stars 38 forks source link

Rust apps hit SIGSEGV randomly during syscalls, regression in Rust 1.71.0 and newer #131

Open BarryBo opened 1 year ago

BarryBo commented 1 year ago

In MUSL, syscall_cp_c() polls for pthread_self()->cancel not equal to zero, and if true, calls cancel() to stop the thread. This code is all correct, but when Rust apps make a syscall, either the pthread_self() pointer is bad, or the value of ->cancel is bad (0x10000 typeically), and the attempt to cancel the sole thread crashes with SIGSEGV.

This is only a problem for Rust apps built with Rust 1.71.0 or newer, Older builds of rustc don't repro the issue. I'll investigate.

Here is a typical callstack from a SIGSEGV during a syscall for open() in a single-threaded app that isn't using pthreads for anything:

(gdb) info stack

0 0x00000000 in ?? ()

1 0xbeec564e in pthread_exit () from target:/lib/ld-musl-armhf.so.1

2 0xbeed70fa in ?? () from target:/lib/ld-musl-armhf.so.1

3 0xbeed7156 in ?? () from target:/lib/ld-musl-armhf.so.1

4 0xbeeaa3ca in open64 () from target:/lib/ld-musl-armhf.so.1

5 0xbeeaa248 in __z_azsp_open () from target:/lib/ld-musl-armhf.so.1

6 0x0002f916 in ADC_Open (id=0) at /opt/azurespheresdk/Sysroots/16/usr/include/applibs/adc_internal.h:24

7 0x0003044c in ADC_Open_inline (id=0) at ../azure-sphere-sys/static_inline_helpers.c:30

8 0x0001c4d8 in azure_sphere::applibs::adc::AdcController::new (controller_id=0)

at azure-sphere/src/applibs/adc.rs:63

9 0x00016ca0 in allapis::main () at samples/allapis/src/main.rs:62

BarryBo commented 1 year ago

Rust 1.71.0 contains this commit: https://github.com/rust-lang/rust/commit/4af36a4c6f9da83770579b0fc4c41ab17943c8bb "Upgrade to musl 1.2.3" the previous was musl 1.1.24.