Skip to content

Commit 3db6ec3

Browse files
committed
prevent more deallocations of statics
1 parent fbfd2d4 commit 3db6ec3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/eval_context.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
329329
}
330330
},
331331
}
332+
// see comment on `initialized` field
333+
assert!(!global_value.initialized);
334+
global_value.initialized = true;
332335
assert!(global_value.mutable);
333336
global_value.mutable = mutable;
334337
} else {
@@ -868,7 +871,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
868871
_ => {
869872
let ptr = self.alloc_ptr_with_substs(global_val.ty, cid.substs)?;
870873
self.write_value_to_ptr(global_val.value, ptr, global_val.ty)?;
871-
self.memory.mark_static(ptr.alloc_id, global_val.mutable)?;
874+
// see comment on `initialized` field
875+
if global_val.initialized {
876+
self.memory.mark_static(ptr.alloc_id, global_val.mutable)?;
877+
}
872878
let lval = self.globals.get_mut(&cid).expect("already checked");
873879
*lval = Global {
874880
value: Value::ByRef(ptr),

src/lvalue.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ pub struct GlobalId<'tcx> {
5757
#[derive(Copy, Clone, Debug)]
5858
pub struct Global<'tcx> {
5959
pub(super) value: Value,
60+
/// Only used in `force_allocation` to ensure we don't mark the memory
61+
/// before the static is initialized. It is possible to convert a
62+
/// global which initially is `Value::ByVal(PrimVal::Undef)` and gets
63+
/// lifted to an allocation before the static is fully initialized
64+
pub(super) initialized: bool,
6065
pub(super) mutable: bool,
6166
pub(super) ty: Ty<'tcx>,
6267
}
@@ -102,6 +107,7 @@ impl<'tcx> Global<'tcx> {
102107
value: Value::ByVal(PrimVal::Undef),
103108
mutable: true,
104109
ty,
110+
initialized: false,
105111
}
106112
}
107113
}

tests/run-pass/issue-5917.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 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+
12+
struct T (&'static [isize]);
13+
static t : T = T (&[5, 4, 3]);
14+
pub fn main () {
15+
let T(ref v) = t;
16+
assert_eq!(v[0], 5);
17+
}

0 commit comments

Comments
 (0)