Skip to content

Commit 45cbad0

Browse files
committed
Fix stack to always be aligned on 16 bytes
1 parent beda7f5 commit 45cbad0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/RevLoader.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,16 @@ bool RevLoader::LoadProgramArgs( const std::string& exe, const std::vector<std::
487487
XLEN ArgvOffset = sizeof( XLEN ) * ( args.size() + 1 );
488488

489489
// Compute the total size, rounding each string up to a multiple of sizeof( XLEN )
490+
// The terminating 0 byte is included, so arg.size() is rounded up to next multiple
490491
XLEN ArgArraySize = ArgvOffset;
491492
for( auto& arg : args )
492493
ArgArraySize += ( arg.size() | ( sizeof( XLEN ) - 1 ) ) + 1;
493494

494-
// OldStackTop is the current StackTop rounded down to a multiple of sizeof(XLEN)
495-
const XLEN OldStackTop = mem->GetStackTop() & ~( sizeof( XLEN ) - 1 );
495+
// Round ArgArraySize up to a multiple of 16 bytes
496+
ArgArraySize = ( ( ArgArraySize - 1 ) | XLEN{ 15 } ) + 1;
497+
498+
// OldStackTop is the current StackTop rounded down to a multiple of 16 bytes
499+
const XLEN OldStackTop = mem->GetStackTop() & ~XLEN{ 15 };
496500

497501
// Allocate ArgArraySize elements at ArgArrayBase
498502
// Set the new StackTop to ArgArraySize bytes below OldStackTop

0 commit comments

Comments
 (0)