diff --git a/demo/hello_world/Makefile b/demo/hello_world/Makefile index a828241e..7cb79a58 100644 --- a/demo/hello_world/Makefile +++ b/demo/hello_world/Makefile @@ -1,4 +1,4 @@ -CC := musl-clang +CC := occlum-gcc CFLAGS := -fPIC -pie .PHONY: all test test-native clean @@ -9,7 +9,7 @@ hello_world: hello_world.c $(CC) $(CFLAGS) $^ -o $@ # Run hello_world on Occlum inside an SGX enclave -test: +test: hello_world rm -rf occlum_workspace mkdir occlum_workspace cd occlum_workspace && \ diff --git a/etc/template/Occlum.json b/etc/template/Occlum.json index 93930e04..4910544e 100644 --- a/etc/template/Occlum.json +++ b/etc/template/Occlum.json @@ -7,6 +7,9 @@ "default_heap_size": "16MB", "default_mmap_size": "32MB" }, + "env": [ + "OCCLUM=yes" + ], "mount": [ { "target": "/", diff --git a/test/cpuid/main.c b/test/cpuid/main.c index 2e5ed4be..bf9ee27a 100644 --- a/test/cpuid/main.c +++ b/test/cpuid/main.c @@ -121,9 +121,6 @@ static int test_cpuid_with_basic_leaf_one() { if (cpu.eax == 0) { throw_error("faild to call cpuid with eax=1"); } - if (!((cpu.ecx >> 6) & 1)) { - throw_error("smx is not enabled"); - } return 0; } diff --git a/test/env/main.c b/test/env/main.c index 61b0b4ad..b63ea913 100644 --- a/test/env/main.c +++ b/test/env/main.c @@ -27,7 +27,7 @@ const char* expect_argv[EXPECT_ARGC] = { // Expected child arguments const int child_argc = 2; -const char* child_argv[child_argc + 1] = { +const char* child_argv[3] = { "env", "child", NULL diff --git a/test/test_common.mk b/test/test_common.mk index cf924395..26733c90 100644 --- a/test/test_common.mk +++ b/test/test_common.mk @@ -15,10 +15,10 @@ CXX_OBJS := $(addprefix $(BUILD_DIR)/test/obj/$(TEST_NAME)/,$(CXX_SRCS:%.cc=%.o) ALL_BUILD_SUBDIRS := $(sort $(patsubst %/,%,$(dir $(BIN) $(C_OBJS) $(CXX_OBJS)))) -CC := /usr/local/occlum/bin/musl-clang -CXX := /usr/local/occlum/bin/musl-clang++ +CC := occlum-gcc +CXX := occlum-g++ -C_FLAGS = -Wall -I../include -O2 -fPIC $(EXTRA_C_FLAGS) +C_FLAGS = -Wall -Wno-return-local-addr -I../include -O2 -fPIC $(EXTRA_C_FLAGS) LINK_FLAGS = $(C_FLAGS) -pie $(EXTRA_LINK_FLAGS) .PHONY: all test test-native clean diff --git a/test/unix_socket_throughput/main.c b/test/unix_socket_throughput/main.c index 25e8cf5d..8312c87f 100644 --- a/test/unix_socket_throughput/main.c +++ b/test/unix_socket_throughput/main.c @@ -110,7 +110,7 @@ int main(int argc, const char* argv[]) { int child_pid; extern char ** environ; - const char* new_argv[] = {"./dev_null", NULL}; + char* new_argv[] = {"./dev_null", NULL}; if (posix_spawn(&child_pid, "dev_null", &file_actions, NULL, new_argv, environ) < 0) { printf("ERROR: failed to spawn a child process\n"); diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile index 7ba7f269..fce8bdea 100644 --- a/tools/docker/Dockerfile +++ b/tools/docker/Dockerfile @@ -63,9 +63,9 @@ RUN curl https://sh.rustup.rs -sSf | \ rm -rf /root/.cargo/registry && rm -rf /root/.cargo/git # Install Occlum toolchain -WORKDIR /tmp -COPY build_toolchain.sh /tmp -RUN ./build_toolchain.sh +COPY toolchains/gcc /tmp +WORKDIR /tmp/gcc +RUN ./build.sh ENV PATH="/opt/occlum/build/bin:/usr/local/occlum/bin:$PATH" WORKDIR /root diff --git a/tools/docker/build.sh b/tools/docker/build.sh new file mode 100755 index 00000000..822d5d3f --- /dev/null +++ b/tools/docker/build.sh @@ -0,0 +1,17 @@ +#!/bin/bash +script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +set -e + +if [[ ( "$#" < 1 ) ]] ; then + echo "Error: tag is not given" + echo "" + echo "Usage: run command" + echo " build.sh " + echo "to build a Docker image with a tag (e.g., occlum/occlum:latest)." + exit 1 +fi +tag=$1 + +cd "$script_dir/.." +docker build -f "$script_dir/Dockerfile" -t "$tag" . diff --git a/tools/occlum b/tools/occlum index 880c7041..01a4b75a 100755 --- a/tools/occlum +++ b/tools/occlum @@ -17,12 +17,6 @@ report_arg_error() { echo " occlum run " } - -get_conf_user_space_size() { - cat "$working_dir/Occlum.json" | \ - python -c "import sys, json; print json.load(sys.stdin)['vm']['user_space_size']" -} - get_conf_default_stack_size() { cat "$working_dir/Occlum.json" | \ python -c "import sys, json; print json.load(sys.stdin)['process']['default_stack_size']" @@ -38,6 +32,15 @@ get_conf_default_mmap_size() { python -c "import sys, json; print json.load(sys.stdin)['process']['default_mmap_size']" } +get_conf_user_space_size() { + cat "$working_dir/Occlum.json" | \ + python -c "import sys, json; print json.load(sys.stdin)['vm']['user_space_size']" +} + +get_conf_env() { + cat "$working_dir/Occlum.json" | \ + python -c "import sys, json; print json.dumps(json.load(sys.stdin)['env'])" +} get_occlum_conf_file_mac() { "$occlum_dir/build/bin/occlum-protect-integrity" show-mac "$context_dir/build/Occlum.json.protected" @@ -63,12 +66,13 @@ cmd_init() { mkdir -p image/host mkdir -p image/tmp - cp \ - /lib/ld-musl-x86_64.so.1 \ - /usr/local/occlum/lib/libc++.so.1 \ - /usr/local/occlum/lib/libc++abi.so.1 \ - /usr/local/occlum/lib/libunwind.so.1 \ - image/lib/ + local occlum_gcc_lib=/usr/local/occlum/x86_64-linux-musl/lib + cp -t image/lib/ \ + /lib/ld-musl-x86_64.so.1 \ + "$occlum_gcc_lib/libc.so" \ + "$occlum_gcc_lib/libstdc++.so.6" \ + "$occlum_gcc_lib/libgcc_s.so.1" \ + "$occlum_gcc_lib/libgomp.so.1" cp "$occlum_dir"/etc/template/* "$working_dir"/ @@ -101,12 +105,10 @@ cmd_build() { export OCCLUM_CONF_DEFAULT_STACK_SIZE=`get_conf_default_stack_size` export OCCLUM_CONF_DEFAULT_HEAP_SIZE=`get_conf_default_heap_size` export OCCLUM_CONF_DEFAULT_MMAP_SIZE=`get_conf_default_mmap_size` + export OCCLUM_CONF_ENV=`get_conf_env` cd "$context_dir/build" "$occlum_dir/build/bin/occlum-gen-default-occlum-json"\ - > "Occlum_new.json" - jq -s '.[0] + .[1]' "../../Occlum.json" "Occlum_new.json"\ > "Occlum.json" - rm -f "Occlum_new.json" "$occlum_dir/build/bin/occlum-protect-integrity" protect Occlum.json export OCCLUM_BUILTIN_CONF_FILE_MAC=`get_occlum_conf_file_mac` diff --git a/tools/occlum-gen-default-occlum-json b/tools/occlum-gen-default-occlum-json index e77c8fee..95fb0908 100755 --- a/tools/occlum-gen-default-occlum-json +++ b/tools/occlum-gen-default-occlum-json @@ -33,6 +33,7 @@ cat < ++#include ++ ++#pragma GCC visibility pop ++ + static inline void + futex_wait (int *addr, int val) + { +- long res; ++ int err = syscall (SYS_futex, addr, gomp_futex_wait, val, NULL); ++ if (__builtin_expect (err < 0 && errno == ENOSYS, 0)) ++ { ++ gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG; ++ gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG; ++ syscall (SYS_futex, addr, gomp_futex_wait, val, NULL); ++ } ++ ++/* long res; + + register long r10 __asm__("%r10") = 0; + __asm volatile ("syscall" +@@ -51,12 +66,21 @@ futex_wait (int *addr, int val) + "d" (val), "r" (r10) + : "r11", "rcx", "memory"); + } ++*/ + } + + static inline void + futex_wake (int *addr, int count) + { +- long res; ++ int err = syscall (SYS_futex, addr, gomp_futex_wake, count); ++ if (__builtin_expect (err < 0 && errno == ENOSYS, 0)) ++ { ++ gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG; ++ gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG; ++ syscall (SYS_futex, addr, gomp_futex_wake, count); ++ } ++ ++/* long res; + + __asm volatile ("syscall" + : "=a" (res) +@@ -73,6 +97,7 @@ futex_wake (int *addr, int count) + "d" (count) + : "r11", "rcx", "memory"); + } ++*/ + } + #else + # ifndef SYS_futex diff --git a/tools/toolchains/gcc/build.sh b/tools/toolchains/gcc/build.sh new file mode 100755 index 00000000..908c4cf6 --- /dev/null +++ b/tools/toolchains/gcc/build.sh @@ -0,0 +1,55 @@ +#!/bin/bash +THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +BUILD_DIR=/tmp/occlum_gcc_toolchain +INSTALL_DIR=/opt/occlum/toolchains/gcc + +# Exit if any command fails +set -e + +# Clean previous build and installation if any +rm -rf ${BUILD_DIR} +rm -rf ${INSTALL_DIR} + +# Create the build directory +mkdir -p ${BUILD_DIR} +cd ${BUILD_DIR} + +# Download musl-cross-make project +git clone https://github.com/richfelker/musl-cross-make +cd musl-cross-make +git checkout d969dea983a2cc54a1e0308a0cdeb6c3307e4bfa + +# Let musl-cross-make build for x86-64 Linux +TARGET=x86_64-linux-musl +# We will check out the branch ${MUSL_VER} from ${MUSL_REPO} +MUSL_REPO=https://github.com/occlum/musl +MUSL_VER=1.1.20 +# We will use this version of GCC +GCC_VER=8.3.0 + +# This patch replaces syscall instruction with libc's syscall wrapper +cp ${THIS_DIR}/0014-libgomp-*.diff patches/gcc-${GCC_VER}/ + +# Build musl-gcc toolchain for Occlum +cat > config.mak <