Skip to content

Commit e6d1a0e

Browse files
committed
Auto merge of #86844 - bjorn3:global_alloc_improvements, r=pnkfelix
Support #[global_allocator] without the allocator shim This makes it possible to use liballoc/libstd in combination with `--emit obj` if you use `#[global_allocator]`. This is what rust-for-linux uses right now and systemd may use in the future. Currently they have to depend on the exact implementation of the allocator shim to create one themself as `--emit obj` doesn't create an allocator shim. Note that currently the allocator shim also defines the oom error handler, which is normally required too. Once `#![feature(default_alloc_error_handler)]` becomes the only option, this can be avoided. In addition when using only fallible allocator methods and either `--cfg no_global_oom_handling` for liballoc (like rust-for-linux) or `--gc-sections` no references to the oom error handler will exist. To avoid this feature being insta-stable, you will have to define `__rust_no_alloc_shim_is_unstable` to avoid linker errors. (Labeling this with both T-compiler and T-lang as it originally involved both an implementation detail and had an insta-stable user facing change. As noted above, the `__rust_no_alloc_shim_is_unstable` symbol requirement should prevent unintended dependence on this unstable feature.)
2 parents a3b816b + 2253e86 commit e6d1a0e

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

src/allocator.rs

+45-34
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
use crate::prelude::*;
55

6-
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
6+
use rustc_ast::expand::allocator::{
7+
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
8+
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
9+
};
710
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
811
use rustc_session::config::OomStrategy;
9-
use rustc_span::symbol::sym;
1012

1113
/// Returns whether an allocator shim was created
1214
pub(crate) fn codegen(
@@ -34,41 +36,43 @@ fn codegen_inner(
3436
) {
3537
let usize_ty = module.target_config().pointer_type();
3638

37-
for method in ALLOCATOR_METHODS {
38-
let mut arg_tys = Vec::with_capacity(method.inputs.len());
39-
for ty in method.inputs.iter() {
40-
match *ty {
41-
AllocatorTy::Layout => {
42-
arg_tys.push(usize_ty); // size
43-
arg_tys.push(usize_ty); // align
44-
}
45-
AllocatorTy::Ptr => arg_tys.push(usize_ty),
46-
AllocatorTy::Usize => arg_tys.push(usize_ty),
39+
if kind == AllocatorKind::Default {
40+
for method in ALLOCATOR_METHODS {
41+
let mut arg_tys = Vec::with_capacity(method.inputs.len());
42+
for ty in method.inputs.iter() {
43+
match *ty {
44+
AllocatorTy::Layout => {
45+
arg_tys.push(usize_ty); // size
46+
arg_tys.push(usize_ty); // align
47+
}
48+
AllocatorTy::Ptr => arg_tys.push(usize_ty),
49+
AllocatorTy::Usize => arg_tys.push(usize_ty),
4750

48-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
51+
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
52+
}
4953
}
50-
}
51-
let output = match method.output {
52-
AllocatorTy::ResultPtr => Some(usize_ty),
53-
AllocatorTy::Unit => None,
54+
let output = match method.output {
55+
AllocatorTy::ResultPtr => Some(usize_ty),
56+
AllocatorTy::Unit => None,
5457

55-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
56-
panic!("invalid allocator output")
57-
}
58-
};
58+
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
59+
panic!("invalid allocator output")
60+
}
61+
};
5962

60-
let sig = Signature {
61-
call_conv: module.target_config().default_call_conv,
62-
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
63-
returns: output.into_iter().map(AbiParam::new).collect(),
64-
};
65-
crate::common::create_wrapper_function(
66-
module,
67-
unwind_context,
68-
sig,
69-
&format!("__rust_{}", method.name),
70-
&kind.fn_name(method.name),
71-
);
63+
let sig = Signature {
64+
call_conv: module.target_config().default_call_conv,
65+
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
66+
returns: output.into_iter().map(AbiParam::new).collect(),
67+
};
68+
crate::common::create_wrapper_function(
69+
module,
70+
unwind_context,
71+
sig,
72+
&global_fn_name(method.name),
73+
&default_fn_name(method.name),
74+
);
75+
}
7276
}
7377

7478
let sig = Signature {
@@ -81,7 +85,7 @@ fn codegen_inner(
8185
unwind_context,
8286
sig,
8387
"__rust_alloc_error_handler",
84-
&alloc_error_handler_kind.fn_name(sym::oom),
88+
&alloc_error_handler_name(alloc_error_handler_kind),
8589
);
8690

8791
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
@@ -90,4 +94,11 @@ fn codegen_inner(
9094
let val = oom_strategy.should_panic();
9195
data_ctx.define(Box::new([val]));
9296
module.define_data(data_id, &data_ctx).unwrap();
97+
98+
let data_id =
99+
module.declare_data(NO_ALLOC_SHIM_IS_UNSTABLE, Linkage::Export, false, false).unwrap();
100+
let mut data_ctx = DataContext::new();
101+
data_ctx.set_align(1);
102+
data_ctx.define(Box::new([0]));
103+
module.define_data(data_id, &data_ctx).unwrap();
93104
}

0 commit comments

Comments
 (0)