Skip to content

Commit 73a3d5d

Browse files
committed
Ensure stack is always aligned with 8 bytes
In dynamic linking mode, consider external functions may use data types such as 'long long int' or other equivalents. The program may encounter a Bus error if external functions access 8-byte data on the stack that is not properly aligned. To prevent the aforementioned issue, these changes adjust the Arm code generator to ensure that the program's stack is always aligned with 8 bytes. Since the RISC-V architecture does not yet support dynamic linking, the corresponding code generator is not modified in this commit.
1 parent f4992ba commit 73a3d5d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/arm-codegen.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
219219
switch (ph2_ir->op) {
220220
case OP_define:
221221
emit(__sw(__AL, __lr, __sp, -4));
222-
emit(__movw(__AL, __r8, ph2_ir->src0 + 4));
223-
emit(__movt(__AL, __r8, ph2_ir->src0 + 4));
222+
ofs = ALIGN_UP(ph2_ir->src0 + 4, 8);
223+
emit(__movw(__AL, __r8, ofs));
224+
emit(__movt(__AL, __r8, ofs));
224225
emit(__sub_r(__AL, __sp, __sp, __r8));
225226
return;
226227
case OP_load_constant:
@@ -342,8 +343,9 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
342343
emit(__mov_r(__AL, __r0, __r0));
343344
else
344345
emit(__mov_r(__AL, __r0, rn));
345-
emit(__movw(__AL, __r8, ph2_ir->src1 + 4));
346-
emit(__movt(__AL, __r8, ph2_ir->src1 + 4));
346+
ofs = ALIGN_UP(ph2_ir->src1 + 4, 8);
347+
emit(__movw(__AL, __r8, ofs));
348+
emit(__movt(__AL, __r8, ofs));
347349
emit(__add_r(__AL, __sp, __sp, __r8));
348350
emit(__lw(__AL, __lr, __sp, -4));
349351
emit(__bx(__AL, __lr));
@@ -486,6 +488,8 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
486488
void plt_generate(void);
487489
void code_generate(void)
488490
{
491+
int ofs;
492+
489493
if (dynlink) {
490494
plt_generate();
491495
/* Call __libc_start_main() */
@@ -521,8 +525,9 @@ void code_generate(void)
521525
/* For both static and dynamic linking, we need to set up the stack
522526
* and call the main function.
523527
* */
524-
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
525-
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
528+
ofs = ALIGN_UP(GLOBAL_FUNC->stack_size, 8);
529+
emit(__movw(__AL, __r8, ofs));
530+
emit(__movt(__AL, __r8, ofs));
526531
emit(__sub_r(__AL, __sp, __sp, __r8));
527532
emit(__mov_r(__AL, __r12, __sp));
528533

@@ -533,8 +538,8 @@ void code_generate(void)
533538
56)); /* PC+8: skip exit (24) + syscall (36) + ret (4) - 8 */
534539

535540
/* exit - only for static linking */
536-
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
537-
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
541+
emit(__movw(__AL, __r8, ofs));
542+
emit(__movt(__AL, __r8, ofs));
538543
emit(__add_r(__AL, __sp, __sp, __r8));
539544
emit(__mov_r(__AL, __r0, __r0));
540545
emit(__mov_i(__AL, __r7, 1));
@@ -568,8 +573,8 @@ void code_generate(void)
568573
* will return to __libc_start_main. */
569574
emit(__b(__AL, MAIN_BB->elf_offset - elf_code->size));
570575
} else {
571-
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
572-
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
576+
emit(__movw(__AL, __r8, ofs));
577+
emit(__movt(__AL, __r8, ofs));
573578
emit(__add_r(__AL, __r8, __r12, __r8));
574579
emit(__lw(__AL, __r0, __r8, 0));
575580
emit(__add_i(__AL, __r1, __r8, 4));

src/defs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
#define HOST_PTR_SIZE __SIZEOF_POINTER__
9292
#endif
9393

94+
#ifndef ALIGN_UP
95+
#define ALIGN_UP(val, align) (((val) + (align) - 1) & ~((align) - 1))
96+
#endif
97+
9498
/* Common data structures */
9599
typedef struct arena_block {
96100
char *memory;

0 commit comments

Comments
 (0)