|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// compile-flags: -Z force-dropflag-checks=on |
| 12 | + |
| 13 | +// Quick-and-dirty test to ensure -Z force-dropflag-checks=on works as |
| 14 | +// expected. Note that the inlined drop-flag is slated for removal |
| 15 | +// (RFC 320); when that happens, the -Z flag and this test should |
| 16 | +// simply be removed. |
| 17 | +// |
| 18 | +// See also drop-flag-skip-sanity-check.rs. |
| 19 | + |
| 20 | +use std::env; |
| 21 | +use std::old_io::process::{Command, ExitSignal, ExitStatus}; |
| 22 | + |
| 23 | +fn main() { |
| 24 | + let args: Vec<String> = env::args().collect(); |
| 25 | + if args.len() > 1 && args[1] == "test" { |
| 26 | + return test(); |
| 27 | + } |
| 28 | + |
| 29 | + let mut p = Command::new(&args[0]).arg("test").spawn().unwrap(); |
| 30 | + // The invocation should fail due to the drop-flag sanity check. |
| 31 | + assert!(!p.wait().unwrap().success()); |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug)] |
| 35 | +struct Corrupted { |
| 36 | + x: u8 |
| 37 | +} |
| 38 | + |
| 39 | +impl Drop for Corrupted { |
| 40 | + fn drop(&mut self) { println!("dropping"); } |
| 41 | +} |
| 42 | + |
| 43 | +fn test() { |
| 44 | + { |
| 45 | + let mut c1 = Corrupted { x: 1 }; |
| 46 | + let mut c2 = Corrupted { x: 2 }; |
| 47 | + unsafe { |
| 48 | + let p1 = &mut c1 as *mut Corrupted as *mut u8; |
| 49 | + let p2 = &mut c2 as *mut Corrupted as *mut u8; |
| 50 | + for i in 0..std::mem::size_of::<Corrupted>() { |
| 51 | + // corrupt everything, *including the drop flag. |
| 52 | + // |
| 53 | + // (We corrupt via two different means to safeguard |
| 54 | + // against the hypothetical assignment of the |
| 55 | + // dtor_needed/dtor_done values to v and v+k. that |
| 56 | + // happen to match with one of the corruption values |
| 57 | + // below.) |
| 58 | + *p1.offset(i as isize) += 2; |
| 59 | + *p2.offset(i as isize) += 3; |
| 60 | + } |
| 61 | + } |
| 62 | + // Here, at the end of the scope of `c1` and `c2`, the |
| 63 | + // drop-glue should detect the corruption of (at least one of) |
| 64 | + // the drop-flags. |
| 65 | + } |
| 66 | + println!("We should never get here."); |
| 67 | +} |
0 commit comments