forked from gnzlbg/jemallocator
-
Notifications
You must be signed in to change notification settings - Fork 89
Make unprefixed consistently override the system allocator #109
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
Open
madsmtm
wants to merge
2
commits into
tikv:main
Choose a base branch
from
madsmtm:used-unprefixed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
[workspace] | ||
members = ["jemallocator", "jemallocator-global", "jemalloc-ctl", "jemalloc-sys"] | ||
members = [ | ||
"jemallocator", | ||
"jemallocator-global", | ||
"jemalloc-ctl", | ||
"jemalloc-sys", | ||
"test-dylib", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "test-dylib" | ||
version = "0.0.0" | ||
license = "MIT OR Apache-2.0" | ||
description = "A test helper for jemalloc-sys" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
libc = { version = "^0.2.8", default-features = false } | ||
tikv-jemalloc-sys = { path = "../jemalloc-sys" } | ||
|
||
[build-dependencies] | ||
cc = "^1.0.13" | ||
|
||
[features] | ||
unprefixed_malloc_on_supported_platforms = [ | ||
"tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms", | ||
] | ||
|
||
[[bin]] | ||
name = "test-dylib" | ||
test = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! Build shared library `dep.c`. | ||
use std::{env, path::PathBuf}; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=src/dep.c"); | ||
|
||
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); | ||
|
||
// NOTE: Only for testing, extension is wrong when cross-compiling. | ||
let dylib = out_dir.join(format!( | ||
"{}dep{}", | ||
env::consts::DLL_PREFIX, | ||
env::consts::DLL_SUFFIX | ||
)); | ||
|
||
let status = cc::Build::new() | ||
.get_compiler() | ||
.to_command() | ||
.arg("src/dep.c") | ||
.arg("-shared") | ||
.arg("-o") | ||
.arg(&dylib) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
|
||
println!("cargo:rustc-link-lib=dylib=dep"); | ||
println!("cargo:rustc-link-search=native={}", out_dir.display()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#define _GNU_SOURCE | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <dlfcn.h> | ||
|
||
const char* lookup_malloc_address(void) { | ||
Dl_info info; | ||
if (!dladdr((void *)malloc, &info)) { | ||
printf("failed finding `malloc`\n"); | ||
abort(); | ||
} | ||
return info.dli_fname; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! Test that the symbol address of `malloc` in shared libraries come from | ||
//! the place we'd expect it to. | ||
use core::ffi::{c_char, CStr}; | ||
use std::{env, path::Path}; | ||
|
||
use tikv_jemalloc_sys as _; | ||
|
||
extern "C-unwind" { | ||
fn lookup_malloc_address() -> *const c_char; | ||
} | ||
|
||
fn main() { | ||
let actual = unsafe { CStr::from_ptr(lookup_malloc_address()).to_str().unwrap() }; | ||
|
||
if cfg!(target_vendor = "apple") { | ||
// macOS / Mach-O symbols are not overriden, they are hooked into with | ||
// `zone_register`. | ||
assert_eq!(actual, "/usr/lib/system/libsystem_malloc.dylib"); | ||
} else if cfg!(all(target_os = "linux", target_env = "gnu")) { | ||
if cfg!(feature = "unprefixed_malloc_on_supported_platforms") { | ||
// When unprefixed, `malloc` is loaded from the current exe. | ||
// `target/*/debug/test-dylib` | ||
let dir = env::current_dir().unwrap(); | ||
let exe = env::current_exe().unwrap(); | ||
assert_eq!(Path::new(actual), exe.strip_prefix(dir).unwrap()); | ||
} else if cfg!(target_arch = "x86_64") { | ||
// Otherwise, the system `libc` contains `malloc`. | ||
assert_eq!(actual, "/lib/x86_64-linux-gnu/libc.so.6"); | ||
} else if cfg!(target_arch = "x86") { | ||
assert_eq!(actual, "/lib/i386-linux-gnu/libc.so.6"); | ||
} else if cfg!(target_arch = "aarch64") { | ||
assert_eq!(actual, "/lib/aarch64-linux-gnu/libc.so.6"); | ||
} else { | ||
panic!("unknown architecture. {:?}", actual); | ||
} | ||
} else { | ||
panic!("unsupported platform for this test. {:?}", actual); | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any references on how these statics are processed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's the reference on
#[used]
, and the equivalent Clang attribute and GCC attribute.But these are somewhat vague, perhaps intentionally so as this is very much a linker concept? I don't really have a good reference on linkers, the best I can do is reference this piece of source code in
rustc
that talks about a workaround for static libs, and the following section from the manual page forld64
:(Note that Rust
.rlib
s are internally archives / static libraries, and so the rules for static libaries apply to them as well).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or if you have more specific questions about how things work then I can try to answer them, to the best of my ability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. How about adding a test to show it work as expected? You can add a dylib crate that allocs in the root directory and then add a test crate that links both the dylib and jemalloc-sys. If it works as expected the test shoud be able to use jemalloc's free to dealloc the pointer from dylib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay.
I found the closest thing to a reference link, and have rewritten the docs around it to hopefully be clearer.
I have also added two tests:
malloc_and_libc_are_interoperable_when_overridden
, which tests that the overriding actually works on macOS.test-dylib
, which tests that when linking a dylib, the symbol is correctly overridden. Note that I couldn't reproduce it with the current nightly, so something might have changed recently that makes this hack redundant nowadays? Unsure, though it doesn't hurt to have in any case.Failed CI run of the first commit with just the tests: https://github.com/madsmtm/jemallocator/actions/runs/15490515399
Successful CI run after the second commit: https://github.com/madsmtm/jemallocator/actions/runs/15490429305