Skip to content

Commit 258986c

Browse files
authored
Merge pull request #2293 from jieyouxu/rustc-pull
Rustc pull
2 parents 519d351 + d96f1c3 commit 258986c

24 files changed

+128
-83
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ MIRI_LOG=rustc_mir::interpret=info,miri::stacked_borrows ./miri run tests/pass/v
153153

154154
Note that you will only get `info`, `warn` or `error` messages if you use a prebuilt compiler.
155155
In order to get `debug` and `trace` level messages, you need to build miri with a locally built
156-
compiler that has `debug=true` set in `config.toml`.
156+
compiler that has `debug=true` set in `bootstrap.toml`.
157157

158158
#### Debugging error messages
159159

cargo-miri/src/setup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn setup(
115115
// https://github.com/rust-lang/miri/issues/1421,
116116
// https://github.com/rust-lang/miri/issues/2429). Looks like setting
117117
// `RUSTC_WRAPPER` to the empty string overwrites `build.rustc-wrapper` set via
118-
// `config.toml`.
118+
// `bootstrap.toml`.
119119
command.env("RUSTC_WRAPPER", "");
120120

121121
if show_setup {

src/alloc_addresses/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
377377
fn get_global_alloc_bytes(
378378
&self,
379379
id: AllocId,
380-
kind: MemoryKind,
381380
bytes: &[u8],
382381
align: Align,
383382
) -> InterpResult<'tcx, MiriAllocBytes> {
@@ -386,7 +385,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
386385
// In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`.
387386
// This additional call ensures that some `MiriAllocBytes` are always prepared, just in case
388387
// this function gets called before the first time `addr_from_alloc_id` gets called.
389-
this.addr_from_alloc_id(id, kind)?;
388+
this.addr_from_alloc_id(id, MiriMemoryKind::Global.into())?;
390389
// The memory we need here will have already been allocated during an earlier call to
391390
// `addr_from_alloc_id` for this allocation. So don't create a new `MiriAllocBytes` here, instead
392391
// fetch the previously prepared bytes from `prepared_alloc_bytes`.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ extern crate rustc_index;
7171
extern crate rustc_middle;
7272
extern crate rustc_session;
7373
extern crate rustc_span;
74+
extern crate rustc_symbol_mangling;
7475
extern crate rustc_target;
7576
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
7677
// shipped only as rmeta files.

src/machine.rs

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,59 @@ impl<'tcx> MiriMachine<'tcx> {
814814
.and_then(|(_allocated, deallocated)| *deallocated)
815815
.map(Span::data)
816816
}
817+
818+
fn init_allocation(
819+
ecx: &MiriInterpCx<'tcx>,
820+
id: AllocId,
821+
kind: MemoryKind,
822+
size: Size,
823+
align: Align,
824+
) -> InterpResult<'tcx, AllocExtra<'tcx>> {
825+
if ecx.machine.tracked_alloc_ids.contains(&id) {
826+
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind));
827+
}
828+
829+
let borrow_tracker = ecx
830+
.machine
831+
.borrow_tracker
832+
.as_ref()
833+
.map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
834+
835+
let data_race = ecx.machine.data_race.as_ref().map(|data_race| {
836+
data_race::AllocState::new_allocation(
837+
data_race,
838+
&ecx.machine.threads,
839+
size,
840+
kind,
841+
ecx.machine.current_span(),
842+
)
843+
});
844+
let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
845+
846+
// If an allocation is leaked, we want to report a backtrace to indicate where it was
847+
// allocated. We don't need to record a backtrace for allocations which are allowed to
848+
// leak.
849+
let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces {
850+
None
851+
} else {
852+
Some(ecx.generate_stacktrace())
853+
};
854+
855+
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
856+
ecx.machine
857+
.allocation_spans
858+
.borrow_mut()
859+
.insert(id, (ecx.machine.current_span(), None));
860+
}
861+
862+
interp_ok(AllocExtra {
863+
borrow_tracker,
864+
data_race,
865+
weak_memory,
866+
backtrace,
867+
sync: FxHashMap::default(),
868+
})
869+
}
817870
}
818871

819872
impl VisitProvenance for MiriMachine<'_> {
@@ -1200,57 +1253,15 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
12001253
}
12011254
}
12021255

1203-
fn init_alloc_extra(
1256+
fn init_local_allocation(
12041257
ecx: &MiriInterpCx<'tcx>,
12051258
id: AllocId,
12061259
kind: MemoryKind,
12071260
size: Size,
12081261
align: Align,
12091262
) -> InterpResult<'tcx, Self::AllocExtra> {
1210-
if ecx.machine.tracked_alloc_ids.contains(&id) {
1211-
ecx.emit_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id, size, align, kind));
1212-
}
1213-
1214-
let borrow_tracker = ecx
1215-
.machine
1216-
.borrow_tracker
1217-
.as_ref()
1218-
.map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
1219-
1220-
let data_race = ecx.machine.data_race.as_ref().map(|data_race| {
1221-
data_race::AllocState::new_allocation(
1222-
data_race,
1223-
&ecx.machine.threads,
1224-
size,
1225-
kind,
1226-
ecx.machine.current_span(),
1227-
)
1228-
});
1229-
let weak_memory = ecx.machine.weak_memory.then(weak_memory::AllocState::new_allocation);
1230-
1231-
// If an allocation is leaked, we want to report a backtrace to indicate where it was
1232-
// allocated. We don't need to record a backtrace for allocations which are allowed to
1233-
// leak.
1234-
let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces {
1235-
None
1236-
} else {
1237-
Some(ecx.generate_stacktrace())
1238-
};
1239-
1240-
if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
1241-
ecx.machine
1242-
.allocation_spans
1243-
.borrow_mut()
1244-
.insert(id, (ecx.machine.current_span(), None));
1245-
}
1246-
1247-
interp_ok(AllocExtra {
1248-
borrow_tracker,
1249-
data_race,
1250-
weak_memory,
1251-
backtrace,
1252-
sync: FxHashMap::default(),
1253-
})
1263+
assert!(kind != MiriMemoryKind::Global.into());
1264+
MiriMachine::init_allocation(ecx, id, kind, size, align)
12541265
}
12551266

12561267
fn adjust_alloc_root_pointer(
@@ -1340,13 +1351,13 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13401351
alloc: &'b Allocation,
13411352
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>
13421353
{
1343-
let kind = Self::GLOBAL_KIND.unwrap().into();
13441354
let alloc = alloc.adjust_from_tcx(
13451355
&ecx.tcx,
1346-
|bytes, align| ecx.get_global_alloc_bytes(id, kind, bytes, align),
1356+
|bytes, align| ecx.get_global_alloc_bytes(id, bytes, align),
13471357
|ptr| ecx.global_root_pointer(ptr),
13481358
)?;
1349-
let extra = Self::init_alloc_extra(ecx, id, kind, alloc.size(), alloc.align)?;
1359+
let kind = MiriMemoryKind::Global.into();
1360+
let extra = MiriMachine::init_allocation(ecx, id, kind, alloc.size(), alloc.align)?;
13501361
interp_ok(Cow::Owned(alloc.with_extra(extra)))
13511362
}
13521363

src/shims/extern_static.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Provides the `extern static` that this platform expects.
22
3+
use rustc_symbol_mangling::mangle_internal_symbol;
4+
35
use crate::*;
46

57
impl<'tcx> MiriMachine<'tcx> {
@@ -50,7 +52,11 @@ impl<'tcx> MiriMachine<'tcx> {
5052
// "__rust_alloc_error_handler_should_panic"
5153
let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
5254
let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
53-
Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
55+
Self::alloc_extern_static(
56+
ecx,
57+
&mangle_internal_symbol(*ecx.tcx, "__rust_alloc_error_handler_should_panic"),
58+
val,
59+
)?;
5460

5561
if ecx.target_os_is_unix() {
5662
// "environ" is mandated by POSIX.

src/shims/foreign_items.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_middle::mir::interpret::AllocInit;
1212
use rustc_middle::ty::Ty;
1313
use rustc_middle::{mir, ty};
1414
use rustc_span::Symbol;
15+
use rustc_symbol_mangling::mangle_internal_symbol;
1516
use rustc_target::callconv::{Conv, FnAbi};
1617

1718
use self::helpers::{ToHost, ToSoft};
@@ -51,17 +52,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5152

5253
// Some shims forward to other MIR bodies.
5354
match link_name.as_str() {
54-
"__rust_alloc_error_handler" => {
55+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler") => {
5556
// Forward to the right symbol that implements this function.
5657
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
5758
// in real code, this symbol does not exist without an allocator
5859
throw_unsup_format!(
5960
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6061
);
6162
};
62-
let name = alloc_error_handler_name(handler_kind);
63+
let name =
64+
mangle_internal_symbol(*this.tcx, alloc_error_handler_name(handler_kind));
6365
let handler = this
64-
.lookup_exported_symbol(Symbol::intern(name))?
66+
.lookup_exported_symbol(Symbol::intern(&name))?
6567
.expect("missing alloc error handler symbol");
6668
return interp_ok(Some(handler));
6769
}
@@ -136,15 +138,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
136138
// Find it if it was not cached.
137139
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
138140
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
141+
if tcx.is_foreign_item(def_id) {
142+
// Skip over imports of items
143+
return interp_ok(());
144+
}
145+
139146
let attrs = tcx.codegen_fn_attrs(def_id);
147+
// FIXME use tcx.symbol_name(instance) instead
140148
let symbol_name = if let Some(export_name) = attrs.export_name {
141149
export_name
142-
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
150+
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
151+
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
152+
{
143153
tcx.item_name(def_id)
144154
} else {
145155
// Skip over items without an explicitly defined symbol name.
146156
return interp_ok(());
147157
};
158+
let symbol_name =
159+
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
160+
Symbol::intern(&mangle_internal_symbol(tcx, symbol_name.as_str()))
161+
} else {
162+
symbol_name
163+
};
148164
if symbol_name == link_name {
149165
if let Some((original_instance, original_cnum)) = instance_and_crate {
150166
// Make sure we are consistent wrt what is 'first' and 'second'.
@@ -489,7 +505,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
489505
}
490506

491507
// Rust allocation
492-
"__rust_alloc" | "miri_alloc" => {
508+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
509+
|| name == "miri_alloc" =>
510+
{
493511
let default = |ecx: &mut MiriInterpCx<'tcx>| {
494512
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
495513
// macro is used, we act like no shim exists, so that the exported function can run.
@@ -500,9 +518,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
500518
ecx.check_rustc_alloc_request(size, align)?;
501519

502520
let memory_kind = match link_name.as_str() {
503-
"__rust_alloc" => MiriMemoryKind::Rust,
504521
"miri_alloc" => MiriMemoryKind::Miri,
505-
_ => unreachable!(),
522+
_ => MiriMemoryKind::Rust,
506523
};
507524

508525
let ptr = ecx.allocate_ptr(
@@ -516,15 +533,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
516533
};
517534

518535
match link_name.as_str() {
519-
"__rust_alloc" => return this.emulate_allocator(default),
520536
"miri_alloc" => {
521537
default(this)?;
522538
return interp_ok(EmulateItemResult::NeedsReturn);
523539
}
524-
_ => unreachable!(),
540+
_ => return this.emulate_allocator(default),
525541
}
526542
}
527-
"__rust_alloc_zeroed" => {
543+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
528544
return this.emulate_allocator(|this| {
529545
// See the comment for `__rust_alloc` why `check_shim` is only called in the
530546
// default case.
@@ -543,7 +559,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
543559
this.write_pointer(ptr, dest)
544560
});
545561
}
546-
"__rust_dealloc" | "miri_dealloc" => {
562+
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
563+
|| name == "miri_dealloc" =>
564+
{
547565
let default = |ecx: &mut MiriInterpCx<'tcx>| {
548566
// See the comment for `__rust_alloc` why `check_shim` is only called in the
549567
// default case.
@@ -554,9 +572,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
554572
let align = ecx.read_target_usize(align)?;
555573

556574
let memory_kind = match link_name.as_str() {
557-
"__rust_dealloc" => MiriMemoryKind::Rust,
558575
"miri_dealloc" => MiriMemoryKind::Miri,
559-
_ => unreachable!(),
576+
_ => MiriMemoryKind::Rust,
560577
};
561578

562579
// No need to check old_size/align; we anyway check that they match the allocation.
@@ -568,17 +585,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
568585
};
569586

570587
match link_name.as_str() {
571-
"__rust_dealloc" => {
572-
return this.emulate_allocator(default);
573-
}
574588
"miri_dealloc" => {
575589
default(this)?;
576590
return interp_ok(EmulateItemResult::NeedsReturn);
577591
}
578-
_ => unreachable!(),
592+
_ => return this.emulate_allocator(default),
579593
}
580594
}
581-
"__rust_realloc" => {
595+
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
582596
return this.emulate_allocator(|this| {
583597
// See the comment for `__rust_alloc` why `check_shim` is only called in the
584598
// default case.

tests/fail/alloc/alloc_error_handler_custom.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ note: inside `miri_start`
2121
|
2222
LL | handle_alloc_error(Layout::for_value(&0));
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
2524

2625
error: aborting due to 1 previous error
2726

tests/fail/alloc/too_large.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#![feature(rustc_attrs)]
2+
13
extern "Rust" {
4+
#[rustc_std_internal_symbol]
25
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
36
}
47

tests/fail/alloc/unsupported_big_alignment.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// because rustc does not support alignments that large.
33
// https://github.com/rust-lang/miri/issues/3687
44

5+
#![feature(rustc_attrs)]
6+
57
extern "Rust" {
8+
#[rustc_std_internal_symbol]
69
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
710
}
811

0 commit comments

Comments
 (0)