Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Check alignment of raw pointers in debug mode #54915
It is UB to
This would have caught #54908.
However, I guess before someone starts implementing this, we should get @rust-lang/compiler to say that they think this is a good idea. It's in debug mode only, and it's a rather cheap check (as alignment is always a power of two), so I expect the perf impact to be acceptable.
See also Zig, that exposes such alignment information in the type system itself:
Using syntax like:
The run-time check perf impact is probably acceptable because currently debug builds are often 1000%+ slower.
I suggest to add a switch like -Z force-overflow-checks that could be switched on in "efficient experiment" release builds too if desired, because sometimes debug builds are so slow you can't use them well to test some code.
More generally, what other basic invariants like that could rustc verify automatically (in debug builds)?
Good point. We have something similar for integer overflow, don't we?
Tons of them, but which have a good perf-benefit trade-off? Also note that this wouldn't reliably verify the alignment invariant, one can still
Some other things coming to my mind are:
For the last (or the last two) to be viable, however, we need a libstd compiled with debug assertion to be used when compiling programs in debug mode.
Everything else I can think of involves some kind of type-based recursion on e.g. the return value of
Finally, we'd probably have to do some communication work to get people to run their test suites in debug mode, so that this actually reaches all the projects out there. Is there any way to get debug assertions, but also get optimizations, so that this doesn't make everything quite so much slower? It does seem useful to me to have code with extra checks optimized; that means the compiler can assume fewer things (less UB) but it can still e.g. optimize away the checks if it can prove they always succeed, and it can do all the other optimizations (inlining and whatnot).
With Cargo, the flags are placed in [profile.*] sections. The following are two of the defaults for dev builds
# The development profile, used for `cargo build`. [profile.dev] opt-level = 0 # controls the `--opt-level` the compiler builds with. # 0-1 is good for debugging. 2 is well-optimized. Max is 3. # 's' attempts to reduce size, 'z' reduces size even more. debug-assertions = true # controls whether debug assertions are enabled # (e.g. debug_assert!() and arithmetic overflow checks)
Fortunately, the default travis-ci rust set-up runs