Skip to content

Commit

Permalink
Merge pull request llvm#190 from ltratt/shadowstack_offset_zero
Browse files Browse the repository at this point in the history
In the shadowstack, don't turn an offset of 0 into a GEP.
  • Loading branch information
ptersilie authored Aug 13, 2024
2 parents 3fbd812 + 4e229d5 commit 53c39ba
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions llvm/lib/Transforms/Yk/ShadowStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,21 @@ class YkShadowStack : public ModulePass {
size_t AllocaSize = *AllocaSizeInBits / sizeof(uintptr_t);
size_t Align = AI.getAlign().value();
Offset = int((Offset + (Align - 1)) / Align) * Align;
GetElementPtrInst *GEP = GetElementPtrInst::Create(
Int8Ty, SSPtr, {ConstantInt::get(Int32Ty, Offset)}, "",
cast<Instruction>(&AI));
Builder.SetInsertPoint(GEP);
Builder.CreateBitCast(GEP, AI.getAllocatedType()->getPointerTo());
cast<Value>(I).replaceAllUsesWith(GEP);
if (Offset == 0) {
// If the offset is 0, we don't want to create `ptr_add
// %shadowstack, 0` as later parts of the pipeline are clever
// enough to recognise that as an alias: instead simply replace
// this variable with a direct reference to the shadow stack
// pointer.
cast<Value>(I).replaceAllUsesWith(SSPtr);
} else {
GetElementPtrInst *GEP = GetElementPtrInst::Create(
Int8Ty, SSPtr, {ConstantInt::get(Int32Ty, Offset)}, "",
cast<Instruction>(&AI));
Builder.SetInsertPoint(GEP);
Builder.CreateBitCast(GEP, AI.getAllocatedType()->getPointerTo());
cast<Value>(I).replaceAllUsesWith(GEP);
}
RemoveAllocas.push_back(cast<Instruction>(&AI));
Offset += AllocaSize;
} else if (isa<CallInst>(I)) {
Expand Down

0 comments on commit 53c39ba

Please sign in to comment.