cargo fmt, fix build with variants, more build logging

Signed-off-by: strawberry <strawberry@pupbrain.dev>
This commit is contained in:
strawberry 2023-11-12 18:14:46 -05:00
parent 3f517c73c8
commit 3169b1e6fd
3 changed files with 54 additions and 39 deletions

View file

@ -2,11 +2,16 @@
the sys repo, rust wrapper the sys repo, rust wrapper
to build, just run `cargo build -r` which will build the light variant by default.
if you want default (called `standard` in this crate) variant, do `cargo build -r --features=standard --no-default-features`
### TODO: ### TODO:
- [ ] test if this even works - [ ] test if this even works
- [ ] add support for explicit make config args on top of choosing variant - [ ] add support for explicit make config args on top of choosing variant
- [ ] make build script better overall - [ ] make build script better overall
- [ ] support C preprocessor macro definitions - [ ] support C preprocessor macro definitions
- [ ] maybe add support for building both variants if both are specified, or dont use a default light variant
- [ ] add support for hardened_malloc `make clean` upon `cargo clean`
- [ ] potentially add support for cross-compiling so i can build on apple silicon for linux x86? - [ ] potentially add support for cross-compiling so i can build on apple silicon for linux x86?
- [ ] add support for hardened_malloc's tests and our own tests - [ ] add support for hardened_malloc's tests and our own tests
- [ ] add github CI/CD - [ ] add github CI/CD

View file

@ -1,49 +1,60 @@
use std::{env, process::Command, path::Path}; use std::{
env::{self, current_dir},
path::Path,
process::Command,
};
/// If submodules were not synced, sync them to actually build hardened_malloc /// If submodules were not synced, sync them to actually build hardened_malloc
fn update_submodules() { fn update_submodules() {
let program = "git"; let program = "git";
let dir = "../";
let args = ["submodule", "update", "--init", "--recursive"]; let args = ["submodule", "update", "--init", "--recursive"];
println!( println!(
"[hardened_malloc-sys]: Running command: \"{} {}\" in directory: {}", "[hardened_malloc-sys]: Running command: \"{} {}\" in directory: {:?}",
program, program,
args.join(" "), args.join(" "),
dir current_dir(),
); );
let ret = Command::new(program).current_dir(dir).args(args).status(); let ret = Command::new(program).args(args).status();
match ret.map(|status| (status.success(), status.code())) { match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => (), Ok((true, _)) => (),
Ok((false, Some(c))) => panic!("[hardened_malloc-sys]: Command failed with error code {}", c), Ok((false, Some(c))) => panic!(
Ok((false, None)) => panic!("[hardened_malloc-sys]: Command exited with no error code, possibly killed by system"), "[hardened_malloc-sys]: Command failed with error code {}",
c
),
Ok((false, None)) => panic!(
"[hardened_malloc-sys]: Command exited with no error code, possibly killed by system"
),
Err(e) => panic!("[hardened_malloc-sys]: Command failed with error: {}", e), Err(e) => panic!("[hardened_malloc-sys]: Command failed with error: {}", e),
} }
} }
fn main() { fn main() {
if !Path::new("src/hardened_malloc/Makefile").exists() { if !Path::new("src/hardened_malloc/Makefile").exists() {
println!("src/hardened_malloc/Makefile does not exist, running submodule sync");
update_submodules(); update_submodules();
} }
let variant: &str; let variant;
if cfg!(feature = "light") { if cfg!(feature = "light") {
variant = "light"; variant = "light";
} else { } else {
variant = "default"; variant = "default"; // "default" is hardened_malloc's default.mk. this crate's feature uses "standard" for "default"
} }
let build_args = ["VARIANT=".to_owned() + variant, "V=".to_owned() + "1"];
//TODO: handle support for explicit make flags like N_ARENA=1 and such //TODO: handle support for explicit make flags like N_ARENA=1 and such
let mut make_command = Command::new("make"); let mut make_command = Command::new("make");
println!("running {:?} with args {:?}", make_command, build_args);
let make_output = make_command let make_output = make_command
.current_dir("src/hardened_malloc/") .current_dir("src/hardened_malloc/")
.env("V", "1") // always verbose mode for cargo .args(build_args)
.env("VARIANT", variant) .output()
.output() .unwrap_or_else(|error| {
.unwrap_or_else(|error| { panic!("Failed to run 'make {}': ", error);
panic!("Failed to run 'make {}': ", error); });
});
if !make_output.status.success() { if !make_output.status.success() {
panic!( panic!(
"[hardened_malloc-sys]: building hardened_malloc failed:\n{:?}\n{}\n{}", "[hardened_malloc-sys]: building hardened_malloc failed:\n{:?}\n{}\n{}",
@ -54,8 +65,8 @@ fn main() {
} }
//println!("cargo:rustc-link-search=native=src/hardened_malloc"); //println!("cargo:rustc-link-search=native=src/hardened_malloc");
//println!("cargo:rerun-if-changed=build.rs"); //println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/hardened_malloc/"); println!("cargo:rerun-if-changed=src/hardened_malloc/");
//println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap()); //println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap());
} }

View file

@ -1,23 +1,23 @@
#![no_std] #![no_std]
use core::ffi::{c_void, c_int}; use core::ffi::{c_int, c_void};
extern crate libc; extern crate libc;
extern "C" { extern "C" {
/* /*
TODO: implement this TODO: implement this
#ifdef __ANDROID__ #ifdef __ANDROID__
#define H_MALLOC_USABLE_SIZE_CONST const #define H_MALLOC_USABLE_SIZE_CONST const
#else #else
#define H_MALLOC_USABLE_SIZE_CONST #define H_MALLOC_USABLE_SIZE_CONST
#endif #endif
for: for:
// glibc extensions // glibc extensions
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr); size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
*/ */
/* C standard */ /* C standard */
@ -47,13 +47,13 @@ size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
pub fn h_malloc_set_state(ptr: *mut c_void) -> c_int; pub fn h_malloc_set_state(ptr: *mut c_void) -> c_int;
/*TODO: implement this see the top: /*TODO: implement this see the top:
#if defined(__GLIBC__) || defined(__ANDROID__) #if defined(__GLIBC__) || defined(__ANDROID__)
struct mallinfo h_mallinfo(void); struct mallinfo h_mallinfo(void);
#endif #endif
#ifndef __ANDROID__ #ifndef __ANDROID__
int h_malloc_info(int options, FILE *fp); int h_malloc_info(int options, FILE *fp);
#endif #endif
*/ */
/* hardened_malloc extensions */ /* hardened_malloc extensions */
@ -63,7 +63,6 @@ int h_malloc_info(int options, FILE *fp);
/// similar to malloc_object_size, but avoids locking so the results are much more limited /// similar to malloc_object_size, but avoids locking so the results are much more limited
pub fn h_malloc_object_size_fast(ptr: *const c_void) -> usize; pub fn h_malloc_object_size_fast(ptr: *const c_void) -> usize;
/// The free function with an extra parameter for passing the size requested at /// The free function with an extra parameter for passing the size requested at
/// allocation time. /// allocation time.
/// ///
@ -75,4 +74,4 @@ int h_malloc_info(int options, FILE *fp);
/// allocator implementation uses it to improve security by checking that the /// allocator implementation uses it to improve security by checking that the
/// passed size matches the allocated size. /// passed size matches the allocated size.
pub fn h_free_sized(ptr: *mut c_void, expected_size: usize) -> c_void; pub fn h_free_sized(ptr: *mut c_void, expected_size: usize) -> c_void;
} }