Skip to content

Commit 4387a58

Browse files
committed
Update comments and simplify miri allocator handling a bit
1 parent 37f25fe commit 4387a58

File tree

5 files changed

+29
-98
lines changed

5 files changed

+29
-98
lines changed

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

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

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

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);

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

+22-69
Original file line numberDiff line numberDiff line change
@@ -521,80 +521,33 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
521521
}
522522

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

540-
let ptr = ecx.allocate_ptr(
541-
Size::from_bytes(size),
542-
Align::from_bytes(align).unwrap(),
543-
memory_kind.into(),
544-
)?;
529+
this.check_rustc_alloc_request(size, align)?;
545530

546-
ecx.write_pointer(ptr, dest)
547-
};
531+
let ptr = this.allocate_ptr(
532+
Size::from_bytes(size),
533+
Align::from_bytes(align).unwrap(),
534+
MiriMemoryKind::Miri.into(),
535+
)?;
548536

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

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

600553
// C memory handling functions

0 commit comments

Comments
 (0)