diff --git a/src/Enclave.edl b/src/Enclave.edl index b268b29d..91f4fdfe 100644 --- a/src/Enclave.edl +++ b/src/Enclave.edl @@ -15,7 +15,7 @@ enclave { * * @retval On success, return 0; otherwise, return -1. */ - public int occlum_ecall_init([in, string] const char* log_level); + public int occlum_ecall_init([in, string] const char* log_level, [in, string] const char* instance_dir); /* * Create a new LibOS process to do the task specified by the given diff --git a/src/libos/src/config.rs b/src/libos/src/config.rs index 0976aa66..52cde5d2 100644 --- a/src/libos/src/config.rs +++ b/src/libos/src/config.rs @@ -33,9 +33,8 @@ lazy_static! { .cause_err(|e| errno!(EINVAL, "invalid config JSON"))?; Ok(config) } - - let config_path = "./.occlum/build/Occlum.json.protected"; - match load_config(config_path) { + let config_path = unsafe { format!("{}{}", INSTANCE_DIR, "/build/Occlum.json.protected") }; + match load_config(&config_path) { Err(e) => { error!("failed to load config: {}", e.backtrace()); panic!(); diff --git a/src/libos/src/entry.rs b/src/libos/src/entry.rs index 2927a6ef..a80ad6e5 100644 --- a/src/libos/src/entry.rs +++ b/src/libos/src/entry.rs @@ -11,7 +11,8 @@ use util::mem_util::from_untrusted::*; use util::sgx::allow_debug as sgx_allow_debug; use sgx_tse::*; -const ENCLAVE_PATH: &'static str = ".occlum/build/lib/libocclum-libos.signed.so"; +pub static mut INSTANCE_DIR: String = String::new(); +static mut ENCLAVE_PATH: String = String::new(); lazy_static! { static ref INIT_ONCE: Once = Once::new(); @@ -19,11 +20,13 @@ lazy_static! { } #[no_mangle] -pub extern "C" fn occlum_ecall_init(log_level: *const c_char) -> i32 { +pub extern "C" fn occlum_ecall_init(log_level: *const c_char, instance_dir: *const c_char) -> i32 { if HAS_INIT.load(Ordering::SeqCst) == true { return EXIT_STATUS_INTERNAL_ERROR; } + assert!(!instance_dir.is_null()); + let log_level = { let input_log_level = match parse_log_level(log_level) { Err(e) => { @@ -52,6 +55,12 @@ pub extern "C" fn occlum_ecall_init(log_level: *const c_char) -> i32 { // Register exception handlers (support cpuid & rdtsc for now) register_exception_handlers(); + unsafe { + let dir_str: &str = CStr::from_ptr(instance_dir).to_str().unwrap(); + INSTANCE_DIR.push_str(dir_str); + ENCLAVE_PATH.push_str(&INSTANCE_DIR); + ENCLAVE_PATH.push_str("/build/lib/libocclum-libos.signed.so"); + } HAS_INIT.store(true, Ordering::SeqCst); }); @@ -76,7 +85,8 @@ pub extern "C" fn occlum_ecall_new_process( return EXIT_STATUS_INTERNAL_ERROR; } }; - let _ = backtrace::enable_backtrace(ENCLAVE_PATH, PrintFormat::Short); + + let _ = unsafe { backtrace::enable_backtrace(&ENCLAVE_PATH, PrintFormat::Short) }; panic::catch_unwind(|| { backtrace::__rust_begin_short_backtrace(|| { match do_new_process(&path, &args, &host_stdio_fds) { @@ -97,7 +107,7 @@ pub extern "C" fn occlum_ecall_exec_thread(libos_pid: i32, host_tid: i32) -> i32 return EXIT_STATUS_INTERNAL_ERROR; } - let _ = backtrace::enable_backtrace(ENCLAVE_PATH, PrintFormat::Short); + let _ = unsafe { backtrace::enable_backtrace(&ENCLAVE_PATH, PrintFormat::Short) }; panic::catch_unwind(|| { backtrace::__rust_begin_short_backtrace(|| { match do_exec_thread(libos_pid as pid_t, host_tid as pid_t) { diff --git a/src/pal/src/pal_api.c b/src/pal/src/pal_api.c index 0bcd5afa..855b3abd 100644 --- a/src/pal/src/pal_api.c +++ b/src/pal/src/pal_api.c @@ -34,7 +34,7 @@ int occlum_pal_init(const struct occlum_pal_attr* attr) { // automatically done by Intel SGX SDK). eid = pal_get_enclave_id(); int ret; - sgx_status_t ecall_status = occlum_ecall_init(eid, &ret, attr->log_level); + sgx_status_t ecall_status = occlum_ecall_init(eid, &ret, attr->log_level, attr->instance_dir); if (ecall_status != SGX_SUCCESS) { const char* sgx_err = pal_get_sgx_error_msg(ecall_status); PAL_ERROR("Failed to do ECall: %s", sgx_err); diff --git a/tools/occlum b/tools/occlum index 5758b225..5650716e 100755 --- a/tools/occlum +++ b/tools/occlum @@ -3,8 +3,11 @@ this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" occlum_dir="$( cd "$( dirname "$this_dir/../../../" )" >/dev/null 2>&1 && pwd )" +if [ -z $OCCLUM_INSTANCE_DIR ];then + OCCLUM_INSTANCE_DIR=".occlum" +fi working_dir=`pwd` -context_dir="$working_dir/.occlum" +context_dir="$working_dir/$OCCLUM_INSTANCE_DIR" SGX_SDK="${SGX_SDK:-/opt/intel/sgxsdk}" SGX_GDB="$SGX_SDK/bin/sgx-gdb" diff --git a/tools/occlum-gen-default-occlum-json b/tools/occlum-gen-default-occlum-json index 8e81b474..8f0c55e1 100755 --- a/tools/occlum-gen-default-occlum-json +++ b/tools/occlum-gen-default-occlum-json @@ -1,4 +1,9 @@ #!/bin/bash + +if [ -z $OCCLUM_INSTANCE_DIR ];then + OCCLUM_INSTANCE_DIR=".occlum" +fi + cat <