Skip to content

Commit 548dcbb

Browse files
committed
Auto merge of #147692 - matthiaskrgr:rollup-bqhlwyw, r=matthiaskrgr
Rollup of 12 pull requests Successful merges: - rust-lang/rust#146187 (Unstably constify `ptr::drop_in_place` and related methods) - rust-lang/rust#146503 (std: improve handling of timed condition variable waits on macOS) - rust-lang/rust#147526 (Move computation of allocator shim contents to cg_ssa) - rust-lang/rust#147630 (Bitset cleanups) - rust-lang/rust#147638 (bpf: return results larger than one register indirectly) - rust-lang/rust#147666 (Replace manual implementation with `carrying_mul_add`) - rust-lang/rust#147669 (fix missing link to `std::char` in `std` docs) - rust-lang/rust#147673 (pretty print u128 with display) - rust-lang/rust#147677 (Fewer exceptions in `span()` on parsed attributes) - rust-lang/rust#147680 (Fix ICE caused by associated_item_def_ids on wrong type in resolve diag) - rust-lang/rust#147682 (convert `rustc_main` to the new attribute parsing infrastructure) - rust-lang/rust#147683 (only check duplicates on old/unparsed attributes) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a2b1ce7 + 4cc3262 commit 548dcbb

File tree

10 files changed

+89
-21
lines changed

10 files changed

+89
-21
lines changed

src/shims/foreign_items.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::path::Path;
44

55
use rustc_abi::{Align, AlignFromBytesError, CanonAbi, Size};
6-
use rustc_ast::expand::allocator::alloc_error_handler_name;
6+
use rustc_ast::expand::allocator::AllocatorKind;
77
use rustc_hir::attrs::Linkage;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::CrateNum;
@@ -52,6 +52,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5252

5353
// Some shims forward to other MIR bodies.
5454
match link_name.as_str() {
55+
// This allocator function has forwarding shims synthesized during normal codegen
56+
// (see `allocator_shim_contents`); this is where we emulate that behavior.
57+
// FIXME should use global_fn_name, but mangle_internal_symbol requires a static str.
5558
name if name == this.mangle_internal_symbol("__rust_alloc_error_handler") => {
5659
// Forward to the right symbol that implements this function.
5760
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
@@ -60,12 +63,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6063
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6164
);
6265
};
63-
let name = Symbol::intern(
64-
this.mangle_internal_symbol(alloc_error_handler_name(handler_kind)),
65-
);
66-
let handler =
67-
this.lookup_exported_symbol(name)?.expect("missing alloc error handler symbol");
68-
return interp_ok(Some(handler));
66+
if handler_kind == AllocatorKind::Default {
67+
let name =
68+
Symbol::intern(this.mangle_internal_symbol("__rdl_alloc_error_handler"));
69+
let handler = this
70+
.lookup_exported_symbol(name)?
71+
.expect("missing alloc error handler symbol");
72+
return interp_ok(Some(handler));
73+
}
74+
// Fall through to the `lookup_exported_symbol` below which should find
75+
// a `__rust_alloc_error_handler`.
6976
}
7077
_ => {}
7178
}

src/shims/unix/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
815815
"pthread_cond_timedwait" => {
816816
let [cond, mutex, abstime] =
817817
this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
818-
this.pthread_cond_timedwait(cond, mutex, abstime, dest)?;
818+
this.pthread_cond_timedwait(cond, mutex, abstime, dest, /* macos_relative_np */ false)?;
819819
}
820820
"pthread_cond_destroy" => {
821821
let [cond] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;

src/shims/unix/macos/foreign_items.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
307307
this.os_unfair_lock_assert_not_owner(lock_op)?;
308308
}
309309

310+
"pthread_cond_timedwait_relative_np" => {
311+
let [cond, mutex, reltime] =
312+
this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
313+
this.pthread_cond_timedwait(cond, mutex, reltime, dest, /* macos_relative_np */ true)?;
314+
}
315+
310316
_ => return interp_ok(EmulateItemResult::NotSupported),
311317
};
312318

src/shims/unix/sync.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
834834
&mut self,
835835
cond_op: &OpTy<'tcx>,
836836
mutex_op: &OpTy<'tcx>,
837-
abstime_op: &OpTy<'tcx>,
837+
timeout_op: &OpTy<'tcx>,
838838
dest: &MPlaceTy<'tcx>,
839+
macos_relative_np: bool,
839840
) -> InterpResult<'tcx> {
840841
let this = self.eval_context_mut();
841842

@@ -844,7 +845,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
844845

845846
// Extract the timeout.
846847
let duration = match this
847-
.read_timespec(&this.deref_pointer_as(abstime_op, this.libc_ty_layout("timespec"))?)?
848+
.read_timespec(&this.deref_pointer_as(timeout_op, this.libc_ty_layout("timespec"))?)?
848849
{
849850
Some(duration) => duration,
850851
None => {
@@ -853,14 +854,23 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
853854
return interp_ok(());
854855
}
855856
};
856-
if data.clock == TimeoutClock::RealTime {
857-
this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?;
858-
}
857+
858+
let (clock, anchor) = if macos_relative_np {
859+
// `pthread_cond_timedwait_relative_np` always measures time against the
860+
// monotonic clock, regardless of the condvar clock.
861+
(TimeoutClock::Monotonic, TimeoutAnchor::Relative)
862+
} else {
863+
if data.clock == TimeoutClock::RealTime {
864+
this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?;
865+
}
866+
867+
(data.clock, TimeoutAnchor::Absolute)
868+
};
859869

860870
this.condvar_wait(
861871
data.condvar_ref,
862872
mutex_ref,
863-
Some((data.clock, TimeoutAnchor::Absolute, duration)),
873+
Some((clock, anchor, duration)),
864874
Scalar::from_i32(0),
865875
this.eval_libc("ETIMEDOUT"), // retval_timeout
866876
dest.clone(),

tests/fail/alloc/alloc_error_handler.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | crate::process::abort()
77
|
88
= note: BACKTRACE:
99
= note: inside `std::alloc::rust_oom` at RUSTLIB/std/src/alloc.rs:LL:CC
10-
= note: inside `std::alloc::_::__rg_oom` at RUSTLIB/std/src/alloc.rs:LL:CC
10+
= note: inside `std::alloc::_::__rust_alloc_error_handler` at RUSTLIB/std/src/alloc.rs:LL:CC
1111
= note: inside `std::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1212
= note: inside `std::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1313
note: inside `main`

tests/fail/alloc/alloc_error_handler_custom.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | core::intrinsics::abort();
77
|
88
= note: BACKTRACE:
99
= note: inside `alloc_error_handler` at tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC
10-
note: inside `_::__rg_oom`
10+
note: inside `_::__rust_alloc_error_handler`
1111
--> tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC
1212
|
1313
LL | #[alloc_error_handler]

tests/fail/alloc/alloc_error_handler_no_std.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | core::intrinsics::abort();
99
|
1010
= note: BACKTRACE:
1111
= note: inside `panic_handler` at tests/fail/alloc/alloc_error_handler_no_std.rs:LL:CC
12-
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_oom` at RUSTLIB/alloc/src/alloc.rs:LL:CC
12+
= note: inside `alloc::alloc::__alloc_error_handler::__rdl_alloc_error_handler` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1313
= note: inside `alloc::alloc::handle_alloc_error::rt_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1414
= note: inside `alloc::alloc::handle_alloc_error` at RUSTLIB/alloc/src/alloc.rs:LL:CC
1515
note: inside `miri_start`

tests/fail/stacked_borrows/drop_in_place_retag.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error: Undefined Behavior: trying to retag from <TAG> for Unique permission at ALLOC[0x0], but that tag only grants SharedReadOnly permission for this location
22
--> RUSTLIB/core/src/ptr/mod.rs:LL:CC
33
|
4-
LL | pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this error occurs as part of retag at ALLOC[0x0..0x1]
4+
LL | / pub const unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T)
5+
LL | | where
6+
LL | | T: [const] Destruct,
7+
| |________________________^ this error occurs as part of retag at ALLOC[0x0..0x1]
68
|
79
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
810
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

tests/fail/unaligned_pointers/drop_in_place.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required ALIGN byte alignment but found ALIGN)
22
--> RUSTLIB/core/src/ptr/mod.rs:LL:CC
33
|
4-
LL | pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
4+
LL | / pub const unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T)
5+
LL | | where
6+
LL | | T: [const] Destruct,
7+
| |________________________^ Undefined Behavior occurred here
68
|
79
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
810
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@only-target: apple # `pthread_cond_timedwait_relative_np` is a non-standard extension
2+
3+
use std::time::Instant;
4+
5+
// FIXME: remove once this is in libc.
6+
mod libc {
7+
pub use ::libc::*;
8+
unsafe extern "C" {
9+
pub unsafe fn pthread_cond_timedwait_relative_np(
10+
cond: *mut libc::pthread_cond_t,
11+
lock: *mut libc::pthread_mutex_t,
12+
timeout: *const libc::timespec,
13+
) -> libc::c_int;
14+
}
15+
}
16+
17+
fn main() {
18+
unsafe {
19+
let mut mutex: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
20+
let mut cond: libc::pthread_cond_t = libc::PTHREAD_COND_INITIALIZER;
21+
22+
// Wait for 100 ms.
23+
let timeout = libc::timespec { tv_sec: 0, tv_nsec: 100_000_000 };
24+
25+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
26+
27+
let current_time = Instant::now();
28+
assert_eq!(
29+
libc::pthread_cond_timedwait_relative_np(&mut cond, &mut mutex, &timeout),
30+
libc::ETIMEDOUT
31+
);
32+
let elapsed_time = current_time.elapsed().as_millis();
33+
// This is actually deterministic (since isolation remains enabled),
34+
// but can change slightly with Rust updates.
35+
assert!(90 <= elapsed_time && elapsed_time <= 110);
36+
37+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex), 0);
38+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex), 0);
39+
assert_eq!(libc::pthread_cond_destroy(&mut cond), 0);
40+
}
41+
}

0 commit comments

Comments
 (0)