fix(core/emulator): fix crash in micropython built with --asan
What changed, and why it matters
This commit fixes a crash that happened only when running the Trezor firmware emulator with AddressSanitizer (ASAN), a debugging tool used during development. The crash occurred because ASAN's stack-use-after-return detection interfered with MicroPython's internal stack check, causing a false RuntimeError on startup. The fix disables that specific ASAN feature for emulator builds and also turns off leak detection because the firmware does not use the heap. This change only affects developer/debug builds and does not change the security behavior of real Trezor devices.
No security response required. Treat as a normal build-system/debugging fix. If reviewing, verify that the ASAN flags are correctly stripped during Rust binding generation and that the emulator still builds with and without the `asan` feature.
Security signals we found
Change is confined to ASAN/debug emulator builds and does not affect production firmware
No runtime code path changes for real devices
No memory corruption, privilege escalation, or cryptographic weakness introduced
Fixes a developer tooling crash, not a product security vulnerability
Evidence from the diff
The patch adds an asan.c file providing __asan_default_options() to set detect_leaks=0, and updates build.rs to pass a compiler-specific flag disabling ASAN’s use-after-return instrumentation (-fsanitize-address-use-after-return=never for Clang, --param=asan-use-after-return=0 for GCC). It also exposes CLibrary::uses_clang() and strips both ASAN flags when generating Rust bindings. The issue is a known incompatibility between ASAN’s fake-stack mechanism and MicroPython’s mp_cstack_check, which upstream MicroPython also disables in ASAN CI builds.
Changed components
core/embed/models/asan.ccore/embed/models/build.rscore/embed/xbuild/src/clibrary/mod.rscore/embed/xbuild/src/clibrary/rust_bindings.rsInspect captured patch +52 / −0
### core/embed/models/asan.c
@@ -0,0 +1,26 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Default AddressSanitizer runtime options, picked up by the ASAN runtime
+// at startup. ASAN_OPTIONS in the environment still overrides them.
+//
+// The firmware does not allocate on the heap, so leak detection only
+// reports allocations made by third-party libraries (SDL, GTK loaded through
+// libdecor on Wayland, fontconfig, ...) that we have no control over.
+const char* __asan_default_options(void) { return "detect_leaks=0"; }
### core/embed/models/build.rs
@@ -63,10 +63,27 @@ fn main() -> Result<()> {
lib.add_flags(["-fstack-protector-all", "-fno-omit-frame-pointer", "-O1"]);
if cfg!(feature = "asan") {
+ // ASAN's stack-use-after-return detection moves address-taken
+ // locals to a heap-allocated fake stack, which breaks
+ // MicroPython's C stack depth check (mp_cstack_check) and
+ // makes it raise RuntimeError on startup. Upstream MicroPython
+ // disables it in its ASAN CI builds for the same reason.
+ // The flag is spelled differently for gcc and clang (clang
+ // rejects the gcc param as unused under -Werror).
+ let use_after_return_flag = if lib.uses_clang() {
+ "-fsanitize-address-use-after-return=never"
+ } else {
+ "--param=asan-use-after-return=0"
+ };
+
lib.add_flags([
"-fsanitize=address,undefined",
"-fno-optimize-sibling-calls",
+ use_after_return_flag,
]);
+
+ // Default ASAN runtime options (disables leak detection)
+ lib.add_source("asan.c");
}
} else if cfg!(feature = "mcu_stm32") {
// arm-none-eabi-gcc uses short enums (-fshort-enums) by default,
### core/embed/xbuild/src/clibrary/mod.rs
@@ -374,6 +374,13 @@ impl CLibrary {
}
}
+ /// Returns whether the library's configured C compiler is Clang-like.
+ pub fn uses_clang(&self) -> bool {
+ self.get_merged_attrs()
+ .get_configured_compiler()
+ .is_like_clang()
+ }
+
/// Returns the merged compile attributes for the library, combining both
/// private and public attributes.
pub fn get_merged_attrs(&self) -> CompileAttrs {
### core/embed/xbuild/src/clibrary/rust_bindings.rs
@@ -53,6 +53,8 @@ impl CLibrary {
// Remove GCC-only flags that make clang fail.
attrs.remove_flag("-mcmse");
attrs.remove_flag("-fsingle-precision-constant");
+ attrs.remove_flag("--param=asan-use-after-return=0");
+ attrs.remove_flag("-fsanitize-address-use-after-return=never");
for (filename, builder) in builders {
let mut content = Vec::<u8>::new();Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.