|
| 1 | +//! Test that the symbol address of `malloc` in shared libraries come from |
| 2 | +//! the place we'd expect it to. |
| 3 | +use core::ffi::{c_char, CStr}; |
| 4 | +use std::{env, path::Path}; |
| 5 | + |
| 6 | +use tikv_jemalloc_sys as _; |
| 7 | + |
| 8 | +extern "C-unwind" { |
| 9 | + fn lookup_malloc_address() -> *const c_char; |
| 10 | +} |
| 11 | + |
| 12 | +fn main() { |
| 13 | + let actual = unsafe { CStr::from_ptr(lookup_malloc_address()).to_str().unwrap() }; |
| 14 | + |
| 15 | + if cfg!(target_vendor = "apple") { |
| 16 | + // macOS / Mach-O symbols are not overriden, they are hooked into with |
| 17 | + // `zone_register`. |
| 18 | + assert_eq!(actual, "/usr/lib/system/libsystem_malloc.dylib"); |
| 19 | + } else if cfg!(all(target_os = "linux", target_env = "gnu")) { |
| 20 | + if cfg!(feature = "unprefixed_malloc_on_supported_platforms") { |
| 21 | + // When unprefixed, `malloc` is loaded from the current exe. |
| 22 | + // `target/*/debug/test-dylib` |
| 23 | + let dir = env::current_dir().unwrap(); |
| 24 | + let exe = env::current_exe().unwrap(); |
| 25 | + assert_eq!(Path::new(actual), exe.strip_prefix(dir).unwrap()); |
| 26 | + } else if cfg!(target_arch = "x86_64") { |
| 27 | + // Otherwise, the system `libc` contains `malloc`. |
| 28 | + assert_eq!(actual, "/lib/x86_64-linux-gnu/libc.so.6"); |
| 29 | + } else if cfg!(target_arch = "x86") { |
| 30 | + assert_eq!(actual, "/lib/i386-linux-gnu/libc.so.6"); |
| 31 | + } else if cfg!(target_arch = "aarch64") { |
| 32 | + assert_eq!(actual, "/lib/aarch64-linux-gnu/libc.so.6"); |
| 33 | + } else { |
| 34 | + panic!("unknown architecture. {:?}", actual); |
| 35 | + } |
| 36 | + } else { |
| 37 | + panic!("unsupported platform for this test. {:?}", actual); |
| 38 | + }; |
| 39 | +} |
0 commit comments