Skip to content

Commit 01b7b7d

Browse files
committed
core::rt: Use unsafe pointers instead of transmuted regions
1 parent ab284d4 commit 01b7b7d

File tree

10 files changed

+86
-80
lines changed

10 files changed

+86
-80
lines changed

src/libcore/rt/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ fn initialize_call_frame(regs: &mut Registers, fptr: *c_void, arg: *c_void, sp:
111111
let sp = align_down(sp);
112112
let sp = mut_offset(sp, -4);
113113

114-
unsafe { *sp = arg as uint; }
114+
unsafe { *sp = arg as uint };
115115
let sp = mut_offset(sp, -1);
116-
unsafe { *sp = 0; } // The final return address
116+
unsafe { *sp = 0 }; // The final return address
117117

118118
regs.esp = sp as u32;
119119
regs.eip = fptr as u32;
@@ -195,7 +195,7 @@ fn initialize_call_frame(regs: &mut Registers, fptr: *c_void, arg: *c_void, sp:
195195

196196
fn align_down(sp: *mut uint) -> *mut uint {
197197
unsafe {
198-
let sp = transmute::<*mut uint, uint>(sp);
198+
let sp: uint = transmute(sp);
199199
let sp = sp & !(16 - 1);
200200
transmute::<uint, *mut uint>(sp)
201201
}

src/libcore/rt/io/net/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl TcpStream {
3535
rtdebug!("borrowing io to connect");
3636
let io = unsafe_borrow_io();
3737
rtdebug!("about to connect");
38-
io.tcp_connect(addr)
38+
(*io).tcp_connect(addr)
3939
};
4040

4141
match stream {
@@ -91,7 +91,7 @@ pub struct TcpListener {
9191

9292
impl TcpListener {
9393
pub fn bind(addr: IpAddr) -> Option<TcpListener> {
94-
let listener = unsafe { unsafe_borrow_io().tcp_bind(addr) };
94+
let listener = unsafe { (*unsafe_borrow_io()).tcp_bind(addr) };
9595
match listener {
9696
Ok(l) => {
9797
Some(TcpListener {

src/libcore/rt/local_sched.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,24 @@ pub fn borrow(f: &fn(&mut Scheduler)) {
7878
///
7979
/// Because this leaves the Scheduler in thread-local storage it is possible
8080
/// For the Scheduler pointer to be aliased
81-
pub unsafe fn unsafe_borrow() -> &mut Scheduler {
81+
pub unsafe fn unsafe_borrow() -> *mut Scheduler {
8282
let key = tls_key();
8383
let mut void_sched: *mut c_void = tls::get(key);
8484
rtassert!(void_sched.is_not_null());
8585
{
86-
let void_sched_ptr = &mut void_sched;
87-
let sched: &mut ~Scheduler = {
88-
cast::transmute::<&mut *mut c_void, &mut ~Scheduler>(void_sched_ptr)
89-
};
90-
let sched: &mut Scheduler = &mut **sched;
86+
let sched: *mut *mut c_void = &mut void_sched;
87+
let sched: *mut ~Scheduler = sched as *mut ~Scheduler;
88+
let sched: *mut Scheduler = &mut **sched;
9189
return sched;
9290
}
9391
}
9492

95-
pub unsafe fn unsafe_borrow_io() -> &mut IoFactoryObject {
96-
let sched = unsafe_borrow();
97-
return sched.event_loop.io().unwrap();
93+
pub unsafe fn unsafe_borrow_io() -> *mut IoFactoryObject {
94+
unsafe {
95+
let sched = unsafe_borrow();
96+
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
97+
return io;
98+
}
9899
}
99100

100101
fn tls_key() -> tls::Key {

src/libcore/rt/local_services.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,17 @@ pub fn borrow_local_services(f: &fn(&mut LocalServices)) {
169169
}
170170
}
171171
172-
pub unsafe fn unsafe_borrow_local_services() -> &mut LocalServices {
173-
use cast::transmute_mut_region;
174-
175-
match local_sched::unsafe_borrow().current_task {
176-
Some(~ref mut task) => {
177-
transmute_mut_region(&mut task.local_services)
178-
}
179-
None => {
180-
// Don't fail. Infinite recursion
181-
abort!("no local services for schedulers yet")
172+
pub unsafe fn unsafe_borrow_local_services() -> *mut LocalServices {
173+
unsafe {
174+
match (*local_sched::unsafe_borrow()).current_task {
175+
Some(~ref mut task) => {
176+
let s: *mut LocalServices = &mut task.local_services;
177+
return s;
178+
}
179+
None => {
180+
// Don't fail. Infinite recursion
181+
abort!("no local services for schedulers yet")
182+
}
182183
}
183184
}
184185
}

src/libcore/rt/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/*! Rust runtime services, including the task scheduler and I/O interface
12-
13-
# XXX
14-
15-
* Unsafe uses of borrowed pointers should just use unsafe pointers
16-
17-
*/
18-
11+
//! Rust runtime services, including the task scheduler and I/O interface
1912
2013
#[doc(hidden)];
2114

src/libcore/rt/sched.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub impl Scheduler {
106106
}
107107
}
108108

109+
let scheduler = &mut *scheduler;
109110
scheduler.event_loop.callback(run_scheduler_once);
110111
scheduler.event_loop.run();
111112
}
@@ -179,18 +180,18 @@ pub impl Scheduler {
179180
// Take pointers to both the task and scheduler's saved registers.
180181
unsafe {
181182
let sched = local_sched::unsafe_borrow();
182-
let (sched_context, _, next_task_context) = sched.get_contexts();
183+
let (sched_context, _, next_task_context) = (*sched).get_contexts();
183184
let next_task_context = next_task_context.unwrap();
184185
// Context switch to the task, restoring it's registers
185186
// and saving the scheduler's
186187
Context::swap(sched_context, next_task_context);
187188

188189
let sched = local_sched::unsafe_borrow();
189190
// The running task should have passed ownership elsewhere
190-
assert!(sched.current_task.is_none());
191+
assert!((*sched).current_task.is_none());
191192

192193
// Running tasks may have asked us to do some cleanup
193-
sched.run_cleanup_job();
194+
(*sched).run_cleanup_job();
194195
}
195196
}
196197

@@ -208,21 +209,25 @@ pub impl Scheduler {
208209

209210
rtdebug!("blocking task");
210211

211-
let blocked_task = this.current_task.swap_unwrap();
212-
let f_fake_region = unsafe { transmute::<&fn(~Task), &fn(~Task)>(f) };
213-
let f_opaque = ClosureConverter::from_fn(f_fake_region);
214-
this.enqueue_cleanup_job(GiveTask(blocked_task, f_opaque));
212+
unsafe {
213+
let blocked_task = this.current_task.swap_unwrap();
214+
let f_fake_region = transmute::<&fn(~Task), &fn(~Task)>(f);
215+
let f_opaque = ClosureConverter::from_fn(f_fake_region);
216+
this.enqueue_cleanup_job(GiveTask(blocked_task, f_opaque));
217+
}
215218

216219
local_sched::put(this);
217220

218-
let sched = unsafe { local_sched::unsafe_borrow() };
219-
let (sched_context, last_task_context, _) = sched.get_contexts();
220-
let last_task_context = last_task_context.unwrap();
221-
Context::swap(last_task_context, sched_context);
221+
unsafe {
222+
let sched = local_sched::unsafe_borrow();
223+
let (sched_context, last_task_context, _) = (*sched).get_contexts();
224+
let last_task_context = last_task_context.unwrap();
225+
Context::swap(last_task_context, sched_context);
222226

223-
// We could be executing in a different thread now
224-
let sched = unsafe { local_sched::unsafe_borrow() };
225-
sched.run_cleanup_job();
227+
// We could be executing in a different thread now
228+
let sched = local_sched::unsafe_borrow();
229+
(*sched).run_cleanup_job();
230+
}
226231
}
227232

228233
/// Switch directly to another task, without going through the scheduler.
@@ -244,14 +249,14 @@ pub impl Scheduler {
244249

245250
unsafe {
246251
let sched = local_sched::unsafe_borrow();
247-
let (_, last_task_context, next_task_context) = sched.get_contexts();
252+
let (_, last_task_context, next_task_context) = (*sched).get_contexts();
248253
let last_task_context = last_task_context.unwrap();
249254
let next_task_context = next_task_context.unwrap();
250255
Context::swap(last_task_context, next_task_context);
251256

252257
// We could be executing in a different thread now
253258
let sched = local_sched::unsafe_borrow();
254-
sched.run_cleanup_job();
259+
(*sched).run_cleanup_job();
255260
}
256261
}
257262

@@ -356,10 +361,10 @@ pub impl Task {
356361
// have asked us to do some cleanup.
357362
unsafe {
358363
let sched = local_sched::unsafe_borrow();
359-
sched.run_cleanup_job();
364+
(*sched).run_cleanup_job();
360365

361366
let sched = local_sched::unsafe_borrow();
362-
let task = sched.current_task.get_mut_ref();
367+
let task = (*sched).current_task.get_mut_ref();
363368
// FIXME #6141: shouldn't neet to put `start()` in another closure
364369
task.local_services.run(||start());
365370
}

src/libcore/rt/uv/uvio.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,12 @@ impl RtioTcpStream for UvTcpStream {
327327
#[test]
328328
fn test_simple_io_no_connect() {
329329
do run_in_newsched_task {
330-
let io = unsafe { local_sched::unsafe_borrow_io() };
331-
let addr = next_test_ip4();
332-
let maybe_chan = io.tcp_connect(addr);
333-
assert!(maybe_chan.is_err());
330+
unsafe {
331+
let io = local_sched::unsafe_borrow_io();
332+
let addr = next_test_ip4();
333+
let maybe_chan = (*io).tcp_connect(addr);
334+
assert!(maybe_chan.is_err());
335+
}
334336
}
335337
}
336338

@@ -343,7 +345,7 @@ fn test_simple_tcp_server_and_client() {
343345
do spawntask_immediately {
344346
unsafe {
345347
let io = local_sched::unsafe_borrow_io();
346-
let mut listener = io.tcp_bind(addr).unwrap();
348+
let mut listener = (*io).tcp_bind(addr).unwrap();
347349
let mut stream = listener.accept().unwrap();
348350
let mut buf = [0, .. 2048];
349351
let nread = stream.read(buf).unwrap();
@@ -360,7 +362,7 @@ fn test_simple_tcp_server_and_client() {
360362
do spawntask_immediately {
361363
unsafe {
362364
let io = local_sched::unsafe_borrow_io();
363-
let mut stream = io.tcp_connect(addr).unwrap();
365+
let mut stream = (*io).tcp_connect(addr).unwrap();
364366
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
365367
stream.close();
366368
}
@@ -375,7 +377,7 @@ fn test_read_and_block() {
375377

376378
do spawntask_immediately {
377379
let io = unsafe { local_sched::unsafe_borrow_io() };
378-
let mut listener = io.tcp_bind(addr).unwrap();
380+
let mut listener = unsafe { (*io).tcp_bind(addr).unwrap() };
379381
let mut stream = listener.accept().unwrap();
380382
let mut buf = [0, .. 2048];
381383

@@ -412,13 +414,15 @@ fn test_read_and_block() {
412414
}
413415

414416
do spawntask_immediately {
415-
let io = unsafe { local_sched::unsafe_borrow_io() };
416-
let mut stream = io.tcp_connect(addr).unwrap();
417-
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
418-
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
419-
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
420-
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
421-
stream.close();
417+
unsafe {
418+
let io = local_sched::unsafe_borrow_io();
419+
let mut stream = (*io).tcp_connect(addr).unwrap();
420+
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
421+
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
422+
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
423+
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
424+
stream.close();
425+
}
422426
}
423427

424428
}
@@ -433,7 +437,7 @@ fn test_read_read_read() {
433437
do spawntask_immediately {
434438
unsafe {
435439
let io = local_sched::unsafe_borrow_io();
436-
let mut listener = io.tcp_bind(addr).unwrap();
440+
let mut listener = (*io).tcp_bind(addr).unwrap();
437441
let mut stream = listener.accept().unwrap();
438442
let mut buf = [1, .. 2048];
439443
let mut total_bytes_written = 0;
@@ -447,20 +451,22 @@ fn test_read_read_read() {
447451
}
448452

449453
do spawntask_immediately {
450-
let io = unsafe { local_sched::unsafe_borrow_io() };
451-
let mut stream = io.tcp_connect(addr).unwrap();
452-
let mut buf = [0, .. 2048];
453-
let mut total_bytes_read = 0;
454-
while total_bytes_read < MAX {
455-
let nread = stream.read(buf).unwrap();
456-
rtdebug!("read %u bytes", nread as uint);
457-
total_bytes_read += nread;
458-
for uint::range(0, nread) |i| {
459-
assert!(buf[i] == 1);
454+
unsafe {
455+
let io = local_sched::unsafe_borrow_io();
456+
let mut stream = (*io).tcp_connect(addr).unwrap();
457+
let mut buf = [0, .. 2048];
458+
let mut total_bytes_read = 0;
459+
while total_bytes_read < MAX {
460+
let nread = stream.read(buf).unwrap();
461+
rtdebug!("read %u bytes", nread as uint);
462+
total_bytes_read += nread;
463+
for uint::range(0, nread) |i| {
464+
assert!(buf[i] == 1);
465+
}
460466
}
467+
rtdebug!("read %u bytes total", total_bytes_read as uint);
468+
stream.close();
461469
}
462-
rtdebug!("read %u bytes total", total_bytes_read as uint);
463-
stream.close();
464470
}
465471
}
466472
}

src/libcore/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
218218
gc::cleanup_stack_for_failure();
219219
unsafe {
220220
let local_services = unsafe_borrow_local_services();
221-
match local_services.unwinder {
221+
match (*local_services).unwinder {
222222
Some(ref mut unwinder) => unwinder.begin_unwind(),
223223
None => abort!("failure without unwinder. aborting process")
224224
}

src/libcore/task/local_data_priv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Handle {
3636
}
3737
_ => {
3838
let local_services = unsafe_borrow_local_services();
39-
NewHandle(&mut local_services.storage)
39+
NewHandle(&mut (*local_services).storage)
4040
}
4141
}
4242
}

src/libuv

0 commit comments

Comments
 (0)