Skip to content

Commit fd8b135

Browse files
authored
Merge pull request #191 from jacob-hughes/untracked-box
Prevent unsound Box allocations
2 parents ed2408f + b3a50b4 commit fd8b135

File tree

8 files changed

+39
-2
lines changed

8 files changed

+39
-2
lines changed

library/alloc/src/boxed.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,15 @@ impl<T, A: Allocator> Box<T, A> {
401401
/// # Examples
402402
///
403403
/// ```
404+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
404405
/// #![feature(allocator_api)]
405406
///
406407
/// use std::alloc::System;
407408
///
408409
/// let five = Box::new_in(5, System);
409410
/// ```
410411
#[cfg(not(no_global_oom_handling))]
412+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
411413
#[unstable(feature = "allocator_api", issue = "32838")]
412414
#[must_use]
413415
#[inline]
@@ -429,13 +431,15 @@ impl<T, A: Allocator> Box<T, A> {
429431
///
430432
/// ```
431433
/// #![feature(allocator_api)]
434+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
432435
///
433436
/// use std::alloc::System;
434437
///
435438
/// let five = Box::try_new_in(5, System)?;
436439
/// # Ok::<(), std::alloc::AllocError>(())
437440
/// ```
438441
#[unstable(feature = "allocator_api", issue = "32838")]
442+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
439443
#[inline]
440444
pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
441445
where
@@ -452,6 +456,7 @@ impl<T, A: Allocator> Box<T, A> {
452456
///
453457
/// ```
454458
/// #![feature(allocator_api)]
459+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
455460
///
456461
/// use std::alloc::System;
457462
///
@@ -463,6 +468,7 @@ impl<T, A: Allocator> Box<T, A> {
463468
/// assert_eq!(*five, 5)
464469
/// ```
465470
#[unstable(feature = "allocator_api", issue = "32838")]
471+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
466472
#[cfg(not(no_global_oom_handling))]
467473
#[must_use]
468474
// #[unstable(feature = "new_uninit", issue = "63291")]
@@ -486,6 +492,7 @@ impl<T, A: Allocator> Box<T, A> {
486492
///
487493
/// ```
488494
/// #![feature(allocator_api)]
495+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
489496
///
490497
/// use std::alloc::System;
491498
///
@@ -499,6 +506,7 @@ impl<T, A: Allocator> Box<T, A> {
499506
/// ```
500507
#[unstable(feature = "allocator_api", issue = "32838")]
501508
// #[unstable(feature = "new_uninit", issue = "63291")]
509+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
502510
pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
503511
where
504512
A: Allocator,
@@ -522,6 +530,7 @@ impl<T, A: Allocator> Box<T, A> {
522530
///
523531
/// ```
524532
/// #![feature(allocator_api)]
533+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
525534
///
526535
/// use std::alloc::System;
527536
///
@@ -533,6 +542,7 @@ impl<T, A: Allocator> Box<T, A> {
533542
///
534543
/// [zeroed]: mem::MaybeUninit::zeroed
535544
#[unstable(feature = "allocator_api", issue = "32838")]
545+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
536546
#[cfg(not(no_global_oom_handling))]
537547
// #[unstable(feature = "new_uninit", issue = "63291")]
538548
#[must_use]
@@ -560,6 +570,7 @@ impl<T, A: Allocator> Box<T, A> {
560570
///
561571
/// ```
562572
/// #![feature(allocator_api)]
573+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
563574
///
564575
/// use std::alloc::System;
565576
///
@@ -573,6 +584,7 @@ impl<T, A: Allocator> Box<T, A> {
573584
/// [zeroed]: mem::MaybeUninit::zeroed
574585
#[unstable(feature = "allocator_api", issue = "32838")]
575586
// #[unstable(feature = "new_uninit", issue = "63291")]
587+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
576588
pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
577589
where
578590
A: Allocator,
@@ -594,6 +606,7 @@ impl<T, A: Allocator> Box<T, A> {
594606
/// [`into_pin`](Box::into_pin) if you already have a `Box<T, A>`, or if you want to
595607
/// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
596608
#[cfg(not(no_global_oom_handling))]
609+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
597610
#[unstable(feature = "allocator_api", issue = "32838")]
598611
#[must_use]
599612
#[inline(always)]
@@ -773,6 +786,7 @@ impl<T, A: Allocator> Box<[T], A> {
773786
///
774787
/// ```
775788
/// #![feature(allocator_api)]
789+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
776790
///
777791
/// use std::alloc::System;
778792
///
@@ -786,6 +800,7 @@ impl<T, A: Allocator> Box<[T], A> {
786800
/// assert_eq!(*values, [1, 2, 3])
787801
/// ```
788802
#[cfg(not(no_global_oom_handling))]
803+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
789804
#[unstable(feature = "allocator_api", issue = "32838")]
790805
// #[unstable(feature = "new_uninit", issue = "63291")]
791806
#[must_use]
@@ -803,6 +818,7 @@ impl<T, A: Allocator> Box<[T], A> {
803818
///
804819
/// ```
805820
/// #![feature(allocator_api)]
821+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
806822
///
807823
/// use std::alloc::System;
808824
///
@@ -814,6 +830,7 @@ impl<T, A: Allocator> Box<[T], A> {
814830
///
815831
/// [zeroed]: mem::MaybeUninit::zeroed
816832
#[cfg(not(no_global_oom_handling))]
833+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
817834
#[unstable(feature = "allocator_api", issue = "32838")]
818835
// #[unstable(feature = "new_uninit", issue = "63291")]
819836
#[must_use]
@@ -828,6 +845,7 @@ impl<T, A: Allocator> Box<[T], A> {
828845
///
829846
/// ```
830847
/// #![feature(allocator_api)]
848+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
831849
///
832850
/// use std::alloc::System;
833851
///
@@ -842,6 +860,7 @@ impl<T, A: Allocator> Box<[T], A> {
842860
/// # Ok::<(), std::alloc::AllocError>(())
843861
/// ```
844862
#[unstable(feature = "allocator_api", issue = "32838")]
863+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
845864
#[inline]
846865
pub fn try_new_uninit_slice_in(
847866
len: usize,
@@ -869,6 +888,7 @@ impl<T, A: Allocator> Box<[T], A> {
869888
///
870889
/// ```
871890
/// #![feature(allocator_api)]
891+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
872892
///
873893
/// use std::alloc::System;
874894
///
@@ -881,6 +901,7 @@ impl<T, A: Allocator> Box<[T], A> {
881901
///
882902
/// [zeroed]: mem::MaybeUninit::zeroed
883903
#[unstable(feature = "allocator_api", issue = "32838")]
904+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
884905
#[inline]
885906
pub fn try_new_zeroed_slice_in(
886907
len: usize,
@@ -1125,6 +1146,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11251146
/// using [`Box::into_raw_with_allocator`]:
11261147
/// ```
11271148
/// #![feature(allocator_api)]
1149+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
11281150
///
11291151
/// use std::alloc::System;
11301152
///
@@ -1135,6 +1157,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11351157
/// Manually create a `Box` from scratch by using the system allocator:
11361158
/// ```
11371159
/// #![feature(allocator_api, slice_ptr_get)]
1160+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
11381161
///
11391162
/// use std::alloc::{Allocator, Layout, System};
11401163
///
@@ -1152,6 +1175,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11521175
/// [memory layout]: self#memory-layout
11531176
#[unstable(feature = "allocator_api", issue = "32838")]
11541177
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1178+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
11551179
#[inline]
11561180
pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
11571181
Box(unsafe { Unique::new_unchecked(raw) }, alloc)
@@ -1179,6 +1203,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11791203
/// using [`Box::into_non_null_with_allocator`]:
11801204
/// ```
11811205
/// #![feature(allocator_api, box_vec_non_null)]
1206+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
11821207
///
11831208
/// use std::alloc::System;
11841209
///
@@ -1189,6 +1214,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11891214
/// Manually create a `Box` from scratch by using the system allocator:
11901215
/// ```
11911216
/// #![feature(allocator_api, box_vec_non_null, slice_ptr_get)]
1217+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
11921218
///
11931219
/// use std::alloc::{Allocator, Layout, System};
11941220
///
@@ -1206,6 +1232,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12061232
#[unstable(feature = "allocator_api", issue = "32838")]
12071233
// #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")]
12081234
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
1235+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
12091236
#[inline]
12101237
pub const unsafe fn from_non_null_in(raw: NonNull<T>, alloc: A) -> Self {
12111238
// SAFETY: guaranteed by the caller.
@@ -1348,6 +1375,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
13481375
/// for automatic cleanup:
13491376
/// ```
13501377
/// #![feature(allocator_api)]
1378+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
13511379
///
13521380
/// use std::alloc::System;
13531381
///
@@ -1359,6 +1387,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
13591387
/// the memory:
13601388
/// ```
13611389
/// #![feature(allocator_api)]
1390+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
13621391
///
13631392
/// use std::alloc::{Allocator, Layout, System};
13641393
/// use std::ptr::{self, NonNull};
@@ -1410,6 +1439,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
14101439
/// [`Box::from_non_null_in`] for automatic cleanup:
14111440
/// ```
14121441
/// #![feature(allocator_api, box_vec_non_null)]
1442+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
14131443
///
14141444
/// use std::alloc::System;
14151445
///
@@ -1421,6 +1451,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
14211451
/// the memory:
14221452
/// ```
14231453
/// #![feature(allocator_api, box_vec_non_null)]
1454+
/// #![cfg_attr(not(bootstrap), allow(untracked_heap_allocation))]
14241455
///
14251456
/// use std::alloc::{Allocator, Layout, System};
14261457
///

library/alloc/src/rc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
14761476
/// assert_eq!(&*x, "hello");
14771477
/// ```
14781478
#[must_use = "losing the pointer will leak memory"]
1479+
#[cfg_attr(not(bootstrap), rustc_alloc_in)]
14791480
#[unstable(feature = "allocator_api", issue = "32838")]
14801481
pub fn into_raw_with_allocator(this: Self) -> (*const T, A) {
14811482
let this = mem::ManuallyDrop::new(this);

tests/ui/box/issue-95036.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//@ build-pass
33

44
#![feature(allocator_api)]
5+
#![allow(untracked_heap_allocation)]
56

67
#[inline(never)]
78
pub fn by_ref(node: &mut Box<[u8; 1], &std::alloc::Global>) {

tests/ui/box/large-allocator-ice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ build-pass
22
#![feature(allocator_api)]
33
#![allow(unused_must_use)]
4+
#![allow(untracked_heap_allocation)]
45

56
use std::alloc::Allocator;
67

tests/ui/debuginfo/debuginfo-box-with-large-allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// fixes issue #94725
44

55
#![feature(allocator_api)]
6+
#![allow(untracked_heap_allocation)]
67

78
use std::alloc::{AllocError, Allocator, Layout};
89
use std::ptr::NonNull;

tests/ui/lint/must_not_suspend/allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![feature(must_not_suspend, allocator_api)]
44
#![deny(must_not_suspend)]
5+
#![allow(untracked_heap_allocation)]
56

67
use std::alloc::*;
78
use std::ptr::NonNull;

tests/ui/lint/must_not_suspend/allocator.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: allocator `MyAllocatorWhichMustNotSuspend` held across a suspend point, but should not be
2-
--> $DIR/allocator.rs:24:9
2+
--> $DIR/allocator.rs:25:9
33
|
44
LL | let x = Box::new_in(1i32, MyAllocatorWhichMustNotSuspend);
55
| ^
@@ -8,7 +8,7 @@ LL | suspend().await;
88
| ----- the value is held across this suspend point
99
|
1010
help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point
11-
--> $DIR/allocator.rs:24:9
11+
--> $DIR/allocator.rs:25:9
1212
|
1313
LL | let x = Box::new_in(1i32, MyAllocatorWhichMustNotSuspend);
1414
| ^

tests/ui/lto/issue-100772.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//@ only-x86_64-unknown-linux-gnu
66

77
#![feature(allocator_api)]
8+
#![allow(untracked_heap_allocation)]
89

910
fn main() {
1011
let _ = Box::new_in(&[0, 1], &std::alloc::Global);

0 commit comments

Comments
 (0)