diff --git a/Makefile b/Makefile index 19944192..16a09f47 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,16 @@ -.PHONY: all build_src build_test test clean +.PHONY: all src test clean -all: build_src build_test +all: src submodule: git submodule init git submodule update cd deps/sgx_protect_file && make -build_src: +src: @$(MAKE) --no-print-directory -C src -build_test: - @$(MAKE) --no-print-directory -C test - -test: build_test +test: @$(MAKE) --no-print-directory -C test run clean: diff --git a/src/libos/Cargo.toml b/src/libos/Cargo.toml index 75f56a60..3861aef7 100644 --- a/src/libos/Cargo.toml +++ b/src/libos/Cargo.toml @@ -3,7 +3,7 @@ name = "Occlum" version = "0.0.1" [lib] -name = "libos" +name = "occlum_rs" crate-type = ["staticlib"] [dependencies] diff --git a/src/libos/Makefile b/src/libos/Makefile index abcb38a8..0abbf47b 100644 --- a/src/libos/Makefile +++ b/src/libos/Makefile @@ -4,7 +4,7 @@ DEBUG=1 LIBOS_ENCLAVE := libocclum.signed.so LIBOS_SO := libocclum.so # Link $(LIBOS_A), $(C_OBJS) and all dependencies -LIBOS_A := liblibos.a # Built from Rust code +LIBOS_A := libocclum_rs.a # Built from Rust code RUST_SRCS := $(wildcard src/*.rs src/*/*.rs src/*/*/*.rs) C_SRCS := $(sort $(filter-out src/Enclave_t.c, $(wildcard src/*.c src/*/*.c))) src/Enclave_t.c @@ -18,7 +18,7 @@ ENCLAVE_KEY := Enclave_private.pem C_FLAGS := $(SGX_CFLAGS_T) -fno-stack-protector -I./include/ _Other_Link_Flags := -L../../deps/rust-sgx-sdk/compiler-rt/ -L. -_Other_Enclave_Libs := -lcompiler-rt-patch -llibos -lsgx_tprotected_fs +_Other_Enclave_Libs := -lcompiler-rt-patch -locclum_rs -lsgx_tprotected_fs LINK_FLAGS := $(SGX_LFLAGS_T) .PHONY: all compiler-rt clean @@ -43,19 +43,21 @@ $(S_OBJS): %.o: %.S $(EDL_C_SRCS): $(SGX_EDGER8R) ../Enclave.edl @cd src/ && $(SGX_EDGER8R) --trusted ../../Enclave.edl --search-path $(SGX_SDK)/include --search-path ../../../deps/rust-sgx-sdk/edl/ - @echo "GEN => $(EDL_C_SRCS)" + @echo "GEN <= $(EDL_C_SRCS)" compiler-rt: - @$(MAKE) --no-print-directory -C ../../deps/rust-sgx-sdk/compiler-rt/ 2> /dev/null + @$(MAKE) --no-print-directory -C ../../deps/rust-sgx-sdk/compiler-rt/ > /dev/null ifeq ($(DEBUG), 1) $(LIBOS_A): $(RUST_SRCS) - RUSTC_BOOTSTRAP=1 cargo build - cp ./target/debug/$(LIBOS_A) $(LIBOS_A) + @RUSTC_BOOTSTRAP=1 cargo build + @cp ./target/debug/$(LIBOS_A) $(LIBOS_A) + @echo "CARGO (debug) => $(LIBOS_A)" else $(LIBOS_A): $(RUST_SRCS) - RUSTC_BOOTSTRAP=1 cargo build --release - cp ./target/release/$(LIBOS_A) $(LIBOS_A) + @RUSTC_BOOTSTRAP=1 cargo build --release + @cp ./target/release/$(LIBOS_A) $(LIBOS_A) + @echo "CARGO (release) => $(LIBOS_A)" endif clean: diff --git a/src/libos/src/lib.rs b/src/libos/src/lib.rs index 1eb64503..b33bcc5f 100644 --- a/src/libos/src/lib.rs +++ b/src/libos/src/lib.rs @@ -1,6 +1,6 @@ #![allow(unused)] -#![crate_name = "libos"] +#![crate_name = "occlum_rs"] #![crate_type = "staticlib"] #![cfg_attr(not(target_env = "sgx"), no_std)] diff --git a/src/pal/Makefile b/src/pal/Makefile index cc191410..0eb29021 100644 --- a/src/pal/Makefile +++ b/src/pal/Makefile @@ -11,6 +11,7 @@ Link_Flags := $(SGX_LFLAGS_U) Link_Flags += -L../../deps/rust-sgx-sdk/sgx_ustdc/ -lsgx_ustdc -lsgx_uprotected_fs +.PHONY: all all: $(Bin) $(Bin): $(EDL_Gen_Files) $(Objs) sgx_ustdc @@ -30,8 +31,7 @@ $(Objs): %.o: %.c # .PHONY: sgx_ustdc sgx_ustdc: - @$(MAKE) --no-print-directory -C ../../deps/rust-sgx-sdk/sgx_ustdc/ 2> /dev/null - + @$(MAKE) --no-print-directory -C ../../deps/rust-sgx-sdk/sgx_ustdc/ > /dev/null .PHONY: clean clean: diff --git a/src/pal/atomic.h b/src/pal/atomic.h index bce3381b..9118a260 100644 --- a/src/pal/atomic.h +++ b/src/pal/atomic.h @@ -1,11 +1,11 @@ #ifndef __ATOMIC_H_ #define __ATOMIC_H_ -static inline int a_load(int* n) { +static inline int a_load(volatile int* n) { return *(volatile int*)n; } -static inline int a_fetch_and_add(int* n, int a) { +static inline int a_fetch_and_add(volatile int* n, int a) { return __sync_fetch_and_add(n, a); } diff --git a/src/pal/futex.c b/src/pal/futex.c index cf7ed4b2..7c74577b 100644 --- a/src/pal/futex.c +++ b/src/pal/futex.c @@ -18,15 +18,15 @@ static inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a #define syscall(num, a1, a2, a3, a4, a5, a6) \ __syscall6((num), (long)(a1), (long)(a2), (long)(a3), (long)(a4), (long)(a5), (long)(a6)) -static inline int futex(void *addr1, int op, int val1, struct timespec *timeout, +static inline int futex(volatile void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3) { return (int) syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3); } -int futex_wait(int* uaddr, int val) { +int futex_wait(volatile int* uaddr, int val) { return futex(uaddr, FUTEX_WAIT, val, NULL, NULL, 0); } -int futex_wakeup(int* uaddr) { +int futex_wakeup(volatile int* uaddr) { return futex(uaddr, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); } diff --git a/src/pal/futex.h b/src/pal/futex.h index 4e037b67..da87e00c 100644 --- a/src/pal/futex.h +++ b/src/pal/futex.h @@ -3,7 +3,7 @@ #include -int futex_wait(int* uaddr, int val); -int futex_wakeup(int* uaddr); +int futex_wait(volatile int* uaddr, int val); +int futex_wakeup(volatile int* uaddr); #endif /* __ATOMIC_H_ */ diff --git a/src/pal/pal.c b/src/pal/pal.c index 24353177..b02d1a57 100644 --- a/src/pal/pal.c +++ b/src/pal/pal.c @@ -169,7 +169,6 @@ int initialize_enclave(const char* enclave_path) if (fp != NULL) fclose(fp); return -1; } - printf("[+] global_eid: %ld\n", global_eid); /* Step 3: save the launch token if it is updated */ if (updated == FALSE || fp == NULL) { diff --git a/src/pal/task.c b/src/pal/task.c index b0eca86d..6c7db174 100644 --- a/src/pal/task.c +++ b/src/pal/task.c @@ -6,8 +6,9 @@ #include "sgx_urts.h" #include "Enclave_u.h" -static int num_tasks = 0; -static int main_task_status = 0; +static volatile int num_tasks = 0; +static volatile int main_task_status = 0; +static volatile int any_fatal_error = 0; static int BEGIN_TASK(void) { return a_fetch_and_add(&num_tasks, 1) == 0; @@ -32,6 +33,7 @@ static void* __run_task_thread(void* _data) { if(sgx_ret != SGX_SUCCESS) { // TODO: deal with ECALL error printf("ERROR: ECall libos_run failed\n"); + any_fatal_error = 1; } if (data->is_main_task) main_task_status = status; @@ -64,5 +66,5 @@ int wait_all_tasks(void) { while ((cur_num_tasks = a_load(&num_tasks)) != 0) { futex_wait(&num_tasks, cur_num_tasks); } - return main_task_status; + return any_fatal_error ? -1 : main_task_status; } diff --git a/src/sgxenv.mk b/src/sgxenv.mk index a33a1216..4bd5981d 100644 --- a/src/sgxenv.mk +++ b/src/sgxenv.mk @@ -69,6 +69,7 @@ endif # SGX_CFLAGS_T := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector SGX_CFLAGS_T += -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -I$(SGX_SDK)/include/epid + # Before use this linker flag, the user should define $(_Other_Enclave_Libs), # and $(_Other_Link_Flags) # diff --git a/test/Makefile b/test/Makefile index 0ad8fa51..91a0216c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,6 +6,11 @@ BUILD_TEST_SUITES := $(TEST_SUITES:%=%) RUN_TEST_SUITES := $(TEST_SUITES:%=run-%) CLEAN_TEST_SUITES := $(TEST_SUITES:%=clean-%) +CYAN :=\033[1;36m +GREEN :=\033[1;32m +RED :=\033[1;31m +NO_COLOR :=\033[0m + .PHONY: all build test clean $(BUILD_TEST_SUITES) $(RUN_TEST_SUITES) $(CLEAN_TEST_SUITES) ############################################################################# @@ -17,7 +22,9 @@ all: build build: $(BUILD_TEST_SUITES) $(BUILD_TEST_SUITES): %: - @$(MAKE) -C $@ + @echo "$(CYAN)BUILD TEST => $@$(NO_COLOR)" + @$(MAKE) --no-print-directory -C $@ + @echo "$(GREEN)DONE$(NO_COLOR)" ############################################################################# # Run tests @@ -26,13 +33,19 @@ $(BUILD_TEST_SUITES): %: run: build $(RUN_TEST_SUITES) pal: $(PROJECT_DIR)/src/pal/pal - cp $< pal + @cp $< pal libocclum.signed.so: $(PROJECT_DIR)/src/libos/libocclum.signed.so - cp $< libocclum.signed.so + @cp $< libocclum.signed.so $(RUN_TEST_SUITES): run-%: % pal libocclum.signed.so - @$(MAKE) -C $< run + @echo "$(CYAN)TEST => $<$(NO_COLOR)" + @$(MAKE) --no-print-directory -C $< run ; \ + if [ $$? -eq 0 ] ; then \ + echo "$(GREEN)PASS$(NO_COLOR)" ; \ + else \ + echo "$(RED)FAILED$(NO_COLOR)" ; \ + fi ; ############################################################################# # Misc @@ -42,4 +55,4 @@ clean: $(CLEAN_TEST_SUITES) @$(RM) -f pal libocclum.signed.so $(CLEAN_TEST_SUITES): clean-%: - @$(MAKE) -C $(patsubst clean-%,%,$@) clean + @$(MAKE) --no-print-directory -C $(patsubst clean-%,%,$@) clean diff --git a/test/hello_world/main.c b/test/hello_world/main.c index a0bd3801..9ca7457d 100644 --- a/test/hello_world/main.c +++ b/test/hello_world/main.c @@ -4,7 +4,7 @@ static const char* msg = "Hello World\n"; -int main(void) { +int main(int argc) { printf("%s", msg); return 0; } diff --git a/test/test_common.mk b/test/test_common.mk index 8c94d41a..0558f7e5 100644 --- a/test/test_common.mk +++ b/test/test_common.mk @@ -25,34 +25,42 @@ LINK_FLAGS = $(C_FLAGS) $(EXTRA_LINK_FLAGS) all: $(BIN_ENC_NAME) $(BIN_ENC_NAME): $(BIN_NAME) - $(RM) -f $(BIN_ENC_NAME) - cd $(PROJECT_DIR)/deps/sgx_protect_file/ && \ - ./sgx_protect_file encrypt -i $(CUR_DIR)/$(BIN_NAME) -o $(CUR_DIR)/$(BIN_ENC_NAME) -k 123 + @$(RM) -f $(BIN_ENC_NAME) + @cd $(PROJECT_DIR)/deps/sgx_protect_file/ && \ + ./sgx_protect_file encrypt \ + -i $(CUR_DIR)/$(BIN_NAME) \ + -o $(CUR_DIR)/$(BIN_ENC_NAME) \ + -k 123 > /dev/null + @echo "GEN => $@" debug: $(OBJDUMP_FILE) $(READELF_FILE) $(OBJDUMP_FILE): $(BIN_NAME) - objdump -d $(BIN_NAME) > $(OBJDUMP_FILE) + @objdump -d $(BIN_NAME) > $(OBJDUMP_FILE) + @echo "OBJDUMP => $@" $(READELF_FILE): $(BIN_NAME) - readelf -a -d $(BIN_NAME) > $(READELF_FILE) + @readelf -a -d $(BIN_NAME) > $(READELF_FILE) + @echo "READELF => $@" $(BIN_NAME): $(C_OBJS) - $(CC) $^ $(LINK_FLAGS) -o $(BIN_NAME) + @$(CC) $^ $(LINK_FLAGS) -o $(BIN_NAME) 2> /dev/null + @echo "LINK => $@" $(C_OBJS): %.o: %.c - $(CC) $(C_FLAGS) -c $< -o $@ + @$(CC) $(C_FLAGS) -c $< -o $@ 2> /dev/null + @echo "CC <= $@" ############################################################################# # Test ############################################################################# run: $(BIN_ENC_NAME) - cd ../ && RUST_BACKTRACE=1 ./pal $(CUR_DIR)/$(BIN_ENC_NAME) + @cd ../ && RUST_BACKTRACE=1 ./pal $(CUR_DIR)/$(BIN_ENC_NAME) ############################################################################# # Misc ############################################################################# clean: - $(RM) -f *.o *.S $(BIN_NAME) $(BIN_ENC_NAME) $(OBJDUMP_FILE) $(READELF_FILE) + @-$(RM) -f *.o *.S $(BIN_NAME) $(BIN_ENC_NAME) $(OBJDUMP_FILE) $(READELF_FILE)