Skip to content

Rollup of 11 pull requests #120496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a984193
Deduplicate more sized errors on call exprs
estebank Jan 24, 2024
e23937c
adapt test for v0 symbol mangling
krasimirgg Jan 24, 2024
f68741b
Stop checking `err_count` in macro_rules validity checking
oli-obk Jan 25, 2024
f6f0e04
Remove an unused error count check
oli-obk Jan 25, 2024
646c8fc
Statically ensure an error is reported in report_arg_errors
oli-obk Jan 25, 2024
2b60e56
Statically ensure report_selection_error actually reports an error
oli-obk Jan 25, 2024
3042da0
Remove has_errors check in builtin macro parsing
oli-obk Jan 25, 2024
054e1e3
Track ErrorGuaranteed instead of conjuring it from thin air
oli-obk Jan 25, 2024
9199742
Revert "Add the wasm32-wasi-preview2 target"
fmease Jan 28, 2024
6837b81
Fix some `Arc` allocator leaks
Nemo157 Jan 28, 2024
492df34
Supress unhelpful diagnostics for unresolved top level attributes
chenyukang Jan 27, 2024
6468d44
Improve error message when `cargo build` is used to build the compiler
Noratrieb Jan 29, 2024
96e6cfa
Improve display of crate name when hovered
GuillaumeGomez Jan 29, 2024
b4efe07
Remove some unnecessary check logic for lang items in HIR typeck
compiler-errors Jan 29, 2024
bf4de3a
Remove `raw_os_nonzero` feature.
reitermarkus Jan 24, 2024
ad526d8
add missing potential_query_instability for keys and values in hashmap
chenyukang Jan 30, 2024
efff267
Rollup merge of #117906 - GuillaumeGomez:improve-crate-name-hover, r=…
GuillaumeGomez Jan 30, 2024
ee2e9e1
Rollup merge of #118533 - chenyukang:yukang-fix-118455, r=petrochenkov
GuillaumeGomez Jan 30, 2024
0a4fd52
Rollup merge of #120293 - estebank:issue-102629, r=nnethercote
GuillaumeGomez Jan 30, 2024
4f4ceef
Rollup merge of #120295 - reitermarkus:remove-ffi-nonzero, r=dtolnay
GuillaumeGomez Jan 30, 2024
6a1d34f
Rollup merge of #120310 - krasimirgg:jan-v0-sym, r=Mark-Simulacrum
GuillaumeGomez Jan 30, 2024
b28e6f1
Rollup merge of #120342 - oli-obk:track_errors6, r=nnethercote
GuillaumeGomez Jan 30, 2024
d10f33a
Rollup merge of #120434 - fmease:revert-speeder, r=petrochenkov
GuillaumeGomez Jan 30, 2024
a5aa355
Rollup merge of #120445 - Nemo157:arc-plug, r=Mark-Simulacrum
GuillaumeGomez Jan 30, 2024
399b81f
Rollup merge of #120475 - Nilstrieb:cargo-build-my-a-, r=michaelwoeri…
GuillaumeGomez Jan 30, 2024
f3f1472
Rollup merge of #120476 - compiler-errors:lang-items-yeet, r=Nilstrieb
GuillaumeGomez Jan 30, 2024
27bc496
Rollup merge of #120485 - chenyukang:yukang-add-query-instability-che…
GuillaumeGomez Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix some Arc allocator leaks
This doesn't matter for the stable `Global` allocator as it is a ZST
singleton, but other allocators may rely on all instances being dropped.
  • Loading branch information
Nemo157 committed Jan 28, 2024
commit 6837b812e69a0c024021e3a828bcdc5c7d8f1b4d
44 changes: 20 additions & 24 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ impl<T: ?Sized> Arc<T> {
}

impl<T: ?Sized, A: Allocator> Arc<T, A> {
#[inline]
fn internal_into_inner_with_allocator(self) -> (NonNull<ArcInner<T>>, A) {
let this = mem::ManuallyDrop::new(self);
(this.ptr, unsafe { ptr::read(&this.alloc) })
}

#[inline]
unsafe fn from_inner_in(ptr: NonNull<ArcInner<T>>, alloc: A) -> Self {
Self { ptr, phantom: PhantomData, alloc }
Expand Down Expand Up @@ -1271,12 +1277,9 @@ impl<T, A: Allocator> Arc<mem::MaybeUninit<T>, A> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub unsafe fn assume_init(self) -> Arc<T, A>
where
A: Clone,
{
let md_self = mem::ManuallyDrop::new(self);
unsafe { Arc::from_inner_in(md_self.ptr.cast(), md_self.alloc.clone()) }
pub unsafe fn assume_init(self) -> Arc<T, A> {
let (ptr, alloc) = self.internal_into_inner_with_allocator();
unsafe { Arc::from_inner_in(ptr.cast(), alloc) }
}
}

Expand Down Expand Up @@ -1316,12 +1319,9 @@ impl<T, A: Allocator> Arc<[mem::MaybeUninit<T>], A> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub unsafe fn assume_init(self) -> Arc<[T], A>
where
A: Clone,
{
let md_self = mem::ManuallyDrop::new(self);
unsafe { Arc::from_ptr_in(md_self.ptr.as_ptr() as _, md_self.alloc.clone()) }
pub unsafe fn assume_init(self) -> Arc<[T], A> {
let (ptr, alloc) = self.internal_into_inner_with_allocator();
unsafe { Arc::from_ptr_in(ptr.as_ptr() as _, alloc) }
}
}

Expand Down Expand Up @@ -2409,7 +2409,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Arc<T, A> {
}
}

impl<A: Allocator + Clone> Arc<dyn Any + Send + Sync, A> {
impl<A: Allocator> Arc<dyn Any + Send + Sync, A> {
/// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
///
/// # Examples
Expand All @@ -2436,10 +2436,8 @@ impl<A: Allocator + Clone> Arc<dyn Any + Send + Sync, A> {
{
if (*self).is::<T>() {
unsafe {
let ptr = self.ptr.cast::<ArcInner<T>>();
let alloc = self.alloc.clone();
mem::forget(self);
Ok(Arc::from_inner_in(ptr, alloc))
let (ptr, alloc) = self.internal_into_inner_with_allocator();
Ok(Arc::from_inner_in(ptr.cast(), alloc))
}
} else {
Err(self)
Expand Down Expand Up @@ -2479,10 +2477,8 @@ impl<A: Allocator + Clone> Arc<dyn Any + Send + Sync, A> {
T: Any + Send + Sync,
{
unsafe {
let ptr = self.ptr.cast::<ArcInner<T>>();
let alloc = self.alloc.clone();
mem::forget(self);
Arc::from_inner_in(ptr, alloc)
let (ptr, alloc) = self.internal_into_inner_with_allocator();
Arc::from_inner_in(ptr.cast(), alloc)
}
}
}
Expand Down Expand Up @@ -3438,13 +3434,13 @@ impl From<Arc<str>> for Arc<[u8]> {
}

#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
impl<T, A: Allocator + Clone, const N: usize> TryFrom<Arc<[T], A>> for Arc<[T; N], A> {
impl<T, A: Allocator, const N: usize> TryFrom<Arc<[T], A>> for Arc<[T; N], A> {
type Error = Arc<[T], A>;

fn try_from(boxed_slice: Arc<[T], A>) -> Result<Self, Self::Error> {
if boxed_slice.len() == N {
let alloc = boxed_slice.alloc.clone();
Ok(unsafe { Arc::from_raw_in(Arc::into_raw(boxed_slice) as *mut [T; N], alloc) })
let (ptr, alloc) = boxed_slice.internal_into_inner_with_allocator();
Ok(unsafe { Arc::from_inner_in(ptr.cast(), alloc) })
} else {
Err(boxed_slice)
}
Expand Down
65 changes: 60 additions & 5 deletions library/alloc/src/sync/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::*;

use std::clone::Clone;
use std::mem::MaybeUninit;
use std::option::Option::None;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::mpsc::channel;
use std::sync::Mutex;
use std::thread;

struct Canary(*mut atomic::AtomicUsize);
struct Canary(*mut AtomicUsize);

impl Drop for Canary {
fn drop(&mut self) {
Expand All @@ -21,6 +23,37 @@ impl Drop for Canary {
}
}

struct AllocCanary<'a>(&'a AtomicUsize);

impl<'a> AllocCanary<'a> {
fn new(counter: &'a AtomicUsize) -> Self {
counter.fetch_add(1, SeqCst);
Self(counter)
}
}

unsafe impl Allocator for AllocCanary<'_> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
std::alloc::Global.allocate(layout)
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { std::alloc::Global.deallocate(ptr, layout) }
}
}

impl Clone for AllocCanary<'_> {
fn clone(&self) -> Self {
Self::new(self.0)
}
}

impl Drop for AllocCanary<'_> {
fn drop(&mut self) {
self.0.fetch_sub(1, SeqCst);
}
}

#[test]
#[cfg_attr(target_os = "emscripten", ignore)]
fn manually_share_arc() {
Expand Down Expand Up @@ -295,16 +328,16 @@ fn weak_self_cyclic() {

#[test]
fn drop_arc() {
let mut canary = atomic::AtomicUsize::new(0);
let x = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize));
let mut canary = AtomicUsize::new(0);
let x = Arc::new(Canary(&mut canary as *mut AtomicUsize));
drop(x);
assert!(canary.load(Acquire) == 1);
}

#[test]
fn drop_arc_weak() {
let mut canary = atomic::AtomicUsize::new(0);
let arc = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize));
let mut canary = AtomicUsize::new(0);
let arc = Arc::new(Canary(&mut canary as *mut AtomicUsize));
let arc_weak = Arc::downgrade(&arc);
assert!(canary.load(Acquire) == 0);
drop(arc);
Expand Down Expand Up @@ -660,3 +693,25 @@ fn arc_drop_dereferenceable_race() {
thread.join().unwrap();
}
}

#[test]
fn arc_doesnt_leak_allocator() {
let counter = AtomicUsize::new(0);

{
let arc: Arc<dyn Any + Send + Sync, _> = Arc::new_in(5usize, AllocCanary::new(&counter));
drop(arc.downcast::<usize>().unwrap());

let arc: Arc<dyn Any + Send + Sync, _> = Arc::new_in(5usize, AllocCanary::new(&counter));
drop(unsafe { arc.downcast_unchecked::<usize>() });

let arc = Arc::new_in(MaybeUninit::<usize>::new(5usize), AllocCanary::new(&counter));
drop(unsafe { arc.assume_init() });

let arc: Arc<[MaybeUninit<usize>], _> =
Arc::new_zeroed_slice_in(5, AllocCanary::new(&counter));
drop(unsafe { arc.assume_init() });
}

assert_eq!(counter.load(SeqCst), 0);
}