feat(core): implement failing stub trampoline and host dlsym resolver
This commit is contained in:
parent
298884e194
commit
df210a88a1
3 changed files with 39 additions and 0 deletions
|
|
@ -4,3 +4,4 @@ pub mod elf;
|
|||
pub mod mmap;
|
||||
pub mod reloc;
|
||||
pub mod symbols;
|
||||
pub mod trampoline;
|
||||
|
|
|
|||
21
crates/molotov-core/src/trampoline.rs
Normal file
21
crates/molotov-core/src/trampoline.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use std::ffi::CString;
|
||||
|
||||
pub extern "C" fn generic_failing_stub() -> i32 {
|
||||
// ENOSYS is errno 38 on Linux x86_64
|
||||
-38
|
||||
}
|
||||
|
||||
pub fn resolve_symbol_address(symbol: &str) -> Result<u64, String> {
|
||||
// 1. Host lookup via dlsym
|
||||
let c_name = CString::new(symbol).map_err(|e| e.to_string())?;
|
||||
let host_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c_name.as_ptr()) };
|
||||
|
||||
if !host_ptr.is_null() {
|
||||
Ok(host_ptr as u64)
|
||||
} else {
|
||||
// 2. Return explicit failure stub trampoline (never fake 0)
|
||||
tracing::warn!("Resolving unimplemented symbol '{}' to generic_failing_stub (-ENOSYS)", symbol);
|
||||
Ok(generic_failing_stub as *const () as u64)
|
||||
}
|
||||
}
|
||||
17
crates/molotov-core/tests/trampoline_test.rs
Normal file
17
crates/molotov-core/tests/trampoline_test.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use molotov_core::trampoline::resolve_symbol_address;
|
||||
|
||||
#[test]
|
||||
fn test_resolve_standard_host_symbol() {
|
||||
let addr = resolve_symbol_address("malloc").expect("malloc should resolve to host glibc");
|
||||
assert_ne!(addr, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unimplemented_stub_returns_failing_function() {
|
||||
let stub_fn = resolve_symbol_address("unimplemented_android_telemetry")
|
||||
.expect("Should resolve to a failing stub trampoline");
|
||||
let func: extern "C" fn() -> i32 = unsafe { std::mem::transmute(stub_fn) };
|
||||
let ret = func();
|
||||
assert_eq!(ret, -38, "Stub must return -ENOSYS (-38), never fake 0 or success");
|
||||
}
|
||||
Reference in a new issue