Skip to content

Commit 6ba0ce4

Browse files
committed
Auto merge of #145647 - RalfJung:miri, r=RalfJung
miri subtree update Subtree update of `miri` to rust-lang/miri@980da67. Created using https://github.com/rust-lang/josh-sync. r? `@ghost`
2 parents 57e620e + 4676552 commit 6ba0ce4

File tree

70 files changed

+214
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+214
-51
lines changed

src/tools/miri/.github/workflows/ci.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ jobs:
6969
sudo apt update
7070
# Install needed packages
7171
sudo apt install $(echo "libatomic1: zlib1g-dev:" | sed 's/:/:${{ matrix.multiarch }}/g')
72-
- name: Install rustup on Windows ARM
73-
if: ${{ matrix.os == 'windows-11-arm' }}
74-
run: |
75-
curl -LOs https://static.rust-lang.org/rustup/dist/aarch64-pc-windows-msvc/rustup-init.exe
76-
./rustup-init.exe -y --no-modify-path
77-
echo "$USERPROFILE/.cargo/bin" >> "$GITHUB_PATH"
7872
- uses: ./.github/workflows/setup
7973
with:
8074
toolchain_flags: "--host ${{ matrix.host_target }}"
@@ -169,7 +163,13 @@ jobs:
169163
run: rustup toolchain install nightly --profile minimal
170164
- name: Install rustup-toolchain-install-master
171165
run: cargo install -f rustup-toolchain-install-master
172-
- name: Push changes to a branch and create PR
166+
# Create a token for the next step so it can create a PR that actually runs CI.
167+
- uses: actions/create-github-app-token@v2
168+
id: app-token
169+
with:
170+
app-id: ${{ vars.APP_CLIENT_ID }}
171+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
172+
- name: pull changes from rustc and create PR
173173
run: |
174174
# Make it easier to see what happens.
175175
set -x
@@ -198,9 +198,9 @@ jobs:
198198
BRANCH="rustup-$(date -u +%Y-%m-%d)"
199199
git switch -c $BRANCH
200200
git push -u origin $BRANCH
201-
gh pr create -B master --title 'Automatic Rustup' --body 'Please close and re-open this PR to trigger CI, then enable auto-merge.'
201+
gh pr create -B master --title 'Automatic Rustup' --body "Update \`rustc\` to https://github.com/rust-lang/rust/commit/$(cat rust-version)."
202202
env:
203-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
203+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
204204

205205
cron-fail-notify:
206206
name: cronjob failure notification

src/tools/miri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ native-lib = ["dep:libffi", "dep:libloading", "dep:capstone", "dep:ipc-channel",
7878

7979
[lints.rust.unexpected_cfgs]
8080
level = "warn"
81+
check-cfg = ['cfg(bootstrap)']
8182

8283
# Be aware that this file is inside a workspace when used via the
8384
# submodule in the rustc repo. That means there are many cargo features

src/tools/miri/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
733dab558992d902d6d17576de1da768094e2cf3
1+
f605b57042ffeb320d7ae44490113a827139b766

src/tools/miri/src/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ pub fn report_error<'tcx>(
282282
},
283283
TreeBorrowsUb { title: _, details, history } => {
284284
let mut helps = vec![
285-
note!("this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental")
285+
note!("this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental"),
286+
note!("see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/tree-borrows.md for further information"),
286287
];
287288
for m in details {
288289
helps.push(note!("{m}"));

src/tools/miri/src/machine.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use rand::rngs::StdRng;
1212
use rand::{Rng, SeedableRng};
1313
use rustc_abi::{Align, ExternAbi, Size};
1414
use rustc_apfloat::{Float, FloatConvert};
15-
use rustc_hir::attrs::InlineAttr;
1615
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1716
#[allow(unused)]
1817
use rustc_data_structures::static_assert_size;
18+
use rustc_hir::attrs::InlineAttr;
1919
use rustc_middle::mir;
2020
use rustc_middle::query::TyCtxtAt;
2121
use rustc_middle::ty::layout::{
@@ -1396,6 +1396,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
13961396
GlobalDataRaceHandler::Genmc(genmc_ctx) =>
13971397
genmc_ctx.memory_load(machine, ptr.addr(), range.size)?,
13981398
GlobalDataRaceHandler::Vclocks(_data_race) => {
1399+
let _trace = enter_trace_span!(data_race::before_memory_read);
13991400
let AllocDataRaceHandler::Vclocks(data_race, weak_memory) = &alloc_extra.data_race
14001401
else {
14011402
unreachable!();
@@ -1431,6 +1432,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
14311432
genmc_ctx.memory_store(machine, ptr.addr(), range.size)?;
14321433
}
14331434
GlobalDataRaceHandler::Vclocks(_global_state) => {
1435+
let _trace = enter_trace_span!(data_race::before_memory_write);
14341436
let AllocDataRaceHandler::Vclocks(data_race, weak_memory) =
14351437
&mut alloc_extra.data_race
14361438
else {
@@ -1467,6 +1469,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
14671469
GlobalDataRaceHandler::Genmc(genmc_ctx) =>
14681470
genmc_ctx.handle_dealloc(machine, ptr.addr(), size, align, kind)?,
14691471
GlobalDataRaceHandler::Vclocks(_global_state) => {
1472+
let _trace = enter_trace_span!(data_race::before_memory_deallocation);
14701473
let data_race = alloc_extra.data_race.as_vclocks_mut().unwrap();
14711474
data_race.write(
14721475
alloc_id,
@@ -1677,6 +1680,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
16771680
local: mir::Local,
16781681
) -> InterpResult<'tcx> {
16791682
if let Some(data_race) = &frame.extra.data_race {
1683+
let _trace = enter_trace_span!(data_race::after_local_read);
16801684
data_race.local_read(local, &ecx.machine);
16811685
}
16821686
interp_ok(())
@@ -1688,6 +1692,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
16881692
storage_live: bool,
16891693
) -> InterpResult<'tcx> {
16901694
if let Some(data_race) = &ecx.frame().extra.data_race {
1695+
let _trace = enter_trace_span!(data_race::after_local_write);
16911696
data_race.local_write(local, storage_live, &ecx.machine);
16921697
}
16931698
interp_ok(())
@@ -1710,6 +1715,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
17101715
if let Some(data_race) =
17111716
&machine.threads.active_thread_stack().last().unwrap().extra.data_race
17121717
{
1718+
let _trace = enter_trace_span!(data_race::after_local_moved_to_memory);
17131719
data_race.local_moved_to_memory(
17141720
local,
17151721
alloc_info.data_race.as_vclocks_mut().unwrap(),

src/tools/miri/src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5757
let ptr = left.to_scalar().to_pointer(this)?;
5858
// We do the actual operation with usize-typed scalars.
5959
let left = ImmTy::from_uint(ptr.addr().bytes(), this.machine.layouts.usize);
60-
let result = this.binary_op(bin_op, &left, &right)?;
60+
let result = this.binary_op(bin_op, &left, right)?;
6161
// Construct a new pointer with the provenance of `ptr` (the LHS).
6262
let result_ptr = Pointer::new(
6363
ptr.provenance,

src/tools/miri/src/shims/extern_static.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'tcx> MiriMachine<'tcx> {
6262
}
6363
"android" => {
6464
Self::null_ptr_extern_statics(ecx, &["bsd_signal"])?;
65-
Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom"])?;
65+
Self::weak_symbol_extern_statics(ecx, &["signal", "getrandom", "gettid"])?;
6666
}
6767
"windows" => {
6868
// "_tls_used"

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

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::Path;
55
use rustc_abi::{Align, AlignFromBytesError, CanonAbi, Size};
66
use rustc_apfloat::Float;
77
use rustc_ast::expand::allocator::alloc_error_handler_name;
8+
use rustc_hir::attrs::Linkage;
89
use rustc_hir::def::DefKind;
910
use rustc_hir::def_id::CrateNum;
1011
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -138,7 +139,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
138139
Entry::Occupied(e) => e.into_mut(),
139140
Entry::Vacant(e) => {
140141
// Find it if it was not cached.
141-
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
142+
143+
struct SymbolTarget<'tcx> {
144+
instance: ty::Instance<'tcx>,
145+
cnum: CrateNum,
146+
is_weak: bool,
147+
}
148+
let mut symbol_target: Option<SymbolTarget<'tcx>> = None;
142149
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
143150
let attrs = tcx.codegen_fn_attrs(def_id);
144151
// Skip over imports of items.
@@ -155,40 +162,80 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
155162

156163
let instance = Instance::mono(tcx, def_id);
157164
let symbol_name = tcx.symbol_name(instance).name;
165+
let is_weak = attrs.linkage == Some(Linkage::WeakAny);
158166
if symbol_name == link_name.as_str() {
159-
if let Some((original_instance, original_cnum)) = instance_and_crate {
160-
// Make sure we are consistent wrt what is 'first' and 'second'.
161-
let original_span = tcx.def_span(original_instance.def_id()).data();
162-
let span = tcx.def_span(def_id).data();
163-
if original_span < span {
164-
throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
165-
link_name,
166-
first: original_span,
167-
first_crate: tcx.crate_name(original_cnum),
168-
second: span,
169-
second_crate: tcx.crate_name(cnum),
170-
});
171-
} else {
172-
throw_machine_stop!(TerminationInfo::MultipleSymbolDefinitions {
173-
link_name,
174-
first: span,
175-
first_crate: tcx.crate_name(cnum),
176-
second: original_span,
177-
second_crate: tcx.crate_name(original_cnum),
178-
});
167+
if let Some(original) = &symbol_target {
168+
// There is more than one definition with this name. What we do now
169+
// depends on whether one or both definitions are weak.
170+
match (is_weak, original.is_weak) {
171+
(false, true) => {
172+
// Original definition is a weak definition. Override it.
173+
174+
symbol_target = Some(SymbolTarget {
175+
instance: ty::Instance::mono(tcx, def_id),
176+
cnum,
177+
is_weak,
178+
});
179+
}
180+
(true, false) => {
181+
// Current definition is a weak definition. Keep the original one.
182+
}
183+
(true, true) | (false, false) => {
184+
// Either both definitions are non-weak or both are weak. In
185+
// either case return an error. For weak definitions we error
186+
// because it is unspecified which definition would have been
187+
// picked by the linker.
188+
189+
// Make sure we are consistent wrt what is 'first' and 'second'.
190+
let original_span =
191+
tcx.def_span(original.instance.def_id()).data();
192+
let span = tcx.def_span(def_id).data();
193+
if original_span < span {
194+
throw_machine_stop!(
195+
TerminationInfo::MultipleSymbolDefinitions {
196+
link_name,
197+
first: original_span,
198+
first_crate: tcx.crate_name(original.cnum),
199+
second: span,
200+
second_crate: tcx.crate_name(cnum),
201+
}
202+
);
203+
} else {
204+
throw_machine_stop!(
205+
TerminationInfo::MultipleSymbolDefinitions {
206+
link_name,
207+
first: span,
208+
first_crate: tcx.crate_name(cnum),
209+
second: original_span,
210+
second_crate: tcx.crate_name(original.cnum),
211+
}
212+
);
213+
}
214+
}
179215
}
216+
} else {
217+
symbol_target = Some(SymbolTarget {
218+
instance: ty::Instance::mono(tcx, def_id),
219+
cnum,
220+
is_weak,
221+
});
180222
}
181-
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
182-
throw_ub_format!(
183-
"attempt to call an exported symbol that is not defined as a function"
184-
);
185-
}
186-
instance_and_crate = Some((ty::Instance::mono(tcx, def_id), cnum));
187223
}
188224
interp_ok(())
189225
})?;
190226

191-
e.insert(instance_and_crate.map(|ic| ic.0))
227+
// Once we identified the instance corresponding to the symbol, ensure
228+
// it is a function. It is okay to encounter non-functions in the search above
229+
// as long as the final instance we arrive at is a function.
230+
if let Some(SymbolTarget { instance, .. }) = symbol_target {
231+
if !matches!(tcx.def_kind(instance.def_id()), DefKind::Fn | DefKind::AssocFn) {
232+
throw_ub_format!(
233+
"attempt to call an exported symbol that is not defined as a function"
234+
);
235+
}
236+
}
237+
238+
e.insert(symbol_target.map(|SymbolTarget { instance, .. }| instance))
192239
}
193240
};
194241
match instance {

src/tools/miri/src/shims/native_lib/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
246246
let p_map = alloc.provenance();
247247
for idx in overlap {
248248
// If a provenance was read by the foreign code, expose it.
249-
if let Some((prov, _idx)) = p_map.get_byte(Size::from_bytes(idx), this) {
249+
if let Some((prov, _idx)) = p_map.get_byte(Size::from_bytes(idx), this)
250+
{
250251
this.expose_provenance(prov)?;
251252
}
252253
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use rustc_span::Symbol;
44
use rustc_target::callconv::FnAbi;
55

66
use crate::shims::unix::android::thread::prctl;
7+
use crate::shims::unix::env::EvalContextExt as _;
78
use crate::shims::unix::linux_like::epoll::EvalContextExt as _;
89
use crate::shims::unix::linux_like::eventfd::EvalContextExt as _;
910
use crate::shims::unix::linux_like::syscall::syscall;
1011
use crate::*;
1112

12-
pub fn is_dyn_sym(_name: &str) -> bool {
13-
false
13+
pub fn is_dyn_sym(name: &str) -> bool {
14+
matches!(name, "gettid")
1415
}
1516

1617
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
@@ -54,6 +55,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5455
this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
5556
}
5657

58+
"gettid" => {
59+
let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
60+
let result = this.unix_gettid(link_name.as_str())?;
61+
this.write_scalar(result, dest)?;
62+
}
63+
5764
// Dynamically invoked syscalls
5865
"syscall" => syscall(this, link_name, abi, args, dest)?,
5966

0 commit comments

Comments
 (0)