@@ -148,9 +148,12 @@ class YkShadowStack : public ModulePass {
148148 " dynamically sized stack!" );
149149 return false ;
150150 }
151- // Calculate this `AllocaInst`s size and create a replacement
152- // pointer into the shadow stack.
153- size_t AllocaSize = *AllocaSizeInBits / 8 ;
151+ // Calculate this `AllocaInst`s size, aligning its pointer if
152+ // necessary, and create a replacement pointer into the shadow
153+ // stack.
154+ size_t AllocaSize = *AllocaSizeInBits / sizeof (uintptr_t );
155+ size_t Align = AI.getAlign ().value ();
156+ Offset = int ((Offset + (Align - 1 )) / Align) * Align;
154157 GetElementPtrInst *GEP = GetElementPtrInst::Create (
155158 Int8Ty, SSPtr, {ConstantInt::get (Int32Ty, Offset)}, " " ,
156159 cast<Instruction>(&AI));
@@ -205,7 +208,14 @@ class YkShadowStack : public ModulePass {
205208 }
206209
207210 // Adjust shadow stack pointer before a call, and reset it back to
208- // its previous value upon returning.
211+ // its previous value upon returning. Make sure to align the shadow
212+ // stack to a 16 byte boundary before calling, as required by the
213+ // calling convention.
214+ #ifdef __x86_64__
215+ Offset = int ((Offset + (16 - 1 )) / 16 ) * 16 ;
216+ #else
217+ #error unknown platform
218+ #endif
209219 GetElementPtrInst *GEP = GetElementPtrInst::Create (
210220 Int8Ty, SSPtr, {ConstantInt::get (Int32Ty, Offset)}, " " , &I);
211221 Builder.SetInsertPoint (&I);
0 commit comments