Skip to content

Commit bf8ff10

Browse files
committed
---
yaml --- r: 66224 b: refs/heads/master c: a09972d h: refs/heads/master v: v3
1 parent 1dca157 commit bf8ff10

File tree

5 files changed

+295
-230
lines changed

5 files changed

+295
-230
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: aa9210d25afb3779e1d95722b73b62a7be6274fe
2+
refs/heads/master: a09972db3545344048b90e90d1f1821b621a38b9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/src/libstd/cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ptr::mut_null;
1515
use repr::BoxRepr;
1616
use sys::TypeDesc;
1717
use cast::transmute;
18-
#[cfg(not(test))] use unstable::lang::clear_task_borrow_list;
18+
#[cfg(not(test))] use rt::borrowck::clear_task_borrow_list;
1919

2020
#[cfg(not(test))] use ptr::to_unsafe_ptr;
2121

trunk/src/libstd/rt/borrowck.rs

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
// Copyright 2012 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+
use cast::transmute;
12+
use libc::{c_char, c_void, size_t, STDERR_FILENO};
13+
use io;
14+
use io::{Writer, WriterUtil};
15+
use managed::raw::BoxRepr;
16+
use option::{Option, None, Some};
17+
use uint;
18+
use str;
19+
use str::OwnedStr;
20+
use sys;
21+
use vec::ImmutableVector;
22+
23+
#[allow(non_camel_case_types)]
24+
type rust_task = c_void;
25+
26+
pub static FROZEN_BIT: uint = 1 << (uint::bits - 1);
27+
pub static MUT_BIT: uint = 1 << (uint::bits - 2);
28+
static ALL_BITS: uint = FROZEN_BIT | MUT_BIT;
29+
30+
#[deriving(Eq)]
31+
struct BorrowRecord {
32+
box: *mut BoxRepr,
33+
file: *c_char,
34+
line: size_t
35+
}
36+
37+
fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
38+
unsafe {
39+
let cur_task: *rust_task = rust_try_get_task();
40+
if cur_task.is_not_null() {
41+
let ptr = rust_take_task_borrow_list(cur_task);
42+
if ptr.is_null() {
43+
None
44+
} else {
45+
let v: ~[BorrowRecord] = transmute(ptr);
46+
Some(v)
47+
}
48+
} else {
49+
None
50+
}
51+
}
52+
}
53+
54+
fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
55+
unsafe {
56+
let cur_task: *rust_task = rust_try_get_task();
57+
if cur_task.is_not_null() {
58+
let mut borrow_list: ~[BorrowRecord] = {
59+
let ptr = rust_take_task_borrow_list(cur_task);
60+
if ptr.is_null() { ~[] } else { transmute(ptr) }
61+
};
62+
borrow_list = f(borrow_list);
63+
rust_set_task_borrow_list(cur_task, transmute(borrow_list));
64+
}
65+
}
66+
}
67+
68+
pub unsafe fn clear_task_borrow_list() {
69+
// pub because it is used by the box annihilator.
70+
let _ = try_take_task_borrow_list();
71+
}
72+
73+
unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
74+
debug_borrow("fail_borrowed: ", box, 0, 0, file, line);
75+
76+
match try_take_task_borrow_list() {
77+
None => { // not recording borrows
78+
let msg = "borrowed";
79+
do str::as_buf(msg) |msg_p, _| {
80+
sys::begin_unwind_(msg_p as *c_char, file, line);
81+
}
82+
}
83+
Some(borrow_list) => { // recording borrows
84+
let mut msg = ~"borrowed";
85+
let mut sep = " at ";
86+
for borrow_list.rev_iter().advance |entry| {
87+
if entry.box == box {
88+
msg.push_str(sep);
89+
let filename = str::raw::from_c_str(entry.file);
90+
msg.push_str(filename);
91+
msg.push_str(fmt!(":%u", entry.line as uint));
92+
sep = " and at ";
93+
}
94+
}
95+
do str::as_buf(msg) |msg_p, _| {
96+
sys::begin_unwind_(msg_p as *c_char, file, line)
97+
}
98+
}
99+
}
100+
}
101+
102+
/// Because this code is so perf. sensitive, use a static constant so that
103+
/// debug printouts are compiled out most of the time.
104+
static ENABLE_DEBUG: bool = false;
105+
106+
#[inline]
107+
unsafe fn debug_borrow<T>(tag: &'static str,
108+
p: *const T,
109+
old_bits: uint,
110+
new_bits: uint,
111+
filename: *c_char,
112+
line: size_t) {
113+
//! A useful debugging function that prints a pointer + tag + newline
114+
//! without allocating memory.
115+
116+
if ENABLE_DEBUG && ::rt::env::get().debug_borrow {
117+
debug_borrow_slow(tag, p, old_bits, new_bits, filename, line);
118+
}
119+
120+
unsafe fn debug_borrow_slow<T>(tag: &'static str,
121+
p: *const T,
122+
old_bits: uint,
123+
new_bits: uint,
124+
filename: *c_char,
125+
line: size_t) {
126+
let dbg = STDERR_FILENO as io::fd_t;
127+
dbg.write_str(tag);
128+
dbg.write_hex(p as uint);
129+
dbg.write_str(" ");
130+
dbg.write_hex(old_bits);
131+
dbg.write_str(" ");
132+
dbg.write_hex(new_bits);
133+
dbg.write_str(" ");
134+
dbg.write_cstr(filename);
135+
dbg.write_str(":");
136+
dbg.write_hex(line as uint);
137+
dbg.write_str("\n");
138+
}
139+
}
140+
141+
trait DebugPrints {
142+
fn write_hex(&self, val: uint);
143+
unsafe fn write_cstr(&self, str: *c_char);
144+
}
145+
146+
impl DebugPrints for io::fd_t {
147+
fn write_hex(&self, mut i: uint) {
148+
let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
149+
'9', 'a', 'b', 'c', 'd', 'e', 'f'];
150+
static uint_nibbles: uint = ::uint::bytes << 1;
151+
let mut buffer = [0_u8, ..uint_nibbles+1];
152+
let mut c = uint_nibbles;
153+
while c > 0 {
154+
c -= 1;
155+
buffer[c] = letters[i & 0xF] as u8;
156+
i >>= 4;
157+
}
158+
self.write(buffer.slice(0, uint_nibbles));
159+
}
160+
161+
unsafe fn write_cstr(&self, p: *c_char) {
162+
use libc::strlen;
163+
use vec;
164+
165+
let len = strlen(p);
166+
let p: *u8 = transmute(p);
167+
do vec::raw::buf_as_slice(p, len as uint) |s| {
168+
self.write(s);
169+
}
170+
}
171+
}
172+
173+
#[inline]
174+
pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
175+
let a: *mut BoxRepr = transmute(a);
176+
let old_ref_count = (*a).header.ref_count;
177+
let new_ref_count = old_ref_count | FROZEN_BIT;
178+
179+
debug_borrow("borrow_as_imm:", a, old_ref_count, new_ref_count, file, line);
180+
181+
if (old_ref_count & MUT_BIT) != 0 {
182+
fail_borrowed(a, file, line);
183+
}
184+
185+
(*a).header.ref_count = new_ref_count;
186+
187+
old_ref_count
188+
}
189+
190+
#[inline]
191+
pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
192+
let a: *mut BoxRepr = transmute(a);
193+
let old_ref_count = (*a).header.ref_count;
194+
let new_ref_count = old_ref_count | MUT_BIT | FROZEN_BIT;
195+
196+
debug_borrow("borrow_as_mut:", a, old_ref_count, new_ref_count, file, line);
197+
198+
if (old_ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
199+
fail_borrowed(a, file, line);
200+
}
201+
202+
(*a).header.ref_count = new_ref_count;
203+
204+
old_ref_count
205+
}
206+
207+
pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
208+
file: *c_char, line: size_t) {
209+
if (old_ref_count & ALL_BITS) == 0 {
210+
// was not borrowed before
211+
let a: *mut BoxRepr = transmute(a);
212+
debug_borrow("record_borrow:", a, old_ref_count, 0, file, line);
213+
do swap_task_borrow_list |borrow_list| {
214+
let mut borrow_list = borrow_list;
215+
borrow_list.push(BorrowRecord {box: a, file: file, line: line});
216+
borrow_list
217+
}
218+
}
219+
}
220+
221+
pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
222+
file: *c_char, line: size_t) {
223+
if (old_ref_count & ALL_BITS) == 0 {
224+
// was not borrowed before, so we should find the record at
225+
// the end of the list
226+
let a: *mut BoxRepr = transmute(a);
227+
debug_borrow("unrecord_borrow:", a, old_ref_count, 0, file, line);
228+
do swap_task_borrow_list |borrow_list| {
229+
let mut borrow_list = borrow_list;
230+
assert!(!borrow_list.is_empty());
231+
let br = borrow_list.pop();
232+
if br.box != a || br.file != file || br.line != line {
233+
let err = fmt!("wrong borrow found, br=%?", br);
234+
do str::as_buf(err) |msg_p, _| {
235+
sys::begin_unwind_(msg_p as *c_char, file, line)
236+
}
237+
}
238+
borrow_list
239+
}
240+
}
241+
}
242+
243+
#[inline]
244+
pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
245+
file: *c_char, line: size_t) {
246+
// Sometimes the box is null, if it is conditionally frozen.
247+
// See e.g. #4904.
248+
if !a.is_null() {
249+
let a: *mut BoxRepr = transmute(a);
250+
let old_ref_count = (*a).header.ref_count;
251+
let new_ref_count =
252+
(old_ref_count & !ALL_BITS) | (orig_ref_count & ALL_BITS);
253+
254+
debug_borrow("return_to_mut:",
255+
a, old_ref_count, new_ref_count, file, line);
256+
257+
(*a).header.ref_count = new_ref_count;
258+
}
259+
}
260+
261+
#[inline]
262+
pub unsafe fn check_not_borrowed(a: *u8,
263+
file: *c_char,
264+
line: size_t) {
265+
let a: *mut BoxRepr = transmute(a);
266+
let ref_count = (*a).header.ref_count;
267+
debug_borrow("check_not_borrowed:", a, ref_count, 0, file, line);
268+
if (ref_count & FROZEN_BIT) != 0 {
269+
fail_borrowed(a, file, line);
270+
}
271+
}
272+
273+
274+
extern {
275+
#[rust_stack]
276+
pub fn rust_take_task_borrow_list(task: *rust_task) -> *c_void;
277+
278+
#[rust_stack]
279+
pub fn rust_set_task_borrow_list(task: *rust_task, map: *c_void);
280+
281+
#[rust_stack]
282+
pub fn rust_try_get_task() -> *rust_task;
283+
}

trunk/src/libstd/rt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ pub mod util;
162162
// Global command line argument storage
163163
pub mod args;
164164

165+
// Support for dynamic borrowck
166+
pub mod borrowck;
167+
165168
/// Set up a default runtime configuration, given compiler-supplied arguments.
166169
///
167170
/// This is invoked by the `start` _language item_ (unstable::lang) to

0 commit comments

Comments
 (0)