Rust libraries
Since Rust based onchain programs must be deterministic while running in a resource-constrained, single-threaded environment, they have some limitations on various libraries. Onchain Rust programs support most of Rust’s libstd, libcore, and liballoc, as well as many 3rd party crates. There are some limitations since these programs run in a resource-constrained, single-threaded environment, as well as being deterministic:- No access to
randstd::fsstd::netstd::futurestd::processstd::syncstd::taskstd::threadstd::time
- Limited access to:
std::hashstd::os
- Bincode is extremely computationally expensive in both cycles and call depth and should be avoided
- String formatting should be avoided since it is also computationally expensive.
- No support for
println!,print!, use themsg!macro instead. - The runtime enforces a limit on the number of instructions a program can execute during the processing of one instruction. See computation budget for more information.
Compute budget
To prevent abuse of the blockchain’s computational resources, each transaction is allocated a compute budget. Exceeding this compute budget will result in the transaction failing. See the computational constraints documentation for more specific details.Call stack depth - CallDepthExceeded error
Solana programs are constrained to run quickly, and to facilitate this, the
program’s call stack is limited to a max depth of 64 frames.
When a program exceeds the allowed call stack depth limit, the transaction fails
with InstructionError(_, ProgramFailedToComplete) and the program log
failed: exceeded max BPF to BPF call depth. (The VM-internal error is named
CallDepthExceeded, but clients never see that name.)
CPI call depth - CallDepth error
Cross-program invocations allow programs to invoke other programs directly, but
the depth is constrained currently to 4.
SIMD-0268 raises the maximum
CPI depth from
4 to 8 once its feature gate activates.CallDepth error
Float Rust types support
Programs support a limited subset of Rust’s float operations. If a program attempts to use a float operation that is not supported, the runtime will report an unresolved symbol error. Float operations are performed via software libraries, specifically LLVM’s float built-ins. Due to the software emulated, they consume more compute units than integer operations. In general, fixed point operations are recommended where possible. The Solana Program Library math tests will report the performance of some math operations. To run the test, sync the repo and run:Static writable data
Program shared objects do not support writable shared data. Programs are shared between multiple parallel executions using the same shared read-only code and data. This means that developers should not include any static writable or global variables in programs. In the future a copy-on-write mechanism could be added to support writable data.Signed division
The SBF instruction set does not support signed division. Signed division in Rust still works: the compiler lowers it to software helper routines (compiler_builtins), which cost additional compute units compared to
unsigned division.