@@ -522,9 +522,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
522
522
523
523
void storeOrigin (IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin,
524
524
unsigned Alignment, bool AsCall) {
525
+ unsigned OriginAlignment = std::max (kMinOriginAlignment , Alignment);
525
526
if (isa<StructType>(Shadow->getType ())) {
526
- IRB.CreateAlignedStore (updateOrigin (Origin, IRB), getOriginPtr (Addr, IRB),
527
- Alignment);
527
+ IRB.CreateAlignedStore (updateOrigin (Origin, IRB),
528
+ getOriginPtr (Addr, IRB, Alignment),
529
+ OriginAlignment);
528
530
} else {
529
531
Value *ConvertedShadow = convertToShadowTyNoVec (Shadow, IRB);
530
532
// TODO(eugenis): handle non-zero constant shadow by inserting an
@@ -549,7 +551,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
549
551
Cmp, IRB.GetInsertPoint (), false , MS.OriginStoreWeights );
550
552
IRBuilder<> IRBNew (CheckTerm);
551
553
IRBNew.CreateAlignedStore (updateOrigin (Origin, IRBNew),
552
- getOriginPtr (Addr, IRBNew), Alignment);
554
+ getOriginPtr (Addr, IRBNew, Alignment),
555
+ OriginAlignment);
553
556
}
554
557
}
555
558
}
@@ -573,11 +576,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
573
576
574
577
if (SI.isAtomic ()) SI.setOrdering (addReleaseOrdering (SI.getOrdering ()));
575
578
576
- if (MS.TrackOrigins ) {
577
- unsigned Alignment = std::max (kMinOriginAlignment , SI.getAlignment ());
578
- storeOrigin (IRB, Addr, Shadow, getOrigin (Val), Alignment,
579
+ if (MS.TrackOrigins )
580
+ storeOrigin (IRB, Addr, Shadow, getOrigin (Val), SI.getAlignment (),
579
581
InstrumentWithCalls);
580
- }
581
582
}
582
583
}
583
584
@@ -739,16 +740,17 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
739
740
// / address.
740
741
// /
741
742
// / OriginAddr = (ShadowAddr + OriginOffset) & ~3ULL
742
- Value *getOriginPtr (Value *Addr, IRBuilder<> &IRB) {
743
+ Value *getOriginPtr (Value *Addr, IRBuilder<> &IRB, unsigned Alignment ) {
743
744
Value *ShadowLong =
744
- IRB.CreateAnd (IRB.CreatePointerCast (Addr, MS.IntptrTy ),
745
- ConstantInt::get (MS.IntptrTy , ~MS.ShadowMask ));
746
- Value *Add =
747
- IRB.CreateAdd (ShadowLong,
748
- ConstantInt::get (MS.IntptrTy , MS.OriginOffset ));
749
- Value *SecondAnd =
750
- IRB.CreateAnd (Add, ConstantInt::get (MS.IntptrTy , ~3ULL ));
751
- return IRB.CreateIntToPtr (SecondAnd, PointerType::get (IRB.getInt32Ty (), 0 ));
745
+ IRB.CreateAnd (IRB.CreatePointerCast (Addr, MS.IntptrTy ),
746
+ ConstantInt::get (MS.IntptrTy , ~MS.ShadowMask ));
747
+ Value *Origin = IRB.CreateAdd (
748
+ ShadowLong, ConstantInt::get (MS.IntptrTy , MS.OriginOffset ));
749
+ if (Alignment < kMinOriginAlignment ) {
750
+ uint64_t Mask = kMinOriginAlignment - 1 ;
751
+ Origin = IRB.CreateAnd (Origin, ConstantInt::get (MS.IntptrTy , ~Mask));
752
+ }
753
+ return IRB.CreateIntToPtr (Origin, PointerType::get (IRB.getInt32Ty (), 0 ));
752
754
}
753
755
754
756
// / \brief Compute the shadow address for a given function argument.
@@ -1052,9 +1054,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1052
1054
1053
1055
if (MS.TrackOrigins ) {
1054
1056
if (PropagateShadow) {
1055
- unsigned Alignment = std::max (kMinOriginAlignment , I.getAlignment ());
1056
- setOrigin (&I,
1057
- IRB.CreateAlignedLoad (getOriginPtr (Addr, IRB), Alignment));
1057
+ unsigned Alignment = I.getAlignment ();
1058
+ unsigned OriginAlignment = std::max (kMinOriginAlignment , Alignment);
1059
+ setOrigin (&I, IRB.CreateAlignedLoad (getOriginPtr (Addr, IRB, Alignment),
1060
+ OriginAlignment));
1058
1061
} else {
1059
1062
setOrigin (&I, getCleanOrigin ());
1060
1063
}
@@ -1706,7 +1709,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1706
1709
// FIXME: use ClStoreCleanOrigin
1707
1710
// FIXME: factor out common code from materializeStores
1708
1711
if (MS.TrackOrigins )
1709
- IRB.CreateStore (getOrigin (&I, 1 ), getOriginPtr (Addr, IRB));
1712
+ IRB.CreateStore (getOrigin (&I, 1 ), getOriginPtr (Addr, IRB, 1 ));
1710
1713
return true ;
1711
1714
}
1712
1715
@@ -1733,7 +1736,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1733
1736
1734
1737
if (MS.TrackOrigins ) {
1735
1738
if (PropagateShadow)
1736
- setOrigin (&I, IRB.CreateLoad (getOriginPtr (Addr, IRB)));
1739
+ setOrigin (&I, IRB.CreateLoad (getOriginPtr (Addr, IRB, 1 )));
1737
1740
else
1738
1741
setOrigin (&I, getCleanOrigin ());
1739
1742
}
0 commit comments