Add Glibc as an optional libc and fix test cases

This commit is contained in:
LI Qing 2020-11-09 16:18:49 +08:00 committed by Zongmin.Gu
parent 1a00884e1c
commit 22b02850a3
24 changed files with 123 additions and 23 deletions

@ -56,6 +56,9 @@ src:
test: test:
@$(MAKE) --no-print-directory -C test test @$(MAKE) --no-print-directory -C test test
test-glibc:
@$(MAKE) --no-print-directory -C test test-glibc
OCCLUM_PREFIX ?= /opt/occlum OCCLUM_PREFIX ?= /opt/occlum
install: $(OCCLUM_PREFIX)/sgxsdk-tools/lib64/libsgx_uae_service_sim.so install: $(OCCLUM_PREFIX)/sgxsdk-tools/lib64/libsgx_uae_service_sim.so
@# Install both libraries for HW mode and SIM mode @# Install both libraries for HW mode and SIM mode

@ -66,6 +66,9 @@ postbuild:
# Test targets # Test targets
############################################################################# #############################################################################
test-glibc:
@OCCLUM_TEST_GLIBC=1 $(MAKE) test
test: build pretest $(TEST_TARGETS) posttest test: build pretest $(TEST_TARGETS) posttest
pretest: pretest:

@ -2,13 +2,13 @@
"resource_limits": { "resource_limits": {
"kernel_space_heap_size": "40MB", "kernel_space_heap_size": "40MB",
"kernel_space_stack_size": "1MB", "kernel_space_stack_size": "1MB",
"user_space_size": "128MB", "user_space_size": "224MB",
"max_num_of_threads": 32 "max_num_of_threads": 32
}, },
"process": { "process": {
"default_stack_size": "4MB", "default_stack_size": "4MB",
"default_heap_size": "8MB", "default_heap_size": "8MB",
"default_mmap_size": "32MB" "default_mmap_size": "80MB"
}, },
"entry_points": [ "entry_points": [
"/bin" "/bin"

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>

@ -1,5 +1,5 @@
include ../test_common.mk include ../test_common.mk
EXTRA_C_FLAGS := -Wno-incompatible-pointer-types-discards-qualifiers EXTRA_C_FLAGS := -Wno-incompatible-pointer-types-discards-qualifiers
EXTRA_LINK_FLAGS := EXTRA_LINK_FLAGS := -lpthread
BIN_ARGS := BIN_ARGS :=

@ -1,5 +1,5 @@
include ../test_common.mk include ../test_common.mk
EXTRA_C_FLAGS := EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS := EXTRA_LINK_FLAGS := -lpthread
BIN_ARGS := BIN_ARGS :=

@ -1,3 +1,4 @@
#define _LARGEFILE64_SOURCE
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include "test_fs.h" #include "test_fs.h"
@ -5,9 +6,14 @@
// ============================================================================ // ============================================================================
// Helper macro // Helper macro
// ============================================================================ // ============================================================================
#ifdef __GLIBC__
#define TMPFILE_FLAG __O_TMPFILE
#else
#define TMPFILE_FLAG O_TMPFILE
#endif
#define CREATION_FLAGS_MASK (O_CLOEXEC | O_CREAT| O_DIRECTORY | O_EXCL | \ #define CREATION_FLAGS_MASK (O_CLOEXEC | O_CREAT| O_DIRECTORY | O_EXCL | \
O_NOCTTY | O_NOFOLLOW | O_TMPFILE | O_TRUNC) O_NOCTTY | O_NOFOLLOW | TMPFILE_FLAG | O_TRUNC)
// ============================================================================ // ============================================================================
// Test cases for fcntl // Test cases for fcntl

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>

@ -1,8 +1,10 @@
#define _GNU_SOURCE
#include <errno.h> #include <errno.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/time.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <unistd.h> #include <unistd.h>

@ -1,5 +1,5 @@
include ../test_common.mk include ../test_common.mk
EXTRA_C_FLAGS := -Wno-stringop-truncation EXTRA_C_FLAGS := -Wno-stringop-truncation
EXTRA_LINK_FLAGS := EXTRA_LINK_FLAGS := -lpthread
BIN_ARGS := BIN_ARGS :=

@ -1,5 +1,5 @@
include ../test_common.mk include ../test_common.mk
EXTRA_C_FLAGS := EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS := EXTRA_LINK_FLAGS := -lpthread
BIN_ARGS := BIN_ARGS :=

@ -45,8 +45,10 @@ static int getdents_with_big_enough_buffer(bool use_explicit_syscall) {
while (1) { while (1) {
if (use_explicit_syscall) { if (use_explicit_syscall) {
len = syscall(__NR_getdents, fd, buf, sizeof(buf)); len = syscall(__NR_getdents, fd, buf, sizeof(buf));
#ifndef __GLIBC__
} else { } else {
len = getdents(fd, (struct dirent *)buf, sizeof(buf)); len = getdents(fd, (struct dirent *)buf, sizeof(buf));
#endif
} }
if (len < 0) { if (len < 0) {
close(fd); close(fd);
@ -60,10 +62,12 @@ static int getdents_with_big_enough_buffer(bool use_explicit_syscall) {
return 0; return 0;
} }
#ifndef __GLIBC__
static int test_getdents_with_big_enough_buffer() { static int test_getdents_with_big_enough_buffer() {
bool use_explicit_syscall = false; bool use_explicit_syscall = false;
return getdents_with_big_enough_buffer(use_explicit_syscall); return getdents_with_big_enough_buffer(use_explicit_syscall);
} }
#endif
static int test_getdents_via_explicit_syscall_with_big_enough_buffer() { static int test_getdents_via_explicit_syscall_with_big_enough_buffer() {
bool use_explicit_syscall = true; bool use_explicit_syscall = true;
@ -80,8 +84,10 @@ static int getdents_with_too_small_buffer(bool use_explicit_syscall) {
} }
if (use_explicit_syscall) { if (use_explicit_syscall) {
len = syscall(__NR_getdents, fd, buf, sizeof(buf)); len = syscall(__NR_getdents, fd, buf, sizeof(buf));
#ifndef __GLIBC__
} else { } else {
len = getdents(fd, (struct dirent *)buf, sizeof(buf)); len = getdents(fd, (struct dirent *)buf, sizeof(buf));
#endif
} }
if (len >= 0 || errno != EINVAL) { if (len >= 0 || errno != EINVAL) {
close(fd); close(fd);
@ -91,10 +97,12 @@ static int getdents_with_too_small_buffer(bool use_explicit_syscall) {
return 0; return 0;
} }
#ifndef __GLIBC__
static int test_getdents_with_too_small_buffer() { static int test_getdents_with_too_small_buffer() {
bool use_explicit_syscall = false; bool use_explicit_syscall = false;
return getdents_with_too_small_buffer(use_explicit_syscall); return getdents_with_too_small_buffer(use_explicit_syscall);
} }
#endif
static int test_getdents_via_explicit_syscall_with_too_small_buffer() { static int test_getdents_via_explicit_syscall_with_too_small_buffer() {
bool use_explicit_syscall = true; bool use_explicit_syscall = true;
@ -107,9 +115,13 @@ static int test_getdents_via_explicit_syscall_with_too_small_buffer() {
static test_case_t test_cases[] = { static test_case_t test_cases[] = {
TEST_CASE(test_readdir), TEST_CASE(test_readdir),
#ifndef __GLIBC__
TEST_CASE(test_getdents_with_big_enough_buffer), TEST_CASE(test_getdents_with_big_enough_buffer),
#endif
TEST_CASE(test_getdents_via_explicit_syscall_with_big_enough_buffer), TEST_CASE(test_getdents_via_explicit_syscall_with_big_enough_buffer),
#ifndef __GLIBC__
TEST_CASE(test_getdents_with_too_small_buffer), TEST_CASE(test_getdents_with_too_small_buffer),
#endif
TEST_CASE(test_getdents_via_explicit_syscall_with_too_small_buffer), TEST_CASE(test_getdents_via_explicit_syscall_with_too_small_buffer),
}; };

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <netdb.h> #include <netdb.h>

@ -1,5 +1,5 @@
include ../test_common.mk include ../test_common.mk
EXTRA_C_FLAGS := -Wno-return-stack-address -Wno-unused-but-set-variable EXTRA_C_FLAGS := -Wno-return-stack-address -Wno-unused-but-set-variable
EXTRA_LINK_FLAGS := EXTRA_LINK_FLAGS := -lpthread
BIN_ARGS := BIN_ARGS :=

@ -1,6 +1,8 @@
#define _GNU_SOURCE
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
#include "test_fs.h" #include "test_fs.h"
// ============================================================================ // ============================================================================

@ -77,7 +77,7 @@ static int __test_readlink_from_proc_self_fd(const char *file_path) {
} }
static int __test_realpath(const char *file_path) { static int __test_realpath(const char *file_path) {
char buf[128] = { 0 }; char buf[PATH_MAX] = { 0 };
char dir_buf[PATH_MAX] = { 0 }; char dir_buf[PATH_MAX] = { 0 };
char base_buf[PATH_MAX] = { 0 }; char base_buf[PATH_MAX] = { 0 };
char *dir_name, *file_name, *res; char *dir_name, *file_name, *res;

@ -17,8 +17,13 @@ CXX_OBJS := $(addprefix $(BUILD_DIR)/test/obj/$(TEST_NAME)/,$(CXX_SRCS:%.cc=%.o)
ALL_BUILD_SUBDIRS := $(sort $(patsubst %/,%,$(dir $(BIN) $(C_OBJS) $(CXX_OBJS)))) ALL_BUILD_SUBDIRS := $(sort $(patsubst %/,%,$(dir $(BIN) $(C_OBJS) $(CXX_OBJS))))
CC := occlum-gcc ifeq ($(OCCLUM_TEST_GLIBC), 1)
CXX := occlum-g++ CC = gcc
CXX = g++
else
CC = occlum-gcc
CXX = occlum-g++
endif
C_FLAGS = -Wall -Wno-return-local-addr -I../include -O2 -fPIC $(EXTRA_C_FLAGS) C_FLAGS = -Wall -Wno-return-local-addr -I../include -O2 -fPIC $(EXTRA_C_FLAGS)
ifeq ($(SGX_MODE), SIM) ifeq ($(SGX_MODE), SIM)

@ -12,6 +12,7 @@ RUN yum install epel-release -y && \
dnf group install 'Development Tools' -y && \ dnf group install 'Development Tools' -y && \
dnf --enablerepo=PowerTools install -y \ dnf --enablerepo=PowerTools install -y \
astyle \ astyle \
bison \
boost-devel \ boost-devel \
cmake \ cmake \
createrepo \ createrepo \
@ -19,6 +20,7 @@ RUN yum install epel-release -y && \
expect \ expect \
fuse-devel \ fuse-devel \
fuse-libs \ fuse-libs \
gawk \
gmp-devel \ gmp-devel \
golang \ golang \
jq \ jq \
@ -67,11 +69,16 @@ RUN curl https://sh.rustup.rs -sSf | \
cargo install sccache cargo install sccache
# Install Occlum toolchain # Install Occlum toolchain
COPY toolchains/gcc /tmp/gcc COPY toolchains/musl-gcc /tmp/musl-gcc
WORKDIR /tmp WORKDIR /tmp
RUN cd gcc && ./build.sh && ./install_zlib.sh && rm -rf /tmp/gcc RUN cd musl-gcc && ./build.sh && ./install_zlib.sh && rm -rf /tmp/musl-gcc
ENV PATH="/opt/occlum/build/bin:/usr/local/occlum/bin:$PATH" ENV PATH="/opt/occlum/build/bin:/usr/local/occlum/bin:$PATH"
# Install glibc
COPY toolchains/glibc /tmp/glibc
WORKDIR /tmp
RUN cd glibc && ./build.sh && rm -rf /tmp/glibc
# Install Occlum Golang toolchain # Install Occlum Golang toolchain
COPY toolchains/golang /tmp/golang COPY toolchains/golang /tmp/golang
WORKDIR /tmp WORKDIR /tmp
@ -83,7 +90,7 @@ COPY toolchains/rust /tmp/rust
WORKDIR /tmp WORKDIR /tmp
RUN cd rust && ./build.sh && rm -rf /tmp/rust RUN cd rust && ./build.sh && rm -rf /tmp/rust
ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH" ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH"
ENV LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/intel/sgxsdk/sdk_libs" ENV LD_LIBRARY_PATH="/opt/intel/sgxsdk/sdk_libs"
# Install Occlum Java toolchain (JDK 11) # Install Occlum Java toolchain (JDK 11)
ARG JDK11_PATH=/opt/occlum/toolchains/jvm/java-11-openjdk ARG JDK11_PATH=/opt/occlum/toolchains/jvm/java-11-openjdk

@ -14,6 +14,7 @@ RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-i
astyle \ astyle \
autoconf \ autoconf \
automake \ automake \
bison \
build-essential \ build-essential \
ca-certificates \ ca-certificates \
cmake \ cmake \
@ -21,6 +22,7 @@ RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-i
debhelper \ debhelper \
expect \ expect \
g++ \ g++ \
gawk \
gdb \ gdb \
git-core \ git-core \
golang-go \ golang-go \
@ -79,11 +81,16 @@ RUN curl https://sh.rustup.rs -sSf | \
cargo install sccache cargo install sccache
# Install Occlum toolchain # Install Occlum toolchain
COPY toolchains/gcc /tmp/gcc COPY toolchains/musl-gcc /tmp/musl-gcc
WORKDIR /tmp WORKDIR /tmp
RUN cd gcc && ./build.sh && ./install_zlib.sh && rm -rf /tmp/gcc RUN cd musl-gcc && ./build.sh && ./install_zlib.sh && rm -rf /tmp/musl-gcc
ENV PATH="/opt/occlum/build/bin:/usr/local/occlum/bin:$PATH" ENV PATH="/opt/occlum/build/bin:/usr/local/occlum/bin:$PATH"
# Install glibc
COPY toolchains/glibc /tmp/glibc
WORKDIR /tmp
RUN cd glibc && ./build.sh && rm -rf /tmp/glibc
# Install Occlum Golang toolchain # Install Occlum Golang toolchain
COPY toolchains/golang /tmp/golang COPY toolchains/golang /tmp/golang
WORKDIR /tmp WORKDIR /tmp
@ -95,7 +102,7 @@ COPY toolchains/rust /tmp/rust
WORKDIR /tmp WORKDIR /tmp
RUN cd rust && ./build.sh && rm -rf /tmp/rust RUN cd rust && ./build.sh && rm -rf /tmp/rust
ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH" ENV PATH="/opt/occlum/toolchains/rust/bin:$PATH"
ENV LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/intel/sgxsdk/sdk_libs" ENV LD_LIBRARY_PATH="/opt/intel/sgxsdk/sdk_libs"
# Install Occlum Java toolchain (JDK 11) # Install Occlum Java toolchain (JDK 11)
ARG JDK11_PATH=/opt/occlum/toolchains/jvm/java-11-openjdk ARG JDK11_PATH=/opt/occlum/toolchains/jvm/java-11-openjdk

@ -129,20 +129,43 @@ cmd_init() {
mkdir -p image mkdir -p image
mkdir -p image/bin mkdir -p image/bin
mkdir -p image/lib mkdir -p image/lib
mkdir -p image/lib64
mkdir -p image/root mkdir -p image/root
mkdir -p image/host mkdir -p image/host
mkdir -p image/tmp mkdir -p image/tmp
local occlum_glibc_lib=/opt/occlum/glibc/lib
local cpu_lib=/sys/devices/system/cpu
if [ -d "$occlum_glibc_lib" ]; then
mkdir -p "image/$occlum_glibc_lib"
mkdir -p "image/$cpu_lib"
fi
# add default /etc/hosts # add default /etc/hosts
mkdir -p image/etc mkdir -p image/etc
echo "127.0.0.1 localhost" > image/etc/hosts echo "127.0.0.1 localhost" > image/etc/hosts
local occlum_gcc_lib=/usr/local/occlum/x86_64-linux-musl/lib # add musl
cp -t image/lib/ \ local occlum_musl_lib=/usr/local/occlum/x86_64-linux-musl/lib
cp -t image/lib \
/lib/ld-musl-x86_64.so.1 \ /lib/ld-musl-x86_64.so.1 \
"$occlum_gcc_lib/libc.so" \ "$occlum_musl_lib/libc.so" \
"$occlum_gcc_lib/libstdc++.so.6" \ "$occlum_musl_lib/libstdc++.so.6" \
"$occlum_gcc_lib/libgcc_s.so.1" \ "$occlum_musl_lib/libgcc_s.so.1" \
"$occlum_gcc_lib/libgomp.so.1" "$occlum_musl_lib/libgomp.so.1"
# add glibc
if [ -d "$occlum_glibc_lib" ]; then
cp -t image/lib64 \
"$occlum_glibc_lib/ld-linux-x86-64.so.2"
ln -sf /lib64/ld-linux-x86-64.so.2 "image/$occlum_glibc_lib/ld-linux-x86-64.so.2"
cp -t "image/$occlum_glibc_lib" \
"$occlum_glibc_lib/libc.so.6" \
"$occlum_glibc_lib/libpthread.so.0" \
"$occlum_glibc_lib/libm.so.6" \
"/usr/lib/x86_64-linux-gnu/libstdc++.so.6" \
"/usr/lib/gcc/x86_64-linux-gnu/7/libgcc_s.so.1"
cp -t "image/$cpu_lib" \
"$cpu_lib/online"
fi
cp "$occlum_dir"/etc/template/Occlum.json "$instance_dir"/ cp "$occlum_dir"/etc/template/Occlum.json "$instance_dir"/
chmod 644 "$instance_dir"/Occlum.json chmod 644 "$instance_dir"/Occlum.json

27
tools/toolchains/glibc/build.sh Executable file

@ -0,0 +1,27 @@
#!/bin/bash
SRC_DIR=/tmp/glibc/glibc
BUILD_DIR=/tmp/glibc/glibc_build
INSTALL_DIR=/opt/occlum/glibc
# Exit if any command fails
set -e
# Clean previous build and installation if any
rm -rf ${SRC_DIR}
rm -rf ${BUILD_DIR}
rm -rf ${INSTALL_DIR}
mkdir -p ${SRC_DIR}
cd ${SRC_DIR}
# Download glibc
git clone -b occlum-glibc-2.27 https://github.com/occlum/glibc .
mkdir -p ${BUILD_DIR}
cd ${BUILD_DIR}
# Build and install glibc
unset LD_LIBRARY_PATH
CFLAGS="-O2 -g" ${SRC_DIR}/configure \
--prefix=${INSTALL_DIR} --with-tls --without-selinux \
--enable-stack-protector=strong --disable-nscd
make
make install