From 2366e443961a227a8ebb6fc8dd2b2cd8a1489995 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 29 May 2024 10:53:14 +0200 Subject: [PATCH 1/3] Initial commit for NIF inspection tool --- Cargo.toml | 2 +- rustler_tool/.cargo/config.toml | 2 + rustler_tool/Cargo.toml | 9 ++ rustler_tool/src/fake_symbols.rs | 148 +++++++++++++++++++++++++++++++ rustler_tool/src/main.rs | 42 +++++++++ rustler_tool/src/nif.rs | 67 ++++++++++++++ 6 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 rustler_tool/.cargo/config.toml create mode 100644 rustler_tool/Cargo.toml create mode 100644 rustler_tool/src/fake_symbols.rs create mode 100644 rustler_tool/src/main.rs create mode 100644 rustler_tool/src/nif.rs diff --git a/Cargo.toml b/Cargo.toml index 6b494faa..a61dc1f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ members = [ "rustler_tests/native/rustler_serde_test", "rustler_tests/native/dynamic_load", "rustler_tests/native/rustler_compile_tests", - "rustler_benchmarks/native/benchmark", + "rustler_benchmarks/native/benchmark", "rustler_tool", ] default-members = [ "rustler", diff --git a/rustler_tool/.cargo/config.toml b/rustler_tool/.cargo/config.toml new file mode 100644 index 00000000..e2bab392 --- /dev/null +++ b/rustler_tool/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.x86_64-unknown-linux-gnu] +rustflags = [ "-C", "link-args=-Wl,-export-dynamic" ] diff --git a/rustler_tool/Cargo.toml b/rustler_tool/Cargo.toml new file mode 100644 index 00000000..14f40e0e --- /dev/null +++ b/rustler_tool/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rustler_tool" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5", features = [ "derive" ] } +libloading = "0.8" +rustler_sys = { version = "2.4.0", path = "../rustler_sys" } diff --git a/rustler_tool/src/fake_symbols.rs b/rustler_tool/src/fake_symbols.rs new file mode 100644 index 00000000..d385e072 --- /dev/null +++ b/rustler_tool/src/fake_symbols.rs @@ -0,0 +1,148 @@ +#[no_mangle] +pub static enif_alloc: usize = 0; +#[no_mangle] +pub static enif_alloc_binary: usize = 0; +#[no_mangle] +pub static enif_alloc_env: usize = 0; +#[no_mangle] +pub static enif_binary_to_term: usize = 0; +#[no_mangle] +pub static enif_clear_env: usize = 0; +#[no_mangle] +pub static enif_compare: usize = 0; +#[no_mangle] +pub static enif_consume_timeslice: usize = 0; +#[no_mangle] +pub static enif_free: usize = 0; +#[no_mangle] +pub static enif_free_env: usize = 0; +#[no_mangle] +pub static enif_get_atom: usize = 0; +#[no_mangle] +pub static enif_get_atom_length: usize = 0; +#[no_mangle] +pub static enif_get_double: usize = 0; +#[no_mangle] +pub static enif_get_int: usize = 0; +#[no_mangle] +pub static enif_get_list_cell: usize = 0; +#[no_mangle] +pub static enif_get_list_length: usize = 0; +#[no_mangle] +pub static enif_get_local_pid: usize = 0; +#[no_mangle] +pub static enif_get_long: usize = 0; +#[no_mangle] +pub static enif_get_map_size: usize = 0; +#[no_mangle] +pub static enif_get_map_value: usize = 0; +#[no_mangle] +pub static enif_get_resource: usize = 0; +#[no_mangle] +pub static enif_get_tuple: usize = 0; +#[no_mangle] +pub static enif_get_uint: usize = 0; +#[no_mangle] +pub static enif_get_ulong: usize = 0; +#[no_mangle] +pub static enif_hash: usize = 0; +#[no_mangle] +pub static enif_inspect_binary: usize = 0; +#[no_mangle] +pub static enif_inspect_iolist_as_binary: usize = 0; +#[no_mangle] +pub static enif_is_atom: usize = 0; +#[no_mangle] +pub static enif_is_binary: usize = 0; +#[no_mangle] +pub static enif_is_empty_list: usize = 0; +#[no_mangle] +pub static enif_is_fun: usize = 0; +#[no_mangle] +pub static enif_is_identical: usize = 0; +#[no_mangle] +pub static enif_is_list: usize = 0; +#[no_mangle] +pub static enif_is_map: usize = 0; +#[no_mangle] +pub static enif_is_number: usize = 0; +#[no_mangle] +pub static enif_is_pid: usize = 0; +#[no_mangle] +pub static enif_is_port: usize = 0; +#[no_mangle] +pub static enif_is_process_alive: usize = 0; +#[no_mangle] +pub static enif_is_ref: usize = 0; +#[no_mangle] +pub static enif_is_tuple: usize = 0; +#[no_mangle] +pub static enif_make_atom_len: usize = 0; +#[no_mangle] +pub static enif_make_badarg: usize = 0; +#[no_mangle] +pub static enif_make_binary: usize = 0; +#[no_mangle] +pub static enif_make_copy: usize = 0; +#[no_mangle] +pub static enif_make_double: usize = 0; +#[no_mangle] +pub static enif_make_existing_atom_len: usize = 0; +#[no_mangle] +pub static enif_make_int: usize = 0; +#[no_mangle] +pub static enif_make_list_cell: usize = 0; +#[no_mangle] +pub static enif_make_list_from_array: usize = 0; +#[no_mangle] +pub static enif_make_long: usize = 0; +#[no_mangle] +pub static enif_make_map_from_arrays: usize = 0; +#[no_mangle] +pub static enif_make_map_put: usize = 0; +#[no_mangle] +pub static enif_make_map_remove: usize = 0; +#[no_mangle] +pub static enif_make_map_update: usize = 0; +#[no_mangle] +pub static enif_make_new_binary: usize = 0; +#[no_mangle] +pub static enif_make_new_map: usize = 0; +#[no_mangle] +pub static enif_make_reverse_list: usize = 0; +#[no_mangle] +pub static enif_make_sub_binary: usize = 0; +#[no_mangle] +pub static enif_make_tuple_from_array: usize = 0; +#[no_mangle] +pub static enif_make_uint: usize = 0; +#[no_mangle] +pub static enif_make_ulong: usize = 0; +#[no_mangle] +pub static enif_map_iterator_create: usize = 0; +#[no_mangle] +pub static enif_map_iterator_destroy: usize = 0; +#[no_mangle] +pub static enif_map_iterator_get_pair: usize = 0; +#[no_mangle] +pub static enif_map_iterator_next: usize = 0; +#[no_mangle] +pub static enif_map_iterator_prev: usize = 0; +#[no_mangle] +pub static enif_open_resource_type: usize = 0; +#[no_mangle] +pub static enif_raise_exception: usize = 0; +#[no_mangle] +pub static enif_realloc_binary: usize = 0; +#[no_mangle] +pub static enif_release_binary: usize = 0; +#[no_mangle] +pub static enif_schedule_nif: usize = 0; +#[no_mangle] +pub static enif_self: usize = 0; +#[no_mangle] +pub static enif_snprintf: usize = 0; +#[no_mangle] +pub static enif_term_to_binary: usize = 0; +#[no_mangle] +pub static enif_term_type: usize = 0; diff --git a/rustler_tool/src/main.rs b/rustler_tool/src/main.rs new file mode 100644 index 00000000..b977e80c --- /dev/null +++ b/rustler_tool/src/main.rs @@ -0,0 +1,42 @@ +#[cfg(unix)] +mod fake_symbols; +mod nif; + +use std::path::PathBuf; + +use clap::{Parser, Subcommand}; + +use crate::nif::NifLibrary; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// does testing things + Nif { path: PathBuf }, +} + +fn main() { + let cli = Cli::parse(); + + match &cli.command { + Some(Commands::Nif { path }) => { + println!("Extracting nifs from {:?}", path); + + let lib = NifLibrary::load(&path).unwrap(); + + println!("Found library {} with nifs", lib.name); + for nif in lib.nifs { + println!(" {}/{}", nif.name, nif.arity); + } + } + None => { + panic!("No command given") + } + } +} diff --git a/rustler_tool/src/nif.rs b/rustler_tool/src/nif.rs new file mode 100644 index 00000000..673e1204 --- /dev/null +++ b/rustler_tool/src/nif.rs @@ -0,0 +1,67 @@ +use libloading::{Library, Symbol}; +use rustler_sys::ErlNifEntry; +use std::ffi::CStr; +use std::path::{Path, PathBuf}; + +pub struct Nif { + pub name: String, + pub arity: usize, + pub flags: usize, +} + +pub struct NifLibrary { + pub path: PathBuf, + pub name: String, + pub nifs: Vec, +} + +#[cfg(unix)] +unsafe fn maybe_call_nif_init( + lib: &Library, +) -> Result<*const ErlNifEntry, Box> { + let func: Symbol *const ErlNifEntry> = lib.get(b"nif_init")?; + + Ok(func()) +} + +#[cfg(windows)] +unsafe fn maybe_call_nif_init( + lib: &Library, +) -> Result<*const ErlNifEntry, Box> { + use rustler_sys::TWinDynNifCallbacks; + static NULL_CALLBACKS: TWinDynNifCallbacks = TWinDynNifCallbacks {}; + let func: Symbol *const ErlNifEntry> = + lib.get(b"nif_init")?; + + func(&NULL_CALLBACKS) +} + +impl NifLibrary { + pub fn load(path: &Path) -> Result> { + unsafe { + let lib = Library::new(&path)?; + let entry = maybe_call_nif_init(&lib)?; + + let name = CStr::from_ptr((*entry).name).to_str()?.to_string(); + let nif_array = + std::slice::from_raw_parts((*entry).funcs, (*entry).num_of_funcs as usize); + + let nifs = nif_array + .into_iter() + .filter_map(|f| { + Some(Nif { + name: CStr::from_ptr((*f).name).to_str().ok()?.to_string(), + arity: (*f).arity as usize, + flags: (*f).flags as usize, + }) + }) + .collect(); + + Ok(NifLibrary { + path: path.to_path_buf(), + name, + nifs, + }) + } + } +} From df18a0faac4b2ea747361432f7be995da09e6f6e Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 6 Jun 2024 19:25:28 +0200 Subject: [PATCH 2/3] Implement enif_alloc/free in case these are used by the lib's allocator --- rustler_tool/src/fake_symbols.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/rustler_tool/src/fake_symbols.rs b/rustler_tool/src/fake_symbols.rs index d385e072..f445d799 100644 --- a/rustler_tool/src/fake_symbols.rs +++ b/rustler_tool/src/fake_symbols.rs @@ -1,5 +1,28 @@ -#[no_mangle] -pub static enif_alloc: usize = 0; +use std::alloc::{Layout, alloc, dealloc}; + +const HEADER: usize = 8; +const ALIGNMENT: usize = 8; + +#[no_mangle] +pub unsafe extern "C" fn enif_alloc(size: usize) -> *mut u8 { + if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) { + let ptr = alloc(layout); + *(ptr as *mut usize) = size; + return ptr.wrapping_add(HEADER); + } + + std::ptr::null_mut() +} + +#[no_mangle] +pub unsafe extern "C" fn enif_free(ptr: *mut u8) { + let real_ptr = ptr.wrapping_sub(HEADER); + let size = *(real_ptr as *const usize); + if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) { + dealloc(real_ptr, layout); + } +} + #[no_mangle] pub static enif_alloc_binary: usize = 0; #[no_mangle] @@ -13,8 +36,6 @@ pub static enif_compare: usize = 0; #[no_mangle] pub static enif_consume_timeslice: usize = 0; #[no_mangle] -pub static enif_free: usize = 0; -#[no_mangle] pub static enif_free_env: usize = 0; #[no_mangle] pub static enif_get_atom: usize = 0; From 0e803af1e8cf032d79ca0a17ed8b7788becf6682 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 6 Jun 2024 20:45:55 +0200 Subject: [PATCH 3/3] Fix allocator usage and implement simple module formatting --- rustler_tool/src/fake_symbols.rs | 18 +++++++- rustler_tool/src/main.rs | 38 +++++++++++++---- rustler_tool/src/nif.rs | 14 ++++--- rustler_tool/src/nif_elixir.rs | 62 +++++++++++++++++++++++++++ rustler_tool/src/nif_erlang.rs | 72 ++++++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 15 deletions(-) create mode 100644 rustler_tool/src/nif_elixir.rs create mode 100644 rustler_tool/src/nif_erlang.rs diff --git a/rustler_tool/src/fake_symbols.rs b/rustler_tool/src/fake_symbols.rs index f445d799..e77c866d 100644 --- a/rustler_tool/src/fake_symbols.rs +++ b/rustler_tool/src/fake_symbols.rs @@ -1,4 +1,4 @@ -use std::alloc::{Layout, alloc, dealloc}; +use std::alloc::{alloc, dealloc, Layout}; const HEADER: usize = 8; const ALIGNMENT: usize = 8; @@ -26,6 +26,8 @@ pub unsafe extern "C" fn enif_free(ptr: *mut u8) { #[no_mangle] pub static enif_alloc_binary: usize = 0; #[no_mangle] +pub static enif_alloc_resource: usize = 0; +#[no_mangle] pub static enif_alloc_env: usize = 0; #[no_mangle] pub static enif_binary_to_term: usize = 0; @@ -98,6 +100,8 @@ pub static enif_is_ref: usize = 0; #[no_mangle] pub static enif_is_tuple: usize = 0; #[no_mangle] +pub static enif_keep_resource: usize = 0; +#[no_mangle] pub static enif_make_atom_len: usize = 0; #[no_mangle] pub static enif_make_badarg: usize = 0; @@ -132,6 +136,10 @@ pub static enif_make_new_map: usize = 0; #[no_mangle] pub static enif_make_reverse_list: usize = 0; #[no_mangle] +pub static enif_make_resource: usize = 0; +#[no_mangle] +pub static enif_make_resource_binary: usize = 0; +#[no_mangle] pub static enif_make_sub_binary: usize = 0; #[no_mangle] pub static enif_make_tuple_from_array: usize = 0; @@ -158,12 +166,20 @@ pub static enif_realloc_binary: usize = 0; #[no_mangle] pub static enif_release_binary: usize = 0; #[no_mangle] +pub static enif_release_resource: usize = 0; +#[no_mangle] pub static enif_schedule_nif: usize = 0; #[no_mangle] pub static enif_self: usize = 0; #[no_mangle] +pub static enif_send: usize = 0; +#[no_mangle] pub static enif_snprintf: usize = 0; #[no_mangle] pub static enif_term_to_binary: usize = 0; #[no_mangle] pub static enif_term_type: usize = 0; +#[no_mangle] +pub static enif_thread_type: usize = 0; +#[no_mangle] +pub static enif_whereis_pid: usize = 0; diff --git a/rustler_tool/src/main.rs b/rustler_tool/src/main.rs index b977e80c..62d92011 100644 --- a/rustler_tool/src/main.rs +++ b/rustler_tool/src/main.rs @@ -1,6 +1,8 @@ #[cfg(unix)] mod fake_symbols; mod nif; +mod nif_elixir; +mod nif_erlang; use std::path::PathBuf; @@ -15,24 +17,44 @@ struct Cli { command: Option, } +#[derive(clap::ValueEnum, Clone, Default, Debug)] +enum OutputFormat { + #[default] + Bare, + Erlang, + Elixir, +} + #[derive(Subcommand)] enum Commands { /// does testing things - Nif { path: PathBuf }, + Nif { + path: PathBuf, + #[arg(short, long, default_value_t, value_enum)] + format: OutputFormat, + }, } fn main() { let cli = Cli::parse(); match &cli.command { - Some(Commands::Nif { path }) => { - println!("Extracting nifs from {:?}", path); - - let lib = NifLibrary::load(&path).unwrap(); + Some(Commands::Nif { path, format }) => { + let lib = NifLibrary::load(path).unwrap(); - println!("Found library {} with nifs", lib.name); - for nif in lib.nifs { - println!(" {}/{}", nif.name, nif.arity); + match format { + OutputFormat::Bare => { + println!("{}", lib.name); + for nif in lib.nifs { + println!(" {}/{}", nif.name, nif.arity); + } + } + OutputFormat::Erlang => { + println!("{}", nif_erlang::LibAsErlang(lib)) + } + OutputFormat::Elixir => { + println!("{}", nif_elixir::LibAsElixir(lib)) + } } } None => { diff --git a/rustler_tool/src/nif.rs b/rustler_tool/src/nif.rs index 673e1204..443a710f 100644 --- a/rustler_tool/src/nif.rs +++ b/rustler_tool/src/nif.rs @@ -39,24 +39,26 @@ unsafe fn maybe_call_nif_init( impl NifLibrary { pub fn load(path: &Path) -> Result> { unsafe { - let lib = Library::new(&path)?; + let lib = Library::new(path)?; let entry = maybe_call_nif_init(&lib)?; let name = CStr::from_ptr((*entry).name).to_str()?.to_string(); let nif_array = std::slice::from_raw_parts((*entry).funcs, (*entry).num_of_funcs as usize); - let nifs = nif_array - .into_iter() + let mut nifs: Vec<_> = nif_array + .iter() .filter_map(|f| { Some(Nif { - name: CStr::from_ptr((*f).name).to_str().ok()?.to_string(), - arity: (*f).arity as usize, - flags: (*f).flags as usize, + name: CStr::from_ptr(f.name).to_str().ok()?.to_string(), + arity: f.arity as usize, + flags: f.flags as usize, }) }) .collect(); + nifs.sort_by_key(|x| x.name.clone()); + Ok(NifLibrary { path: path.to_path_buf(), name, diff --git a/rustler_tool/src/nif_elixir.rs b/rustler_tool/src/nif_elixir.rs new file mode 100644 index 00000000..db106af8 --- /dev/null +++ b/rustler_tool/src/nif_elixir.rs @@ -0,0 +1,62 @@ +use crate::NifLibrary; +use std::fmt; + +pub struct LibAsElixir(pub NifLibrary); + +impl fmt::Display for LibAsElixir { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + writeln!( + f, + "defmodule {} do", + string_to_elixir_atom(&self.0.name, false) + )?; + + for nif in &self.0.nifs { + write!(f, " def {}(", string_to_elixir_atom(&nif.name, true))?; + for i in 0..nif.arity { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "_")?; + } + writeln!(f, "), do: :erlang.nif_error(not_loaded)")?; + } + writeln!(f, "end") + } +} + +fn string_to_elixir_atom(s: &str, func: bool) -> String { + match s { + "false" | "true" | "nil" => s.to_string(), + _ if s.starts_with("Elixir.") => s[7..].to_string(), + _ => { + let mut output = String::new(); + let mut needs_quotes = false; + for c in s.chars() { + match c { + 'a'..='z' | 'A'..='Z' | '0'..='9' | '@' | '_' => output.push(c), + '"' => { + needs_quotes = true; + output.push_str("\\\""); + } + '\\' => { + needs_quotes = true; + output.push_str(r"\\"); + } + _ => { + needs_quotes = true; + output.push(c); + } + } + } + + if needs_quotes { + format!(":\"{}\"", output).to_string() + } else if !func { + format!(":{}", output).to_string() + } else { + output + } + } + } +} diff --git a/rustler_tool/src/nif_erlang.rs b/rustler_tool/src/nif_erlang.rs new file mode 100644 index 00000000..8f0aa968 --- /dev/null +++ b/rustler_tool/src/nif_erlang.rs @@ -0,0 +1,72 @@ +use crate::NifLibrary; +use std::fmt; + +pub struct LibAsErlang(pub NifLibrary); + +impl fmt::Display for LibAsErlang { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "-module({}).\n\n", string_to_erlang_atom(&self.0.name))?; + writeln!(f, "-export([")?; + let count = self.0.nifs.len(); + for (n, nif) in self.0.nifs.iter().enumerate() { + write!(f, " {}/{}", string_to_erlang_atom(&nif.name), nif.arity)?; + if n == count - 1 { + write!(f, ",")?; + } + writeln!(f)?; + } + write!(f, "]).\n\n")?; + + // TODO: On Load function + + for nif in &self.0.nifs { + write!(f, "{}(", string_to_erlang_atom(&nif.name))?; + for i in 0..nif.arity { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "_")?; + } + write!(f, ") ->\n erlang:nif_error(not_loaded).\n\n")?; + } + + Ok(()) + } +} + +fn string_to_erlang_atom(input: &str) -> String { + let mut output = String::with_capacity(input.len()); + let mut needs_quotes = false; + + let mut first = true; + + for c in input.chars() { + match c { + 'A'..='Z' if first => { + needs_quotes = true; + output.push(c); + } + 'a'..='z' | 'A'..='Z' | '0'..='9' | '@' | '_' => output.push(c), + '\'' => { + needs_quotes = true; + output.push_str(r"\'"); + } + '\\' => { + needs_quotes = true; + output.push_str(r"\\"); + } + _ => { + needs_quotes = true; + output.push(c); + } + } + + first = false; + } + + if needs_quotes { + format!("'{}'", output).to_string() + } else { + output + } +}