Skip to content

Commit

Permalink
added new test. Adjusted test as we dropped the 'no jump to first ins…
Browse files Browse the repository at this point in the history
…truction' condition
  • Loading branch information
nebulark committed Oct 12, 2024
1 parent 13bc998 commit 8764e73
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
6 changes: 4 additions & 2 deletions tests/run-make/hotpatch-unaffected/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Check if hotpatch leaves the functions that are already hotpatchable untouched

//@ revisions: x32 x64
//@ revisions: x32 x64 aarch64
//@[x32] only-x86
//@[x64] only-x86_64
// Reason: hotpatch is only implemented for X86
//@[aarch64] only-aarch64

// Reason: hotpatch is only implemented for X86 and aarch64

use run_make_support::{assertion_helpers, llvm, rustc};

Expand Down
34 changes: 7 additions & 27 deletions tests/run-make/hotpatch/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
// hotpatch has two requirements:
// 1. the first instruction of a functin must be at least two bytes long
// 2. there must not be a jump to the first instruction
// 1) the first instruction of a functin must be at least two bytes long
// 2) there must not be a jump to the first instruction

// the hotpatch flag should insert nops as needed to fullfil the requirements,
// but only if the the function does not already fulfill them.
// Over 99% of function in regular codebases already fulfill the conditions,
// so its important to check that those are
// unneccessarily affected

// ----------------------------------------------------------------------------------------------

// regularly this tailcall would jump to the first instruction the function
// CHECK-LABEL: <tailcall_fn>:
// CHECK: jne 0x0 <tailcall_fn>

// hotpatch insert nops so that the tailcall will not jump to the first instruction of the function
// HOTPATCH-LABEL: <tailcall_fn>:
// HOTPATCH-NOT: jne 0x0 <tailcall_fn>

#[no_mangle]
pub fn tailcall_fn() {
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNT: AtomicUsize = AtomicUsize::new(0);
if COUNT.fetch_sub(1, Ordering::Relaxed) != 0 {
tailcall_fn()
}
}
// The LLVM attribute we use '"patchable-function", "prologue-short-redirect"' only ensures 1)
// However in practice 2) rarely matters. Its rare that it occurs and the problems it caused can be
// avoided by the hotpatch tool.
// In this test we check if 1) is ensured by inserted nops as needed

// ----------------------------------------------------------------------------------------------

// empty_fn just returns. Note that 'ret' is a single byte instruction, but hotpatch requires
// a two or more byte instructions to be at the start of the functions.
// Preferably we would also tests a different single byte instruction,
// but I was not able to make rustc emit anything but 'ret'.
// but I was not able to find an example with another one byte intstruction.

// check that if the first instruction is just a single byte, so our test is valid
// CHECK-LABEL: <empty_fn>:
Expand Down
9 changes: 5 additions & 4 deletions tests/run-make/hotpatch/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Check if hotpatch only makes the functions hotpachable that were not,
// but leaving the other functions untouched
// Check if hotpatch makes the functions hotpachable that were not
// More details in lib.rs

//@ revisions: x32 x64
//@[x32] only-x86
//@[x64] only-x86_64
// Reason: hotpatch is only implemented for X86

// Reason: hotpatch is only implemented for x86 and aarch64, but for aarch64 they
// are always hotpatchable so we don't need to check it

use run_make_support::{llvm, rustc};

Expand All @@ -18,7 +19,7 @@ fn main() {

fn dump_lib(libname: &str) -> String {
llvm::llvm_objdump()
.arg("--disassemble-symbols=tailcall_fn,empty_fn")
.arg("--disassemble-symbols=empty_fn")
.input(libname)
.run()
.stdout_utf8()
Expand Down
6 changes: 6 additions & 0 deletions tests/run-make/hotpatch_pdb/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// CHECK: S_OBJNAME{{.*}}hotpatch_pdb{{.*}}.o
// CHECK: S_COMPILE3
// CHECK-NOT: S_
// CHECK: flags = {{.*}}hot patchable

pub fn main() {}
30 changes: 30 additions & 0 deletions tests/run-make/hotpatch_pdb/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Check if hotpatch flag is present in the Codeview.
// This is need so linkers actually pad functions when given the functionpadmin arg.

//@ revisions: x32 x64 aarch64
//@[x32] only-x86
//@[x64] only-x86_64
//@[aarch64] only-aarch64

// Reason: Hotpatch is only implemented for x86 and aarch64

use run_make_support::{llvm, rustc};

fn main() {
let output = rustc()
.input("main.rs")
.arg("-g")
.arg("-Zhotpatch")
.crate_name("hotpatch_pdb")
.crate_type("bin")
.run();

let pdbutil_output = llvm::llvm_pdbutil()
.arg("dump")
.arg("-symbols")
.input("hotpatch_pdb.pdb")
.run()
.stdout_utf8();

llvm::llvm_filecheck().patterns("main.rs").stdin_buf(&pdbutil_output).run();
}

0 comments on commit 8764e73

Please sign in to comment.