Introduce GCC-base toolchain and use it by default

This commit is contained in:
Tate, Hongliang Tian 2019-09-05 05:57:29 +00:00
parent 8ef52c7c2d
commit 00949d9741
13 changed files with 171 additions and 31 deletions

@ -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 && \

@ -7,6 +7,9 @@
"default_heap_size": "16MB",
"default_mmap_size": "32MB"
},
"env": [
"OCCLUM=yes"
],
"mount": [
{
"target": "/",

@ -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;
}

2
test/env/main.c vendored

@ -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

@ -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

@ -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");

@ -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

17
tools/docker/build.sh Executable file

@ -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 <tag>"
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" .

@ -17,12 +17,6 @@ report_arg_error() {
echo " occlum run <program_name> <program_args>"
}
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 \
local occlum_gcc_lib=/usr/local/occlum/x86_64-linux-musl/lib
cp -t image/lib/ \
/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/
"$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`

@ -33,6 +33,7 @@ cat <<EOF
"target": "/tmp",
"type": "ramfs"
}
]
],
"env": $OCCLUM_CONF_ENV
}
EOF

@ -1,6 +1,6 @@
#!/bin/sh
BUILD_DIR=/tmp/occlum_toolchain
INSTALL_DIR=/usr/local/occlum
BUILD_DIR=/tmp/occlum_clang_toolchain
INSTALL_DIR=/opt/occlum/toolchains/clang
# Exit if any command fails
set -e
@ -117,3 +117,6 @@ cd ..
# Remove all source code and build files
rm -rf ${BUILD_DIR}
# Link the toolchain directory
ln -s -f ${INSTALL_DIR} /usr/local/occlum

@ -0,0 +1,62 @@
diff --git a/libgomp/config/linux/x86/futex.h b/libgomp/config/linux/x86/futex.h
index 02d5b95..23c019a 100644
--- a/libgomp/config/linux/x86/futex.h
+++ b/libgomp/config/linux/x86/futex.h
@@ -30,10 +30,25 @@
# define SYS_futex 202
# endif
+#pragma GCC visibility push(default)
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#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

55
tools/toolchains/gcc/build.sh Executable file

@ -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 <<EOF
TARGET = ${TARGET}
OUTPUT = ${INSTALL_DIR}
COMMON_CONFIG += CFLAGS="-fPIC" CXXFLAGS="-fPIC" LDFLAGS="-pie"
GCC_VER = ${GCC_VER}
MUSL_VER = git-${MUSL_VER}
MUSL_REPO = ${MUSL_REPO}
EOF
make
make install
# Remove all source code and build files
rm -rf ${BUILD_DIR}
# Link the toolchain directory
ln -sf ${INSTALL_DIR}/bin/${TARGET}-gcc ${INSTALL_DIR}/bin/occlum-gcc
ln -sf ${INSTALL_DIR}/bin/${TARGET}-g++ ${INSTALL_DIR}/bin/occlum-g++
ln -sf ${INSTALL_DIR}/bin/${TARGET}-ld ${INSTALL_DIR}/bin/occlum-ld
ln -sf ${INSTALL_DIR}/x86_64-linux-musl/lib/libc.so /lib/ld-musl-x86_64.so.1
ln -sf ${INSTALL_DIR} /usr/local/occlum