Skip to content

Commit 53c39ba

Browse files
authored
Merge pull request llvm#190 from ltratt/shadowstack_offset_zero
In the shadowstack, don't turn an offset of 0 into a GEP.
2 parents 3fbd812 + 4e229d5 commit 53c39ba

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

llvm/lib/Transforms/Yk/ShadowStack.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,21 @@ class YkShadowStack : public ModulePass {
156156
size_t AllocaSize = *AllocaSizeInBits / sizeof(uintptr_t);
157157
size_t Align = AI.getAlign().value();
158158
Offset = int((Offset + (Align - 1)) / Align) * Align;
159-
GetElementPtrInst *GEP = GetElementPtrInst::Create(
160-
Int8Ty, SSPtr, {ConstantInt::get(Int32Ty, Offset)}, "",
161-
cast<Instruction>(&AI));
162-
Builder.SetInsertPoint(GEP);
163-
Builder.CreateBitCast(GEP, AI.getAllocatedType()->getPointerTo());
164-
cast<Value>(I).replaceAllUsesWith(GEP);
159+
if (Offset == 0) {
160+
// If the offset is 0, we don't want to create `ptr_add
161+
// %shadowstack, 0` as later parts of the pipeline are clever
162+
// enough to recognise that as an alias: instead simply replace
163+
// this variable with a direct reference to the shadow stack
164+
// pointer.
165+
cast<Value>(I).replaceAllUsesWith(SSPtr);
166+
} else {
167+
GetElementPtrInst *GEP = GetElementPtrInst::Create(
168+
Int8Ty, SSPtr, {ConstantInt::get(Int32Ty, Offset)}, "",
169+
cast<Instruction>(&AI));
170+
Builder.SetInsertPoint(GEP);
171+
Builder.CreateBitCast(GEP, AI.getAllocatedType()->getPointerTo());
172+
cast<Value>(I).replaceAllUsesWith(GEP);
173+
}
165174
RemoveAllocas.push_back(cast<Instruction>(&AI));
166175
Offset += AllocaSize;
167176
} else if (isa<CallInst>(I)) {

0 commit comments

Comments
 (0)