use std::sync::LazyLock;
const RUNNING_ON_VALGRIND: usize = 0x1001;
pub(crate) static ON_VALGRIND: LazyLock<bool> =
LazyLock::new(|| request(0, RUNNING_ON_VALGRIND, 0) > 0);
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
#[inline(always)]
pub(crate) fn request(default: usize, code: usize, arg1: usize) -> usize {
let args: [usize; 6] = [code, arg1, 0, 0, 0, 0];
let mut result = default;
unsafe {
std::arch::asm!(
"rol rdi, 3",
"rol rdi, 13",
"rol rdi, 61",
"rol rdi, 51",
"xchg rbx, rbx",
inout("rdx") result,
in("rax") args.as_ptr(),
options(nostack),
);
}
result
}
#[cfg(all(target_arch = "aarch64", target_os = "linux"))]
#[inline(always)]
pub(crate) fn request(default: usize, code: usize, arg1: usize) -> usize {
let args: [usize; 6] = [code, arg1, 0, 0, 0, 0];
let mut result = default;
unsafe {
std::arch::asm!(
"ror x12, x12, #3",
"ror x12, x12, #13",
"ror x12, x12, #51",
"ror x12, x12, #61",
"orr x10, x10, x10",
inout("x3") result,
in("x4") args.as_ptr(),
options(nostack),
);
}
result
}
#[cfg(not(any(
all(target_arch = "x86_64", target_os = "linux"),
all(target_arch = "aarch64", target_os = "linux")
)))]
#[inline(always)]
pub(crate) fn request(default: usize, _code: usize, _arg1: usize) -> usize {
default
}