Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
eb0fc72
Starting mimalloc integration with libresolve. This commit contains m…
Jun 1, 2026
e324817
Dockerfile: Removed mimalloc build from dockerfile.
Jun 1, 2026
4373a36
WIP: Moved mimalloc build into CMake workflow. I will make sure to do…
Jun 1, 2026
95993c8
CMakeLists.txt: Fixed CMake file to correctly pass path to static mim…
Jun 1, 2026
e742a9e
remediate.rs: Fixed test but we need to test this on a small file to …
Jun 1, 2026
1b952ca
shadowobjs.rs: Cleaned up some comments.
Jun 2, 2026
3fe197c
mimalloc_shadow.c: Modified shim to lookup bounds of mimalloc allocat…
Jun 4, 2026
efaa4a1
WIP: Testing bounds-checking with mimalloc.
Jun 4, 2026
5ec89ad
remediate.rs: This will not pass CI tests, this is just so that I can…
Jun 5, 2026
136fa04
remediate.rs: Uncomment no_mangle attribute and comment logging comma…
Jun 5, 2026
4736ae4
remediate.rs: Adding null pointer handling might fix compilation issu…
Jun 5, 2026
3300c7a
remediate.rs: Adding wrapper for getline function. Testing with chall…
Jun 8, 2026
be15726
remediate.rs: Fixing Rust errors.
Jun 8, 2026
9ef62e8
remediate.rs: Removed the plus one and using resolve wrapped malloc a…
Jun 8, 2026
3b9382c
remediate.rs: Added resolve wrapper for getdelim function.
Jun 8, 2026
9456e38
remediate.rs: Added some logic to identify if the pointer is owned by…
Jun 8, 2026
b60a283
remediate.rs: WIP debugging foreign allocation that occurs in depende…
Jun 9, 2026
e78ea02
remediate.rs: WIP debuggin allocator ownership issues.
Jun 9, 2026
cfe5238
remediate.rs: Removed logging to test performance.
Jun 9, 2026
b2ecfd9
remediate.rs: Make sure all logging is commented out for poller testing.
Jun 12, 2026
6e43fe2
remediate.rs: Comment out all logging.
Jun 12, 2026
317e645
remediate.rs: Commenting out extra line that caused bad compilation.
Jun 12, 2026
735b5e1
remediate.rs: Restored commit 48dbed5 for testing.
Jun 12, 2026
907fbdf
remediate.rs: Fixing remediate.rs since I lost some of the commits to…
Jun 12, 2026
f282321
remediate.rs: Adding logic to call libc free if the pointer does not …
Jun 12, 2026
ddab667
remediate.rs: Fixing issues in compilation.
Jun 12, 2026
7a13525
mimalloc_shadow.c: Switching to mi_is_in_heap to better handle arbitr…
Jun 12, 2026
93415fb
WIP: Testing if the unknown pointer comes from C++ new/delete allocator.
Jun 12, 2026
1a4f7c3
WIP: Fixing compilation issues. Testing if allocation comes from C++ …
Jun 12, 2026
044acb6
remediate.rs: Debugging __resolve_free.
Jun 15, 2026
7b544e8
WIP: Modified Cargo.toml to have cargo build with nightly and added _…
Jun 15, 2026
31df459
remediate.rs: WIP testing if __resolve_asprintf symbol shows up in li…
Jun 15, 2026
ea3de0b
remediate.rs: WIP trying to get libresolve to resolve the __resolve_a…
Jun 15, 2026
e3aaae0
WIP: try adding no_mangle to mimalloc symbols to have libresolve expo…
Jun 15, 2026
9ac8127
WIP: Trying to debug why the __resolve_asprintf symbol is not being r…
Jun 15, 2026
6808c74
remediate.rs: WIP debugging __resolve_asprintf symbol export.
Jun 15, 2026
dd295e4
WIP: debugging __resolve_asprintf symbol visibility and resolution.
Jun 15, 2026
f665322
Fixing compilation issues. Ready to test __resolve_asprintf symbol re…
Jun 15, 2026
9fc5943
WIP: Debugging ABI mismatch issues.
Jun 15, 2026
d63d868
mimalloc_shadow.c: Removed the plus one from len argument in __resolv…
Jun 15, 2026
d82f7e1
remediate.rs: Adding free to see what happens.
Jun 15, 2026
0bfc51d
remediate.rs: Adding resolve wrappers for posx_memalign, reallocarray…
Jun 16, 2026
af20b68
remediate.rs: Testing with libc free.
Jun 16, 2026
726c366
remediate.rs: Comment out logging to test for performance.
Jun 16, 2026
695c157
remediate.rs: Testing without +1 padding.
Jun 16, 2026
06315f0
remediate.rs: WIP skipping pointers that have not been allocated by m…
Jun 16, 2026
5991b59
mimalloc_shadow.c: Testing __resolve_malloc call in __vasprintf
Jun 16, 2026
7af7c59
remediate.rs: Reverting commit that added ptr to strdup ref link.
Jun 16, 2026
1970dad
mimalloc_shadow.c: Modified mi_is_heap_owned fn to return a boolean i…
Jun 18, 2026
145c7e7
mimalloc_shadow.c: Adding +1 back to __resolve_malloc call.
Jun 18, 2026
5916a17
remediate.rs: WIP checking the ptr and return address that is passed …
Jun 18, 2026
71c28de
remediate.rs: Testing with warn.
Jun 18, 2026
d3957df
shadowobjs.rs: Fixing compilation issue.
Jun 18, 2026
9a06e2b
CMakeLists.txt: Fixing CMakeList file to correctly pass archive path …
Jun 18, 2026
d0aaed4
remediate.rs: Try using the mi_is_in_heap_region function to check if…
Jun 18, 2026
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
38 changes: 35 additions & 3 deletions resolve-cveassert/libresolve/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@

include(GNUInstallDirs)
include(ExternalProject)
include(FetchContent)

#### Fetching and building mimalloc project
FetchContent_Declare(
mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG v3.3.2
)

## Force static build
set(MI_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(MI_BUILD_STATIC ON CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(mimalloc)

# Add the shim source to the mimalloc proj
target_sources(mimalloc-static PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mimalloc_shadow.c
)

## mimalloc proj must be PIC to ensure compatibility with libresolve
set_target_properties(mimalloc-static PROPERTIES
POSITION_INDEPENDENT_CODE ON
OUTPUT_NAME mimalloc
)

# Map CMake build type to Cargo flags
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "")
Expand All @@ -24,17 +49,24 @@ file(GLOB_RECURSE RUST_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/sr
add_custom_command(
OUTPUT ${RUST_LIB}
COMMAND
${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${RUST_OUT_DIR} cargo build ${CARGO_FLAGS}
${CMAKE_COMMAND} -E env
CARGO_TARGET_DIR=${RUST_OUT_DIR}
MIMALLOC_LIB_DIR=$<TARGET_FILE_DIR:mimalloc-static>
cargo build ${CARGO_FLAGS}
WORKING_DIRECTORY ${RUST_CRATE_DIR}
COMMENT "Building libresolve.so" VERBATIM
DEPENDS ${RUST_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.lock ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml ${CMAKE_CURRENT_SOURCE_DIR}/rust-toolchain.toml
DEPENDS mimalloc-static ${RUST_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.lock ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml ${CMAKE_CURRENT_SOURCE_DIR}/rust-toolchain.toml
)

add_custom_target(test-libresolve
COMMAND cargo test
COMMAND
${CMAKE_COMMAND} -E env
MIMALLOC_LIB_DIR=${MIMALLOC_LIB_DIR} # Note: Pass the static mimalloc loc so that build.rs does not panic
cargo test
WORKING_DIRECTORY ${RUST_CRATE_DIR}
COMMENT "Running regression tests for libresolve"
)

add_custom_target(libresolve ALL DEPENDS ${RUST_LIB})
add_dependencies(libresolve mimalloc-static)
install(FILES ${RUST_LIB} DESTINATION ${CMAKE_INSTALL_LIBDIR})
23 changes: 23 additions & 0 deletions resolve-cveassert/libresolve/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions resolve-cveassert/libresolve/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[package]
name = "resolve"
build = "src/build.rs"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
cc = "1.2.63"
env_logger = "0.11.8"
libc = "0.2.174"
log = "0.4.29"
Expand All @@ -18,3 +20,5 @@ debug = true
[profile.dev]
panic = "abort"

[toolchain]
channel = "nightly"
82 changes: 82 additions & 0 deletions resolve-cveassert/libresolve/mimalloc_shadow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "mimalloc.h"
#include "mimalloc/internal.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>

extern void* __resolve_malloc(size_t);
extern void __resolve_free(void*);


typedef struct {
void *base;
void *limit;
size_t block_size;
size_t block_index;
} bounds_info_t;


bounds_info_t mi_resolve_ptr(void* p) {
// Can return null if ptr is not owned by mimalloc
mi_page_t *page = _mi_ptr_page(p);

const size_t block_size = page->block_size;

uintptr_t page_start = (uintptr_t)page->page_start;
uintptr_t ptr = (uintptr_t)p;

size_t block_index = (ptr - page_start) / block_size;
uintptr_t base_addr = page_start + block_index * block_size;

bounds_info_t bounds;
bounds.base = (void*)base_addr;
bounds.limit = (void*)(base_addr + block_size);
bounds.block_size = block_size;
bounds.block_index = block_index;
return bounds;
}

bool mi_is_heap_owned(const void* p) {
return _mi_ptr_page(p) != NULL;
}

int __vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap_copy;
va_copy(ap_copy, ap);

int len = vsnprintf(NULL, 0, fmt, ap_copy);
va_end(ap_copy);

if (len < 0) {
// to match glibc behavior
*strp = NULL;
return -1;
}

char *buf = __resolve_malloc((size_t)len + 1);
if (!buf) { return -1; }

va_copy(ap_copy, ap);

int written = vsnprintf(buf, (size_t)len + 1, fmt, ap_copy);

va_end(ap_copy);

if (written < 0) {
__resolve_free(buf);
return -1;
}

*strp = buf;
return written;
}

void *resolve_return_address(unsigned level) {
switch(level) {
case 0: return __builtin_return_address(0);
case 1: return __builtin_return_address(1);
case 2: return __builtin_return_address(2);
default: return NULL;
}
}
6 changes: 6 additions & 0 deletions resolve-cveassert/libresolve/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let dir = std::env::var("MIMALLOC_LIB_DIR").unwrap();
println!("cargo::warning=LINKING_MIMALLOC_ARCHIVE");
println!("cargo::rustc-link-search=native={}", dir);
println!("cargo::rustc-link-lib=static=mimalloc");
}
21 changes: 10 additions & 11 deletions resolve-cveassert/libresolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// LGPL-3; See LICENSE.txt in the repo root for details.

#![feature(btree_cursors)]

#![feature(c_variadic)]
mod remediate;
mod shadowobjs;
mod trace;
Expand All @@ -11,8 +11,8 @@ use libc::{Dl_info, atexit, c_void, dladdr, dlsym};
use std::ffi::{CStr, OsString};
use std::fmt::Display;
use std::fs::{self, File};
use std::path::PathBuf;
use std::io::{self, Seek, Write};
use std::path::PathBuf;
use std::sync::{LazyLock, Mutex};
use std::{env, process};

Expand All @@ -34,23 +34,23 @@ impl<T> MutexWrap<T> {
}

fn idify_file_path(path: &mut PathBuf, id: impl Display) {
let file_name = path.file_name()
let file_name = path
.file_name()
.expect("Path could not be found in file system.")
.to_owned();

let mut updated_file_name = OsString::new();

updated_file_name.push(file_name);
updated_file_name.push("-");
updated_file_name.push(id.to_string());
updated_file_name.push(id.to_string());

path.set_file_name(updated_file_name);
}

/// File for "resolve_dlsym.json"
pub static DLSYM_LOG_FILE: LazyLock<MutexWrap<File>> = LazyLock::new(|| {
let log_dir = env::var("RESOLVE_DLSYM_LOG_DIR")
.unwrap_or_else(|_| ".".to_string());
let log_dir = env::var("RESOLVE_DLSYM_LOG_DIR").unwrap_or_else(|_| ".".to_string());

let mut path = PathBuf::from(log_dir);

Expand Down Expand Up @@ -84,27 +84,26 @@ pub extern "C" fn resolve_init() {
if cfg!(test) {
builder.is_test(true);
} else {
let file = open_resolve_log_file().unwrap_or_else(|err| {
let file = open_resolve_log_file().unwrap_or_else(|err| {
eprintln!("Libresolve log file could not be created.");
eprintln!("Error: {err:?}");
process::exit(12);
});

builder.target(env_logger::Target::Pipe(Box::new(file)));
}

let _ = builder.try_init();
}

fn open_resolve_log_file() -> Result<File, io::Error> {
let log_dir = env::var("RESOLVE_RUNTIME_LOG_DIR")
.unwrap_or_else(|_| ".".to_string());
let log_dir = env::var("RESOLVE_RUNTIME_LOG_DIR").unwrap_or_else(|_| ".".to_string());

let mut path = PathBuf::from(log_dir);

// Ensure the parent directories exist
fs::create_dir_all(&path)?;

// Append the file name
path.push("resolve_log.out");

Expand Down
Loading
Loading