// Copyright 2012 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Runtime calls emitted by the compiler. use cast::transmute; use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int, STDERR_FILENO}; use managed::raw::BoxRepr; use str; use sys; use unstable::exchange_alloc; use cast::transmute; use task::rt::rust_get_task; #[allow(non_camel_case_types)] pub type rust_task = c_void; #[cfg(target_word_size = "32")] pub static FROZEN_BIT: uint = 0x80000000; #[cfg(target_word_size = "64")] pub static FROZEN_BIT: uint = 0x8000000000000000; pub mod rustrt { use unstable::lang::rust_task; use libc::{c_void, c_char, uintptr_t}; pub extern { #[rust_stack] unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t) -> *c_char; #[rust_stack] unsafe fn rust_upcall_free(ptr: *c_char); #[fast_ffi] unsafe fn rust_upcall_malloc_noswitch(td: *c_char, size: uintptr_t) -> *c_char; #[fast_ffi] unsafe fn rust_upcall_free_noswitch(ptr: *c_char); #[rust_stack] fn rust_take_task_borrow_list(task: *rust_task) -> *c_void; #[rust_stack] fn rust_set_task_borrow_list(task: *rust_task, map: *c_void); } } #[lang="fail_"] pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! { sys::begin_unwind_(expr, file, line); } #[lang="fail_bounds_check"] pub fn fail_bounds_check(file: *c_char, line: size_t, index: size_t, len: size_t) { let msg = fmt!("index out of bounds: the len is %d but the index is %d", len as int, index as int); do str::as_buf(msg) |p, _len| { fail_(p as *c_char, file, line); } } struct BorrowRecord { box: *mut BoxRepr, file: *c_char, line: size_t } fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) { unsafe { let cur_task = rust_get_task(); let mut borrow_list: ~[BorrowRecord] = { let ptr = rustrt::rust_take_task_borrow_list(cur_task); if ptr.is_null() { ~[] } else { transmute(ptr) } }; borrow_list = f(borrow_list); rustrt::rust_set_task_borrow_list(cur_task, transmute(borrow_list)); } } pub fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) { if !::rt::env::get().debug_borrows { let msg = "borrowed"; do str::as_buf(msg) |msg_p, _| { fail_(msg_p as *c_char, file, line); } } else { do swap_task_borrow_list |borrow_list| { let mut msg = ~"borrowed"; let mut sep = " at "; for borrow_list.each_reverse |entry| { if entry.box == box { str::push_str(&mut msg, sep); let filename = unsafe { str::raw::from_c_str(entry.file) }; str::push_str(&mut msg, filename); str::push_str(&mut msg, fmt!(":%u", line as uint)); sep = " and at "; } } do str::as_buf(msg) |msg_p, _| { fail_(msg_p as *c_char, file, line) } borrow_list } } } // FIXME #4942: Make these signatures agree with exchange_alloc's signatures #[lang="exchange_malloc"] #[inline(always)] pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { let result = transmute(exchange_alloc::malloc(transmute(td), transmute(size))); debug_ptr("exchange_malloc: ", result); return result; } /// Because this code is so perf. sensitive, use a static constant so that /// debug printouts are compiled out most of the time. static ENABLE_DEBUG_PTR: bool = false; #[inline] pub fn debug_ptr(tag: &'static str, p: *T) { //! A useful debugging function that prints a pointer + tag + newline //! without allocating memory. if ENABLE_DEBUG_PTR && ::rt::env::get().debug_mem { debug_ptr_slow(tag, p); } fn debug_ptr_slow(tag: &'static str, p: *T) { use io; let dbg = STDERR_FILENO as io::fd_t; let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; dbg.write_str(tag); static uint_nibbles: uint = ::uint::bytes << 1; let mut buffer = [0_u8, ..uint_nibbles+1]; let mut i = p as uint; let mut c = uint_nibbles; while c > 0 { c -= 1; buffer[c] = letters[i & 0xF] as u8; i >>= 4; } dbg.write(buffer.slice(0, uint_nibbles)); dbg.write_str("\n"); } } // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from // inside a landing pad may corrupt the state of the exception handler. If a // problem occurs, call exit instead. #[lang="exchange_free"] #[inline(always)] pub unsafe fn exchange_free(ptr: *c_char) { debug_ptr("exchange_free: ", ptr); exchange_alloc::free(transmute(ptr)) } #[lang="malloc"] #[inline(always)] pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { let result = rustrt::rust_upcall_malloc_noswitch(td, size); debug_ptr("local_malloc: ", result); return result; } // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from // inside a landing pad may corrupt the state of the exception handler. If a // problem occurs, call exit instead. #[lang="free"] #[inline(always)] pub unsafe fn local_free(ptr: *c_char) { debug_ptr("local_free: ", ptr); rustrt::rust_upcall_free_noswitch(ptr); } #[cfg(stage0)] #[lang="borrow_as_imm"] #[inline(always)] pub unsafe fn borrow_as_imm(a: *u8) { let a: *mut BoxRepr = transmute(a); (*a).header.ref_count |= FROZEN_BIT; } #[cfg(not(stage0))] #[lang="borrow_as_imm"] #[inline(always)] pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) { let a: *mut BoxRepr = transmute(a); (*a).header.ref_count |= FROZEN_BIT; if ::rt::env::get().debug_borrows { do swap_task_borrow_list |borrow_list| { let mut borrow_list = borrow_list; borrow_list.push(BorrowRecord {box: a, file: file, line: line}); borrow_list } } } #[lang="return_to_mut"] #[inline(always)] pub unsafe fn return_to_mut(a: *u8) { // Sometimes the box is null, if it is conditionally frozen. // See e.g. #4904. if !a.is_null() { let a: *mut BoxRepr = transmute(a); (*a).header.ref_count &= !FROZEN_BIT; } } #[cfg(stage0)] #[lang="check_not_borrowed"] #[inline(always)] pub unsafe fn check_not_borrowed(a: *u8) { let a: *mut BoxRepr = transmute(a); if ((*a).header.ref_count & FROZEN_BIT) != 0 { do str::as_buf("XXX") |file_p, _| { fail_borrowed(a, file_p as *c_char, 0); } } } #[cfg(not(stage0))] #[lang="check_not_borrowed"] #[inline(always)] pub unsafe fn check_not_borrowed(a: *u8, file: *c_char, line: size_t) { let a: *mut BoxRepr = transmute(a); if ((*a).header.ref_count & FROZEN_BIT) != 0 { fail_borrowed(a, file, line); } } #[lang="strdup_uniq"] #[inline(always)] pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str { str::raw::from_buf_len(ptr, len) } #[lang="start"] pub fn start(main: *u8, argc: int, argv: **c_char, crate_map: *u8) -> int { use libc::getenv; use rt::start; unsafe { let use_old_rt = do str::as_c_str("RUST_NEWRT") |s| { getenv(s).is_null() }; if use_old_rt { return rust_start(main as *c_void, argc as c_int, argv, crate_map as *c_void) as int; } else { return start(main, argc, argv, crate_map); } } extern { fn rust_start(main: *c_void, argc: c_int, argv: **c_char, crate_map: *c_void) -> c_int; } } // Local Variables: // mode: rust; // fill-column: 78; // indent-tabs-mode: nil // c-basic-offset: 4 // buffer-file-coding-system: utf-8-unix // End: