Skip to content
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

Make unprefixed consistently override the system allocator #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

madsmtm
Copy link

@madsmtm madsmtm commented Dec 15, 2024

The goal is to move the various workarounds for this feature in rustc to the jemalloc-sys crate instead.

I'm not entirely sure of the history here, but it is possible this was not done in the past because #[used] used to not work in libraries? In any case, we should be able to do it now.

Note that due to rust-lang/rust#133491, #[used] in libraries still doesn't work on macOS.

I tried to write a test for this, but it's just a very platform-specific issue, and I found it hard to exercise properly. You should be able to test it in rustc with something like the following:

#[cfg(feature = "jemalloc")]
use tikv_jemalloc_sys as _;

#[cfg(feature = "jemalloc")]
#[cfg(target_vendor = "apple")]
#[used]
static USED_ZONE_REGISTER: unsafe extern "C" fn() = {
    extern "C" {
        fn _rjem_je_zone_register();
    }
    _rjem_je_zone_register
};

fn main() {
    rustc_driver::main()
}

Copy link

ti-chi-bot bot commented Dec 15, 2024

Welcome @madsmtm! It looks like this is your first PR to tikv/jemallocator 🎉

use super::*;

#[used]
static USED_MALLOC: unsafe extern "C" fn(usize) -> *mut c_void = malloc;
Copy link
Member

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?

Copy link
Author

@madsmtm madsmtm Dec 16, 2024

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 for ld64:

A static library (aka static archive) is a collection of .o files with a table of contents that lists the global symbols in the .o files. ld will only pull .o files out of a static library if needed to resolve some symbol reference. Unlike traditional linkers, ld will continually search a static library while linking.

(Note that Rust .rlibs are internally archives / static libraries, and so the rules for static libaries apply to them as well).

Copy link
Author

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?

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants