sgx.rs 1.87 KB
Newer Older
nhynes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
use std::{
  ffi::CString,
  os::raw::{c_char, c_int},
};

use errors::Result;
use ffi::runtime::TVMValue;
use runtime::{threading::sgx_join_threads, SystemLibModule, TVMArgValue, TVMRetValue};

pub use runtime::threading::tvm_run_worker as run_worker;

#[macro_export]
macro_rules! tvm_ocall {
  ($func: expr) => {
    match $func {
      0 => Ok(()),
      err => Err(format!("SGX error: {}", err)),
    }
  };
}

pub type SgxStatus = u32;

#[cfg(target_env = "sgx")]
extern "C" {
  fn tvm_ocall_packed_func(
    name: *const c_char,
    arg_values: *const TVMValue,
    type_codes: *const c_int,
    num_args: c_int,
    ret_val: *mut TVMValue,
    ret_type_code: *mut c_int,
  ) -> SgxStatus;
}

pub fn ocall_packed_func<S: AsRef<str>>(fn_name: S, args: &[TVMArgValue]) -> Result<TVMRetValue> {
  let mut ret_val = TVMValue { v_int64: 0 };
  let ret_type_code = 0i64;
  unsafe {
    tvm_ocall!(tvm_ocall_packed_func(
      CString::new(fn_name.as_ref()).unwrap().as_ptr(),
      args
        .iter()
        .map(|ref arg| arg.value)
        .collect::<Vec<TVMValue>>()
        .as_ptr(),
      args
        .iter()
        .map(|ref arg| arg.type_code as i32)
        .collect::<Vec<i32>>()
        .as_ptr() as *const i32,
      args.len() as i32,
      &mut ret_val as *mut TVMValue,
      &mut (ret_type_code as i32) as *mut c_int,
    ))?;
  }
  Ok(TVMRetValue::from_tvm_value(ret_val, ret_type_code as i64))
}

#[macro_export]
macro_rules! ocall_packed {
  ($fn_name:expr, $($args:expr),+) => {
63
    ocall_packed_func($fn_name, &[$($args.into(),)+])
nhynes committed
64 65 66
      .expect(concat!("Error calling `", $fn_name, "`"))
  };
  ($fn_name:expr) => {
67
    ocall_packed_func($fn_name, &Vec::new())
nhynes committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      .expect(concat!("Error calling `", $fn_name, "`"))
  }
}

pub fn shutdown() {
  if env!("TVM_NUM_THREADS") != "0" {
    sgx_join_threads()
  }
}

impl Drop for SystemLibModule {
  fn drop(&mut self) {
    shutdown()
  }
}