-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NFC][hwasan] Use enum class
in ShadowMapping
#109617
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
vitalybuka
merged 5 commits into
main
from
users/vitalybuka/spr/nfchwasan-use-enum-class-in-shadowmapping
Sep 23, 2024
Merged
[NFC][hwasan] Use enum class
in ShadowMapping
#109617
vitalybuka
merged 5 commits into
main
from
users/vitalybuka/spr/nfchwasan-use-enum-class-in-shadowmapping
Sep 23, 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 [skip ci]
Created using spr 1.3.4
@llvm/pr-subscribers-llvm-transforms Author: Vitaly Buka (vitalybuka) ChangesFull diff: https://github.com/llvm/llvm-project/pull/109617.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index be0ead40b573d8..2efb97c8759bc9 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -64,6 +64,7 @@
#include "llvm/Transforms/Utils/MemoryTaggingSupport.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
+#include <cstdint>
#include <optional>
#include <random>
@@ -83,8 +84,6 @@ const char kHwasanShadowMemoryDynamicAddress[] =
static const size_t kNumberOfAccessSizes = 5;
static const size_t kDefaultShadowScale = 4;
-static const uint64_t kDynamicShadowSentinel =
- std::numeric_limits<uint64_t>::max();
static const unsigned kShadowBaseAlignment = 32;
@@ -385,44 +384,44 @@ class HWAddressSanitizer {
std::unique_ptr<RandomNumberGenerator> Rng;
/// This struct defines the shadow mapping using the rule:
+ /// If `kFixed`, then
/// shadow = (mem >> Scale) + Offset.
- /// If InGlobal is true, then
+ /// If `kGlobal`, then
+ /// extern char* __hwasan_shadow_memory_dynamic_address;
+ /// shadow = (mem >> Scale) + __hwasan_shadow_memory_dynamic_address
+ /// If `kIfunc`, then
/// extern char __hwasan_shadow[];
/// shadow = (mem >> Scale) + &__hwasan_shadow
- /// If InTls is true, then
+ /// If `kTls`, then
/// extern char *__hwasan_tls;
/// shadow = (mem>>Scale) + align_up(__hwasan_shadow, kShadowBaseAlignment)
///
/// If WithFrameRecord is true, then __hwasan_tls will be used to access the
/// ring buffer for storing stack allocations on targets that support it.
class ShadowMapping {
- uint8_t Scale;
+ enum class OffsetKind {
+ kFixed = 0,
+ kGlobal,
+ kIfunc,
+ kTls,
+ };
+ OffsetKind Kind;
uint64_t Offset;
- bool InGlobal;
- bool InTls;
+ uint8_t Scale;
bool WithFrameRecord;
+ void SetFixed(uint64_t O) {
+ Kind = OffsetKind::kFixed;
+ Offset = O;
+ }
+
public:
void init(Triple &TargetTriple, bool InstrumentWithCalls);
Align getObjectAlignment() const { return Align(1ULL << Scale); }
- bool isInGlobal() const {
- return !InGlobal && !InTls && Offset == kDynamicShadowSentinel;
- }
- bool isInifunc() const {
- assert(!InGlobal || !InTls);
- assert(!InGlobal || Offset == kDynamicShadowSentinel);
- return InGlobal;
- }
- bool isInTls() const {
- assert(!InTls || !InGlobal);
- assert(!InTls || Offset == kDynamicShadowSentinel);
- return InTls;
- }
- bool isFixed() const {
- assert(Offset == kDynamicShadowSentinel || !InTls);
- assert(Offset == kDynamicShadowSentinel || !InGlobal);
- return Offset != kDynamicShadowSentinel;
- }
+ bool isInGlobal() const { return Kind == OffsetKind::kGlobal; }
+ bool isInifunc() const { return Kind == OffsetKind::kIfunc; }
+ bool isInTls() const { return Kind == OffsetKind::kTls; }
+ bool isFixed() const { return Kind == OffsetKind::kFixed; }
uint8_t scale() const { return Scale; };
uint64_t offset() const {
assert(isFixed());
@@ -1930,34 +1929,22 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
if (TargetTriple.isOSFuchsia()) {
// Fuchsia is always PIE, which means that the beginning of the address
// space is always available.
- InGlobal = false;
- InTls = false;
- Offset = 0;
+ SetFixed(0);
WithFrameRecord = true;
} else if (ClMappingOffset.getNumOccurrences() > 0) {
- InGlobal = false;
- InTls = false;
- Offset = ClMappingOffset;
+ SetFixed(ClMappingOffset);
WithFrameRecord = false;
} else if (ClEnableKhwasan || InstrumentWithCalls) {
- InGlobal = false;
- InTls = false;
- Offset = 0;
+ SetFixed(0);
WithFrameRecord = false;
} else if (ClWithIfunc) {
- InGlobal = true;
- InTls = false;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kIfunc;
WithFrameRecord = false;
} else if (ClWithTls) {
- InGlobal = false;
- InTls = true;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kTls;
WithFrameRecord = true;
} else {
- InGlobal = false;
- InTls = false;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kGlobal;
WithFrameRecord = false;
}
}
|
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Vitaly Buka (vitalybuka) ChangesFull diff: https://github.com/llvm/llvm-project/pull/109617.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index be0ead40b573d8..2efb97c8759bc9 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -64,6 +64,7 @@
#include "llvm/Transforms/Utils/MemoryTaggingSupport.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
+#include <cstdint>
#include <optional>
#include <random>
@@ -83,8 +84,6 @@ const char kHwasanShadowMemoryDynamicAddress[] =
static const size_t kNumberOfAccessSizes = 5;
static const size_t kDefaultShadowScale = 4;
-static const uint64_t kDynamicShadowSentinel =
- std::numeric_limits<uint64_t>::max();
static const unsigned kShadowBaseAlignment = 32;
@@ -385,44 +384,44 @@ class HWAddressSanitizer {
std::unique_ptr<RandomNumberGenerator> Rng;
/// This struct defines the shadow mapping using the rule:
+ /// If `kFixed`, then
/// shadow = (mem >> Scale) + Offset.
- /// If InGlobal is true, then
+ /// If `kGlobal`, then
+ /// extern char* __hwasan_shadow_memory_dynamic_address;
+ /// shadow = (mem >> Scale) + __hwasan_shadow_memory_dynamic_address
+ /// If `kIfunc`, then
/// extern char __hwasan_shadow[];
/// shadow = (mem >> Scale) + &__hwasan_shadow
- /// If InTls is true, then
+ /// If `kTls`, then
/// extern char *__hwasan_tls;
/// shadow = (mem>>Scale) + align_up(__hwasan_shadow, kShadowBaseAlignment)
///
/// If WithFrameRecord is true, then __hwasan_tls will be used to access the
/// ring buffer for storing stack allocations on targets that support it.
class ShadowMapping {
- uint8_t Scale;
+ enum class OffsetKind {
+ kFixed = 0,
+ kGlobal,
+ kIfunc,
+ kTls,
+ };
+ OffsetKind Kind;
uint64_t Offset;
- bool InGlobal;
- bool InTls;
+ uint8_t Scale;
bool WithFrameRecord;
+ void SetFixed(uint64_t O) {
+ Kind = OffsetKind::kFixed;
+ Offset = O;
+ }
+
public:
void init(Triple &TargetTriple, bool InstrumentWithCalls);
Align getObjectAlignment() const { return Align(1ULL << Scale); }
- bool isInGlobal() const {
- return !InGlobal && !InTls && Offset == kDynamicShadowSentinel;
- }
- bool isInifunc() const {
- assert(!InGlobal || !InTls);
- assert(!InGlobal || Offset == kDynamicShadowSentinel);
- return InGlobal;
- }
- bool isInTls() const {
- assert(!InTls || !InGlobal);
- assert(!InTls || Offset == kDynamicShadowSentinel);
- return InTls;
- }
- bool isFixed() const {
- assert(Offset == kDynamicShadowSentinel || !InTls);
- assert(Offset == kDynamicShadowSentinel || !InGlobal);
- return Offset != kDynamicShadowSentinel;
- }
+ bool isInGlobal() const { return Kind == OffsetKind::kGlobal; }
+ bool isInifunc() const { return Kind == OffsetKind::kIfunc; }
+ bool isInTls() const { return Kind == OffsetKind::kTls; }
+ bool isFixed() const { return Kind == OffsetKind::kFixed; }
uint8_t scale() const { return Scale; };
uint64_t offset() const {
assert(isFixed());
@@ -1930,34 +1929,22 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
if (TargetTriple.isOSFuchsia()) {
// Fuchsia is always PIE, which means that the beginning of the address
// space is always available.
- InGlobal = false;
- InTls = false;
- Offset = 0;
+ SetFixed(0);
WithFrameRecord = true;
} else if (ClMappingOffset.getNumOccurrences() > 0) {
- InGlobal = false;
- InTls = false;
- Offset = ClMappingOffset;
+ SetFixed(ClMappingOffset);
WithFrameRecord = false;
} else if (ClEnableKhwasan || InstrumentWithCalls) {
- InGlobal = false;
- InTls = false;
- Offset = 0;
+ SetFixed(0);
WithFrameRecord = false;
} else if (ClWithIfunc) {
- InGlobal = true;
- InTls = false;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kIfunc;
WithFrameRecord = false;
} else if (ClWithTls) {
- InGlobal = false;
- InTls = true;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kTls;
WithFrameRecord = true;
} else {
- InGlobal = false;
- InTls = false;
- Offset = kDynamicShadowSentinel;
+ Kind = OffsetKind::kGlobal;
WithFrameRecord = false;
}
}
|
fmayer
approved these changes
Sep 23, 2024
Created using spr 1.3.4 [skip ci]
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.