Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate platform implementation #150

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ backtrace = { version = "0.3" }
once_cell = "1.9"
libc = "^0.2.66"
log = "0.4"
nix = { version = "0.24", default-features = false, features = ["signal", "fs"] }

parking_lot = "0.12"
tempfile = "3.1"
thiserror = "1.0"
Expand All @@ -37,7 +37,15 @@ inferno = { version = "0.11", default-features = false, features = ["nameattr"],
prost = { version = "0.10", optional = true }
prost-derive = { version = "0.10", optional = true }
protobuf = { version = "2.0", optional = true }
criterion = {version = "0.3", optional = true}
criterion = { version = "0.3", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
Copy link

@maksymsur maksymsur Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's better to use more generic and inclusive template unless some other UNIX targets but tested linux and macos may fail

[target.'cfg(target_family = "unix")'.dependencies]

Rest of the code looks good

nix = { version = "0.24", default-features = false, features = [
"signal",
"fs",
] }

[target.'cfg(target_os = "windows")'.dependencies]

[dependencies.symbolic-demangle]
version = "9.0"
Expand Down
6 changes: 3 additions & 3 deletions benches/addr_validate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use criterion::{criterion_group, criterion_main, Criterion};
use pprof::validate;
use pprof::addr_validate;

fn bench_validate_addr(c: &mut Criterion) {
c.bench_function("validate stack addr", |b| {
let stack_addrs = [0; 100];

b.iter(|| {
stack_addrs.iter().for_each(|item| {
validate(item as *const _ as *const libc::c_void);
addr_validate(item as *const _ as *const libc::c_void);
})
})
});
Expand All @@ -19,7 +19,7 @@ fn bench_validate_addr(c: &mut Criterion) {

b.iter(|| {
heap_addrs.iter().for_each(|item| {
validate(item as *const _ as *const libc::c_void);
addr_validate(item as *const _ as *const libc::c_void);
})
})
});
Expand Down
24 changes: 2 additions & 22 deletions src/backtrace/mod.rs → src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use libc::c_void;
use std::path::PathBuf;

pub use crate::platform::TraceImpl;

pub trait Symbol: Sized {
fn name(&self) -> Option<Vec<u8>>;
fn addr(&self) -> Option<*mut c_void>;
Expand Down Expand Up @@ -43,25 +45,3 @@ pub trait Trace {
where
Self: Sized;
}

#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
)))]
mod backtrace_rs;
#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
)))]
pub use backtrace_rs::Trace as TraceImpl;

#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
))]
pub mod frame_pointer;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
))]
pub use frame_pointer::Trace as TraceImpl;
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

// TODO Windows error is not finished
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[cfg(target_os = "windows")]
#[error("{0}")]
NixError(#[from] nix::Error),
OsError(i32),

#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[target.'cfg(target_family = "unix")'.dependencies]

#[error("{0}")]
OsError(#[from] nix::Error),

#[error("{0}")]
IoError(#[from] std::io::Error),
#[error("create profiler error")]
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub const MAX_DEPTH: usize = 32;
/// Define the MAX supported thread name length. TODO: make this variable mutable.
pub const MAX_THREAD_NAME: usize = 16;

mod addr_validate;
mod platform;

mod backtrace;
mod collector;
Expand All @@ -54,13 +54,14 @@ mod frames;
mod profiler;
mod report;
mod timer;
mod validator;

pub use self::addr_validate::validate;
pub use self::collector::{Collector, HashCounter};
pub use self::error::{Error, Result};
pub use self::frames::{Frames, Symbol};
pub use self::profiler::{ProfilerGuard, ProfilerGuardBuilder};
pub use self::report::{Report, ReportBuilder, UnresolvedReport};
pub use self::validator::addr_validate;

#[cfg(feature = "flamegraph")]
pub use inferno::flamegraph;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
impl super::Frame for backtrace::Frame {
impl crate::backtrace::Frame for backtrace::Frame {
type S = backtrace::Symbol;

fn ip(&self) -> usize {
Expand All @@ -16,7 +16,7 @@ impl super::Frame for backtrace::Frame {

pub struct Trace {}

impl super::Trace for Trace {
impl crate::backtrace::Trace for Trace {
type Frame = backtrace::Frame;

fn trace<F: FnMut(&Self::Frame) -> bool>(_: *mut libc::c_void, cb: F) {
Expand Down
49 changes: 49 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[target.'cfg(target_family = "unix")'.dependencies]

mod nix_impl {
mod addr_validate;
mod profiler;
mod timer;

#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
))]
mod frame_pointer;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
))]
pub use frame_pointer::Trace as TraceImpl;

#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
)))]
#[path = "../backtrace_rs.rs"]
mod backtrace_rs;
#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
)))]
pub use backtrace_rs::Trace as TraceImpl;
}

#[cfg(target_os = "windows")]
mod windows_impl {
mod addr_validate;
mod profiler;
mod timer;

#[cfg(feature = "frame-pointer")]
std::compile_error!("frame-pointer feature is currently not supported on windows.");

#[path = "../backtrace_rs.rs"]
mod backtrace_rs;
pub use backtrace_rs::Trace as TraceImpl;
}

#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[target.'cfg(target_family = "unix")'.dependencies]

pub use nix_impl::*;

#[cfg(target_os = "windows")]
pub use windows_impl::*;
86 changes: 45 additions & 41 deletions src/addr_validate.rs → src/platform/nix_impl/addr_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,50 @@ use nix::{
unistd::{close, read, write},
};

use crate::validator::{Validator, ValidatorImpl};

thread_local! {
static MEM_VALIDATE_PIPE: RefCell<[i32; 2]> = RefCell::new([-1, -1]);
}

impl ValidatorImpl for Validator {
fn addr_validate(addr: *const libc::c_void) -> bool {
const CHECK_LENGTH: usize = 2 * size_of::<*const libc::c_void>() / size_of::<u8>();

// read data in the pipe
let valid_read = MEM_VALIDATE_PIPE.with(|pipes| {
let pipes = pipes.borrow();
loop {
let mut buf = [0u8; CHECK_LENGTH];

match read(pipes[0], &mut buf) {
Ok(bytes) => break bytes > 0,
Err(_err @ Errno::EINTR) => continue,
Err(_err @ Errno::EAGAIN) => break true,
Err(_) => break false,
}
}
});

if !valid_read && open_pipe().is_err() {
return false;
}

MEM_VALIDATE_PIPE.with(|pipes| {
let pipes = pipes.borrow();
loop {
let buf = unsafe { std::slice::from_raw_parts(addr as *const u8, CHECK_LENGTH) };

match write(pipes[1], buf) {
Ok(bytes) => break bytes > 0,
Err(_err @ Errno::EINTR) => continue,
Err(_) => break false,
}
}
})
}
}

#[inline]
#[cfg(target_os = "linux")]
fn create_pipe() -> nix::Result<(i32, i32)> {
Expand Down Expand Up @@ -58,65 +98,29 @@ fn open_pipe() -> nix::Result<()> {
})
}

pub fn validate(addr: *const libc::c_void) -> bool {
const CHECK_LENGTH: usize = 2 * size_of::<*const libc::c_void>() / size_of::<u8>();

// read data in the pipe
let valid_read = MEM_VALIDATE_PIPE.with(|pipes| {
let pipes = pipes.borrow();
loop {
let mut buf = [0u8; CHECK_LENGTH];

match read(pipes[0], &mut buf) {
Ok(bytes) => break bytes > 0,
Err(_err @ Errno::EINTR) => continue,
Err(_err @ Errno::EAGAIN) => break true,
Err(_) => break false,
}
}
});

if !valid_read && open_pipe().is_err() {
return false;
}

MEM_VALIDATE_PIPE.with(|pipes| {
let pipes = pipes.borrow();
loop {
let buf = unsafe { std::slice::from_raw_parts(addr as *const u8, CHECK_LENGTH) };

match write(pipes[1], buf) {
Ok(bytes) => break bytes > 0,
Err(_err @ Errno::EINTR) => continue,
Err(_) => break false,
}
}
})
}

#[cfg(test)]
mod test {
use super::*;
use crate::addr_validate;

#[test]
fn validate_stack() {
let i = 0;

assert!(validate(&i as *const _ as *const libc::c_void));
assert!(addr_validate(&i as *const _ as *const libc::c_void));
}

#[test]
fn validate_heap() {
let vec = vec![0; 1000];

for i in vec.iter() {
assert!(validate(i as *const _ as *const libc::c_void));
assert!(addr_validate(i as *const _ as *const libc::c_void));
}
}

#[test]
fn failed_validate() {
assert!(!validate(std::ptr::null::<libc::c_void>()));
assert!(!validate(-1_i32 as usize as *const libc::c_void))
assert!(!addr_validate(std::ptr::null::<libc::c_void>()));
assert!(!addr_validate(-1_i32 as usize as *const libc::c_void))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::null_mut;

use libc::c_void;

use crate::addr_validate::validate;
use crate::addr_validate;

#[derive(Clone, Debug)]
pub struct Frame {
Expand All @@ -16,7 +16,7 @@ extern "C" {

}

impl super::Frame for Frame {
impl crate::backtrace::Frame for Frame {
type S = backtrace::Symbol;

fn ip(&self) -> usize {
Expand All @@ -37,7 +37,7 @@ impl super::Frame for Frame {
}

pub struct Trace {}
impl super::Trace for Trace {
impl crate::backtrace::Trace for Trace {
type Frame = Frame;

fn trace<F: FnMut(&Self::Frame) -> bool>(ucontext: *mut libc::c_void, mut cb: F) {
Expand Down Expand Up @@ -91,7 +91,7 @@ impl super::Trace for Trace {
break;
}

if !validate(frame_pointer as *const libc::c_void) {
if !addr_validate(frame_pointer as *const libc::c_void) {
break;
}
last_frame_pointer = frame_pointer;
Expand Down
Loading