-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
global_allocator does not work with -C prefer-dynamic #100781
Comments
Does this also happen with |
Non-UB (I believe) example that fails for me (SIGSEGV normally, completes successfully with use std::alloc::{GlobalAlloc, Layout};
#[global_allocator]
static ALLOC: Alloc = Alloc;
pub struct Alloc;
#[allow(unconditional_recursion)]
fn abort() -> ! {
abort()
}
unsafe impl GlobalAlloc for Alloc {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
abort()
}
unsafe fn alloc_zeroed(&self, _layout: Layout) -> *mut u8 {
abort()
}
unsafe fn realloc(&self, _ptr: *mut u8, _layout: Layout, _new_size: usize) -> *mut u8 {
abort()
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
abort()
}
}
fn main() {
let _ = Box::new(1);
} |
You can't use |
That's a very interesting case. Now, calls to GlobalAlloc are allowed to be optimized out, so this behavior isn't technically illegal, but it's certainly very odd and deserves some deeper investigation. |
Optimization is not the cause of what is going on. Also the allocator will be called before the |
It seems that #112794 accidentally fixed this. I was under the impression that rustc would error out when using |
Not really, iirc. You could shim something by having the dll manually get an exported symbol from the exe when the dll is loaded. I'm not sure how I feel about that though. |
Would making usage of |
That sounds reasonable to me. And it doesn't rule out doing something differently in the future. |
At minimum we need to be issuing a diagnostic here, and we can FCP erroring on top. |
@bjorn3 how would we do this? @Zoxc Would any of this impact your experiments with improving rustc's allocator linkage? It's not acceptable for Rust language features like |
rust/compiler/rustc_codegen_ssa/src/base.rs Lines 586 to 601 in 773fb88
|
There should be logic to ensure that the crate graph only has 1 global allocator and give an error. That rule is not broken when using custom allocators in rustc. |
That logic only checks for explicit global allocators. It doesn't consider the allocator shim that is generated for rust dylibs. |
On Mach-O based systems, weak symbols are exempted from two-level namespacing. Overriding with a strong definition should work as long as:
There's also a more 'manual' way this could be done. The image containing the default implementation could also define a function pointer global variable, initialized to point to the default implementation. rustc would change all callers to use the function pointer instead of doing direct calls. And for an image that wanted to override the implementation, rustc would add a static initializer function that overwrites the function pointer. That should work on all platforms. But it's a bit iffy from a security perspective (you lose the benefit of RELRO and related mitigations). |
Could not find anything related to this online. Using
RUSTFLAGS="-C prefer-dynamic" cargo run
on this does not panic.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: