]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/drop-flag-sanity-check.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / test / run-pass / drop-flag-sanity-check.rs
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 // ignore-emscripten
13
14 // Quick-and-dirty test to ensure -Z force-dropflag-checks=on works as
15 // expected. Note that the inlined drop-flag is slated for removal
16 // (RFC 320); when that happens, the -Z flag and this test should
17 // simply be removed.
18 //
19 // See also drop-flag-skip-sanity-check.rs.
20
21 use std::env;
22 use std::process::Command;
23
24 fn main() {
25 let args: Vec<String> = env::args().collect();
26 if args.len() > 1 && args[1] == "test" {
27 return test();
28 }
29
30 let mut p = Command::new(&args[0]).arg("test").spawn().unwrap();
31 // The invocation should fail due to the drop-flag sanity check.
32 assert!(!p.wait().unwrap().success());
33 }
34
35 #[derive(Debug)]
36 struct Corrupted {
37 x: u8
38 }
39
40 impl Drop for Corrupted {
41 fn drop(&mut self) { println!("dropping"); }
42 }
43
44 fn test() {
45 {
46 let mut c1 = Corrupted { x: 1 };
47 let mut c2 = Corrupted { x: 2 };
48 unsafe {
49 let p1 = &mut c1 as *mut Corrupted as *mut u8;
50 let p2 = &mut c2 as *mut Corrupted as *mut u8;
51 for i in 0..std::mem::size_of::<Corrupted>() {
52 // corrupt everything, *including the drop flag.
53 //
54 // (We corrupt via two different means to safeguard
55 // against the hypothetical assignment of the
56 // dtor_needed/dtor_done values to v and v+k. that
57 // happen to match with one of the corruption values
58 // below.)
59 *p1.offset(i as isize) += 2;
60 *p2.offset(i as isize) += 3;
61 }
62 }
63 // Here, at the end of the scope of `c1` and `c2`, the
64 // drop-glue should detect the corruption of (at least one of)
65 // the drop-flags.
66 }
67 println!("We should never get here.");
68 }