Skip to content

Commit d179738

Browse files
committed
Update comments and simplify miri allocator handling a bit
1 parent 1a51663 commit d179738

File tree

5 files changed

+30
-98
lines changed

5 files changed

+30
-98
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+3-5
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

Diff for: compiler/rustc_monomorphize/src/partitioning.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -889,18 +889,9 @@ fn mono_item_visibility<'tcx>(
889889
//
890890
// FIXME update comment
891891
// * Second is "std internal symbols". Currently this is primarily used
892-
// for allocator symbols. Allocators are a little weird in their
893-
// implementation, but the idea is that the compiler, at the last
894-
// minute, defines an allocator with an injected object file. The
895-
// `alloc` crate references these symbols (`__rust_alloc`) and the
896-
// definition doesn't get hooked up until a linked crate artifact is
897-
// generated.
898-
//
899-
// The symbols synthesized by the compiler (`__rust_alloc`) are thin
900-
// veneers around the actual implementation, some other symbol which
901-
// implements the same ABI. These symbols (things like `__rg_alloc`,
902-
// `__rdl_alloc`, `__rde_alloc`, etc), are all tagged with "std
903-
// internal symbols".
892+
// for allocator symbols and the unwinder runtime to allow cyclic
893+
// dependencies between the defining and using crate and to allow
894+
// replacing them.
904895
//
905896
// The std-internal symbols here **should not show up in a dll as an
906897
// exported interface**, so they return `false` from

Diff for: library/std/src/alloc.rs

+1-2
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

Diff for: src/tools/miri/src/shims/alloc.rs

-10
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5151
Align::from_bytes(prev_power_of_two(size)).unwrap()
5252
}
5353

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

Diff for: src/tools/miri/src/shims/foreign_items.rs

+23-69
Original file line numberDiff line numberDiff line change
@@ -527,80 +527,34 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
527527
}
528528

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

546-
let ptr = ecx.allocate_ptr(
547-
Size::from_bytes(size),
548-
Align::from_bytes(align).unwrap(),
549-
memory_kind.into(),
550-
)?;
535+
this.check_rustc_alloc_request(size, align)?;
551536

552-
ecx.write_pointer(ptr, dest)
553-
};
537+
let ptr = this.allocate_ptr(
538+
Size::from_bytes(size),
539+
Align::from_bytes(align).unwrap(),
540+
MiriMemoryKind::Miri.into(),
541+
)?;
554542

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

591-
match link_name.as_str() {
592-
"__rust_dealloc" => {
593-
return this.emulate_allocator();
594-
}
595-
"miri_dealloc" => {
596-
default(this)?;
597-
return interp_ok(EmulateItemResult::NeedsReturn);
598-
}
599-
_ => unreachable!(),
600-
}
601-
}
602-
"__rust_realloc" => {
603-
return this.emulate_allocator();
552+
// No need to check old_size/align; we anyway check that they match the allocation.
553+
this.deallocate_ptr(
554+
ptr,
555+
Some((Size::from_bytes(old_size), Align::from_bytes(align).unwrap())),
556+
MiriMemoryKind::Miri.into(),
557+
);
604558
}
605559

606560
// C memory handling functions

0 commit comments

Comments
 (0)