Skip to content

Conversation

@davidsemakula
Copy link
Collaborator

@davidsemakula davidsemakula commented Sep 2, 2025

Summary

Closes #2115

  • [n] y/n | Does it introduce breaking changes?
  • [n] y/n | Is it dependent on the specific version of ink or pallet-contracts?

Description

See #2115 for a detailed description of historical issues and limitations of the current solution.

There were 2 primary root causes for the historical issues:

  1. Internal issues in rustc related to how library functions from #![no_builtins] annotated crates (most notably compiler intrinsics from the compiler-builtins crate) interacted with LTO (Link Time Optimization) (i.e. LTO would sometimes errorneously optimize out these functions or replace them with recursive calls)
  2. No exposure/forwarding of non-default/optional features from the compiler-builtins crate (most notably the mem feature for generating memory-related intrinsics like memcpy) in standard library crates (i.e. alloc, core, std e.t.c), meaning these features couldn't be explicitly enabled with the -Z build-std-features flag when compiling standard library crates using -Z build-std flag.

(1) was generally the more complicated issue, and was ultimately solved by

With the latter explicitly mentioning previously reported issues from ink! as resolved i.e:

(2) was resolved by

Accordingly, with this PR, we now explicitly enable the mem feature of the compiler-builtins crate ( via the compiler-builtins-mem standard library feature).

NOTE: This is functionally equivalent to the previous "work around" for WebAssembly i.e.

As explained by the deprecation notice in rlibc readme

DEPRECATED: This crate used to provide a Rust implementation of some libc functions such as memcpy / memmove / memset, required when developing freestanding applications.
The compiler-builtins crate now provides optimized versions of these functions when enabling the optional mem feature

Lastly, we prefer to enable the mem feature explicitly via the -Zbuild-std-features flag, even if the compiler-builtins crate already attempts to automatically enable the mem feature for certain targets (e.g. targets with a -none suffix) via it's build script, see:

Follow ups/ related TODOs

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have added an entry to CHANGELOG.md
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

@davidsemakula davidsemakula marked this pull request as ready for review September 2, 2025 18:30
@davidsemakula davidsemakula changed the title Re-enable support for cargo (and rustc) "overflow-checks" setting/flag Re-enable rustc overflow checks Sep 2, 2025
@davidsemakula davidsemakula force-pushed the semakula/overflow-checks branch 5 times, most recently from f87e6da to dd48dd4 Compare September 8, 2025 19:47
@davidsemakula davidsemakula force-pushed the semakula/overflow-checks branch from dd48dd4 to e22e6b4 Compare September 8, 2025 20:40
Copy link
Collaborator

@cmichi cmichi left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you for researching it!

Could you check if ink-docs requires updating? I have some distant memory about at least an FAQ point.

@cmichi cmichi merged commit 7865a31 into master Sep 8, 2025
17 checks passed
@cmichi cmichi deleted the semakula/overflow-checks branch September 8, 2025 21:42
@davidsemakula
Copy link
Collaborator Author

Could you check if ink-docs requires updating? I have some distant memory about at least an FAQ point.

Yeah, there's an FAQ point in ink! docs that mentions me, will update it.

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.

Overflow safety: Provide robust overflow checks for arithmetic operations

3 participants