Skip to content

Commit

Permalink
Preserve uext and sext flags for parameters on x86_64 and apple aarch64
Browse files Browse the repository at this point in the history
This is required by the ABI and prevents a miscompilation when calling
LLVM compiled functions.
  • Loading branch information
bjorn3 committed Oct 22, 2023
1 parent b995e2f commit d295399
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 5 deletions.
10 changes: 7 additions & 3 deletions cranelift/codegen/src/isa/aarch64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,10 +1148,14 @@ impl ABIMachineSpec for AArch64MachineDeps {
}

fn get_ext_mode(
_call_conv: isa::CallConv,
_specified: ir::ArgumentExtension,
call_conv: isa::CallConv,
specified: ir::ArgumentExtension,
) -> ir::ArgumentExtension {
ir::ArgumentExtension::None
if call_conv == isa::CallConv::AppleAarch64 {
specified
} else {
ir::ArgumentExtension::None
}
}

fn compute_frame_layout(
Expand Down
4 changes: 2 additions & 2 deletions cranelift/codegen/src/isa/x64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,9 @@ impl ABIMachineSpec for X64ABIMachineSpec {

fn get_ext_mode(
_call_conv: isa::CallConv,
_specified: ir::ArgumentExtension,
specified: ir::ArgumentExtension,
) -> ir::ArgumentExtension {
ir::ArgumentExtension::None
specified
}

fn compute_frame_layout(
Expand Down
69 changes: 69 additions & 0 deletions cranelift/filetests/filetests/isa/aarch64/uext-sext-handling.clif
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
test compile precise-output
target aarch64

function u0:0(i8) system_v {
; The aapcs64 call conv ignores the uext and sext flags
sig0 = (i8 uext) system_v
fn0 = u0:0 sig0

block0(v0: i8):
call fn0(v0)
return
}

; VCode:
; stp fp, lr, [sp, #-16]!
; mov fp, sp
; block0:
; load_ext_name x2, User(userextname0)+0
; blr x2
; ldp fp, lr, [sp], #16
; ret
;
; Disassembled:
; block0: ; offset 0x0
; stp x29, x30, [sp, #-0x10]!
; mov x29, sp
; block1: ; offset 0x8
; ldr x2, #0x10
; b #0x18
; .byte 0x00, 0x00, 0x00, 0x00 ; reloc_external Abs8 u0:0 0
; .byte 0x00, 0x00, 0x00, 0x00
; blr x2
; ldp x29, x30, [sp], #0x10
; ret

function u0:0(i8) apple_aarch64 {
; The aaple aarch64 call conv respects the uext and sext flags
sig0 = (i8 uext) apple_aarch64
fn0 = u0:0 sig0

block0(v0: i8):
call fn0(v0)
return
}

; VCode:
; stp fp, lr, [sp, #-16]!
; mov fp, sp
; block0:
; uxtb w0, w0
; load_ext_name x4, User(userextname0)+0
; blr x4
; ldp fp, lr, [sp], #16
; ret
;
; Disassembled:
; block0: ; offset 0x0
; stp x29, x30, [sp, #-0x10]!
; mov x29, sp
; block1: ; offset 0x8
; uxtb w0, w0
; ldr x4, #0x14
; b #0x1c
; .byte 0x00, 0x00, 0x00, 0x00 ; reloc_external Abs8 u0:0 0
; .byte 0x00, 0x00, 0x00, 0x00
; blr x4
; ldp x29, x30, [sp], #0x10
; ret

75 changes: 75 additions & 0 deletions cranelift/filetests/filetests/isa/x64/uext-sext-handling.clif
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
test compile precise-output
target x86_64

function u0:0(i8) system_v {
; The x86_64 system_v call conv respects uext and sext
sig0 = (i8 uext) system_v
fn0 = u0:0 sig0

block0(v0: i8):
call fn0(v0)
return
}

; VCode:
; pushq %rbp
; movq %rsp, %rbp
; block0:
; movzbq %dil, %rdi
; load_ext_name userextname0+0, %rdx
; call *%rdx
; movq %rbp, %rsp
; popq %rbp
; ret
;
; Disassembled:
; block0: ; offset 0x0
; pushq %rbp
; movq %rsp, %rbp
; block1: ; offset 0x4
; movzbq %dil, %rdi
; movabsq $0, %rdx ; reloc_external Abs8 u0:0 0
; callq *%rdx
; movq %rbp, %rsp
; popq %rbp
; retq

function u0:0(i8) windows_fastcall {
; The x86_64 windows_fastcall call conv respects uext and sext
sig0 = (i8 uext) windows_fastcall
fn0 = u0:0 sig0

block0(v0: i8):
call fn0(v0)
return
}

; VCode:
; pushq %rbp
; movq %rsp, %rbp
; block0:
; subq %rsp, $32, %rsp
; virtual_sp_offset_adjust 32
; movzbq %cl, %rcx
; load_ext_name userextname0+0, %r9
; call *%r9
; addq %rsp, $32, %rsp
; virtual_sp_offset_adjust -32
; movq %rbp, %rsp
; popq %rbp
; ret
;
; Disassembled:
; block0: ; offset 0x0
; pushq %rbp
; movq %rsp, %rbp
; block1: ; offset 0x4
; subq $0x20, %rsp
; movzbq %cl, %rcx
; movabsq $0, %r9 ; reloc_external Abs8 u0:0 0
; callq *%r9
; addq $0x20, %rsp
; movq %rbp, %rsp
; popq %rbp
; retq

0 comments on commit d295399

Please sign in to comment.