solana-labs / solana

Web-Scale Blockchain for fast, secure, scalable, decentralized apps and marketplaces.
https://solanalabs.com
Apache License 2.0
13.03k stars 4.2k forks source link

Stack frame size of 0x1000 exceeded during unoptimized builds #23737

Closed jackcmay closed 2 years ago

jackcmay commented 2 years ago

Problem

TOB-SOL-020

Currently, the size limit for a BPF program stack frame is 0x1000 bytes. However, when building a BPF program outside of the release profile, or with optimizations disabled, the Solana SDK code exceeds this limit. To accommodate the cases where the problematic code is not actually used by the resulting program, the compiler error about stack frame size is actually a warning. This can have the effect that contract developers become used to seeing these errors in the build logs, and fail to properly triage the ones that are indicative of actual problems. As an example, building the tutorial hello world example using cargo +bpf build --target bpfel-unknown-unknown, results in several errors reported in the Solana SDK code, apart from errors in dependent crates.

nop@nop-vm:~/solana/example-helloworld/src/program-rust$ cargo +bpf build --target
bpfel-unknown-unknown
   Compiling proc-macro2 v1.0.27
   ...
   Compiling sha2 v0.9.9
   Compiling sha3 v0.9.1
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/sha2-0.9.9/src/sha256/soft
.rs:165 Function _ZN4sha26sha2564soft23sha256_digest_block_u3217h504bb74f46fd78cdE
Stack offset of 11872 exceeded max offset of 4096 by 7776 bytes, please minimize
large stack variables
   Compiling proc-macro-crate v0.1.5
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/sha2-0.9.9/src/sha512/soft
.rs:98 Function _ZN4sha26sha5124soft23sha512_digest_block_u6417h07d77bdfc74a196cE
Stack offset of 19112 exceeded max offset of 4096 by 15016 bytes, please minimize
large stack variables
   Compiling borsh-derive v0.9.3
   Compiling borsh v0.9.3
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
system_instruction.rs:141:10 Function
_ZN14solana_program18system_instruction1_105_$LT$impl$u20$serde..ser..Serialize$u20$
for$u20$solana_program..system_instruction..SystemInstruction$GT$9serialize17h3a781b
7d9d0fbb81E Stack offset of 6368 exceeded max offset of 4096 by 2272 bytes, please
minimize large stack variables
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
system_instruction.rs:141 Function
_ZN14solana_program18system_instruction1_105_$LT$impl$u20$serde..ser..Serialize$u20$
for$u20$solana_program..system_instruction..SystemInstruction$GT$9serialize17h3a781b
7d9d0fbb81E Stack offset of 4584 exceeded max offset of 4096 by 488 bytes, please
minimize large stack variables
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
system_instruction.rs:141 Function
_ZN14solana_program18system_instruction1_105_$LT$impl$u20$serde..ser..Serialize$u20$
for$u20$solana_program..system_instruction..SystemInstruction$GT$9serialize17h3a781b
7d9d0fbb81E Stack offset of 7576 exceeded max offset of 4096 by 3480 bytes, please
minimize large stack variables
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
system_instruction.rs:141 Function
_ZN120_$LT$solana_program..system_instruction..SystemInstruction$u20$as$u20$solana_f
rozen_abi..abi_example..AbiEnumVisitor$GT$13visit_for_abi17h51b6a7a10b71e7d0E Stack
offset of 9360 exceeded max offset of 4096 by 5264 bytes, please minimize large
stack variables
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
system_instruction.rs:141 Function
_ZN120_$LT$solana_program..system_instruction..SystemInstruction$u20$as$u20$solana_f
rozen_abi..abi_example..AbiEnumVisitor$GT$13visit_for_abi17h51b6a7a10b71e7d0E Stack
offset of 9352 exceeded max offset of 4096 by 5256 bytes, please minimize large
stack variables
Error:
/home/nop/.cargo/registry/src/github.com-1ecc6299db9ec823/solana-program-1.8.14/src/
instruction.rs:22 Function
_ZN112_$LT$solana_program..instruction..InstructionError$u20$as$u20$solana_frozen_ab
i..abi_example..AbiEnumVisitor$GT$13visit_for_abi17h5e37ad11d032f493E Stack offset
of 32296 exceeded max offset of 4096 by 28200 bytes, please minimize large stack
variables
   Compiling solana-bpf-helloworld v0.0.1
(/home/nop/solana/example-helloworld/src/program-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 27.45s

Proposed Solution

Short term, if the code causing the too large stack frames are not needed for the BPF feature, consider using conditional compilation directives such as cfg and cfg_attr, to

prevent that code from being built for BPF targets. This would remove the code from the BPF build, eliminating the error. Long term, it is our understanding that the Solana project intends to move away from the fixed stack frame size, and we concur that this is a prudent way to address the identified stack issues. A dynamic stack size does not place a burden on the smart contract developers to ensure this requirement is met, and allows crates that contain code with larger stack frames to be used.

dmakarov commented 2 years ago

We're implementing the proposed long term solution, namely the BPF programs will use stack frames of varying size with support from the compiler. The dynamic allocation of stack frames with varying size will be managed by the virtual machine running BPF programs. The resolution of this issue depends on the issue #23481.

In the meantime, our tooling infrastructure doesn't support unoptimized compilation. The Solana SDK's cargo-build-bpf tool always builds release version of smart contracts.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any activity in past 7 days after it was closed. Please open a new issue for related bugs.