Skip to content

Commit 860328f

Browse files
committed
Update comments and simplify miri allocator handling a bit
1 parent a60e45f commit 860328f

File tree

5 files changed

+30
-99
lines changed

5 files changed

+30
-99
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::hash_map::Entry::*;
22

3-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE};
3+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
44
use rustc_data_structures::unord::UnordMap;
55
use rustc_hir::def::DefKind;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
@@ -207,10 +207,8 @@ fn exported_symbols_provider_local(
207207

208208
// Mark allocator shim symbols as exported only if they were generated.
209209
if needs_allocator_shim(tcx) {
210-
for symbol_name in ALLOCATOR_METHODS
211-
.iter()
212-
.map(|method| format!("__rust_{}", method.name))
213-
.chain(["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()])
210+
for symbol_name in
211+
["__rust_alloc_error_handler".to_string(), OomStrategy::SYMBOL.to_string()]
214212
{
215213
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
216214

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -901,18 +901,9 @@ fn mono_item_visibility<'tcx>(
901901
//
902902
// FIXME update comment
903903
// * Second is "std internal symbols". Currently this is primarily used
904-
// for allocator symbols. Allocators are a little weird in their
905-
// implementation, but the idea is that the compiler, at the last
906-
// minute, defines an allocator with an injected object file. The
907-
// `alloc` crate references these symbols (`__rust_alloc`) and the
908-
// definition doesn't get hooked up until a linked crate artifact is
909-
// generated.
910-
//
911-
// The symbols synthesized by the compiler (`__rust_alloc`) are thin
912-
// veneers around the actual implementation, some other symbol which
913-
// implements the same ABI. These symbols (things like `__rg_alloc`,
914-
// `__rdl_alloc`, `__rde_alloc`, etc), are all tagged with "std
915-
// internal symbols".
904+
// for allocator symbols and the unwinder runtime to allow cyclic
905+
// dependencies between the defining and using crate and to allow
906+
// replacing them.
916907
//
917908
// The std-internal symbols here **should not show up in a dll as an
918909
// exported interface**, so they return `false` from

library/std/src/alloc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ pub mod __default_lib_allocator {
385385
// These are used as a fallback for implementing the `__rust_alloc`, etc symbols
386386
// (see `src/liballoc/alloc.rs`) when there is no `#[global_allocator]` attribute.
387387

388-
// for symbol names src/librustc_ast/expand/allocator.rs
389-
// for signatures src/librustc_allocator/lib.rs
388+
// for symbol names and signatures see compiler/rustc_ast/src/expand/allocator.rs
390389

391390
// linkage directives are provided as part of the current compiler allocator
392391
// ABI

src/tools/miri/src/shims/alloc.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4949
Align::from_bytes(prev_power_of_two(size)).unwrap()
5050
}
5151

52-
/// Emulates calling the internal __rust_* allocator functions
53-
fn emulate_allocator(&mut self) -> InterpResult<'tcx, EmulateItemResult> {
54-
// When `#[global_allocator]` is used, `__rust_*` is defined by the macro expansion
55-
// of this attribute. As such we have to call an exported Rust function,
56-
// and not execute any Miri shim. Somewhat unintuitively doing so is done
57-
// by returning `NotSupported`, which triggers the `lookup_exported_symbol`
58-
// fallback case in `emulate_foreign_item`.
59-
interp_ok(EmulateItemResult::NotSupported)
60-
}
61-
6252
fn malloc(&mut self, size: u64, init: AllocInit) -> InterpResult<'tcx, Pointer> {
6353
let this = self.eval_context_mut();
6454
let align = this.malloc_align(size);

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 23 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -522,81 +522,34 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
522522
}
523523

524524
// Rust allocation
525-
"__rust_alloc" | "miri_alloc" => {
526-
let default = |ecx: &mut MiriInterpCx<'tcx>| {
527-
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
528-
// macro is used, we act like no shim exists, so that the exported function can run.
529-
let [size, align] = ecx.check_shim(abi, Conv::Rust, link_name, args)?;
530-
let size = ecx.read_target_usize(size)?;
531-
let align = ecx.read_target_usize(align)?;
532-
533-
ecx.check_rustc_alloc_request(size, align)?;
534-
535-
let memory_kind = match link_name.as_str() {
536-
"__rust_alloc" => MiriMemoryKind::Rust,
537-
"miri_alloc" => MiriMemoryKind::Miri,
538-
_ => unreachable!(),
539-
};
525+
"miri_alloc" => {
526+
let [size, align] = this.check_shim(abi, Conv::Rust, link_name, args)?;
527+
let size = this.read_target_usize(size)?;
528+
let align = this.read_target_usize(align)?;
540529

541-
let ptr = ecx.allocate_ptr(
542-
Size::from_bytes(size),
543-
Align::from_bytes(align).unwrap(),
544-
memory_kind.into(),
545-
AllocInit::Uninit,
546-
)?;
530+
this.check_rustc_alloc_request(size, align)?;
547531

548-
ecx.write_pointer(ptr, dest)
549-
};
532+
let ptr = this.allocate_ptr(
533+
Size::from_bytes(size),
534+
Align::from_bytes(align).unwrap(),
535+
MiriMemoryKind::Miri.into(),
536+
AllocInit::Uninit,
537+
)?;
550538

551-
match link_name.as_str() {
552-
"__rust_alloc" => return this.emulate_allocator(),
553-
"miri_alloc" => {
554-
default(this)?;
555-
return interp_ok(EmulateItemResult::NeedsReturn);
556-
}
557-
_ => unreachable!(),
558-
}
539+
this.write_pointer(ptr, dest)?;
559540
}
560-
"__rust_alloc_zeroed" => {
561-
return this.emulate_allocator();
562-
}
563-
"__rust_dealloc" | "miri_dealloc" => {
564-
let default = |ecx: &mut MiriInterpCx<'tcx>| {
565-
// See the comment for `__rust_alloc` why `check_shim` is only called in the
566-
// default case.
567-
let [ptr, old_size, align] =
568-
ecx.check_shim(abi, Conv::Rust, link_name, args)?;
569-
let ptr = ecx.read_pointer(ptr)?;
570-
let old_size = ecx.read_target_usize(old_size)?;
571-
let align = ecx.read_target_usize(align)?;
572-
573-
let memory_kind = match link_name.as_str() {
574-
"__rust_dealloc" => MiriMemoryKind::Rust,
575-
"miri_dealloc" => MiriMemoryKind::Miri,
576-
_ => unreachable!(),
577-
};
578-
579-
// No need to check old_size/align; we anyway check that they match the allocation.
580-
ecx.deallocate_ptr(
581-
ptr,
582-
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
583-
memory_kind.into(),
584-
)
585-
};
541+
"miri_dealloc" => {
542+
let [ptr, old_size, align] = this.check_shim(abi, Conv::Rust, link_name, args)?;
543+
let ptr = this.read_pointer(ptr)?;
544+
let old_size = this.read_target_usize(old_size)?;
545+
let align = this.read_target_usize(align)?;
586546

587-
match link_name.as_str() {
588-
"__rust_dealloc" => {
589-
return this.emulate_allocator();
590-
}
591-
"miri_dealloc" => {
592-
default(this)?;
593-
return interp_ok(EmulateItemResult::NeedsReturn);
594-
}
595-
_ => unreachable!(),
596-
}
597-
}
598-
"__rust_realloc" => {
599-
return this.emulate_allocator();
547+
// No need to check old_size/align; we anyway check that they match the allocation.
548+
this.deallocate_ptr(
549+
ptr,
550+
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
551+
MiriMemoryKind::Miri.into(),
552+
)?;
600553
}
601554

602555
// C memory handling functions

0 commit comments

Comments
 (0)