Skip to content

add -fsanitize=... flag if sanitizer is enabled in rust#1672

Open
kamulos wants to merge 1 commit intorust-lang:mainfrom
kamulos:enable_sanitizer
Open

add -fsanitize=... flag if sanitizer is enabled in rust#1672
kamulos wants to merge 1 commit intorust-lang:mainfrom
kamulos:enable_sanitizer

Conversation

@kamulos
Copy link

@kamulos kamulos commented Feb 5, 2026

resolves #1666

I tried this mainly using ASAN. It works for me on gnu linux using clang and gcc. It triggers when the x86_64-unknown-linux-gnuasan target is used, and also when RUSTFLAGS="-Z sanitizer=address" is set.

I don't know this crate well, so I am unsure about a few things:

  1. Where is the correct place in the source code to put this?
  2. Hopefully CARGO_CFG_SANITIZE is only set on targets, that support a specific sanitizer. Does this mean, there are no additional checks of the target necessary?
  3. Especially with MSVC I have no idea if this would work or what would need to be changed
  4. Should it be this general (just passing the sanitizer string to the C compiler) or should it be more like a whitelist (for example enabling it only for asan and then extending it in the future)?

}

if let Some(sanitizer) = self.getenv("CARGO_CFG_SANITIZE") {
let mut sanitizer_flag = OsString::from("-fsanitize=");
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that rust and clang support hwaddress sanitizer, but gcc doesn't.

So we probably want to map it to normal address right?

cc @madsmtm

Copy link
Contributor

@madsmtm madsmtm Feb 11, 2026

Choose a reason for hiding this comment

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

I agree that we should parse the value here, and only emit the flag on tested configurations

(Which also means I think we should have some sort of test for this in CI).

self.add_inherited_rustflags(&mut cmd, &target)?;
}

if let Some(sanitizer) = self.getenv("CARGO_CFG_SANITIZE") {
Copy link
Contributor

Choose a reason for hiding this comment

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

The ordering here is a bit significant. I think it should be before add_inherited_rustflags?

(Probably also before envflags("CFLAGS") above, but add_inherited_rustflags isn't, so let's not do that just yet).

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe place it'd make sense to place in add_default_flags?

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.

Feature: Add -fsanitize=address for x86_64-unknown-linux-gnuasan

3 participants