Skip to content

Commit

Permalink
build(core): allow exporting Rust functions' stack sizes
Browse files Browse the repository at this point in the history
It is useful to find the top-most stack consuming functions:

```
$ RUST_EMIT_STACK_SIZES=1 make build_firmware
$ arm-none-eabi-size -A build/firmware/firmware.elf | grep .stack_sizes
.stack_sizes          7523           0

$ cargo install [email protected]
$ stack-sizes build/firmware/firmware.elf | grep trezor_lib | sort -k2 -n | tail -n10
0x081c1721	3536	trezor_lib::ui::api::firmware_micropython::new_confirm_properties::h2ab0feebaf154486
0x081c0e7d	3560	trezor_lib::ui::api::firmware_micropython::new_confirm_modify_output::h04465b97d57fafb6
0x081c6161	3688	trezor_lib::ui::api::firmware_micropython::new_show_checklist::he16b109bc4dff398
0x081c4089	4240	trezor_lib::ui::api::firmware_micropython::new_request_pin::h3280c5eff8900a22
0x081be3e1	4960	trezor_lib::ui::api::firmware_micropython::new_confirm_action::h860f874d714ace74
0x081bf545	5096	trezor_lib::ui::api::firmware_micropython::new_confirm_emphasized::h9ade56f5c88001c0
0x081c1ded	5736	trezor_lib::ui::api::firmware_micropython::new_confirm_summary::he2e1274bbc07703e
0x081c7ee9	6760	trezor_lib::ui::api::firmware_micropython::new_show_remaining_shares::h1f67cbfdfeb4c683
0x081c127d	6768	trezor_lib::ui::api::firmware_micropython::new_confirm_more::h107a4be9b5431bb4
0x081c5441	8312	trezor_lib::ui::api::firmware_micropython::new_show_address_details::h352e0b87c58914ce
```

[no changelog]
  • Loading branch information
romanz committed Jan 29, 2025
1 parent d9c547c commit de75780
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ QUIET_MODE ?= 0
TREZOR_DISABLE_ANIMATION ?= $(if $(filter 0,$(PYOPT)),1,0)
STORAGE_INSECURE_TESTING_MODE ?= 0
RUST_PRINT_TYPES_SIZES ?= 0
RUST_EMIT_STACK_SIZES ?= 0

# OpenOCD interface default. Alternative: ftdi/olimex-arm-usb-tiny-h
OPENOCD_INTERFACE ?= stlink
Expand Down Expand Up @@ -124,6 +125,7 @@ SCONS_VARS = \
PYOPT="$(PYOPT)" \
QUIET_MODE="$(QUIET_MODE)" \
RUST_PRINT_TYPES_SIZES="$(RUST_PRINT_TYPES_SIZES)" \
RUST_EMIT_STACK_SIZES="$(RUST_EMIT_STACK_SIZES)" \
SCM_REVISION="$(SCM_REVISION)" \
STORAGE_INSECURE_TESTING_MODE="$(STORAGE_INSECURE_TESTING_MODE)" \
THP="$(THP)" \
Expand Down
6 changes: 6 additions & 0 deletions core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,12 @@ def cargo_build():
# see https://nnethercote.github.io/perf-book/type-sizes.html#measuring-type-sizes for more details
env.Append(ENV={'RUSTFLAGS': '-Z print-type-sizes'})

if ARGUMENTS.get('RUST_EMIT_STACK_SIZES', '0') == '1':
# see the following links for more details:
# - https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/emit-stack-sizes.html
# - https://blog.japaric.io/stack-analysis/
env.Append(ENV={'RUSTFLAGS': '-Z emit-stack-sizes'})

bindgen_macros = tools.get_bindgen_defines(env.get("CPPDEFINES"), ALLPATHS)
build_dir = str(Dir('.').abspath)

Expand Down

0 comments on commit de75780

Please sign in to comment.