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.)
This commit is contained in:
commit
a2b1646c59
@ -1,20 +1,28 @@
|
||||
use rustc_span::symbol::{sym, Symbol};
|
||||
|
||||
#[derive(Clone, Debug, Copy, HashStable_Generic)]
|
||||
#[derive(Clone, Debug, Copy, Eq, PartialEq, HashStable_Generic)]
|
||||
pub enum AllocatorKind {
|
||||
Global,
|
||||
Default,
|
||||
}
|
||||
|
||||
impl AllocatorKind {
|
||||
pub fn fn_name(&self, base: Symbol) -> String {
|
||||
match *self {
|
||||
AllocatorKind::Global => format!("__rg_{base}"),
|
||||
AllocatorKind::Default => format!("__rdl_{base}"),
|
||||
}
|
||||
pub fn global_fn_name(base: Symbol) -> String {
|
||||
format!("__rust_{base}")
|
||||
}
|
||||
|
||||
pub fn default_fn_name(base: Symbol) -> String {
|
||||
format!("__rdl_{base}")
|
||||
}
|
||||
|
||||
pub fn alloc_error_handler_name(alloc_error_handler_kind: AllocatorKind) -> &'static str {
|
||||
match alloc_error_handler_kind {
|
||||
AllocatorKind::Global => "__rg_oom",
|
||||
AllocatorKind::Default => "__rdl_oom",
|
||||
}
|
||||
}
|
||||
|
||||
pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable";
|
||||
|
||||
pub enum AllocatorTy {
|
||||
Layout,
|
||||
Ptr,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::util::check_builtin_macro_attribute;
|
||||
|
||||
use rustc_ast::expand::allocator::{
|
||||
AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
|
||||
global_fn_name, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
|
||||
};
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::{self as ast, AttrVec, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
|
||||
@ -40,8 +40,7 @@ pub fn expand(
|
||||
|
||||
// Generate a bunch of new items using the AllocFnFactory
|
||||
let span = ecx.with_def_site_ctxt(item.span);
|
||||
let f =
|
||||
AllocFnFactory { span, ty_span, kind: AllocatorKind::Global, global: item.ident, cx: ecx };
|
||||
let f = AllocFnFactory { span, ty_span, global: item.ident, cx: ecx };
|
||||
|
||||
// Generate item statements for the allocator methods.
|
||||
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
|
||||
@ -63,7 +62,6 @@ pub fn expand(
|
||||
struct AllocFnFactory<'a, 'b> {
|
||||
span: Span,
|
||||
ty_span: Span,
|
||||
kind: AllocatorKind,
|
||||
global: Ident,
|
||||
cx: &'b ExtCtxt<'a>,
|
||||
}
|
||||
@ -92,7 +90,7 @@ fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
|
||||
}));
|
||||
let item = self.cx.item(
|
||||
self.span,
|
||||
Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span),
|
||||
Ident::from_str_and_span(&global_fn_name(method.name), self.span),
|
||||
self.attrs(),
|
||||
kind,
|
||||
);
|
||||
|
@ -3,10 +3,12 @@
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
||||
use rustc_ast::expand::allocator::{
|
||||
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
|
||||
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
|
||||
};
|
||||
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
|
||||
use rustc_session::config::OomStrategy;
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
/// Returns whether an allocator shim was created
|
||||
pub(crate) fn codegen(
|
||||
@ -34,6 +36,7 @@ fn codegen_inner(
|
||||
) {
|
||||
let usize_ty = module.target_config().pointer_type();
|
||||
|
||||
if kind == AllocatorKind::Default {
|
||||
for method in ALLOCATOR_METHODS {
|
||||
let mut arg_tys = Vec::with_capacity(method.inputs.len());
|
||||
for ty in method.inputs.iter() {
|
||||
@ -66,10 +69,11 @@ fn codegen_inner(
|
||||
module,
|
||||
unwind_context,
|
||||
sig,
|
||||
&format!("__rust_{}", method.name),
|
||||
&kind.fn_name(method.name),
|
||||
&global_fn_name(method.name),
|
||||
&default_fn_name(method.name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let sig = Signature {
|
||||
call_conv: module.target_config().default_call_conv,
|
||||
@ -81,7 +85,7 @@ fn codegen_inner(
|
||||
unwind_context,
|
||||
sig,
|
||||
"__rust_alloc_error_handler",
|
||||
&alloc_error_handler_kind.fn_name(sym::oom),
|
||||
&alloc_error_handler_name(alloc_error_handler_kind),
|
||||
);
|
||||
|
||||
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
|
||||
@ -90,4 +94,11 @@ fn codegen_inner(
|
||||
let val = oom_strategy.should_panic();
|
||||
data_ctx.define(Box::new([val]));
|
||||
module.define_data(data_id, &data_ctx).unwrap();
|
||||
|
||||
let data_id =
|
||||
module.declare_data(NO_ALLOC_SHIM_IS_UNSTABLE, Linkage::Export, false, false).unwrap();
|
||||
let mut data_ctx = DataContext::new();
|
||||
data_ctx.set_align(1);
|
||||
data_ctx.define(Box::new([0]));
|
||||
module.define_data(data_id, &data_ctx).unwrap();
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
#[cfg(feature="master")]
|
||||
use gccjit::FnAttribute;
|
||||
use gccjit::{FunctionType, GlobalKind, ToRValue};
|
||||
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
||||
use rustc_ast::expand::allocator::{
|
||||
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
|
||||
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
|
||||
};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_session::config::OomStrategy;
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
use crate::GccContext;
|
||||
|
||||
@ -22,6 +24,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
let i8p = i8.make_pointer();
|
||||
let void = context.new_type::<()>();
|
||||
|
||||
if kind == AllocatorKind::Default {
|
||||
for method in ALLOCATOR_METHODS {
|
||||
let mut types = Vec::with_capacity(method.inputs.len());
|
||||
for ty in method.inputs.iter() {
|
||||
@ -44,7 +47,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
panic!("invalid allocator output")
|
||||
}
|
||||
};
|
||||
let name = format!("__rust_{}", method.name);
|
||||
let name = global_fn_name(method.name);
|
||||
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
@ -59,7 +62,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
// TODO(antoyo): emit unwind tables.
|
||||
}
|
||||
|
||||
let callee = kind.fn_name(method.name);
|
||||
let callee = default_fn_name(method.name);
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
@ -86,6 +89,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
|
||||
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
|
||||
}
|
||||
}
|
||||
|
||||
let types = [usize, usize];
|
||||
let name = "__rust_alloc_error_handler".to_string();
|
||||
@ -99,7 +103,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
|
||||
}
|
||||
|
||||
let callee = alloc_error_handler_kind.fn_name(sym::oom);
|
||||
let callee = alloc_error_handler_name(alloc_error_handler_kind);
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
@ -123,4 +127,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
|
||||
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
|
||||
let value = context.new_rvalue_from_int(i8, value as i32);
|
||||
global.global_set_initializer_rvalue(value);
|
||||
|
||||
let name = NO_ALLOC_SHIM_IS_UNSTABLE.to_string();
|
||||
let global = context.new_global(None, GlobalKind::Exported, i8, name);
|
||||
let value = context.new_rvalue_from_int(i8, 0);
|
||||
global.global_set_initializer_rvalue(value);
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
use crate::attributes;
|
||||
use libc::c_uint;
|
||||
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
||||
use rustc_ast::expand::allocator::{
|
||||
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
|
||||
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
|
||||
};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_session::config::{DebugInfo, OomStrategy};
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
use crate::debuginfo;
|
||||
use crate::llvm::{self, False, True};
|
||||
@ -29,6 +31,7 @@ pub(crate) unsafe fn codegen(
|
||||
let i8p = llvm::LLVMPointerType(i8, 0);
|
||||
let void = llvm::LLVMVoidTypeInContext(llcx);
|
||||
|
||||
if kind == AllocatorKind::Default {
|
||||
for method in ALLOCATOR_METHODS {
|
||||
let mut args = Vec::with_capacity(method.inputs.len());
|
||||
for ty in method.inputs.iter() {
|
||||
@ -57,8 +60,9 @@ pub(crate) unsafe fn codegen(
|
||||
args.len() as c_uint,
|
||||
False,
|
||||
);
|
||||
let name = format!("__rust_{}", method.name);
|
||||
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
|
||||
let name = global_fn_name(method.name);
|
||||
let llfn =
|
||||
llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
|
||||
|
||||
if tcx.sess.target.default_hidden_visibility {
|
||||
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
||||
@ -68,7 +72,7 @@ pub(crate) unsafe fn codegen(
|
||||
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
|
||||
}
|
||||
|
||||
let callee = kind.fn_name(method.name);
|
||||
let callee = default_fn_name(method.name);
|
||||
let callee =
|
||||
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
|
||||
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
|
||||
@ -99,6 +103,7 @@ pub(crate) unsafe fn codegen(
|
||||
}
|
||||
llvm::LLVMDisposeBuilder(llbuilder);
|
||||
}
|
||||
}
|
||||
|
||||
// rust alloc error handler
|
||||
let args = [usize, usize]; // size, align
|
||||
@ -118,7 +123,7 @@ pub(crate) unsafe fn codegen(
|
||||
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
|
||||
}
|
||||
|
||||
let callee = alloc_error_handler_kind.fn_name(sym::oom);
|
||||
let callee = alloc_error_handler_name(alloc_error_handler_kind);
|
||||
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
|
||||
// -> ! DIFlagNoReturn
|
||||
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
|
||||
@ -156,6 +161,14 @@ pub(crate) unsafe fn codegen(
|
||||
let llval = llvm::LLVMConstInt(i8, val as u64, False);
|
||||
llvm::LLVMSetInitializer(ll_g, llval);
|
||||
|
||||
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
|
||||
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
|
||||
if tcx.sess.target.default_hidden_visibility {
|
||||
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
|
||||
}
|
||||
let llval = llvm::LLVMConstInt(i8, 0, False);
|
||||
llvm::LLVMSetInitializer(ll_g, llval);
|
||||
|
||||
if tcx.sess.opts.debuginfo != DebugInfo::None {
|
||||
let dbg_cx = debuginfo::CodegenUnitDebugContext::new(llmod);
|
||||
debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use std::collections::hash_map::Entry::*;
|
||||
|
||||
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
|
||||
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE};
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
|
||||
@ -241,6 +241,17 @@ fn exported_symbols_provider_local(
|
||||
used: false,
|
||||
},
|
||||
));
|
||||
|
||||
let exported_symbol =
|
||||
ExportedSymbol::NoDefId(SymbolName::new(tcx, NO_ALLOC_SHIM_IS_UNSTABLE));
|
||||
symbols.push((
|
||||
exported_symbol,
|
||||
SymbolExportInfo {
|
||||
level: SymbolExportLevel::Rust,
|
||||
kind: SymbolExportKind::Data,
|
||||
used: false,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {
|
||||
|
@ -4,7 +4,7 @@
|
||||
use crate::locator::{CrateError, CrateLocator, CratePaths};
|
||||
use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob};
|
||||
|
||||
use rustc_ast::expand::allocator::AllocatorKind;
|
||||
use rustc_ast::expand::allocator::{alloc_error_handler_name, global_fn_name, AllocatorKind};
|
||||
use rustc_ast::{self as ast, *};
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_data_structures::svh::Svh;
|
||||
@ -1048,7 +1048,7 @@ fn visit_item(&mut self, item: &'ast ast::Item) {
|
||||
}
|
||||
}
|
||||
|
||||
let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc));
|
||||
let name = Symbol::intern(&global_fn_name(sym::alloc));
|
||||
let mut f = Finder { name, spans: Vec::new() };
|
||||
visit::walk_crate(&mut f, krate);
|
||||
f.spans
|
||||
@ -1070,7 +1070,7 @@ fn visit_item(&mut self, item: &'ast ast::Item) {
|
||||
}
|
||||
}
|
||||
|
||||
let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::oom));
|
||||
let name = Symbol::intern(alloc_error_handler_name(AllocatorKind::Global));
|
||||
let mut f = Finder { name, spans: Vec::new() };
|
||||
visit::walk_crate(&mut f, krate);
|
||||
f.spans
|
||||
|
@ -37,6 +37,9 @@
|
||||
#[rustc_allocator_zeroed]
|
||||
#[rustc_nounwind]
|
||||
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
||||
|
||||
#[cfg(not(bootstrap))]
|
||||
static __rust_no_alloc_shim_is_unstable: u8;
|
||||
}
|
||||
|
||||
/// The global memory allocator.
|
||||
@ -90,7 +93,14 @@
|
||||
#[must_use = "losing the pointer will leak memory"]
|
||||
#[inline]
|
||||
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
||||
unsafe { __rust_alloc(layout.size(), layout.align()) }
|
||||
unsafe {
|
||||
// Make sure we don't accidentally allow omitting the allocator shim in
|
||||
// stable code until it is actually stabilized.
|
||||
#[cfg(not(bootstrap))]
|
||||
core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
|
||||
|
||||
__rust_alloc(layout.size(), layout.align())
|
||||
}
|
||||
}
|
||||
|
||||
/// Deallocate memory with the global allocator.
|
||||
|
@ -651,6 +651,10 @@ fn alloc_extern_static(
|
||||
|
||||
/// Sets up the "extern statics" for this machine.
|
||||
fn init_extern_statics(this: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
|
||||
// "__rust_no_alloc_shim_is_unstable"
|
||||
let val = ImmTy::from_int(0, this.machine.layouts.u8);
|
||||
Self::alloc_extern_static(this, "__rust_no_alloc_shim_is_unstable", val)?;
|
||||
|
||||
match this.tcx.sess.target.os.as_ref() {
|
||||
"linux" => {
|
||||
// "environ"
|
||||
|
@ -347,7 +347,6 @@ fn emulate_foreign_item(
|
||||
/// Emulates calling the internal __rust_* allocator functions
|
||||
fn emulate_allocator(
|
||||
&mut self,
|
||||
symbol: Symbol,
|
||||
default: impl FnOnce(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx>,
|
||||
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
|
||||
let this = self.eval_context_mut();
|
||||
@ -359,11 +358,12 @@ fn emulate_allocator(
|
||||
|
||||
match allocator_kind {
|
||||
AllocatorKind::Global => {
|
||||
let (body, instance) = this
|
||||
.lookup_exported_symbol(symbol)?
|
||||
.expect("symbol should be present if there is a global allocator");
|
||||
|
||||
Ok(EmulateByNameResult::MirBody(body, instance))
|
||||
// When `#[global_allocator]` is used, `__rust_*` is defined by the macro expansion
|
||||
// of this attribute. As such we have to call an exported Rust function,
|
||||
// and not execute any Miri shim. Somewhat unintuitively doing so is done
|
||||
// by returning `NotSupported`, which triggers the `lookup_exported_symbol`
|
||||
// fallback case in `emulate_foreign_item`.
|
||||
return Ok(EmulateByNameResult::NotSupported);
|
||||
}
|
||||
AllocatorKind::Default => {
|
||||
default(this)?;
|
||||
@ -558,11 +558,13 @@ fn emulate_foreign_item_by_name(
|
||||
|
||||
// Rust allocation
|
||||
"__rust_alloc" | "miri_alloc" => {
|
||||
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
|
||||
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
|
||||
// macro is used, we act like no shim exists, so that the exported function can run.
|
||||
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
let size = this.read_target_usize(size)?;
|
||||
let align = this.read_target_usize(align)?;
|
||||
|
||||
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
|
||||
Self::check_alloc_request(size, align)?;
|
||||
|
||||
let memory_kind = match link_name.as_str() {
|
||||
@ -581,8 +583,7 @@ fn emulate_foreign_item_by_name(
|
||||
};
|
||||
|
||||
match link_name.as_str() {
|
||||
"__rust_alloc" =>
|
||||
return this.emulate_allocator(Symbol::intern("__rg_alloc"), default),
|
||||
"__rust_alloc" => return this.emulate_allocator(default),
|
||||
"miri_alloc" => {
|
||||
default(this)?;
|
||||
return Ok(EmulateByNameResult::NeedsJumping);
|
||||
@ -591,11 +592,13 @@ fn emulate_foreign_item_by_name(
|
||||
}
|
||||
}
|
||||
"__rust_alloc_zeroed" => {
|
||||
return this.emulate_allocator(|this| {
|
||||
// See the comment for `__rust_alloc` why `check_shim` is only called in the
|
||||
// default case.
|
||||
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
let size = this.read_target_usize(size)?;
|
||||
let align = this.read_target_usize(align)?;
|
||||
|
||||
return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
|
||||
Self::check_alloc_request(size, align)?;
|
||||
|
||||
let ptr = this.allocate_ptr(
|
||||
@ -614,12 +617,15 @@ fn emulate_foreign_item_by_name(
|
||||
});
|
||||
}
|
||||
"__rust_dealloc" | "miri_dealloc" => {
|
||||
let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
|
||||
// See the comment for `__rust_alloc` why `check_shim` is only called in the
|
||||
// default case.
|
||||
let [ptr, old_size, align] =
|
||||
this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
let ptr = this.read_pointer(ptr)?;
|
||||
let old_size = this.read_target_usize(old_size)?;
|
||||
let align = this.read_target_usize(align)?;
|
||||
|
||||
let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
|
||||
let memory_kind = match link_name.as_str() {
|
||||
"__rust_dealloc" => MiriMemoryKind::Rust,
|
||||
"miri_dealloc" => MiriMemoryKind::Miri,
|
||||
@ -635,8 +641,9 @@ fn emulate_foreign_item_by_name(
|
||||
};
|
||||
|
||||
match link_name.as_str() {
|
||||
"__rust_dealloc" =>
|
||||
return this.emulate_allocator(Symbol::intern("__rg_dealloc"), default),
|
||||
"__rust_dealloc" => {
|
||||
return this.emulate_allocator(default);
|
||||
}
|
||||
"miri_dealloc" => {
|
||||
default(this)?;
|
||||
return Ok(EmulateByNameResult::NeedsJumping);
|
||||
@ -645,6 +652,9 @@ fn emulate_foreign_item_by_name(
|
||||
}
|
||||
}
|
||||
"__rust_realloc" => {
|
||||
return this.emulate_allocator(|this| {
|
||||
// See the comment for `__rust_alloc` why `check_shim` is only called in the
|
||||
// default case.
|
||||
let [ptr, old_size, align, new_size] =
|
||||
this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
let ptr = this.read_pointer(ptr)?;
|
||||
@ -653,7 +663,6 @@ fn emulate_foreign_item_by_name(
|
||||
let new_size = this.read_target_usize(new_size)?;
|
||||
// No need to check old_size; we anyway check that they match the allocation.
|
||||
|
||||
return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
|
||||
Self::check_alloc_request(new_size, align)?;
|
||||
|
||||
let align = Align::from_bytes(align).unwrap();
|
||||
|
@ -1,7 +1,7 @@
|
||||
error: memory leaked: ALLOC (Rust heap, size: 4, align: 4), allocated here:
|
||||
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
|
||||
LL | unsafe { __rust_alloc(layout.size(), layout.align()) }
|
||||
LL | __rust_alloc(layout.size(), layout.align())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
@ -1,7 +1,7 @@
|
||||
error: memory leaked: ALLOC (Rust heap, size: 16, align: 4), allocated here:
|
||||
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
|
||||
LL | unsafe { __rust_alloc(layout.size(), layout.align()) }
|
||||
LL | __rust_alloc(layout.size(), layout.align())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
@ -1,7 +1,7 @@
|
||||
error: memory leaked: ALLOC (Rust heap, size: 32, align: 8), allocated here:
|
||||
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
|
||||
LL | unsafe { __rust_alloc(layout.size(), layout.align()) }
|
||||
LL | __rust_alloc(layout.size(), layout.align())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
|
||||
|
@ -31,7 +31,7 @@ fn main() {
|
||||
}
|
||||
|
||||
fn host_to_target_path(path: String) -> PathBuf {
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::{c_char, CStr, CString};
|
||||
|
||||
let path = CString::new(path).unwrap();
|
||||
let mut out = Vec::with_capacity(1024);
|
||||
|
@ -7,6 +7,7 @@
|
||||
pub fn alloc_test(data: u32) {
|
||||
// CHECK-LABEL: @alloc_test
|
||||
// CHECK-NEXT: start:
|
||||
// CHECK-NEXT: {{.*}} load volatile i8, ptr @__rust_no_alloc_shim_is_unstable, align 1
|
||||
// CHECK-NEXT: ret void
|
||||
let x = Box::new(data);
|
||||
drop(x);
|
||||
|
@ -11,8 +11,6 @@
|
||||
|
||||
// Make sure that vtables don't have the unnamed_addr attribute when debuginfo is enabled.
|
||||
// This helps debuggers more reliably map from dyn pointer to concrete type.
|
||||
// CHECK: @vtable.0 = private constant <{
|
||||
// CHECK: @vtable.1 = private constant <{
|
||||
// CHECK: @vtable.2 = private constant <{
|
||||
// CHECK: @vtable.3 = private constant <{
|
||||
// CHECK: @vtable.4 = private constant <{
|
||||
|
@ -7,6 +7,7 @@
|
||||
pub fn sum_me() -> i32 {
|
||||
// CHECK-LABEL: @sum_me
|
||||
// CHECK-NEXT: {{^.*:$}}
|
||||
// CHECK-NEXT: {{.*}} load volatile i8, ptr @__rust_no_alloc_shim_is_unstable, align 1
|
||||
// CHECK-NEXT: ret i32 6
|
||||
vec![1, 2, 3].iter().sum::<i32>()
|
||||
}
|
||||
|
24
tests/run-make/no-alloc-shim/Makefile
Normal file
24
tests/run-make/no-alloc-shim/Makefile
Normal file
@ -0,0 +1,24 @@
|
||||
include ../tools.mk
|
||||
|
||||
# ignore-cross-compile
|
||||
# ignore-msvc FIXME(bjorn3) can't figure out how to link with the MSVC toolchain
|
||||
|
||||
TARGET_LIBDIR = $$($(RUSTC) --print target-libdir)
|
||||
|
||||
all:
|
||||
$(RUSTC) foo.rs --crate-type bin --emit obj -Cpanic=abort
|
||||
ifdef IS_MSVC
|
||||
$(CC) $(CFLAGS) $(TMPDIR)/foo.o $(call OUT_EXE,foo) /link $(TARGET_LIBDIR)/liballoc-*.rlib $(TARGET_LIBDIR)/libcore-*.rlib $(TARGET_LIBDIR)/libcompiler_builtins-*.rlib
|
||||
$(call OUT_EXE,foo)
|
||||
else
|
||||
$(CC) $(CFLAGS) $(TMPDIR)/foo.o $(TARGET_LIBDIR)/liballoc-*.rlib $(TARGET_LIBDIR)/libcore-*.rlib $(TARGET_LIBDIR)/libcompiler_builtins-*.rlib -o $(call RUN_BINFILE,foo)
|
||||
$(call RUN_BINFILE,foo)
|
||||
endif
|
||||
|
||||
# Check that linking without __rust_no_alloc_shim_is_unstable defined fails
|
||||
$(RUSTC) foo.rs --crate-type bin --emit obj -Cpanic=abort --cfg check_feature_gate
|
||||
ifdef IS_MSVC
|
||||
$(CC) $(CFLAGS) $(TMPDIR)/foo.o $(call OUT_EXE,foo) /link $(TARGET_LIBDIR)/liballoc-*.rlib $(TARGET_LIBDIR)/libcore-*.rlib $(TARGET_LIBDIR)/libcompiler_builtins-*.rlib || exit 0 && exit 1
|
||||
else
|
||||
$(CC) $(CFLAGS) $(TMPDIR)/foo.o $(TARGET_LIBDIR)/liballoc-*.rlib $(TARGET_LIBDIR)/libcore-*.rlib $(TARGET_LIBDIR)/libcompiler_builtins-*.rlib -o $(call RUN_BINFILE,foo) || exit 0 && exit 1
|
||||
endif
|
44
tests/run-make/no-alloc-shim/foo.rs
Normal file
44
tests/run-make/no-alloc-shim/foo.rs
Normal file
@ -0,0 +1,44 @@
|
||||
#![feature(default_alloc_error_handler)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
|
||||
#[panic_handler]
|
||||
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn rust_eh_personality() {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOC: Alloc = Alloc;
|
||||
|
||||
struct Alloc;
|
||||
|
||||
unsafe impl GlobalAlloc for Alloc {
|
||||
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
|
||||
core::ptr::null_mut()
|
||||
}
|
||||
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(check_feature_gate))]
|
||||
#[no_mangle]
|
||||
static __rust_no_alloc_shim_is_unstable: u8 = 0;
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn main(_argc: usize, _argv: *const *const i8) -> i32 {
|
||||
unsafe {
|
||||
assert_eq!(alloc::alloc::alloc(Layout::new::<()>()), core::ptr::null_mut());
|
||||
}
|
||||
|
||||
0
|
||||
}
|
Loading…
Reference in New Issue
Block a user