@@ -148,9 +148,12 @@ class YkShadowStack : public ModulePass {
148
148
" dynamically sized stack!" );
149
149
return false ;
150
150
}
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;
154
157
GetElementPtrInst *GEP = GetElementPtrInst::Create (
155
158
Int8Ty, SSPtr, {ConstantInt::get (Int32Ty, Offset)}, " " ,
156
159
cast<Instruction>(&AI));
@@ -205,7 +208,14 @@ class YkShadowStack : public ModulePass {
205
208
}
206
209
207
210
// 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
209
219
GetElementPtrInst *GEP = GetElementPtrInst::Create (
210
220
Int8Ty, SSPtr, {ConstantInt::get (Int32Ty, Offset)}, " " , &I);
211
221
Builder.SetInsertPoint (&I);
0 commit comments