rust: Fix intermittent test failure
PATCH 1/1 rust: Fix intermittent test failure
From: Deepesh Varatharajan <[email protected]>
Fixes [YOCTO #16460]
Fix an intermittent test failure by backporting the upstream Rust commit.
https://github.com/rust-lang/rust/commit/a292cb16d5cfc32923be91c86afaf727682a38dc
Signed-off-by Deepesh Varatharajan <[email protected]>
--- ...se-thread-local-in-c_str_alloc_error.patch | 64 +++++++++++++++++++ meta/recipes-devtools/rust/rust-source.inc | 1 + 2 files changed, 65 insertions(+) create mode 100644 meta/recipes-devtools/rust/files/0006-alloctests-use-thread-local-in-c_str_alloc_error.patch
meta/recipes-devtools/rust/files/0006-alloctests-use-thread-local-in-c_str_alloc_error.patch new file +64 -0
@@ -0,0 +1,64 @@ +From a292cb16d5cfc32923be91c86afaf727682a38dc Mon Sep 17 00:00:00 2001 +From: Jules Bertholet <[email protected]> +Date: Mon, 16 Nov 2026 09:24:03 -0500 +Subject: [PATCH] alloctests: use thread-local in `c_str_alloc_error` + +Upstream-Status: Backport [https://github.com/rust-lang/rust/commit/a292cb16d5cfc32923be91c86afaf727682a38dc] +Signed-off-by: Deepesh Varatharajan <[email protected]> + +diff --git a/library/alloctests/tests/c_str_alloc_error.rs b/library/alloctests/tests/c_str_alloc_error.rs +index 1e7d54d75b3..fc572169583 100644 +--- a/library/alloctests/tests/c_str_alloc_error.rs ++++ b/library/alloctests/tests/c_str_alloc_error.rs +@@ -15,20 +15,22 @@ + #![feature(alloc_error_hook)] + + use std::alloc::{GlobalAlloc, Layout, System, set_alloc_error_hook}; ++use std::cell::Cell; + use std::ffi::CString; + use std::panic::{AssertUnwindSafe, catch_unwind}; +-use std::sync::atomic::{AtomicBool, Ordering}; + + // Once armed, the first allocation of 8 bytes or more fails and disarms, so the + // reallocation inside `clone_into`'s grow path fails while the runtime's own + // smaller allocations keep succeeding. + struct OneShotFailingAlloc; + +-static ARMED: AtomicBool = AtomicBool::new(false); ++thread_local! { ++ static ARMED: Cell<bool> = const { Cell::new(false) }; ++} + + unsafe impl GlobalAlloc for OneShotFailingAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { +- if layout.size() >= 8 && ARMED.swap(false, Ordering::SeqCst) { ++ if layout.size() >= 8 && ARMED.replace(false) { + return core::ptr::null_mut(); + } + unsafe { System.alloc(layout) } +@@ -39,7 +41,7 @@ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { +- if new_size >= 8 && ARMED.swap(false, Ordering::SeqCst) { ++ if new_size >= 8 && ARMED.replace(false) { + return core::ptr::null_mut(); + } + unsafe { System.realloc(ptr, layout, new_size) } +@@ -59,11 +61,11 @@ fn clone_into_alloc_failure_leaves_target_valid() { + let src = CString::new("a fairly long value").unwrap(); + let mut target = CString::new("x").unwrap(); + +- ARMED.store(true, Ordering::SeqCst); ++ ARMED.set(true); + // Under Miri the failing allocator is honored, so this reallocation unwinds; in a + // normal build the allocator can't intercept it and `clone_into` just succeeds. + let res = catch_unwind(AssertUnwindSafe(|| src.as_c_str().clone_into(&mut target))); +- ARMED.store(false, Ordering::SeqCst); ++ ARMED.set(false); + + if cfg!(miri) { + assert!(res.is_err(), "clone_into should have unwound on the alloc failure"); +-- +2.49.0 +
meta/recipes-devtools/rust/rust-source.inc +1 -0
@@ -8,6 +8,7 @@ SRC_URI += "https://static.rust-lang.org/dist/rustc-${RUST_VERSION}-src.tar.xz;n file://0003-cargo-omit-host-triple-from-unit-metadata-hash.patch;patchdir=${RUSTSRC} \ file://0004-Re-bless-tests-adjust-for-new-Cargo-lints.patch;patchdir=${RUSTSRC} \ file://0005-rustc_llvm-drop-native-sysroot-L-when-cross-building.patch;patchdir=${RUSTSRC} \ + file://0006-alloctests-use-thread-local-in-c_str_alloc_error.patch;patchdir=${RUSTSRC} \ " SRC_URI[rust.sha256sum] = "90dab90b8084eb57c35ae2b1d0b70c8e5138ace6023b22c60a5a400f586f63b2"
Signature · 1 line
2.49.0