From 0a6e0cea15dba63bea4b5b55b75076f9cb11c604 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 14 Jun 2022 10:22:47 +0200 Subject: [PATCH 1/2] Change assembly tests to differentiate on architecture instead of target --- .../expected/aarch64-apple-ios-sim.s | 11 -------- .../expected/aarch64-apple-ios.s | 11 -------- ...aarch64-apple-darwin.s => apple-aarch64.s} | 0 .../{armv7-apple-ios.s => apple-armv7.s} | 0 .../{armv7s-apple-ios.s => apple-armv7s.s} | 0 .../{i386-apple-ios.s => apple-x86.s} | 0 .../{x86_64-apple-darwin.s => apple-x86_64.s} | 0 .../expected/i686-apple-darwin.s | 11 -------- .../expected/x86_64-apple-ios.s | 11 -------- .../expected/aarch64-apple-ios-sim.s | 18 ------------- .../expected/aarch64-apple-ios.s | 18 ------------- ...aarch64-apple-darwin.s => apple-aarch64.s} | 0 .../{armv7-apple-ios.s => apple-armv7.s} | 0 .../{armv7s-apple-ios.s => apple-armv7s.s} | 0 .../{i386-apple-ios.s => apple-x86.s} | 0 .../{x86_64-apple-darwin.s => apple-x86_64.s} | 0 .../expected/i686-apple-darwin.s | 25 ------------------ .../expected/x86_64-apple-ios.s | 19 -------------- tests/src/bin/test_assembly.rs | 26 ++++++++++++++----- 19 files changed, 19 insertions(+), 131 deletions(-) delete mode 100644 tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios-sim.s delete mode 100644 tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios.s rename tests/assembly/test_msg_send_zero_cost/expected/{aarch64-apple-darwin.s => apple-aarch64.s} (100%) rename tests/assembly/test_msg_send_zero_cost/expected/{armv7-apple-ios.s => apple-armv7.s} (100%) rename tests/assembly/test_msg_send_zero_cost/expected/{armv7s-apple-ios.s => apple-armv7s.s} (100%) rename tests/assembly/test_msg_send_zero_cost/expected/{i386-apple-ios.s => apple-x86.s} (100%) rename tests/assembly/test_msg_send_zero_cost/expected/{x86_64-apple-darwin.s => apple-x86_64.s} (100%) delete mode 100644 tests/assembly/test_msg_send_zero_cost/expected/i686-apple-darwin.s delete mode 100644 tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-ios.s delete mode 100644 tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios-sim.s delete mode 100644 tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios.s rename tests/assembly/test_retain_autoreleased/expected/{aarch64-apple-darwin.s => apple-aarch64.s} (100%) rename tests/assembly/test_retain_autoreleased/expected/{armv7-apple-ios.s => apple-armv7.s} (100%) rename tests/assembly/test_retain_autoreleased/expected/{armv7s-apple-ios.s => apple-armv7s.s} (100%) rename tests/assembly/test_retain_autoreleased/expected/{i386-apple-ios.s => apple-x86.s} (100%) rename tests/assembly/test_retain_autoreleased/expected/{x86_64-apple-darwin.s => apple-x86_64.s} (100%) delete mode 100644 tests/assembly/test_retain_autoreleased/expected/i686-apple-darwin.s delete mode 100644 tests/assembly/test_retain_autoreleased/expected/x86_64-apple-ios.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios-sim.s b/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios-sim.s deleted file mode 100644 index d73fd4f2a..000000000 --- a/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios-sim.s +++ /dev/null @@ -1,11 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .globl _handle - .p2align 2 -_handle: - b _objc_msgSend - -; Stripped __LLVM section - -; Stripped __LLVM section - -.subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios.s b/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios.s deleted file mode 100644 index d73fd4f2a..000000000 --- a/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-ios.s +++ /dev/null @@ -1,11 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .globl _handle - .p2align 2 -_handle: - b _objc_msgSend - -; Stripped __LLVM section - -; Stripped __LLVM section - -.subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-darwin.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s similarity index 100% rename from tests/assembly/test_msg_send_zero_cost/expected/aarch64-apple-darwin.s rename to tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/armv7-apple-ios.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s similarity index 100% rename from tests/assembly/test_msg_send_zero_cost/expected/armv7-apple-ios.s rename to tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/armv7s-apple-ios.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s similarity index 100% rename from tests/assembly/test_msg_send_zero_cost/expected/armv7s-apple-ios.s rename to tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/i386-apple-ios.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s similarity index 100% rename from tests/assembly/test_msg_send_zero_cost/expected/i386-apple-ios.s rename to tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-darwin.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s similarity index 100% rename from tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-darwin.s rename to tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s diff --git a/tests/assembly/test_msg_send_zero_cost/expected/i686-apple-darwin.s b/tests/assembly/test_msg_send_zero_cost/expected/i686-apple-darwin.s deleted file mode 100644 index f20f298e7..000000000 --- a/tests/assembly/test_msg_send_zero_cost/expected/i686-apple-darwin.s +++ /dev/null @@ -1,11 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .intel_syntax noprefix - .globl _handle - .p2align 4, 0x90 -_handle: - push ebp - mov ebp, esp - pop ebp - jmp _objc_msgSend - -.subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-ios.s b/tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-ios.s deleted file mode 100644 index cd046e70f..000000000 --- a/tests/assembly/test_msg_send_zero_cost/expected/x86_64-apple-ios.s +++ /dev/null @@ -1,11 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .intel_syntax noprefix - .globl _handle - .p2align 4, 0x90 -_handle: - push rbp - mov rbp, rsp - pop rbp - jmp _objc_msgSend - -.subsections_via_symbols diff --git a/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios-sim.s b/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios-sim.s deleted file mode 100644 index 42c336539..000000000 --- a/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios-sim.s +++ /dev/null @@ -1,18 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .globl _handle - .p2align 2 -_handle: - stp x29, x30, [sp, #-16]! - mov x29, sp - bl _objc_msgSend - ; InlineAsm Start - mov x29, x29 - ; InlineAsm End - ldp x29, x30, [sp], #16 - b _objc_retainAutoreleasedReturnValue - -; Stripped __LLVM section - -; Stripped __LLVM section - -.subsections_via_symbols diff --git a/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios.s b/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios.s deleted file mode 100644 index 42c336539..000000000 --- a/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-ios.s +++ /dev/null @@ -1,18 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .globl _handle - .p2align 2 -_handle: - stp x29, x30, [sp, #-16]! - mov x29, sp - bl _objc_msgSend - ; InlineAsm Start - mov x29, x29 - ; InlineAsm End - ldp x29, x30, [sp], #16 - b _objc_retainAutoreleasedReturnValue - -; Stripped __LLVM section - -; Stripped __LLVM section - -.subsections_via_symbols diff --git a/tests/assembly/test_retain_autoreleased/expected/aarch64-apple-darwin.s b/tests/assembly/test_retain_autoreleased/expected/apple-aarch64.s similarity index 100% rename from tests/assembly/test_retain_autoreleased/expected/aarch64-apple-darwin.s rename to tests/assembly/test_retain_autoreleased/expected/apple-aarch64.s diff --git a/tests/assembly/test_retain_autoreleased/expected/armv7-apple-ios.s b/tests/assembly/test_retain_autoreleased/expected/apple-armv7.s similarity index 100% rename from tests/assembly/test_retain_autoreleased/expected/armv7-apple-ios.s rename to tests/assembly/test_retain_autoreleased/expected/apple-armv7.s diff --git a/tests/assembly/test_retain_autoreleased/expected/armv7s-apple-ios.s b/tests/assembly/test_retain_autoreleased/expected/apple-armv7s.s similarity index 100% rename from tests/assembly/test_retain_autoreleased/expected/armv7s-apple-ios.s rename to tests/assembly/test_retain_autoreleased/expected/apple-armv7s.s diff --git a/tests/assembly/test_retain_autoreleased/expected/i386-apple-ios.s b/tests/assembly/test_retain_autoreleased/expected/apple-x86.s similarity index 100% rename from tests/assembly/test_retain_autoreleased/expected/i386-apple-ios.s rename to tests/assembly/test_retain_autoreleased/expected/apple-x86.s diff --git a/tests/assembly/test_retain_autoreleased/expected/x86_64-apple-darwin.s b/tests/assembly/test_retain_autoreleased/expected/apple-x86_64.s similarity index 100% rename from tests/assembly/test_retain_autoreleased/expected/x86_64-apple-darwin.s rename to tests/assembly/test_retain_autoreleased/expected/apple-x86_64.s diff --git a/tests/assembly/test_retain_autoreleased/expected/i686-apple-darwin.s b/tests/assembly/test_retain_autoreleased/expected/i686-apple-darwin.s deleted file mode 100644 index b2ebee241..000000000 --- a/tests/assembly/test_retain_autoreleased/expected/i686-apple-darwin.s +++ /dev/null @@ -1,25 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .intel_syntax noprefix - .globl _handle - .p2align 4, 0x90 -_handle: - push ebp - mov ebp, esp - sub esp, 8 - mov eax, dword ptr [ebp + 8] - mov ecx, dword ptr [ebp + 12] - mov dword ptr [esp + 4], ecx - mov dword ptr [esp], eax - call _objc_msgSend - ## InlineAsm Start - - mov ebp, ebp - - ## InlineAsm End - mov dword ptr [esp], eax - call _objc_retainAutoreleasedReturnValue - add esp, 8 - pop ebp - ret - -.subsections_via_symbols diff --git a/tests/assembly/test_retain_autoreleased/expected/x86_64-apple-ios.s b/tests/assembly/test_retain_autoreleased/expected/x86_64-apple-ios.s deleted file mode 100644 index bdec662f9..000000000 --- a/tests/assembly/test_retain_autoreleased/expected/x86_64-apple-ios.s +++ /dev/null @@ -1,19 +0,0 @@ - .section __TEXT,__text,regular,pure_instructions - .intel_syntax noprefix - .globl _handle - .p2align 4, 0x90 -_handle: - push rbp - mov rbp, rsp - call _objc_msgSend - mov rdi, rax - call _objc_retainAutoreleasedReturnValue - ## InlineAsm Start - - nop - - ## InlineAsm End - pop rbp - ret - -.subsections_via_symbols diff --git a/tests/src/bin/test_assembly.rs b/tests/src/bin/test_assembly.rs index ae7549b08..66d8032a7 100644 --- a/tests/src/bin/test_assembly.rs +++ b/tests/src/bin/test_assembly.rs @@ -5,7 +5,6 @@ use cargo_metadata::Message; use std::env; use std::env::args; -use std::fmt::Write; use std::fs; use std::io; use std::path::Path; @@ -23,13 +22,10 @@ fn strip_section(data: &str, section: &str) -> String { let mut in_removed_section = false; for line in data.lines() { // This only works for the __LLVM sections we're interested in - if line == "" { - in_removed_section = false; - } if line.trim().starts_with(".section") { if line.contains(section) { in_removed_section = true; - write!(res, "; Stripped {section} section\n").unwrap(); + println!("Stripped {section} section"); } else { in_removed_section = false; } @@ -38,6 +34,9 @@ fn strip_section(data: &str, section: &str) -> String { res.push_str(line); res.push('\n'); } + if line == "" { + in_removed_section = false; + } } res } @@ -88,6 +87,7 @@ fn main() { .arg("--message-format=json-render-diagnostics") .arg("--") .arg("--emit=asm") + // .arg("-Zplt=no") .arg("-Cllvm-args=--x86-asm-syntax=intel") .stdout(Stdio::piped()) .stderr(Stdio::inherit()) @@ -123,8 +123,17 @@ fn main() { .unwrap_or(host); println!("Target {target}."); + let architecture = target.split_once("-").unwrap().0; + let architecture = if matches!(architecture, "i386" | "i686") { + "x86" + } else { + architecture + }; + println!("Architecture {architecture}."); - let expected_file = package_path.join("expected").join(format!("{target}.s")); + let expected_file = package_path + .join("expected") + .join(format!("apple-{architecture}.s")); let actual = read_assembly(&artifact).unwrap(); if should_overwrite { @@ -135,7 +144,10 @@ fn main() { panic!("Expected and actual did not match."); } } else { - panic!("Missing assembly output for target {}:\n{}", target, actual); + panic!( + "Missing assembly output for architecture {}:\n{}", + architecture, actual + ); } } } From bc5b235f486906a8c814df4a0c7d4a25e0b9f32e Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 14 Jun 2022 10:45:07 +0200 Subject: [PATCH 2/2] Add assembly test that takes the selector from a static variable --- .../expected/apple-aarch64.s | 21 ++++++++++++++ .../expected/apple-armv7.s | 21 ++++++++++++++ .../expected/apple-armv7s.s | 24 ++++++++++++++++ .../expected/apple-x86.s | 28 +++++++++++++++++++ .../expected/apple-x86_64.s | 20 +++++++++++++ tests/assembly/test_msg_send_zero_cost/lib.rs | 18 ++++++++++++ 6 files changed, 132 insertions(+) diff --git a/tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s index b195a041a..7b25bae3f 100644 --- a/tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s +++ b/tests/assembly/test_msg_send_zero_cost/expected/apple-aarch64.s @@ -4,4 +4,25 @@ _handle: b _objc_msgSend + .globl _handle_with_sel + .p2align 2 +_handle_with_sel: +Lloh0: + adrp x8, _SEL_REF@PAGE +Lloh1: + ldr x1, [x8, _SEL_REF@PAGEOFF] + b _objc_msgSend + .loh AdrpLdr Lloh0, Lloh1 + + .section __TEXT,__const + .globl _SEL +_SEL: + .asciz "someSelector" + + .section __DATA,__const + .globl _SEL_REF + .p2align 3 +_SEL_REF: + .quad _SEL + .subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s index fe45b82cc..7c0461405 100644 --- a/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s +++ b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7.s @@ -6,4 +6,25 @@ _handle: b _objc_msgSend + .globl _handle_with_sel + .p2align 2 + .code 32 +_handle_with_sel: + movw r1, :lower16:(_SEL_REF-(LPC1_0+8)) + movt r1, :upper16:(_SEL_REF-(LPC1_0+8)) +LPC1_0: + ldr r1, [pc, r1] + b _objc_msgSend + + .section __TEXT,__const + .globl _SEL +_SEL: + .asciz "someSelector" + + .section __DATA,__const + .globl _SEL_REF + .p2align 2 +_SEL_REF: + .long _SEL + .subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s index abb037871..845d4984c 100644 --- a/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s +++ b/tests/assembly/test_msg_send_zero_cost/expected/apple-armv7s.s @@ -9,4 +9,28 @@ _handle: bl _objc_msgSend pop {r7, pc} + .globl _handle_with_sel + .p2align 2 + .code 32 +_handle_with_sel: + push {r7, lr} + mov r7, sp + movw r1, :lower16:(_SEL_REF-(LPC1_0+8)) + movt r1, :upper16:(_SEL_REF-(LPC1_0+8)) +LPC1_0: + ldr r1, [pc, r1] + bl _objc_msgSend + pop {r7, pc} + + .section __TEXT,__const + .globl _SEL +_SEL: + .asciz "someSelector" + + .section __DATA,__const + .globl _SEL_REF + .p2align 2 +_SEL_REF: + .long _SEL + .subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s index f20f298e7..abb608b6c 100644 --- a/tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s +++ b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86.s @@ -8,4 +8,32 @@ _handle: pop ebp jmp _objc_msgSend + .globl _handle_with_sel + .p2align 4, 0x90 +_handle_with_sel: + push ebp + mov ebp, esp + sub esp, 8 + call L1$pb +L1$pb: + pop eax + sub esp, 8 + push dword ptr [eax + _SEL_REF-L1$pb] + push dword ptr [ebp + 8] + call _objc_msgSend + add esp, 24 + pop ebp + ret + + .section __TEXT,__const + .globl _SEL +_SEL: + .asciz "someSelector" + + .section __DATA,__const + .globl _SEL_REF + .p2align 2 +_SEL_REF: + .long _SEL + .subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s index cd046e70f..31578b0e1 100644 --- a/tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s +++ b/tests/assembly/test_msg_send_zero_cost/expected/apple-x86_64.s @@ -8,4 +8,24 @@ _handle: pop rbp jmp _objc_msgSend + .globl _handle_with_sel + .p2align 4, 0x90 +_handle_with_sel: + push rbp + mov rbp, rsp + mov rsi, qword ptr [rip + _SEL_REF] + pop rbp + jmp _objc_msgSend + + .section __TEXT,__const + .globl _SEL +_SEL: + .asciz "someSelector" + + .section __DATA,__const + .globl _SEL_REF + .p2align 3 +_SEL_REF: + .quad _SEL + .subsections_via_symbols diff --git a/tests/assembly/test_msg_send_zero_cost/lib.rs b/tests/assembly/test_msg_send_zero_cost/lib.rs index faf37d5cb..b175345d7 100644 --- a/tests/assembly/test_msg_send_zero_cost/lib.rs +++ b/tests/assembly/test_msg_send_zero_cost/lib.rs @@ -1,4 +1,6 @@ //! Test that the inner part of msg_send! is inlined into an objc_msgSend +use core::mem; +use core::ptr; use objc2::runtime::{Object, Sel}; use objc2::MessageReceiver; @@ -7,3 +9,19 @@ use objc2::MessageReceiver; pub fn handle(obj: &Object, sel: Sel) -> *mut Object { unsafe { MessageReceiver::send_message(obj, sel, ()).unwrap() } } + +// This will definitely not work, but is useful for making the assembly look +// closer to real-world. +#[no_mangle] +static SEL: [u8; 13] = *b"someSelector\0"; +#[no_mangle] +static SEL_REF: Sel = unsafe { mem::transmute(SEL.as_ptr()) }; + +fn selector() -> Sel { + unsafe { ptr::read_volatile(&SEL_REF) } +} + +#[no_mangle] +pub fn handle_with_sel(obj: &Object) -> *mut Object { + unsafe { MessageReceiver::send_message(obj, selector(), ()).unwrap() } +}