librustc: Make C functions unsafe
This commit is contained in:
@@ -280,17 +280,23 @@ extern mod rusti {
|
||||
// I get link errors. This is a bug that needs investigated more.
|
||||
#[doc(hidden)]
|
||||
pub fn atomic_xchng_rel(dst: &mut int, src: int) -> int {
|
||||
unsafe {
|
||||
rusti::atomic_xchg_rel(dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn atomic_add_acq(dst: &mut int, src: int) -> int {
|
||||
unsafe {
|
||||
rusti::atomic_xadd_acq(dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn atomic_sub_rel(dst: &mut int, src: int) -> int {
|
||||
unsafe {
|
||||
rusti::atomic_xsub_rel(dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
|
||||
@@ -93,8 +93,10 @@ type rust_port_id = uint;
|
||||
type GlobalPtr = *libc::uintptr_t;
|
||||
|
||||
fn compare_and_swap(address: &mut int, oldval: int, newval: int) -> bool {
|
||||
unsafe {
|
||||
let old = rusti::atomic_cxchg(address, oldval, newval);
|
||||
old == oldval
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -201,11 +201,13 @@ impl ReprVisitor {
|
||||
|
||||
#[inline(always)]
|
||||
fn visit_ptr_inner(ptr: *c_void, inner: *TyDesc) -> bool {
|
||||
unsafe {
|
||||
let mut u = ReprVisitor(ptr, self.writer);
|
||||
let v = reflect::MovePtrAdaptor(move u);
|
||||
visit_tydesc(inner, (move v) as @TyVisitor);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn write<T:Repr>() -> bool {
|
||||
@@ -558,11 +560,13 @@ impl ReprVisitor : TyVisitor {
|
||||
}
|
||||
|
||||
pub fn write_repr<T>(writer: @Writer, object: &T) {
|
||||
unsafe {
|
||||
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
|
||||
let tydesc = intrinsic::get_tydesc::<T>();
|
||||
let mut u = ReprVisitor(ptr, writer);
|
||||
let v = reflect::MovePtrAdaptor(move u);
|
||||
visit_tydesc(tydesc, (move v) as @TyVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -571,23 +571,29 @@ pub fn try<T:Owned>(f: fn~() -> T) -> Result<T,()> {
|
||||
pub fn yield() {
|
||||
//! Yield control to the task scheduler
|
||||
|
||||
unsafe {
|
||||
let task_ = rt::rust_get_task();
|
||||
let killed = rt::rust_task_yield(task_);
|
||||
if killed && !failing() {
|
||||
fail ~"killed";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn failing() -> bool {
|
||||
//! True if the running task has failed
|
||||
|
||||
unsafe {
|
||||
rt::rust_task_is_unwinding(rt::rust_get_task())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_task() -> Task {
|
||||
//! Get a handle to the running task
|
||||
|
||||
unsafe {
|
||||
TaskHandle(rt::get_task_id())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -608,7 +614,11 @@ pub fn get_task() -> Task {
|
||||
pub unsafe fn unkillable<U>(f: fn() -> U) -> U {
|
||||
struct AllowFailure {
|
||||
t: *rust_task,
|
||||
drop { rt::rust_task_allow_kill(self.t); }
|
||||
drop {
|
||||
unsafe {
|
||||
rt::rust_task_allow_kill(self.t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn AllowFailure(t: *rust_task) -> AllowFailure{
|
||||
@@ -617,17 +627,23 @@ pub unsafe fn unkillable<U>(f: fn() -> U) -> U {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let t = rt::rust_get_task();
|
||||
let _allow_failure = AllowFailure(t);
|
||||
rt::rust_task_inhibit_kill(t);
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
/// The inverse of unkillable. Only ever to be used nested in unkillable().
|
||||
pub unsafe fn rekillable<U>(f: fn() -> U) -> U {
|
||||
struct DisallowFailure {
|
||||
t: *rust_task,
|
||||
drop { rt::rust_task_inhibit_kill(self.t); }
|
||||
drop {
|
||||
unsafe {
|
||||
rt::rust_task_inhibit_kill(self.t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn DisallowFailure(t: *rust_task) -> DisallowFailure {
|
||||
@@ -636,10 +652,12 @@ pub unsafe fn rekillable<U>(f: fn() -> U) -> U {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let t = rt::rust_get_task();
|
||||
let _allow_failure = DisallowFailure(t);
|
||||
rt::rust_task_allow_kill(t);
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -650,10 +668,12 @@ pub unsafe fn atomically<U>(f: fn() -> U) -> U {
|
||||
struct DeferInterrupts {
|
||||
t: *rust_task,
|
||||
drop {
|
||||
unsafe {
|
||||
rt::rust_task_allow_yield(self.t);
|
||||
rt::rust_task_allow_kill(self.t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn DeferInterrupts(t: *rust_task) -> DeferInterrupts {
|
||||
DeferInterrupts {
|
||||
@@ -661,11 +681,13 @@ pub unsafe fn atomically<U>(f: fn() -> U) -> U {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let t = rt::rust_get_task();
|
||||
let _interrupts = DeferInterrupts(t);
|
||||
rt::rust_task_inhibit_kill(t);
|
||||
rt::rust_task_inhibit_yield(t);
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
#[test] #[should_fail] #[ignore(cfg(windows))]
|
||||
@@ -908,9 +930,11 @@ fn test_spawn_sched() {
|
||||
let ch = oldcomm::Chan(&po);
|
||||
|
||||
fn f(i: int, ch: oldcomm::Chan<()>) {
|
||||
unsafe {
|
||||
let parent_sched_id = rt::rust_get_sched_id();
|
||||
|
||||
do spawn_sched(SingleThreaded) {
|
||||
unsafe {
|
||||
let child_sched_id = rt::rust_get_sched_id();
|
||||
assert parent_sched_id != child_sched_id;
|
||||
|
||||
@@ -919,7 +943,9 @@ fn test_spawn_sched() {
|
||||
} else {
|
||||
f(i - 1, ch);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
f(10, ch);
|
||||
@@ -932,13 +958,17 @@ fn test_spawn_sched_childs_on_same_sched() {
|
||||
let ch = oldcomm::Chan(&po);
|
||||
|
||||
do spawn_sched(SingleThreaded) {
|
||||
unsafe {
|
||||
let parent_sched_id = rt::rust_get_sched_id();
|
||||
do spawn {
|
||||
unsafe {
|
||||
let child_sched_id = rt::rust_get_sched_id();
|
||||
// This should be on the same scheduler
|
||||
assert parent_sched_id == child_sched_id;
|
||||
oldcomm::send(ch, ());
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
oldcomm::recv(po);
|
||||
@@ -1185,11 +1215,13 @@ fn test_sched_thread_per_core() {
|
||||
let (port, chan) = pipes::stream();
|
||||
|
||||
do spawn_sched(ThreadPerCore) |move chan| {
|
||||
unsafe {
|
||||
let cores = rt::rust_num_threads();
|
||||
let reported_threads = rt::rust_sched_threads();
|
||||
assert(cores as uint == reported_threads as uint);
|
||||
chan.send(());
|
||||
}
|
||||
}
|
||||
|
||||
port.recv();
|
||||
}
|
||||
@@ -1199,6 +1231,7 @@ fn test_spawn_thread_on_demand() {
|
||||
let (port, chan) = pipes::stream();
|
||||
|
||||
do spawn_sched(ManualThreads(2)) |move chan| {
|
||||
unsafe {
|
||||
let max_threads = rt::rust_sched_threads();
|
||||
assert(max_threads as int == 2);
|
||||
let running_threads = rt::rust_sched_current_nonlazy_threads();
|
||||
@@ -1216,6 +1249,7 @@ fn test_spawn_thread_on_demand() {
|
||||
port2.recv();
|
||||
chan.send(());
|
||||
}
|
||||
}
|
||||
|
||||
port.recv();
|
||||
}
|
||||
|
||||
@@ -308,6 +308,7 @@ struct TCB {
|
||||
notifier: Option<AutoNotify>,
|
||||
// Runs on task exit.
|
||||
drop {
|
||||
unsafe {
|
||||
// If we are failing, the whole taskgroup needs to die.
|
||||
if rt::rust_task_is_unwinding(self.me) {
|
||||
self.notifier.iter(|x| { x.failed = true; });
|
||||
@@ -321,13 +322,15 @@ struct TCB {
|
||||
leave_taskgroup(tg, self.me, true);
|
||||
}
|
||||
}
|
||||
// It doesn't matter whether this happens before or after dealing with
|
||||
// our own taskgroup, so long as both happen before we die. We need to
|
||||
// remove ourself from every ancestor we can, so no cleanup; no break.
|
||||
// It doesn't matter whether this happens before or after dealing
|
||||
// with our own taskgroup, so long as both happen before we die.
|
||||
// We remove ourself from every ancestor we can, so no cleanup; no
|
||||
// break.
|
||||
for each_ancestor(&mut self.ancestors, None) |ancestor_group| {
|
||||
leave_taskgroup(ancestor_group, self.me, false);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn TCB(me: *rust_task, tasks: TaskGroupArc, ancestors: AncestorList,
|
||||
@@ -391,14 +394,16 @@ fn leave_taskgroup(state: TaskGroupInner, me: *rust_task,
|
||||
|
||||
// NB: Runs in destructor/post-exit context. Can't 'fail'.
|
||||
fn kill_taskgroup(state: TaskGroupInner, me: *rust_task, is_main: bool) {
|
||||
unsafe {
|
||||
// NB: We could do the killing iteration outside of the group arc, by
|
||||
// having "let mut newstate" here, swapping inside, and iterating after.
|
||||
// But that would let other exiting tasks fall-through and exit while we
|
||||
// were trying to kill them, causing potential use-after-free. A task's
|
||||
// presence in the arc guarantees it's alive only while we hold the lock,
|
||||
// so if we're failing, all concurrently exiting tasks must wait for us.
|
||||
// To do it differently, we'd have to use the runtime's task refcounting,
|
||||
// but that could leave task structs around long after their task exited.
|
||||
// having "let mut newstate" here, swapping inside, and iterating
|
||||
// after. But that would let other exiting tasks fall-through and exit
|
||||
// while we were trying to kill them, causing potential
|
||||
// use-after-free. A task's presence in the arc guarantees it's alive
|
||||
// only while we hold the lock, so if we're failing, all concurrently
|
||||
// exiting tasks must wait for us. To do it differently, we'd have to
|
||||
// use the runtime's task refcounting, but that could leave task
|
||||
// structs around long after their task exited.
|
||||
let newstate = util::replace(state, None);
|
||||
// Might already be None, if Somebody is failing simultaneously.
|
||||
// That's ok; only one task needs to do the dirty work. (Might also
|
||||
@@ -423,6 +428,7 @@ fn kill_taskgroup(state: TaskGroupInner, me: *rust_task, is_main: bool) {
|
||||
// that the whole taskgroup is failing, to forbid new spawns.
|
||||
}
|
||||
// (note: multiple tasks may reach this point)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME (#2912): Work around core-vs-coretest function duplication. Can't use
|
||||
@@ -434,32 +440,32 @@ macro_rules! taskgroup_key (
|
||||
|
||||
fn gen_child_taskgroup(linked: bool, supervised: bool)
|
||||
-> (TaskGroupArc, AncestorList, bool) {
|
||||
unsafe {
|
||||
let spawner = rt::rust_get_task();
|
||||
/*######################################################################*
|
||||
/*##################################################################*
|
||||
* Step 1. Get spawner's taskgroup info.
|
||||
*######################################################################*/
|
||||
let spawner_group = match unsafe { local_get(spawner,
|
||||
taskgroup_key!()) } {
|
||||
*##################################################################*/
|
||||
let spawner_group = match local_get(spawner, taskgroup_key!()) {
|
||||
None => {
|
||||
// Main task, doing first spawn ever. Lazily initialise here.
|
||||
let mut members = new_taskset();
|
||||
taskset_insert(&mut members, spawner);
|
||||
let tasks =
|
||||
private::exclusive(Some({ mut members: move members,
|
||||
mut descendants: new_taskset() }));
|
||||
private::exclusive(Some({
|
||||
mut members: move members,
|
||||
mut descendants: new_taskset()
|
||||
}));
|
||||
// Main task/group has no ancestors, no notifier, etc.
|
||||
let group =
|
||||
@TCB(spawner, move tasks, AncestorList(None), true, None);
|
||||
unsafe {
|
||||
local_set(spawner, taskgroup_key!(), group);
|
||||
}
|
||||
group
|
||||
}
|
||||
Some(group) => group
|
||||
};
|
||||
/*######################################################################*
|
||||
/*##################################################################*
|
||||
* Step 2. Process spawn options for child.
|
||||
*######################################################################*/
|
||||
*##################################################################*/
|
||||
return if linked {
|
||||
// Child is in the same group as spawner.
|
||||
let g = spawner_group.tasks.clone();
|
||||
@@ -469,14 +475,17 @@ fn gen_child_taskgroup(linked: bool, supervised: bool)
|
||||
(move g, move a, spawner_group.is_main)
|
||||
} else {
|
||||
// Child is in a separate group from spawner.
|
||||
let g = private::exclusive(Some({ mut members: new_taskset(),
|
||||
mut descendants: new_taskset() }));
|
||||
let g = private::exclusive(Some({
|
||||
mut members: new_taskset(),
|
||||
mut descendants: new_taskset()
|
||||
}));
|
||||
let a = if supervised {
|
||||
// Child's ancestors start with the spawner.
|
||||
let old_ancestors = share_ancestors(&mut spawner_group.ancestors);
|
||||
// FIXME(#3068) - The generation counter is only used for a debug
|
||||
// assertion, but initialising it requires locking a mutex. Hence
|
||||
// it should be enabled only in debug builds.
|
||||
let old_ancestors =
|
||||
share_ancestors(&mut spawner_group.ancestors);
|
||||
// FIXME(#3068) - The generation counter is only used for a
|
||||
// debug assertion, but initialising it requires locking a
|
||||
// mutex. Hence it should be enabled only in debug builds.
|
||||
let new_generation =
|
||||
match *old_ancestors {
|
||||
Some(ref arc) => {
|
||||
@@ -496,6 +505,7 @@ fn gen_child_taskgroup(linked: bool, supervised: bool)
|
||||
};
|
||||
(move g, move a, false)
|
||||
};
|
||||
}
|
||||
|
||||
fn share_ancestors(ancestors: &mut AncestorList) -> AncestorList {
|
||||
// Appease the borrow-checker. Really this wants to be written as:
|
||||
@@ -632,6 +642,7 @@ pub fn spawn_raw(opts: TaskOpts, f: fn~()) {
|
||||
}
|
||||
|
||||
fn new_task_in_new_sched(opts: SchedOpts) -> *rust_task {
|
||||
unsafe {
|
||||
if opts.foreign_stack_size != None {
|
||||
fail ~"foreign_stack_size scheduler option unimplemented";
|
||||
}
|
||||
@@ -658,6 +669,7 @@ pub fn spawn_raw(opts: TaskOpts, f: fn~()) {
|
||||
};
|
||||
rt::rust_new_task_in_sched(sched_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -19,8 +19,10 @@ mod intrinsic {
|
||||
// FIXME (#3727): remove this when the interface has settled and the
|
||||
// version in sys is no longer present.
|
||||
pub fn get_tydesc<T>() -> *TyDesc {
|
||||
unsafe {
|
||||
rusti::get_tydesc::<T>() as *TyDesc
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TyDesc = {
|
||||
size: uint,
|
||||
|
||||
@@ -56,6 +56,7 @@ use syntax::ast::{type_value_ns, ty_param_bound, unnamed_field};
|
||||
use syntax::ast::{variant, view_item, view_item_export, view_item_import};
|
||||
use syntax::ast::{view_item_use, view_path_glob, view_path_list};
|
||||
use syntax::ast::{view_path_simple, visibility, anonymous, named, not};
|
||||
use syntax::ast::{unsafe_fn};
|
||||
use syntax::ast_util::{def_id_of_def, dummy_sp, local_def};
|
||||
use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
|
||||
use syntax::ast_util::{Privacy, Public, Private, visibility_to_privacy};
|
||||
@@ -1643,8 +1644,8 @@ impl Resolver {
|
||||
foreign_item.span);
|
||||
|
||||
match /*bad*/copy foreign_item.node {
|
||||
foreign_item_fn(_, purity, type_parameters) => {
|
||||
let def = def_fn(local_def(foreign_item.id), purity);
|
||||
foreign_item_fn(_, _, type_parameters) => {
|
||||
let def = def_fn(local_def(foreign_item.id), unsafe_fn);
|
||||
(*name_bindings).define_value(Public, def, foreign_item.span);
|
||||
|
||||
do self.with_type_parameter_rib
|
||||
|
||||
@@ -3342,7 +3342,7 @@ fn check_intrinsic_type(ccx: @crate_ctxt, it: @ast::foreign_item) {
|
||||
}
|
||||
};
|
||||
let fty = ty::mk_fn(tcx, FnTyBase {
|
||||
meta: FnMeta {purity: ast::impure_fn,
|
||||
meta: FnMeta {purity: ast::unsafe_fn,
|
||||
proto: ast::ProtoBare,
|
||||
onceness: ast::Many,
|
||||
region: ty::re_static,
|
||||
|
||||
@@ -889,9 +889,8 @@ fn ty_of_item(ccx: @crate_ctxt, it: @ast::item)
|
||||
fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item)
|
||||
-> ty::ty_param_bounds_and_ty {
|
||||
match /*bad*/copy it.node {
|
||||
ast::foreign_item_fn(fn_decl, purity, params) => {
|
||||
return ty_of_foreign_fn_decl(ccx, fn_decl, purity, params,
|
||||
local_def(it.id));
|
||||
ast::foreign_item_fn(fn_decl, _, params) => {
|
||||
return ty_of_foreign_fn_decl(ccx, fn_decl, params, local_def(it.id));
|
||||
}
|
||||
ast::foreign_item_const(t) => {
|
||||
let rb = in_binding_rscope(empty_rscope);
|
||||
@@ -962,7 +961,6 @@ fn ty_param_bounds(ccx: @crate_ctxt,
|
||||
|
||||
fn ty_of_foreign_fn_decl(ccx: @crate_ctxt,
|
||||
decl: ast::fn_decl,
|
||||
purity: ast::purity,
|
||||
+ty_params: ~[ast::ty_param],
|
||||
def_id: ast::def_id) -> ty::ty_param_bounds_and_ty {
|
||||
let bounds = ty_param_bounds(ccx, ty_params);
|
||||
@@ -971,7 +969,7 @@ fn ty_of_foreign_fn_decl(ccx: @crate_ctxt,
|
||||
let output_ty = ast_ty_to_ty(ccx, rb, decl.output);
|
||||
|
||||
let t_fn = ty::mk_fn(ccx.tcx, FnTyBase {
|
||||
meta: FnMeta {purity: purity,
|
||||
meta: FnMeta {purity: ast::unsafe_fn,
|
||||
onceness: ast::Many,
|
||||
proto: ast::ProtoBare,
|
||||
bounds: @~[],
|
||||
|
||||
@@ -254,9 +254,13 @@ impl &Arena {
|
||||
// The external interface
|
||||
#[inline(always)]
|
||||
fn alloc<T>(op: fn() -> T) -> &self/T {
|
||||
unsafe {
|
||||
if !rusti::needs_drop::<T>() {
|
||||
self.alloc_pod(op)
|
||||
} else { self.alloc_nonpod(op) }
|
||||
} else {
|
||||
self.alloc_nonpod(op)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,5 +31,7 @@ extern mod rusti {
|
||||
|
||||
#[inline(always)]
|
||||
fn atomic_xchg(dst: &mut int, src: int) -> int {
|
||||
unsafe {
|
||||
rusti::atomic_xchg(dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,10 @@ extern mod rustrt {
|
||||
}
|
||||
|
||||
fn fact(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
|
||||
@@ -30,7 +30,9 @@ pub enum port<T: Owned> {
|
||||
|
||||
/// Constructs a port
|
||||
pub fn port<T: Owned>() -> port<T> {
|
||||
unsafe {
|
||||
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>() as size_t)))
|
||||
}
|
||||
}
|
||||
|
||||
struct port_ptr<T:Owned> {
|
||||
@@ -75,6 +77,7 @@ pub fn recv<T: Owned>(p: port<T>) -> T { recv_((**p).po) }
|
||||
|
||||
/// Receive on a raw port pointer
|
||||
pub fn recv_<T: Owned>(p: *rust_port) -> T {
|
||||
unsafe {
|
||||
let yield = 0;
|
||||
let yieldp = ptr::addr_of(&yield);
|
||||
let mut res;
|
||||
@@ -90,6 +93,7 @@ pub fn recv_<T: Owned>(p: *rust_port) -> T {
|
||||
task::yield();
|
||||
}
|
||||
move res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,8 +27,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
task::yield();
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -26,9 +26,11 @@ fn getbig_call_c_and_fail(i: int) {
|
||||
if i != 0 {
|
||||
getbig_call_c_and_fail(i - 1);
|
||||
} else {
|
||||
unsafe {
|
||||
rustrt::last_os_error();
|
||||
fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct and_then_get_big_again {
|
||||
|
||||
@@ -15,5 +15,7 @@ extern {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
last_os_error();
|
||||
unsafe {
|
||||
let _ = last_os_error();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,7 @@ extern mod rustrt {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let _foo = rustrt::get_task_id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,15 +19,17 @@ extern mod libc {
|
||||
}
|
||||
|
||||
fn atol(s: ~str) -> int {
|
||||
return str::as_buf(s, { |x, _len| libc::atol(x) });
|
||||
return str::as_buf(s, { |x, _len| unsafe { libc::atol(x) } });
|
||||
}
|
||||
|
||||
fn atoll(s: ~str) -> i64 {
|
||||
return str::as_buf(s, { |x, _len| libc::atoll(x) });
|
||||
return str::as_buf(s, { |x, _len| unsafe { libc::atoll(x) } });
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
assert atol(~"1024") * 10 == atol(~"10240");
|
||||
assert (atoll(~"11111111111111111") * 10i64)
|
||||
== atoll(~"111111111111111110");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -23,8 +23,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -27,8 +27,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -23,8 +23,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn fact(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -14,8 +14,10 @@
|
||||
extern mod externcallback(vers = "0.1");
|
||||
|
||||
fn fact(n: uint) -> uint {
|
||||
unsafe {
|
||||
debug!("n = %?", n);
|
||||
externcallback::rustrt::rust_dbg_call(externcallback::cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -27,7 +27,9 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -23,8 +23,10 @@ extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
|
||||
}
|
||||
|
||||
fn count(n: uint) -> uint {
|
||||
unsafe {
|
||||
task::yield();
|
||||
rustrt::rust_dbg_call(cb, n)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -26,6 +26,8 @@ extern mod rustrt2 {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
rustrt1::last_os_error();
|
||||
rustrt2::last_os_error();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,5 +16,7 @@ extern mod rustrt {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
rustrt::get_task_id();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,22 +23,28 @@ extern mod rusti {
|
||||
mod m {
|
||||
#[cfg(target_arch = "x86")]
|
||||
pub fn main() {
|
||||
unsafe {
|
||||
assert ::rusti::pref_align_of::<u64>() == 8u;
|
||||
assert ::rusti::min_align_of::<u64>() == 4u;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub fn main() {
|
||||
unsafe {
|
||||
assert ::rusti::pref_align_of::<u64>() == 8u;
|
||||
assert ::rusti::min_align_of::<u64>() == 8u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "win32")]
|
||||
mod m {
|
||||
#[cfg(target_arch = "x86")]
|
||||
pub fn main() {
|
||||
unsafe {
|
||||
assert ::rusti::pref_align_of::<u64>() == 8u;
|
||||
assert ::rusti::min_align_of::<u64>() == 8u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@ extern mod cci_intrinsic;
|
||||
use cci_intrinsic::atomic_xchg;
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let mut x = 1;
|
||||
atomic_xchg(&mut x, 5);
|
||||
assert x == 5;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ extern mod rusti {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let x = ~mut 1;
|
||||
|
||||
assert rusti::atomic_cxchg(x, 1, 2) == 1;
|
||||
@@ -58,4 +59,5 @@ fn main() {
|
||||
assert rusti::atomic_xsub_acq(x, 1) == 2;
|
||||
assert rusti::atomic_xsub_rel(x, 1) == 1;
|
||||
assert *x == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,9 @@ extern mod rusti {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
do rusti::frame_address |addr| {
|
||||
assert addr.is_not_null();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,15 @@
|
||||
|
||||
#[abi = "rust-intrinsic"]
|
||||
extern mod rusti {
|
||||
#[legacy_exports];
|
||||
fn move_val_init<T>(dst: &mut T, -src: T);
|
||||
fn move_val<T>(dst: &mut T, -src: T);
|
||||
pub fn move_val_init<T>(dst: &mut T, -src: T);
|
||||
pub fn move_val<T>(dst: &mut T, -src: T);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let mut x = @1;
|
||||
let mut y = @2;
|
||||
rusti::move_val(&mut y, move x);
|
||||
assert *y == 1;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ extern mod rusti {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
unsafe {
|
||||
use rusti::*;
|
||||
|
||||
assert(ctpop8(0i8) == 0i8);
|
||||
@@ -116,5 +116,5 @@ fn main() {
|
||||
assert(bswap16(0x0A0Bi16) == 0x0B0Ai16);
|
||||
assert(bswap32(0x0ABBCC0Di32) == 0x0DCCBB0Ai32);
|
||||
assert(bswap64(0x0122334455667708i64) == 0x0877665544332201i64);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ extern mod rusti {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
unsafe {
|
||||
use rusti::*;
|
||||
|
||||
assert(sqrtf32(64f32).fuzzy_eq(&8f32));
|
||||
@@ -100,5 +100,6 @@ fn main() {
|
||||
// undefined reference to llvm.trunc.f32/64
|
||||
//assert(truncf32(0.1f32) == 0.0f32);
|
||||
//assert(truncf64(-0.1f64) == 0.0f64);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,9 @@ fn to_c_int(v: &mut int) -> &mut c_int {
|
||||
}
|
||||
|
||||
fn lgamma(n: c_double, value: &mut int) -> c_double {
|
||||
unsafe {
|
||||
return m::lgamma(n, to_c_int(value));
|
||||
}
|
||||
}
|
||||
|
||||
#[link_name = "m"]
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// 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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
/*
|
||||
A reduced test case for Issue #506, provided by Rob Arnold.
|
||||
|
||||
Testing spawning foreign functions
|
||||
*/
|
||||
|
||||
extern mod std;
|
||||
|
||||
#[abi = "cdecl"]
|
||||
extern mod rustrt {
|
||||
#[legacy_exports];
|
||||
fn rust_dbg_do_nothing();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
task::spawn(rustrt::rust_dbg_do_nothing);
|
||||
}
|
||||
@@ -16,7 +16,9 @@ extern mod rusti {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let addr = rusti::morestack_addr();
|
||||
assert addr.is_not_null();
|
||||
error!("%?", addr);
|
||||
}
|
||||
}
|
||||
@@ -23,18 +23,19 @@ extern mod rustrt {
|
||||
fn rust_get_task();
|
||||
}
|
||||
|
||||
fn calllink01() { rustrt::rust_get_sched_id(); }
|
||||
fn calllink02() { rustrt::last_os_error(); }
|
||||
fn calllink03() { rustrt::rust_getcwd(); }
|
||||
fn calllink08() { rustrt::get_task_id(); }
|
||||
fn calllink09() { rustrt::rust_sched_threads(); }
|
||||
fn calllink10() { rustrt::rust_get_task(); }
|
||||
fn calllink01() { unsafe { rustrt::rust_get_sched_id(); } }
|
||||
fn calllink02() { unsafe { rustrt::last_os_error(); } }
|
||||
fn calllink03() { unsafe { rustrt::rust_getcwd(); } }
|
||||
fn calllink08() { unsafe { rustrt::get_task_id(); } }
|
||||
fn calllink09() { unsafe { rustrt::rust_sched_threads(); } }
|
||||
fn calllink10() { unsafe { rustrt::rust_get_task(); } }
|
||||
|
||||
fn runtest(f: fn~(), frame_backoff: u32) {
|
||||
runtest2(f, frame_backoff, 0 as *u8);
|
||||
}
|
||||
|
||||
fn runtest2(f: fn~(), frame_backoff: u32, last_stk: *u8) -> u32 {
|
||||
unsafe {
|
||||
let curr_stk = rustrt::debug_get_stk_seg();
|
||||
if (last_stk != curr_stk && last_stk != 0 as *u8) {
|
||||
// We switched stacks, go back and try to hit the dynamic linker
|
||||
@@ -50,6 +51,7 @@ fn runtest2(f: fn~(), frame_backoff: u32, last_stk: *u8) -> u32 {
|
||||
0u32
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -45,7 +45,7 @@ mod m {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
unsafe {
|
||||
let x = {c8: 22u8, t: {c64: 44u32}};
|
||||
|
||||
// Send it through the shape code
|
||||
@@ -63,4 +63,5 @@ fn main() {
|
||||
assert sys::size_of::<outer>() == m::size();
|
||||
|
||||
assert y == ~"{c8: 22, t: {c64: 44}}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ mod m {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
unsafe {
|
||||
let x = {c8: 22u8, t: {c64: 44u64}};
|
||||
|
||||
// Send it through the shape code
|
||||
@@ -80,4 +80,5 @@ fn main() {
|
||||
assert sys::size_of::<outer>() == m::m::size();
|
||||
|
||||
assert y == ~"{c8: 22, t: {c64: 44}}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -483,11 +483,13 @@ impl my_visitor {
|
||||
}
|
||||
|
||||
fn visit_inner(inner: *TyDesc) -> bool {
|
||||
unsafe {
|
||||
let u = my_visitor(*self);
|
||||
let v = ptr_visit_adaptor({inner: u});
|
||||
visit_tydesc(inner, v as TyVisitor);
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl my_visitor: movable_ptr {
|
||||
@@ -621,6 +623,7 @@ fn get_tydesc_for<T>(&&_t: T) -> *TyDesc {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let r = (1,2,3,true,false,{x:5,y:4,z:3});
|
||||
let p = ptr::addr_of(&r) as *c_void;
|
||||
let u = my_visitor(@{mut ptr1: p,
|
||||
@@ -637,5 +640,8 @@ fn main() {
|
||||
io::println(fmt!("val: %s", *s));
|
||||
}
|
||||
error!("%?", copy u.vals);
|
||||
assert u.vals == ~[~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3"];
|
||||
assert u.vals == ~[
|
||||
~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3"
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,11 +36,13 @@ fn main() {
|
||||
let new_task_id = rustrt::rust_new_task_in_sched(new_sched_id);
|
||||
assert !new_task_id.is_null();
|
||||
let f = fn~() {
|
||||
unsafe {
|
||||
let child_sched_id = rustrt::rust_get_sched_id();
|
||||
error!("child_sched_id %?", child_sched_id);
|
||||
assert child_sched_id != parent_sched_id;
|
||||
assert child_sched_id == new_sched_id;
|
||||
oldcomm::send(ch, ());
|
||||
}
|
||||
};
|
||||
let fptr = cast::reinterpret_cast(&ptr::addr_of(&f));
|
||||
rustrt::start_task(new_task_id, fptr);
|
||||
|
||||
@@ -19,6 +19,7 @@ extern mod rustrt {
|
||||
}
|
||||
|
||||
fn test1() {
|
||||
unsafe {
|
||||
let q = { a: 0xaaaa_aaaa_aaaa_aaaa_u64,
|
||||
b: 0xbbbb_bbbb_bbbb_bbbb_u64,
|
||||
c: 0xcccc_cccc_cccc_cccc_u64,
|
||||
@@ -32,10 +33,12 @@ fn test1() {
|
||||
assert qq.b == q.d - 1u64;
|
||||
assert qq.c == q.a + 1u64;
|
||||
assert qq.d == q.b - 1u64;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
fn test2() {
|
||||
unsafe {
|
||||
let f = { a: 1.234567890e-15_f64,
|
||||
b: 0b_1010_1010_u8,
|
||||
c: 1.0987654321e-15_f64 };
|
||||
@@ -46,6 +49,7 @@ fn test2() {
|
||||
assert ff.a == f.c + 1.0f64;
|
||||
assert ff.b == 0xff_u8;
|
||||
assert ff.c == f.a - 1.0f64;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
|
||||
Reference in New Issue
Block a user