Skip to content

Commit a21e18f

Browse files
committed
Auto merge of #23535 - pnkfelix:fsk-filling-drop, r=<try>
Replace zeroing-on-drop with filling-on-drop. This is meant to set the stage for removing *all* zeroing and filling (on drop) in the future. Note that the code is meant to be entirely abstract with respect to the particular values used for the drop flags: the final commit demonstrates how to go from zeroing-on-drop to filling-on-drop by changing the value of three constants (in two files). See further discussion on the internals thread: http://internals.rust-lang.org/t/attention-hackers-filling-drop/1715/11 [breaking-change] especially for structs / enums using `#[unsafe_no_drop_flag]`.
2 parents b0fd67b + fd65e4f commit a21e18f

31 files changed

+630
-63
lines changed

src/liballoc/arc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ impl<T> Drop for Arc<T> {
354354
// more than once (but it is guaranteed to be zeroed after the first if
355355
// it's run more than once)
356356
let ptr = *self._ptr;
357-
if ptr.is_null() { return }
357+
// if ptr.is_null() { return }
358+
if ptr.is_null() || ptr as usize == mem::POST_DROP_USIZE { return }
358359

359360
// Because `fetch_sub` is already atomic, we do not need to synchronize
360361
// with other threads unless we are going to delete the object. This
@@ -485,7 +486,7 @@ impl<T> Drop for Weak<T> {
485486
let ptr = *self._ptr;
486487

487488
// see comments above for why this check is here
488-
if ptr.is_null() { return }
489+
if ptr.is_null() || ptr as usize == mem::POST_DROP_USIZE { return }
489490

490491
// If we find out that we were the last weak pointer, then its time to
491492
// deallocate the data entirely. See the discussion in Arc::drop() about

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
#![feature(box_syntax)]
7676
#![feature(optin_builtin_traits)]
7777
#![feature(unboxed_closures)]
78-
#![feature(unsafe_no_drop_flag)]
78+
#![feature(unsafe_no_drop_flag, filling_drop)]
7979
#![feature(core)]
8080
#![feature(unique)]
8181
#![cfg_attr(test, feature(test, alloc, rustc_private))]

src/liballoc/rc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ use core::default::Default;
160160
use core::fmt;
161161
use core::hash::{Hasher, Hash};
162162
use core::marker;
163-
use core::mem::{min_align_of, size_of, forget};
163+
use core::mem::{self, min_align_of, size_of, forget};
164164
use core::nonzero::NonZero;
165165
use core::ops::{Deref, Drop};
166166
use core::option::Option;
@@ -407,7 +407,7 @@ impl<T> Drop for Rc<T> {
407407
fn drop(&mut self) {
408408
unsafe {
409409
let ptr = *self._ptr;
410-
if !ptr.is_null() {
410+
if !ptr.is_null() && ptr as usize != mem::POST_DROP_USIZE {
411411
self.dec_strong();
412412
if self.strong() == 0 {
413413
ptr::read(&**self); // destroy the contained object
@@ -718,7 +718,7 @@ impl<T> Drop for Weak<T> {
718718
fn drop(&mut self) {
719719
unsafe {
720720
let ptr = *self._ptr;
721-
if !ptr.is_null() {
721+
if !ptr.is_null() && ptr as usize != mem::POST_DROP_USIZE {
722722
self.dec_weak();
723723
// the weak count starts at 1, and will only go to zero if all
724724
// the strong pointers have disappeared.

src/libcollections/btree/node.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,11 @@ impl<T> Drop for RawItems<T> {
280280
#[unsafe_destructor]
281281
impl<K, V> Drop for Node<K, V> {
282282
fn drop(&mut self) {
283-
if self.keys.is_null() {
283+
if self.keys.is_null() ||
284+
(unsafe { self.keys.get() as *const K as usize == mem::POST_DROP_USIZE })
285+
{
284286
// Since we have #[unsafe_no_drop_flag], we have to watch
285-
// out for a null value being stored in self.keys. (Using
287+
// out for the sentinel value being stored in self.keys. (Using
286288
// null is technically a violation of the `Unique`
287289
// requirements, though.)
288290
return;

src/libcollections/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#![feature(unicode)]
3737
#![feature(unsafe_destructor)]
3838
#![feature(unique)]
39-
#![feature(unsafe_no_drop_flag)]
39+
#![feature(unsafe_no_drop_flag, filling_drop)]
4040
#![feature(step_by)]
4141
#![feature(str_char)]
4242
#![feature(convert)]

src/libcollections/vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ impl<T> Drop for Vec<T> {
16941694
fn drop(&mut self) {
16951695
// This is (and should always remain) a no-op if the fields are
16961696
// zeroed (when moving out, because of #[unsafe_no_drop_flag]).
1697-
if self.cap != 0 {
1697+
if self.cap != 0 && self.cap != mem::POST_DROP_USIZE {
16981698
unsafe {
16991699
for x in &*self {
17001700
ptr::read(x);
@@ -1977,7 +1977,7 @@ impl<'a, T> ExactSizeIterator for Drain<'a, T> {}
19771977
#[stable(feature = "rust1", since = "1.0.0")]
19781978
impl<'a, T> Drop for Drain<'a, T> {
19791979
fn drop(&mut self) {
1980-
// self.ptr == self.end == null if drop has already been called,
1980+
// self.ptr == self.end == mem::POST_DROP_USIZE if drop has already been called,
19811981
// so we can use #[unsafe_no_drop_flag].
19821982

19831983
// destroy the remaining elements

src/libcore/intrinsics.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,35 @@ extern "rust-intrinsic" {
191191
/// crate it is invoked in.
192192
pub fn type_id<T: ?Sized + 'static>() -> u64;
193193

194+
/// Create a value initialized to so that its drop flag,
195+
/// if any, says that it has been dropped.
196+
///
197+
/// `init_dropped` is unsafe because it returns a datum with all
198+
/// of its bytes set to the drop flag, which generally does not
199+
/// correspond to a valid value.
200+
///
201+
/// This intrinsic is likely to be deprecated in the future when
202+
/// Rust moves to non-zeroing dynamic drop (and thus removes the
203+
/// embedded drop flags that are being established by this
204+
/// intrinsic).
205+
#[cfg(not(stage0))]
206+
pub fn init_dropped<T>() -> T;
207+
194208
/// Create a value initialized to zero.
195209
///
196210
/// `init` is unsafe because it returns a zeroed-out datum,
197-
/// which is unsafe unless T is Copy.
211+
/// which is unsafe unless T is `Copy`. Also, even if T is
212+
/// `Copy`, an all-zero value may not correspond to any legitimate
213+
/// state for the type in question.
198214
pub fn init<T>() -> T;
199215

200216
/// Create an uninitialized value.
217+
///
218+
/// `uninit` is unsafe because there is no guarantee of what its
219+
/// contents are. In particular its drop-flag may be set to any
220+
/// state, which means it may claim either dropped or
221+
/// undropped. In the general case one must use `ptr::write` to
222+
/// initialize memory previous set to the result of `uninit`.
201223
pub fn uninit<T>() -> T;
202224

203225
/// Move a value out of scope without running drop glue.

src/libcore/mem.rs

+69
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@ pub unsafe fn zeroed<T>() -> T {
158158
intrinsics::init()
159159
}
160160

161+
/// Create a value initialized to an unspecified series of bytes.
162+
///
163+
/// The byte sequence usually indicates that the value at the memory
164+
/// in question has been dropped. Thus, *if* T carries a drop flag,
165+
/// any associated destructor will not be run when the value falls out
166+
/// of scope.
167+
///
168+
/// Some code at one time used the `zeroed` function above to
169+
/// accomplish this goal.
170+
///
171+
/// This function is expected to be deprecated with the transition
172+
/// to non-zeroing drop.
173+
#[inline]
174+
#[unstable(feature = "filling_drop")]
175+
pub unsafe fn dropped<T>() -> T {
176+
#[cfg(stage0)]
177+
#[inline(always)]
178+
unsafe fn dropped_impl<T>() -> T { zeroed() }
179+
180+
#[cfg(not(stage0))]
181+
#[inline(always)]
182+
unsafe fn dropped_impl<T>() -> T { intrinsics::init_dropped() }
183+
184+
dropped_impl()
185+
}
186+
161187
/// Create an uninitialized value.
162188
///
163189
/// Care must be taken when using this function, if the type `T` has a destructor and the value
@@ -291,6 +317,49 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
291317
#[stable(feature = "rust1", since = "1.0.0")]
292318
pub fn drop<T>(_x: T) { }
293319

320+
macro_rules! repeat_u8_as_u32 {
321+
($name:expr) => { (($name as u32) << 24 |
322+
($name as u32) << 16 |
323+
($name as u32) << 8 |
324+
($name as u32)) }
325+
}
326+
macro_rules! repeat_u8_as_u64 {
327+
($name:expr) => { ((repeat_u8_as_u32!($name) as u64) << 32 |
328+
(repeat_u8_as_u32!($name) as u64)) }
329+
}
330+
331+
// NOTE: Keep synchronized with values used in librustc_trans::trans::adt.
332+
//
333+
// In particular, the POST_DROP_U8 marker must never equal the
334+
// DTOR_NEEDED_U8 marker.
335+
//
336+
// For a while pnkfelix was using 0xc1 here.
337+
// But having the sign bit set is a pain, so 0x1d is probably better.
338+
//
339+
// And of course, 0x00 brings back the old world of zero'ing on drop.
340+
#[cfg(not(stage0))] #[unstable(feature = "filling_drop")]
341+
pub const POST_DROP_U8: u8 = 0x1d;
342+
#[cfg(not(stage0))] #[unstable(feature = "filling_drop")]
343+
pub const POST_DROP_U32: u32 = repeat_u8_as_u32!(POST_DROP_U8);
344+
#[cfg(not(stage0))] #[unstable(feature = "filling_drop")]
345+
pub const POST_DROP_U64: u64 = repeat_u8_as_u64!(POST_DROP_U8);
346+
347+
#[cfg(target_pointer_width = "32")]
348+
#[cfg(not(stage0))] #[unstable(feature = "filling_drop")]
349+
pub const POST_DROP_USIZE: usize = POST_DROP_U32 as usize;
350+
#[cfg(target_pointer_width = "64")]
351+
#[cfg(not(stage0))] #[unstable(feature = "filling_drop")]
352+
pub const POST_DROP_USIZE: usize = POST_DROP_U64 as usize;
353+
354+
#[cfg(stage0)] #[unstable(feature = "filling_drop")]
355+
pub const POST_DROP_U8: u8 = 0;
356+
#[cfg(stage0)] #[unstable(feature = "filling_drop")]
357+
pub const POST_DROP_U32: u32 = 0;
358+
#[cfg(stage0)] #[unstable(feature = "filling_drop")]
359+
pub const POST_DROP_U64: u64 = 0;
360+
#[cfg(stage0)] #[unstable(feature = "filling_drop")]
361+
pub const POST_DROP_USIZE: usize = 0;
362+
294363
/// Interprets `src` as `&U`, and then reads `src` without moving the contained value.
295364
///
296365
/// This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by

src/libcore/ptr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
230230
tmp
231231
}
232232

233+
/// Variant of read_and_zero that writes the specific drop-flag byte
234+
/// (which may be more appropriate than zero).
235+
#[inline(always)]
236+
#[unstable(feature = "core",
237+
reason = "may play a larger role in std::ptr future extensions")]
238+
pub unsafe fn read_and_drop<T>(dest: *mut T) -> T {
239+
// Copy the data out from `dest`:
240+
let tmp = read(&*dest);
241+
242+
// Now mark `dest` as dropped:
243+
write_bytes(dest, mem::POST_DROP_U8, 1);
244+
245+
tmp
246+
}
247+
233248
/// Overwrites a memory location with the given value without reading or
234249
/// dropping the old value.
235250
///

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
605605
"Print the size of enums and their variants"),
606606
force_overflow_checks: Option<bool> = (None, parse_opt_bool,
607607
"Force overflow checks on or off"),
608+
force_dropflag_checks: Option<bool> = (None, parse_opt_bool,
609+
"Force drop flag checks on or off"),
608610
}
609611

610612
pub fn default_lib_output() -> CrateType {

src/librustc_trans/trans/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ pub fn store_local<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
15281528
let scope = cleanup::var_scope(tcx, p_id);
15291529
bcx = mk_binding_alloca(
15301530
bcx, p_id, &path1.node, scope, (),
1531-
|(), bcx, llval, ty| { zero_mem(bcx, llval, ty); bcx });
1531+
|(), bcx, llval, ty| { drop_done_fill_mem(bcx, llval, ty); bcx });
15321532
});
15331533
bcx
15341534
}

0 commit comments

Comments
 (0)