Closed
Description
The memory sanitizer supports "eager checks", which will check initialization for noundef parameters and return values. It can be enabled by passing true as the fourth parameter to MemorySanitizerOptions. The corresponding clang option is -fsanitize-memory-param-retval
. Not sure whether we need an option or can just unconditionally enable it.
Activity
5225225 commentedon Jul 13, 2022
If there's not a big performance hit, and it just changes behavior, then I think we should just unconditionally enable it, saves people the hassle of needing to know to enable that flag in CI as well if you want the better checks.
In any case, I'll try and implement this, we can probably get away with not changing the rust code side at all, and just always passing
EagerChecks
to betrue
.5225225 commentedon Jul 13, 2022
Also, looks like we're not emitting
noundef
for parameters and return values, we're only doing it for loads. But if you enable optimisations, params and return values seem to get noundef (if they're of a type that we say is noundef, likechar
). Also, the test required#[no_mangle]
on the function that created the undef value. That could just be because it's a rather artificial test case?nikic commentedon Jul 13, 2022
We do emit noundef attributes for params and returns -- maybe you're testing
opt-level=0
? Not sure which test case you are referring to here.5225225 commentedon Jul 13, 2022
What I don't get is how. Does noundef have no effect on unoptimised builds / is it ignored? (As in, in order to get the effect of memory sanitizer here, do you have to run an optimized build?)
I see that we have
scalar_load_metadata
that sets noundef metadata, is that used not just for loads, but also for parameters and returns? I can't see anywhere else that callsnoundef_metadata
.rust/compiler/rustc_codegen_llvm/src/builder.rs
Lines 467 to 488 in 95e8b86
tmiasko commentedon Jul 13, 2022
Attributes should be configured somewhere around here:
rust/compiler/rustc_codegen_llvm/src/abi.rs
Lines 46 to 52 in 95e8b86
rust/compiler/rustc_codegen_llvm/src/abi.rs
Lines 75 to 93 in 95e8b86
nikic commentedon Jul 13, 2022
They are not set as a compile-time optimization. It would be fine to always emit them with memory sanitizer, as they impact its behavior.
5225225 commentedon Jul 13, 2022
Ahhh! Thank you!
So https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/layout.rs#L3223-3226= is where noundef is given to params/return values with validity invariants. And yeah, we don't tell LLVM that anything is noundef if we have no opts on.
Maybe we should, but only if memory sanitizer is enabled? (or unconditionally?) Since it's giving msan information it needs. Not sure how many people run msan in CI and don't run it in release, but it seems non-obvious that you would need to.
Rollup merge of rust-lang#99207 - 5225225:msan-eager-checks, r=jackh726