2021-12-30 00:14:23 +00:00
|
|
|
/*
|
2022-06-02 18:51:06 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2021-12-30 00:14:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Tests for getting backtraces from the guest.
|
|
|
|
|
|
|
|
#![cfg(not(sanitized))]
|
|
|
|
|
|
|
|
use reverie::syscalls::Syscall;
|
|
|
|
use reverie::Error;
|
|
|
|
use reverie::ExitStatus;
|
|
|
|
use reverie::Guest;
|
|
|
|
use reverie::Tool;
|
|
|
|
|
2022-11-03 19:53:11 +00:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-12-30 00:14:23 +00:00
|
|
|
struct TestTool;
|
|
|
|
|
|
|
|
#[reverie::tool]
|
|
|
|
impl Tool for TestTool {
|
2022-11-18 20:36:51 +00:00
|
|
|
type GlobalState = ();
|
|
|
|
type ThreadState = ();
|
|
|
|
|
2021-12-30 00:14:23 +00:00
|
|
|
async fn handle_syscall_event<T: Guest<Self>>(
|
|
|
|
&self,
|
|
|
|
guest: &mut T,
|
|
|
|
syscall: Syscall,
|
|
|
|
) -> Result<i64, Error> {
|
|
|
|
if let Syscall::Getpid(_) = &syscall {
|
|
|
|
let backtrace = guest
|
|
|
|
.backtrace()
|
2022-11-15 17:58:51 +00:00
|
|
|
.expect("failed to get backtrace from guest")
|
|
|
|
.pretty()
|
|
|
|
.expect("failed to get pretty backtrace from guest");
|
2021-12-30 00:14:23 +00:00
|
|
|
|
|
|
|
// There's no guarantee our function is at the top of the stack, so
|
|
|
|
// we simply assert that it is *somewhere* in the stack.
|
|
|
|
assert!(
|
|
|
|
backtrace.iter().any(|frame| {
|
2022-11-15 17:58:51 +00:00
|
|
|
if let Some(symbol) = frame.symbol() {
|
2021-12-30 00:14:23 +00:00
|
|
|
// Due to name mangling, there won't be an exact match.
|
|
|
|
symbol.name.contains("funky_function")
|
|
|
|
} else {
|
2022-11-15 17:58:51 +00:00
|
|
|
false
|
2021-12-30 00:14:23 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
"guest backtrace did not contain our expected function:\n{}",
|
2022-11-15 17:58:51 +00:00
|
|
|
backtrace
|
2021-12-30 00:14:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(guest.inject(syscall).await?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn funky_function() {
|
|
|
|
let _ = unsafe { libc::getpid() };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke() {
|
|
|
|
use reverie_ptrace::testing::test_fn;
|
|
|
|
|
|
|
|
let (output, _) = test_fn::<TestTool, _>(funky_function).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(output.status, ExitStatus::Exited(0));
|
|
|
|
}
|