Skip to content

Commit c9e2c38

Browse files
authored
[NFC][hwasan] Convert ShadowMapping into class (#109616)
In the next patch we can switch to enum.
1 parent 783bac7 commit c9e2c38

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,40 @@ class HWAddressSanitizer {
395395
///
396396
/// If WithFrameRecord is true, then __hwasan_tls will be used to access the
397397
/// ring buffer for storing stack allocations on targets that support it.
398-
struct ShadowMapping {
398+
class ShadowMapping {
399399
uint8_t Scale;
400400
uint64_t Offset;
401401
bool InGlobal;
402402
bool InTls;
403403
bool WithFrameRecord;
404404

405+
public:
405406
void init(Triple &TargetTriple, bool InstrumentWithCalls);
406407
Align getObjectAlignment() const { return Align(1ULL << Scale); }
408+
bool isInGlobal() const {
409+
return !InGlobal && !InTls && Offset == kDynamicShadowSentinel;
410+
}
411+
bool isInIfunc() const {
412+
assert(!InGlobal || !InTls);
413+
assert(!InGlobal || Offset == kDynamicShadowSentinel);
414+
return InGlobal;
415+
}
416+
bool isInTls() const {
417+
assert(!InTls || !InGlobal);
418+
assert(!InTls || Offset == kDynamicShadowSentinel);
419+
return InTls;
420+
}
421+
bool isFixed() const {
422+
assert(Offset == kDynamicShadowSentinel || !InTls);
423+
assert(Offset == kDynamicShadowSentinel || !InGlobal);
424+
return Offset != kDynamicShadowSentinel;
425+
}
426+
uint8_t scale() const { return Scale; };
427+
uint64_t offset() const {
428+
assert(isFixed());
429+
return Offset;
430+
};
431+
bool withFrameRecord() const { return WithFrameRecord; };
407432
};
408433

409434
ShadowMapping Mapping;
@@ -803,13 +828,13 @@ Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
803828
}
804829

805830
Value *HWAddressSanitizer::getShadowNonTls(IRBuilder<> &IRB) {
806-
if (Mapping.Offset != kDynamicShadowSentinel) {
831+
if (Mapping.isFixed()) {
807832
return getOpaqueNoopCast(
808833
IRB, ConstantExpr::getIntToPtr(
809-
ConstantInt::get(IntptrTy, Mapping.Offset), PtrTy));
834+
ConstantInt::get(IntptrTy, Mapping.offset()), PtrTy));
810835
}
811836

812-
if (Mapping.InGlobal)
837+
if (Mapping.isInIfunc())
813838
return getDynamicShadowIfunc(IRB);
814839

815840
Value *GlobalDynamicAddress =
@@ -941,8 +966,8 @@ void HWAddressSanitizer::untagPointerOperand(Instruction *I, Value *Addr) {
941966

942967
Value *HWAddressSanitizer::memToShadow(Value *Mem, IRBuilder<> &IRB) {
943968
// Mem >> Scale
944-
Value *Shadow = IRB.CreateLShr(Mem, Mapping.Scale);
945-
if (Mapping.Offset == 0)
969+
Value *Shadow = IRB.CreateLShr(Mem, Mapping.scale());
970+
if (Mapping.isFixed() && Mapping.offset() == 0)
946971
return IRB.CreateIntToPtr(Shadow, PtrTy);
947972
// (Mem >> Scale) + Offset
948973
return IRB.CreatePtrAdd(ShadowBase, Shadow);
@@ -1008,10 +1033,10 @@ void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
10081033
// representable.
10091034
// In particular, an offset of 4TB (1024 << 32) is representable, and
10101035
// ought to be good enough for anybody.
1011-
if (TargetTriple.isAArch64() && Mapping.Offset != kDynamicShadowSentinel) {
1012-
uint16_t OffsetShifted = Mapping.Offset >> 32;
1036+
if (TargetTriple.isAArch64() && Mapping.isFixed()) {
1037+
uint16_t OffsetShifted = Mapping.offset() >> 32;
10131038
UseFixedShadowIntrinsic =
1014-
static_cast<uint64_t>(OffsetShifted) << 32 == Mapping.Offset;
1039+
static_cast<uint64_t>(OffsetShifted) << 32 == Mapping.offset();
10151040
}
10161041

10171042
if (UseFixedShadowIntrinsic) {
@@ -1021,7 +1046,7 @@ void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
10211046
? Intrinsic::hwasan_check_memaccess_shortgranules_fixedshadow
10221047
: Intrinsic::hwasan_check_memaccess_fixedshadow),
10231048
{Ptr, ConstantInt::get(Int32Ty, AccessInfo),
1024-
ConstantInt::get(Int64Ty, Mapping.Offset)});
1049+
ConstantInt::get(Int64Ty, Mapping.offset())});
10251050
} else {
10261051
IRB.CreateCall(Intrinsic::getDeclaration(
10271052
M, UseShortGranules
@@ -1194,7 +1219,7 @@ void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag,
11941219
{IRB.CreatePointerCast(AI, PtrTy), Tag,
11951220
ConstantInt::get(IntptrTy, AlignedSize)});
11961221
} else {
1197-
size_t ShadowSize = Size >> Mapping.Scale;
1222+
size_t ShadowSize = Size >> Mapping.scale();
11981223
Value *AddrLong = untagPointer(IRB, IRB.CreatePointerCast(AI, IntptrTy));
11991224
Value *ShadowPtr = memToShadow(AddrLong, IRB);
12001225
// If this memset is not inlined, it will be intercepted in the hwasan
@@ -1352,7 +1377,7 @@ Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
13521377
}
13531378

13541379
void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
1355-
if (!Mapping.InTls)
1380+
if (!Mapping.isInTls())
13561381
ShadowBase = getShadowNonTls(IRB);
13571382
else if (!WithFrameRecord && TargetTriple.isAndroid())
13581383
ShadowBase = getDynamicShadowIfunc(IRB);
@@ -1677,7 +1702,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
16771702
IRBuilder<> EntryIRB(&F.getEntryBlock(), InsertPt);
16781703
emitPrologue(EntryIRB,
16791704
/*WithFrameRecord*/ ClRecordStackHistory != none &&
1680-
Mapping.WithFrameRecord &&
1705+
Mapping.withFrameRecord() &&
16811706
!SInfo.AllocasToInstrument.empty());
16821707

16831708
if (!SInfo.AllocasToInstrument.empty()) {

0 commit comments

Comments
 (0)