Skip to content

Disallow building with overflow-checks off#1702

Draft
leighmcculloch wants to merge 6 commits into
mainfrom
disallow-overflow-checks-off
Draft

Disallow building with overflow-checks off#1702
leighmcculloch wants to merge 6 commits into
mainfrom
disallow-overflow-checks-off

Conversation

@leighmcculloch

Copy link
Copy Markdown
Member

What

  • Add a build-script probe in build.rs that detects whether overflow-checks are enabled by attempting an overflowing u8 addition and catching whether it panics
  • Set a custom cfg flag (soroban_sdk_internal_overflow_checks_enabled) when checks are on
  • Add a compile_error! in lib.rs that fires when the flag is absent, with a link to the docs

Why

Cargo's default release profile disables overflow-checks. Without this guard developers can silently build contracts with wrapping arithmetic, leading to unexpected behavior. The tooling for Soroban contracts that generates boilerplate for new contracts sets overflow-checks = true for the release profile, and docs encourage the same pattern, but this makes it a compile-time error so that anyone doesn't accidentally turn that off.

This is a workaround until cfg(overflow_checks) is stabilized (rust-lang/rust#111466).

Known limitations

It may not detect overflow-checks being disabled in cross-compilation setups where the host and target profiles diverge or where the build-override profile is explicitly set to overflow-checks = true when the main profile is set to false. These are advanced configurations, and the check is primarily present to prevent developers using the default release profile that has checks off.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a compile-time guard to prevent building Soroban contracts when integer overflow checks are disabled, using a build-time probe and an internal cfg flag.

Changes:

  • Adds a build-script probe in soroban-sdk/build.rs that detects whether overflow-checks are enabled by attempting an overflowing u8 addition and setting cfg(soroban_sdk_internal_overflow_checks_enabled) when a panic is observed.
  • Introduces a compile_error! in soroban-sdk/src/lib.rs that triggers when the internal overflow-checks cfg flag is absent, with a pointer to the documentation on correctly configuring the release profile.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
soroban-sdk/build.rs Adds a runtime probe and corresponding rustc-cfg/rustc-check-cfg output to encode overflow-checks status into a cfg flag at compile time.
soroban-sdk/src/lib.rs Adds a compile_error! guarded by the new cfg flag to hard-fail builds when overflow checks are disabled, with a link to the recommended release profile configuration.

Comment thread soroban-sdk/build.rs
Comment thread soroban-sdk/src/lib.rs
Comment thread soroban-sdk/src/lib.rs
@leighmcculloch leighmcculloch marked this pull request as draft April 1, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants