-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NFC] [HWASan] pull removeFnAttributes into function #109488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fmayer
merged 2 commits into
main
from
users/fmayer/spr/nfc-hwasan-pull-removefnattributes-into-function
Sep 21, 2024
Merged
[NFC] [HWASan] pull removeFnAttributes into function #109488
fmayer
merged 2 commits into
main
from
users/fmayer/spr/nfc-hwasan-pull-removefnattributes-into-function
Sep 21, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.4
@llvm/pr-subscribers-llvm-transforms Author: Florian Mayer (fmayer) ChangesFull diff: https://github.com/llvm/llvm-project/pull/109488.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 7a5c690c7ea512..50f8802924363a 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -316,6 +316,7 @@ class HWAddressSanitizer {
FunctionAnalysisManager &FAM) const;
void initializeModule();
void createHwasanCtorComdat();
+ void removeFnAttributes(Function *F);
void initializeCallbacks(Module &M);
@@ -591,6 +592,46 @@ void HWAddressSanitizer::createHwasanCtorComdat() {
appendToCompilerUsed(M, Dummy);
}
+void HWAddressSanitizer::removeFnAttributes(Function *F) {
+ // Remove memory attributes that are invalid with HWASan.
+ // HWASan checks read from shadow, which invalidates memory(argmem: *)
+ // Short granule checks on function arguments read from the argument memory
+ // (last byte of the granule), which invalidates writeonly.
+ //
+ // This is not only true for sanitized functions, because AttrInfer can
+ // infer those attributes on libc functions, which is not true if those
+ // are instrumented (Android) or intercepted.
+ //
+ // We might want to model HWASan shadow memory more opaquely to get rid of
+ // this problem altogether, by hiding the shadow memory write in an
+ // intrinsic, essentially like in the AArch64StackTagging pass. But that's
+ // for another day.
+
+ // The API is weird. `onlyReadsMemory` actually means "does not write", and
+ // `onlyWritesMemory` actually means "does not read". So we reconstruct
+ // "accesses memory" && "does not read" <=> "writes".
+ bool Changed = false;
+ if (!F->doesNotAccessMemory()) {
+ bool WritesMemory = !F->onlyReadsMemory();
+ bool ReadsMemory = !F->onlyWritesMemory();
+ if ((WritesMemory && !ReadsMemory) || F->onlyAccessesArgMemory()) {
+ F->removeFnAttr(Attribute::Memory);
+ Changed = true;
+ }
+ }
+ for (Argument &A : F->args()) {
+ if (A.hasAttribute(Attribute::WriteOnly)) {
+ A.removeAttr(Attribute::WriteOnly);
+ Changed = true;
+ }
+ }
+ if (Changed) {
+ // nobuiltin makes sure later passes don't restore assumptions about
+ // the function.
+ F->addFnAttr(Attribute::NoBuiltin);
+ }
+}
+
/// Module-level initialization.
///
/// inserts a call to __hwasan_init to the module's constructor list.
@@ -598,40 +639,8 @@ void HWAddressSanitizer::initializeModule() {
LLVM_DEBUG(dbgs() << "Init " << M.getName() << "\n");
TargetTriple = Triple(M.getTargetTriple());
- for (auto &F : M.functions()) {
- // Remove memory attributes that are invalid with HWASan.
- // HWASan checks read from shadow, which invalidates memory(argmem: *)
- // Short granule checks on function arguments read from the argument memory
- // (last byte of the granule), which invalidates writeonly.
- //
- // This is not only true for sanitized functions, because AttrInfer can
- // infer those attributes on libc functions, which is not true if those
- // are instrumented (Android) or intercepted.
-
- // The API is weird. `onlyReadsMemory` actually means "does not write", and
- // `onlyWritesMemory` actually means "does not read". So we reconstruct
- // "accesses memory" && "does not read" <=> "writes".
- bool Changed = false;
- if (!F.doesNotAccessMemory()) {
- bool WritesMemory = !F.onlyReadsMemory();
- bool ReadsMemory = !F.onlyWritesMemory();
- if ((WritesMemory && !ReadsMemory) || F.onlyAccessesArgMemory()) {
- F.removeFnAttr(Attribute::Memory);
- Changed = true;
- }
- }
- for (Argument &A : F.args()) {
- if (A.hasAttribute(Attribute::WriteOnly)) {
- Changed = true;
- A.removeAttr(Attribute::WriteOnly);
- }
- }
- if (Changed) {
- // nobuiltin makes sure later passes don't restore assumptions about
- // the function.
- F.addFnAttr(Attribute::NoBuiltin);
- }
- }
+ for (Function &F: M.functions())
+ removeFnAttributes(&F);
// x86_64 currently has two modes:
// - Intel LAM (default)
|
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Florian Mayer (fmayer) ChangesFull diff: https://github.com/llvm/llvm-project/pull/109488.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 7a5c690c7ea512..50f8802924363a 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -316,6 +316,7 @@ class HWAddressSanitizer {
FunctionAnalysisManager &FAM) const;
void initializeModule();
void createHwasanCtorComdat();
+ void removeFnAttributes(Function *F);
void initializeCallbacks(Module &M);
@@ -591,6 +592,46 @@ void HWAddressSanitizer::createHwasanCtorComdat() {
appendToCompilerUsed(M, Dummy);
}
+void HWAddressSanitizer::removeFnAttributes(Function *F) {
+ // Remove memory attributes that are invalid with HWASan.
+ // HWASan checks read from shadow, which invalidates memory(argmem: *)
+ // Short granule checks on function arguments read from the argument memory
+ // (last byte of the granule), which invalidates writeonly.
+ //
+ // This is not only true for sanitized functions, because AttrInfer can
+ // infer those attributes on libc functions, which is not true if those
+ // are instrumented (Android) or intercepted.
+ //
+ // We might want to model HWASan shadow memory more opaquely to get rid of
+ // this problem altogether, by hiding the shadow memory write in an
+ // intrinsic, essentially like in the AArch64StackTagging pass. But that's
+ // for another day.
+
+ // The API is weird. `onlyReadsMemory` actually means "does not write", and
+ // `onlyWritesMemory` actually means "does not read". So we reconstruct
+ // "accesses memory" && "does not read" <=> "writes".
+ bool Changed = false;
+ if (!F->doesNotAccessMemory()) {
+ bool WritesMemory = !F->onlyReadsMemory();
+ bool ReadsMemory = !F->onlyWritesMemory();
+ if ((WritesMemory && !ReadsMemory) || F->onlyAccessesArgMemory()) {
+ F->removeFnAttr(Attribute::Memory);
+ Changed = true;
+ }
+ }
+ for (Argument &A : F->args()) {
+ if (A.hasAttribute(Attribute::WriteOnly)) {
+ A.removeAttr(Attribute::WriteOnly);
+ Changed = true;
+ }
+ }
+ if (Changed) {
+ // nobuiltin makes sure later passes don't restore assumptions about
+ // the function.
+ F->addFnAttr(Attribute::NoBuiltin);
+ }
+}
+
/// Module-level initialization.
///
/// inserts a call to __hwasan_init to the module's constructor list.
@@ -598,40 +639,8 @@ void HWAddressSanitizer::initializeModule() {
LLVM_DEBUG(dbgs() << "Init " << M.getName() << "\n");
TargetTriple = Triple(M.getTargetTriple());
- for (auto &F : M.functions()) {
- // Remove memory attributes that are invalid with HWASan.
- // HWASan checks read from shadow, which invalidates memory(argmem: *)
- // Short granule checks on function arguments read from the argument memory
- // (last byte of the granule), which invalidates writeonly.
- //
- // This is not only true for sanitized functions, because AttrInfer can
- // infer those attributes on libc functions, which is not true if those
- // are instrumented (Android) or intercepted.
-
- // The API is weird. `onlyReadsMemory` actually means "does not write", and
- // `onlyWritesMemory` actually means "does not read". So we reconstruct
- // "accesses memory" && "does not read" <=> "writes".
- bool Changed = false;
- if (!F.doesNotAccessMemory()) {
- bool WritesMemory = !F.onlyReadsMemory();
- bool ReadsMemory = !F.onlyWritesMemory();
- if ((WritesMemory && !ReadsMemory) || F.onlyAccessesArgMemory()) {
- F.removeFnAttr(Attribute::Memory);
- Changed = true;
- }
- }
- for (Argument &A : F.args()) {
- if (A.hasAttribute(Attribute::WriteOnly)) {
- Changed = true;
- A.removeAttr(Attribute::WriteOnly);
- }
- }
- if (Changed) {
- // nobuiltin makes sure later passes don't restore assumptions about
- // the function.
- F.addFnAttr(Attribute::NoBuiltin);
- }
- }
+ for (Function &F: M.functions())
+ removeFnAttributes(&F);
// x86_64 currently has two modes:
// - Intel LAM (default)
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
vitalybuka
approved these changes
Sep 21, 2024
guy-david
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Mar 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.