From 59bc04418cc2086eb05fcc143a8206fab9d5f973 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Wed, 18 Nov 2026 17:26:29 +0100 Subject: [PATCH] AArch64: make sure SP is 16-aligned at all times during Pallocframe SP-relative accesses crash if SP mod 16 != 0. This could happen before when setting up the stack frame, in the case where the outgoing arguments area of the frame has size = 8 mod 16. Then, the store of the return address and the back link would take place with SP = 8 mod 16 and crash, before SP is further decreased to a multiple of 16. This commit avoids this problem by aligning the size of the outgoing arguments area to 16. Fixes: #614 Closes: #615 --- aarch64/Asmexpand.ml | 2 ++ aarch64/Stacklayout.v | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/aarch64/Asmexpand.ml b/aarch64/Asmexpand.ml index 4d1f5808c6..9197446454 100644 --- a/aarch64/Asmexpand.ml +++ b/aarch64/Asmexpand.ml @@ -504,6 +504,7 @@ let expand_instruction instr = end; assert (retaddrofs = Z.(add linkofs (of_uint 8))); let sz1 = Z.(sub sz linkofs) in + assert (Z.modulo sz1 _16 = Z.zero); if Z.(le sz1 (of_uint 512)) then begin emit (Pstp(X15, X30, ADpreincr(XSP, I64.repr (Z.neg sz1)))); emit (Pcfi_adjust sz1) @@ -513,6 +514,7 @@ let expand_instruction instr = emit (Pstp(X15, X30, ADimm(XSP, I64.zero))) end; let sz2 = Z.(sub sz sz1) in + assert (Z.modulo sz2 _16 = Z.zero); if Z.(gt sz2 zero) then begin expand_addimm64 XSP XSP (Ptrofs.repr (Z.neg sz2)); emit (Pcfi_adjust sz2) diff --git a/aarch64/Stacklayout.v b/aarch64/Stacklayout.v index cdbc64d574..dccc58c8f1 100644 --- a/aarch64/Stacklayout.v +++ b/aarch64/Stacklayout.v @@ -33,7 +33,7 @@ The stack pointer is kept 16-aligned. Definition fe_ofs_arg := 0. Definition make_env (b: bounds) : frame_env := - let olink := align (4 * b.(bound_outgoing)) 8 in (* back link *) + let olink := align (4 * b.(bound_outgoing)) 16 in (* back link *) let oretaddr := olink + 8 in (* return address *) let ocs := oretaddr + 8 in (* callee-saves *) let ol := align (size_callee_save_area b ocs) 8 in (* locals *) @@ -60,7 +60,7 @@ Lemma frame_env_separated: Proof. Local Opaque Z.add Z.mul sepconj range. intros; simpl. - set (olink := align (4 * b.(bound_outgoing)) 8). + set (olink := align (4 * b.(bound_outgoing)) 16). set (oretaddr := olink + 8). set (ocs := oretaddr + 8). set (ol := align (size_callee_save_area b ocs) 8). @@ -100,7 +100,7 @@ Lemma frame_env_range: 0 <= fe_stack_data fe /\ fe_stack_data fe + bound_stack_data b <= fe_size fe. Proof. intros; simpl. - set (olink := align (4 * b.(bound_outgoing)) 8). + set (olink := align (4 * b.(bound_outgoing)) 16). set (oretaddr := olink + 8). set (ocs := oretaddr + 8). set (ol := align (size_callee_save_area b ocs) 8). @@ -126,15 +126,17 @@ Lemma frame_env_aligned: /\ (align_chunk Mptr | fe_ofs_retaddr fe). Proof. intros; simpl. - set (olink := align (4 * b.(bound_outgoing)) 8). + set (olink := align (4 * b.(bound_outgoing)) 16). set (oretaddr := olink + 8). set (ocs := oretaddr + 8). set (ol := align (size_callee_save_area b ocs) 8). set (ostkdata := align (ol + 4 * b.(bound_local)) 8). change (align_chunk Mptr) with 8. + assert ((8 | olink)). + { apply Z.divide_trans with 16. exists 2; auto. apply align_divides; lia. } split. apply Z.divide_0_r. split. apply align_divides; lia. split. apply align_divides; lia. - split. apply align_divides; lia. - apply Z.divide_add_r. apply align_divides; lia. apply Z.divide_refl. + split. auto. + apply Z.divide_add_r; auto using Z.divide_refl. Qed.