Skip to content

Commit 14303ba

Browse files
committed
Do not copy values of type () or _|_
This can trigger a crash because we assume we can supply null ptrs and undefined values for values of those types, as we should be treated them as zero-size. Interestingly, this crash only shows up (atm) in non-optimized builds. Therefore, I added a -Z no-opt flag so that the new test (capture_nil) can specify that it should not run with optimizations enabled.
1 parent c21b3ff commit 14303ba

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

src/rustc/driver/driver.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,10 @@ fn build_session_options(binary: ~str,
514514
link::output_type_llvm_assembly | link::output_type_assembly => (),
515515
_ => debugging_opts |= session::no_asm_comments
516516
}
517-
let opt_level =
518-
if opt_present(matches, ~"O") {
517+
let opt_level = {
518+
if (debugging_opts & session::no_opt) != 0 {
519+
No
520+
} else if opt_present(matches, ~"O") {
519521
if opt_present(matches, ~"opt-level") {
520522
early_error(demitter, ~"-O and --opt-level both provided");
521523
}
@@ -531,7 +533,8 @@ fn build_session_options(binary: ~str,
531533
~"to be between 0-3")
532534
}
533535
}
534-
} else { No };
536+
} else { No }
537+
};
535538
let target =
536539
match target_opt {
537540
None => host_triple(),

src/rustc/driver/session.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@ type config =
3838
uint_type: uint_ty,
3939
float_type: float_ty};
4040

41-
const ppregions: uint = 1u;
42-
const time_passes: uint = 2u;
43-
const count_llvm_insns: uint = 4u;
44-
const time_llvm_passes: uint = 8u;
45-
const trans_stats: uint = 16u;
46-
const no_asm_comments: uint = 32u;
47-
const no_verify: uint = 64u;
48-
const trace: uint = 128u;
41+
const ppregions: uint = 1 << 0;
42+
const time_passes: uint = 1 << 1;
43+
const count_llvm_insns: uint = 1 << 2;
44+
const time_llvm_passes: uint = 1 << 3;
45+
const trans_stats: uint = 1 << 4;
46+
const no_asm_comments: uint = 1 << 5;
47+
const no_verify: uint = 1 << 6;
48+
const trace: uint = 1 << 7;
4949
// FIXME (#2377): This exists to transition to a Rust crate runtime
5050
// It should be removed
51-
const no_rt: uint = 256u;
52-
const coherence: uint = 512u;
53-
const borrowck_stats: uint = 1024u;
54-
const borrowck_note_pure: uint = 2048;
55-
const borrowck_note_loan: uint = 4096;
56-
const no_landing_pads: uint = 8192;
57-
const debug_llvm: uint = 16384;
58-
const count_type_sizes: uint = 32768;
59-
const meta_stats: uint = 65536;
51+
const no_rt: uint = 1 << 8;
52+
const coherence: uint = 1 << 9;
53+
const borrowck_stats: uint = 1 << 10;
54+
const borrowck_note_pure: uint = 1 << 11;
55+
const borrowck_note_loan: uint = 1 << 12;
56+
const no_landing_pads: uint = 1 << 13;
57+
const debug_llvm: uint = 1 << 14;
58+
const count_type_sizes: uint = 1 << 15;
59+
const meta_stats: uint = 1 << 16;
60+
const no_opt: uint = 1 << 17;
6061

6162
fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
6263
~[(~"ppregions", ~"prettyprint regions with \
@@ -82,7 +83,8 @@ fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
8283
(~"debug-llvm", ~"enable debug output from LLVM", debug_llvm),
8384
(~"count-type-sizes", ~"count the sizes of aggregate types",
8485
count_type_sizes),
85-
(~"meta-stats", ~"gather metadata statistics", meta_stats)
86+
(~"meta-stats", ~"gather metadata statistics", meta_stats),
87+
(~"no-opt", ~"do not optimize, even if -O is passed", no_opt),
8688
]
8789
}
8890

src/rustc/middle/trans/datum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ impl Datum {
241241

242242
let _icx = bcx.insn_ctxt("copy_to");
243243

244+
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
245+
return bcx;
246+
}
247+
244248
debug!("copy_to(self=%s, action=%?, dst=%s)",
245249
self.to_str(bcx.ccx()), action, bcx.val_str(dst));
246250

src/test/run-pass/capture_nil.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// compile-flags:-Z no-opt
2+
use comm::*;
3+
4+
// This test has to be setup just so to trigger
5+
// the condition which was causing us a crash.
6+
// The situation is that we are capturing a
7+
// () value by ref. We generally feel free,
8+
// however, to substitute NULL pointers and
9+
// undefined values for values of () type, and
10+
// so this caused a segfault when we copied into
11+
// the closure.
12+
//
13+
// The fix is just to not emit any actual loads
14+
// or stores for copies of () type (which is of
15+
// course preferable, as the value itself is
16+
// irrelevant).
17+
18+
fn foo(&&x: ()) -> Port<()> {
19+
let p = Port();
20+
let c = Chan(p);
21+
do task::spawn() |copy c, copy x| {
22+
c.send(x);
23+
}
24+
p
25+
}
26+
27+
fn main() {
28+
foo(()).recv()
29+
}

0 commit comments

Comments
 (0)