@@ -395,15 +395,40 @@ class HWAddressSanitizer {
395
395
// /
396
396
// / If WithFrameRecord is true, then __hwasan_tls will be used to access the
397
397
// / ring buffer for storing stack allocations on targets that support it.
398
- struct ShadowMapping {
398
+ class ShadowMapping {
399
399
uint8_t Scale;
400
400
uint64_t Offset;
401
401
bool InGlobal;
402
402
bool InTls;
403
403
bool WithFrameRecord;
404
404
405
+ public:
405
406
void init (Triple &TargetTriple, bool InstrumentWithCalls);
406
407
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; };
407
432
};
408
433
409
434
ShadowMapping Mapping;
@@ -803,13 +828,13 @@ Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
803
828
}
804
829
805
830
Value *HWAddressSanitizer::getShadowNonTls (IRBuilder<> &IRB) {
806
- if (Mapping.Offset != kDynamicShadowSentinel ) {
831
+ if (Mapping.isFixed () ) {
807
832
return getOpaqueNoopCast (
808
833
IRB, ConstantExpr::getIntToPtr (
809
- ConstantInt::get (IntptrTy, Mapping.Offset ), PtrTy));
834
+ ConstantInt::get (IntptrTy, Mapping.offset () ), PtrTy));
810
835
}
811
836
812
- if (Mapping.InGlobal )
837
+ if (Mapping.isInIfunc () )
813
838
return getDynamicShadowIfunc (IRB);
814
839
815
840
Value *GlobalDynamicAddress =
@@ -941,8 +966,8 @@ void HWAddressSanitizer::untagPointerOperand(Instruction *I, Value *Addr) {
941
966
942
967
Value *HWAddressSanitizer::memToShadow (Value *Mem, IRBuilder<> &IRB) {
943
968
// 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 )
946
971
return IRB.CreateIntToPtr (Shadow, PtrTy);
947
972
// (Mem >> Scale) + Offset
948
973
return IRB.CreatePtrAdd (ShadowBase, Shadow);
@@ -1008,10 +1033,10 @@ void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
1008
1033
// representable.
1009
1034
// In particular, an offset of 4TB (1024 << 32) is representable, and
1010
1035
// 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 ;
1013
1038
UseFixedShadowIntrinsic =
1014
- static_cast <uint64_t >(OffsetShifted) << 32 == Mapping.Offset ;
1039
+ static_cast <uint64_t >(OffsetShifted) << 32 == Mapping.offset () ;
1015
1040
}
1016
1041
1017
1042
if (UseFixedShadowIntrinsic) {
@@ -1021,7 +1046,7 @@ void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
1021
1046
? Intrinsic::hwasan_check_memaccess_shortgranules_fixedshadow
1022
1047
: Intrinsic::hwasan_check_memaccess_fixedshadow),
1023
1048
{Ptr, ConstantInt::get (Int32Ty, AccessInfo),
1024
- ConstantInt::get (Int64Ty, Mapping.Offset )});
1049
+ ConstantInt::get (Int64Ty, Mapping.offset () )});
1025
1050
} else {
1026
1051
IRB.CreateCall (Intrinsic::getDeclaration (
1027
1052
M, UseShortGranules
@@ -1194,7 +1219,7 @@ void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag,
1194
1219
{IRB.CreatePointerCast (AI, PtrTy), Tag,
1195
1220
ConstantInt::get (IntptrTy, AlignedSize)});
1196
1221
} else {
1197
- size_t ShadowSize = Size >> Mapping.Scale ;
1222
+ size_t ShadowSize = Size >> Mapping.scale () ;
1198
1223
Value *AddrLong = untagPointer (IRB, IRB.CreatePointerCast (AI, IntptrTy));
1199
1224
Value *ShadowPtr = memToShadow (AddrLong, IRB);
1200
1225
// If this memset is not inlined, it will be intercepted in the hwasan
@@ -1352,7 +1377,7 @@ Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
1352
1377
}
1353
1378
1354
1379
void HWAddressSanitizer::emitPrologue (IRBuilder<> &IRB, bool WithFrameRecord) {
1355
- if (!Mapping.InTls )
1380
+ if (!Mapping.isInTls () )
1356
1381
ShadowBase = getShadowNonTls (IRB);
1357
1382
else if (!WithFrameRecord && TargetTriple.isAndroid ())
1358
1383
ShadowBase = getDynamicShadowIfunc (IRB);
@@ -1677,7 +1702,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
1677
1702
IRBuilder<> EntryIRB (&F.getEntryBlock (), InsertPt);
1678
1703
emitPrologue (EntryIRB,
1679
1704
/* WithFrameRecord*/ ClRecordStackHistory != none &&
1680
- Mapping.WithFrameRecord &&
1705
+ Mapping.withFrameRecord () &&
1681
1706
!SInfo.AllocasToInstrument .empty ());
1682
1707
1683
1708
if (!SInfo.AllocasToInstrument .empty ()) {
0 commit comments