Skip to content

Commit 1900682

Browse files
authored
chore: fix some small things (#3)
addressing some feedback from @kopecs > * https://github.com/semgrep/pyro-caml/blob/5937a13eee7b5f2313ed5d7d11a3d7726a35970f/src/main.rs#L127 Safety comment But also, do we need this for a subprocess or something? Does https://docs.rs/pretty_env_logger/latest/pretty_env_logger/fn.formatted_timed_builder.html and filter_level or whatever work? > * https://github.com/semgrep/pyro-caml/blob/5937a13eee7b5f2313ed5d7d11a3d7726a35970f/src/ocaml_intf.rs#L28 Safety comment (same for others in this file) Stupid why is this unsafe and the docs for the library have no details. I mean, I get it, FFI, but still. > * https://github.com/semgrep/pyro-caml/blob/5937a13eee7b5f2313ed5d7d11a3d7726a35970f/src/backend.rs#L21-L25 OK, I might not understand this, but what does the runtime actually do here? I think I am missing what the actual interaction is. We have our own thread local runtime to call an ffi function? > * https://github.com/semgrep/pyro-caml/blob/5937a13eee7b5f2313ed5d7d11a3d7726a35970f/src/backend.rs#L99 seems not so useful to log that this was just called? As far as the runtime point goes, we can't send runtimes across threads, so this was the easiest way for me to get one where it needs to be. I probably could be more clever about where/when I create it but 🤷
2 parents 5937a13 + 1e22da3 commit 1900682

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/backend.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ impl Backend for CamlSpy {
9696
}
9797

9898
fn sample_rate(&self) -> Result<u32> {
99-
log::debug!(target:LOG_TAG, "request sample rate");
10099
Ok(self.config.sample_rate)
101100
}
102101

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ fn make_agent_builder(
123123
fn main() {
124124
let cli = Cli::parse();
125125

126-
// there is probably a better way to do this
127-
unsafe { std::env::set_var("RUST_LOG", cli.verbosity.log_level_filter().to_string()) };
128-
pretty_env_logger::init_timed();
126+
pretty_env_logger::formatted_timed_builder()
127+
.filter_level(cli.verbosity.log_level_filter())
128+
.init();
129129

130130
let bin = cli.binary;
131131
let args = cli.args;

src/ocaml_intf.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ impl From<CamlIntfError> for PyroscopeError {
2525
impl From<ocaml::Error> for CamlIntfError {
2626
fn from(err: ocaml::Error) -> Self {
2727
if let ocaml::Error::Caml(ocaml::CamlError::Exception(exc)) = &err {
28+
// # Safety
29+
// exc.exception_to_string() is unsafe because it may dereference
30+
// a raw pointer inside the ocaml runtime. However, since we have
31+
// an &Runtime in the call stack, we assume the caller has ensured
32+
// the runtime is valid. (I think)
2833
match unsafe { exc.exception_to_string() } {
2934
Ok(s) => CamlIntfError(format!("Ocaml exception: {}", s)),
3035
Err(utf8error) => CamlIntfError(format!(
@@ -98,10 +103,22 @@ pub fn read_poll(
98103
cursor: Cursor,
99104
interval: f64,
100105
) -> Result<Vec<CamlStackTrace>, CamlIntfError> {
106+
// # Safety
107+
//
108+
// it is unclear why this function is unsafe from the ocaml-rs docs
109+
// but we assume the caller must ensure the gc is valid, and we convert the
110+
// path and int for the caller, so there is no risk they coerced a bad
111+
// value into an ocaml::Value
101112
Ok(unsafe { read_poll_ml(gc, cursor, interval as ocaml::Float) }?.into_vec())
102113
}
103114

104115
pub fn create_cursor(gc: &Runtime, path: &Path, pid: u32) -> Cursor {
116+
// Safety
117+
//
118+
// it is unclear why this function is unsafe from the ocaml-rs docs but we
119+
// assume the caller must ensure the gc is valid, and we conver the path and
120+
// int for the caller, so there is no risk they coerced a bad value into an
121+
// ocaml::Value
105122
unsafe { create_cursor_ml(gc, path.to_str().unwrap().to_string(), pid as ocaml::Int) }
106123
.expect("failed to create cursor")
107124
}

0 commit comments

Comments
 (0)