Skip to content

Commit 4e63d6e

Browse files
committed
Update comments and simplify miri allocator handling a bit
1 parent 125284d commit 4e63d6e

File tree

5 files changed

+18
-70
lines changed

5 files changed

+18
-70
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

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

33
use rustc_abi::{CanonAbi, X86Call};
4-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
4+
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
55
use rustc_data_structures::unord::UnordMap;
66
use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
@@ -213,14 +213,10 @@ fn exported_symbols_provider_local<'tcx>(
213213

214214
// Mark allocator shim symbols as exported only if they were generated.
215215
if needs_allocator_shim(tcx) {
216-
for symbol_name in ALLOCATOR_METHODS
217-
.iter()
218-
.map(|method| mangle_internal_symbol(tcx, global_fn_name(method.name).as_str()))
219-
.chain([
220-
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
221-
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
222-
])
223-
{
216+
for symbol_name in [
217+
mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
218+
mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
219+
] {
224220
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
225221

226222
symbols.push((

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -900,18 +900,9 @@ fn mono_item_visibility<'tcx>(
900900
//
901901
// FIXME update comment
902902
// * Second is "std internal symbols". Currently this is primarily used
903-
// for allocator symbols. Allocators are a little weird in their
904-
// implementation, but the idea is that the compiler, at the last
905-
// minute, defines an allocator with an injected object file. The
906-
// `alloc` crate references these symbols (`__rust_alloc`) and the
907-
// definition doesn't get hooked up until a linked crate artifact is
908-
// generated.
909-
//
910-
// The symbols synthesized by the compiler (`__rust_alloc`) are thin
911-
// veneers around the actual implementation, some other symbol which
912-
// implements the same ABI. These symbols (things like `__rg_alloc`,
913-
// `__rdl_alloc`, `__rde_alloc`, etc), are all tagged with "std
914-
// internal symbols".
903+
// for allocator symbols and the unwinder runtime to allow cyclic
904+
// dependencies between the defining and using crate and to allow
905+
// replacing them.
915906
//
916907
// The std-internal symbols here **should not show up in a dll as an
917908
// 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
@@ -386,8 +386,7 @@ pub mod __default_lib_allocator {
386386
// These are used as a fallback for implementing the `__rust_alloc`, etc symbols
387387
// (see `src/liballoc/alloc.rs`) when there is no `#[global_allocator]` attribute.
388388

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

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

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

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

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

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

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -525,45 +525,30 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
525525
}
526526

527527
// Rust allocation
528-
name if name == this.mangle_internal_symbol("__rust_alloc") || name == "miri_alloc" => {
528+
"miri_alloc" => {
529529
let default = |ecx: &mut MiriInterpCx<'tcx>| {
530530
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
531531
// macro is used, we act like no shim exists, so that the exported function can run.
532532
let [size, align] = ecx.check_shim(abi, CanonAbi::Rust, link_name, args)?;
533533
let size = ecx.read_target_usize(size)?;
534534
let align = ecx.read_target_usize(align)?;
535535

536-
ecx.check_rustc_alloc_request(size, align)?;
537-
538-
let memory_kind = match link_name.as_str() {
539-
"miri_alloc" => MiriMemoryKind::Miri,
540-
_ => MiriMemoryKind::Rust,
541-
};
536+
this.check_rustc_alloc_request(size, align)?;
542537

543538
let ptr = ecx.allocate_ptr(
544539
Size::from_bytes(size),
545540
Align::from_bytes(align).unwrap(),
546-
memory_kind.into(),
541+
MiriMemoryKind::Miri.into(),
547542
AllocInit::Uninit,
548543
)?;
549544

550545
ecx.write_pointer(ptr, dest)
551546
};
552547

553-
match link_name.as_str() {
554-
"miri_alloc" => {
555-
default(this)?;
556-
return interp_ok(EmulateItemResult::NeedsReturn);
557-
}
558-
_ => return this.emulate_allocator(),
559-
}
560-
}
561-
name if name == this.mangle_internal_symbol("__rust_alloc_zeroed") => {
562-
return this.emulate_allocator();
548+
default(this)?;
549+
return interp_ok(EmulateItemResult::NeedsReturn);
563550
}
564-
name if name == this.mangle_internal_symbol("__rust_dealloc")
565-
|| name == "miri_dealloc" =>
566-
{
551+
"miri_dealloc" => {
567552
let default = |ecx: &mut MiriInterpCx<'tcx>| {
568553
// See the comment for `__rust_alloc` why `check_shim` is only called in the
569554
// default case.
@@ -573,29 +558,16 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
573558
let old_size = ecx.read_target_usize(old_size)?;
574559
let align = ecx.read_target_usize(align)?;
575560

576-
let memory_kind = match link_name.as_str() {
577-
"miri_dealloc" => MiriMemoryKind::Miri,
578-
_ => MiriMemoryKind::Rust,
579-
};
580-
581561
// No need to check old_size/align; we anyway check that they match the allocation.
582562
ecx.deallocate_ptr(
583563
ptr,
584564
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
585-
memory_kind.into(),
565+
MiriMemoryKind::Miri.into(),
586566
)
587567
};
588568

589-
match link_name.as_str() {
590-
"miri_dealloc" => {
591-
default(this)?;
592-
return interp_ok(EmulateItemResult::NeedsReturn);
593-
}
594-
_ => return this.emulate_allocator(),
595-
}
596-
}
597-
name if name == this.mangle_internal_symbol("__rust_realloc") => {
598-
return this.emulate_allocator();
569+
default(this)?;
570+
return interp_ok(EmulateItemResult::NeedsReturn);
599571
}
600572

601573
// C memory handling functions

0 commit comments

Comments
 (0)