From fbff05bddbde9b81aedbaed3173f535aab203139 Mon Sep 17 00:00:00 2001 From: Lan Yun Date: Thu, 28 Jul 2022 15:09:18 +0800 Subject: [PATCH] Update the Golang version to 1.18.4 and use it as the default one. Signed-off-by: Lan Yun --- .github/workflows/demo_test.yml | 7 +- demos/golang/go_sqlite/run_go_sqlite_demo.sh | 1 + .../golang/grpc_pingpong/prepare_ping_pong.sh | 23 +- .../golang/adapt-golang1.18.4-to-occlum.patch | 721 ++++++++++++++++++ tools/toolchains/golang/build.sh | 22 +- 5 files changed, 765 insertions(+), 9 deletions(-) create mode 100644 tools/toolchains/golang/adapt-golang1.18.4-to-occlum.patch diff --git a/.github/workflows/demo_test.yml b/.github/workflows/demo_test.yml index 26629c94..ed9b3876 100644 --- a/.github/workflows/demo_test.yml +++ b/.github/workflows/demo_test.yml @@ -56,8 +56,11 @@ jobs: run: docker exec ${{ github.job }} bash -c "cd /root/occlum/demos/embedded_mode && SGX_MODE=SIM make; SGX_MODE=SIM make test" - - name: Run Golang sqlite test - run: docker exec ${{ github.job }} bash -c "cd /root/occlum/demos/golang/go_sqlite/ && SGX_MODE=SIM ./run_go_sqlite_demo.sh" + - name: Run Golang v1.16.3 sqlite test + run: docker exec ${{ github.job }} bash -c "cd /root/occlum/tools/toolchains/golang && ./build.sh go1.16.3 && cd /root/occlum/demos/golang/go_sqlite/ && SGX_MODE=SIM ./run_go_sqlite_demo.sh" + + - name: Run Golang v1.18.4 sqlite test + run: docker exec ${{ github.job }} bash -c "cd /root/occlum/tools/toolchains/golang && ./build.sh go1.18.4 && cd /root/occlum/demos/golang/go_sqlite/ && SGX_MODE=SIM ./run_go_sqlite_demo.sh" - name: Go Server set up and run run: docker exec ${{ github.job }} bash -c "cd /root/occlum/demos/golang/web_server && occlum-go mod init web_server && occlum-go get -u -v github.com/gin-gonic/gin; diff --git a/demos/golang/go_sqlite/run_go_sqlite_demo.sh b/demos/golang/go_sqlite/run_go_sqlite_demo.sh index e92ecbd2..286176ac 100755 --- a/demos/golang/go_sqlite/run_go_sqlite_demo.sh +++ b/demos/golang/go_sqlite/run_go_sqlite_demo.sh @@ -5,6 +5,7 @@ BLUE='\033[1;34m' NC='\033[0m' # Install SQLite with occlum-go +rm -f go.mod occlum-go mod init simple_demo_instance && \ occlum-go get -u -v github.com/mattn/go-sqlite3 diff --git a/demos/golang/grpc_pingpong/prepare_ping_pong.sh b/demos/golang/grpc_pingpong/prepare_ping_pong.sh index 8df0e615..4b25c285 100755 --- a/demos/golang/grpc_pingpong/prepare_ping_pong.sh +++ b/demos/golang/grpc_pingpong/prepare_ping_pong.sh @@ -1,6 +1,5 @@ #!/bin/bash set -e - BLUE='\033[1;34m' NC='\033[0m' @@ -9,6 +8,7 @@ FILE_SET=" occlum_ping occlum_pong go.sum" + for CURR_FILE in $FILE_SET do if [ -f "$CURR_FILE" ]; then @@ -27,9 +27,14 @@ do fi done -# enable Go modules for package management +# enable Go modules for package management +GOVERSION=`occlum-go version|awk -F ' ' '{printf $3}'` export GO111MODULE=on - +if [[ $GOVERSION != 'go1.16.3' ]];then + rm -f go.mod + occlum-go mod init grpc_pingpong + occlum-go mod tidy +fi # update PATH so that the protoc compiler can find the plugin: export PATH="$PATH:$(go env GOPATH)/bin" @@ -41,10 +46,20 @@ fi # install protoc-gen-go and protoc-gen-go-grpc plugin if ! type "protoc-gen-go" > /dev/null 2>&1; then + if [[ $GOVERSION != 'go1.16.3' ]];then occlum-go get google.golang.org/protobuf/cmd/protoc-gen-go + occlum-go install google.golang.org/protobuf/cmd/protoc-gen-go + else + occlum-go get google.golang.org/protobuf/cmd/protoc-gen-go + fi fi if ! type "protoc-gen-go-grpc" > /dev/null 2>&1; then - occlum-go get google.golang.org/grpc/cmd/protoc-gen-go-grpc + if [[ $GOVERSION != 'go1.16.3' ]];then + occlum-go get google.golang.org/grpc/cmd/protoc-gen-go-grpc + occlum-go install google.golang.org/grpc/cmd/protoc-gen-go-grpc + else + occlum-go get google.golang.org/grpc/cmd/protoc-gen-go-grpc + fi fi # compiling pingpong gRPC .proto file diff --git a/tools/toolchains/golang/adapt-golang1.18.4-to-occlum.patch b/tools/toolchains/golang/adapt-golang1.18.4-to-occlum.patch new file mode 100644 index 00000000..91302921 --- /dev/null +++ b/tools/toolchains/golang/adapt-golang1.18.4-to-occlum.patch @@ -0,0 +1,721 @@ +From 59017c2fa01ed4764225fdbfed7a17287f81d87a Mon Sep 17 00:00:00 2001 +From: Lan Yun +Date: Thu, 28 Jul 2022 11:00:15 +0800 +Subject: [PATCH] occlum-go-patch + +Signed-off-by: Lan Yun +--- + src/runtime/malloc.go | 70 ++++++------ + src/runtime/os_linux.go | 4 + + src/runtime/sys_linux_amd64.go | 3 + + src/runtime/sys_linux_amd64.s | 192 +++++++++++++++++++++++++-------- + src/runtime/textflag.h | 18 +++- + src/syscall/asm_linux_amd64.s | 14 +-- + 6 files changed, 212 insertions(+), 89 deletions(-) + create mode 100644 src/runtime/sys_linux_amd64.go + +diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go +index 6ed6ceade2..2a94401c0c 100644 +--- a/src/runtime/malloc.go ++++ b/src/runtime/malloc.go +@@ -645,42 +645,44 @@ func (h *mheap) sysAlloc(n uintptr) (v unsafe.Pointer, size uintptr) { + goto mapped + } + +- // Try to grow the heap at a hint address. +- for h.arenaHints != nil { +- hint := h.arenaHints +- p := hint.addr +- if hint.down { +- p -= n +- } +- if p+n < p { +- // We can't use this, so don't ask. +- v = nil +- } else if arenaIndex(p+n-1) >= 1<= 1<procid to Linux tid ++ MOVL $SYS_gettid, AX ++ SYSCALL_ENHANCE ++ MOVQ AX, m_procid(R13) ++ ++ // In child, set up new stack ++ get_tls(CX) ++ MOVQ R13, g_m(R9) ++ MOVQ R9, g(CX) ++ MOVQ R9, R14 // set g register ++ CALL runtime·stackcheck(SB) ++ ++occlum_nog2: ++ // Call fn ++ CALL R12 ++ ++ // It shouldn't return. If it does, exit that thread. ++ MOVL $111, DI ++ MOVL $SYS_exit, AX ++ SYSCALL_ENHANCE ++ JMP -3(PC) // keep exiting ++ + TEXT runtime·sigaltstack(SB),NOSPLIT,$-8 + MOVQ new+0(FP), DI + MOVQ old+8(FP), SI + MOVQ $SYS_sigaltstack, AX +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS 2(PC) + MOVL $0xf1, 0xf1 // crash +@@ -639,7 +741,7 @@ TEXT runtime·settls(SB),NOSPLIT,$32 + MOVQ DI, SI + MOVQ $0x1002, DI // ARCH_SET_FS + MOVQ $SYS_arch_prctl, AX +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS 2(PC) + MOVL $0xf1, 0xf1 // crash +@@ -647,7 +749,7 @@ TEXT runtime·settls(SB),NOSPLIT,$32 + + TEXT runtime·osyield(SB),NOSPLIT,$0 + MOVL $SYS_sched_yield, AX +- SYSCALL ++ SYSCALL_ENHANCE + RET + + TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0 +@@ -655,7 +757,7 @@ TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0 + MOVQ len+8(FP), SI + MOVQ buf+16(FP), DX + MOVL $SYS_sched_getaffinity, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+24(FP) + RET + +@@ -663,7 +765,7 @@ TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0 + TEXT runtime·epollcreate(SB),NOSPLIT,$0 + MOVL size+0(FP), DI + MOVL $SYS_epoll_create, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+8(FP) + RET + +@@ -671,7 +773,7 @@ TEXT runtime·epollcreate(SB),NOSPLIT,$0 + TEXT runtime·epollcreate1(SB),NOSPLIT,$0 + MOVL flags+0(FP), DI + MOVL $SYS_epoll_create1, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+8(FP) + RET + +@@ -682,7 +784,7 @@ TEXT runtime·epollctl(SB),NOSPLIT,$0 + MOVL fd+8(FP), DX + MOVQ ev+16(FP), R10 + MOVL $SYS_epoll_ctl, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+24(FP) + RET + +@@ -695,7 +797,7 @@ TEXT runtime·epollwait(SB),NOSPLIT,$0 + MOVL timeout+20(FP), R10 + MOVQ $0, R8 + MOVL $SYS_epoll_pwait, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+24(FP) + RET + +@@ -705,7 +807,7 @@ TEXT runtime·closeonexec(SB),NOSPLIT,$0 + MOVQ $2, SI // F_SETFD + MOVQ $1, DX // FD_CLOEXEC + MOVL $SYS_fcntl, AX +- SYSCALL ++ SYSCALL_ENHANCE + RET + + // func runtime·setNonblock(int32 fd) +@@ -714,13 +816,13 @@ TEXT runtime·setNonblock(SB),NOSPLIT,$0-4 + MOVQ $3, SI // F_GETFL + MOVQ $0, DX + MOVL $SYS_fcntl, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL fd+0(FP), DI // fd + MOVQ $4, SI // F_SETFL + MOVQ $0x800, DX // O_NONBLOCK + ORL AX, DX + MOVL $SYS_fcntl, AX +- SYSCALL ++ SYSCALL_ENHANCE + RET + + // int access(const char *name, int mode) +@@ -731,7 +833,7 @@ TEXT runtime·access(SB),NOSPLIT,$0 + MOVL mode+8(FP), DX + MOVL $0, R10 + MOVL $SYS_faccessat, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+16(FP) + RET + +@@ -741,7 +843,7 @@ TEXT runtime·connect(SB),NOSPLIT,$0-28 + MOVQ addr+8(FP), SI + MOVL len+16(FP), DX + MOVL $SYS_connect, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+24(FP) + RET + +@@ -751,7 +853,7 @@ TEXT runtime·socket(SB),NOSPLIT,$0-20 + MOVL typ+4(FP), SI + MOVL prot+8(FP), DX + MOVL $SYS_socket, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVL AX, ret+16(FP) + RET + +@@ -760,6 +862,6 @@ TEXT runtime·sbrk0(SB),NOSPLIT,$0-8 + // Implemented as brk(NULL). + MOVQ $0, DI + MOVL $SYS_brk, AX +- SYSCALL ++ SYSCALL_ENHANCE + MOVQ AX, ret+0(FP) + RET +diff --git a/src/runtime/textflag.h b/src/runtime/textflag.h +index 214075e360..4b7da24e64 100644 +--- a/src/runtime/textflag.h ++++ b/src/runtime/textflag.h +@@ -32,8 +32,20 @@ + #define NOFRAME 512 + // Function can call reflect.Type.Method or reflect.Type.MethodByName. + #define REFLECTMETHOD 1024 +-// Function is the outermost frame of the call stack. Call stack unwinders +-// should stop at this function. ++// Function is the top of the call stack. Call stack unwinders should stop ++// at this function. + #define TOPFRAME 2048 +-// Function is an ABI wrapper. + #define ABIWRAPPER 4096 ++// step one: assign syscall return address to register rcx, occlum know where ++// should return according to rcx. ++// step two: jump to syscall interface address provided by occlum when go ++// .bin file loaded. ++// ++// actually is the assembler instruction: lea 0xc(%rip),%rcx ++#define SYSCALL_ENHANCE \ ++ CMPQ runtime·occlumentry(SB), $0x0 \ ++ JBE 10(PC) \ ++ BYTE $0x48; BYTE $0x8d; BYTE $0x0d; BYTE $0x0c; BYTE $0x00; BYTE $0x00; BYTE $0x00 \ ++ MOVQ runtime·occlumentry(SB), R11 \ ++ JMP R11 \ ++ SYSCALL +diff --git a/src/syscall/asm_linux_amd64.s b/src/syscall/asm_linux_amd64.s +index a9af68d51d..5110d78bb5 100644 +--- a/src/syscall/asm_linux_amd64.s ++++ b/src/syscall/asm_linux_amd64.s +@@ -22,7 +22,7 @@ TEXT ·Syscall(SB),NOSPLIT,$0-56 + MOVQ a2+16(FP), SI + MOVQ a3+24(FP), DX + MOVQ trap+0(FP), AX // syscall entry +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS ok + MOVQ $-1, r1+32(FP) +@@ -48,7 +48,7 @@ TEXT ·Syscall6(SB),NOSPLIT,$0-80 + MOVQ a5+40(FP), R8 + MOVQ a6+48(FP), R9 + MOVQ trap+0(FP), AX // syscall entry +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS ok6 + MOVQ $-1, r1+56(FP) +@@ -70,7 +70,7 @@ TEXT ·RawSyscall(SB),NOSPLIT,$0-56 + MOVQ a2+16(FP), SI + MOVQ a3+24(FP), DX + MOVQ trap+0(FP), AX // syscall entry +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS ok1 + MOVQ $-1, r1+32(FP) +@@ -93,7 +93,7 @@ TEXT ·RawSyscall6(SB),NOSPLIT,$0-80 + MOVQ a5+40(FP), R8 + MOVQ a6+48(FP), R9 + MOVQ trap+0(FP), AX // syscall entry +- SYSCALL ++ SYSCALL_ENHANCE + CMPQ AX, $0xfffffffffffff001 + JLS ok2 + MOVQ $-1, r1+56(FP) +@@ -117,7 +117,7 @@ TEXT ·rawVforkSyscall(SB),NOSPLIT|NOFRAME,$0-32 + MOVQ $0, R9 + MOVQ trap+0(FP), AX // syscall entry + POPQ R12 // preserve return address +- SYSCALL ++ SYSCALL_ENHANCE + PUSHQ R12 + CMPQ AX, $0xfffffffffffff001 + JLS ok2 +@@ -136,7 +136,7 @@ TEXT ·rawSyscallNoError(SB),NOSPLIT,$0-48 + MOVQ a2+16(FP), SI + MOVQ a3+24(FP), DX + MOVQ trap+0(FP), AX // syscall entry +- SYSCALL ++ SYSCALL_ENHANCE + MOVQ AX, r1+32(FP) + MOVQ DX, r2+40(FP) + RET +@@ -157,7 +157,7 @@ ret: + RET + fallback: + MOVL $SYS_gettimeofday, AX +- SYSCALL ++ SYSCALL_ENHANCE + JMP ret + ok7: + MOVQ $0, err+8(FP) +-- +2.25.1 + diff --git a/tools/toolchains/golang/build.sh b/tools/toolchains/golang/build.sh index bf66a518..faf5ec41 100755 --- a/tools/toolchains/golang/build.sh +++ b/tools/toolchains/golang/build.sh @@ -2,6 +2,22 @@ THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" BUILD_DIR=/tmp/occlum_golang_toolchain INSTALL_DIR=/opt/occlum/toolchains/golang +GO_VERSION="1.18.4" + +if [[ $# -ge 1 ]];then +case $1 in + --help|help) + echo "Usage:$0 [go1.16.3|go1.18.4]" + exit + ;; + "go1.16.3"|"1.16.3"|"v1.16.3") + GO_VERSION="1.16.3" + ;; + "go1.18.4"|"1.18.4"|"v1.18.4") + GO_VERSION="1.18.4" + ;; + esac +fi # Exit if any command fails set -e @@ -16,10 +32,10 @@ cd ${BUILD_DIR} # Download Golang git clone https://github.com/golang/go . -# Swtich to Golang 1.13.7 -git checkout -b go1.16.3 tags/go1.16.3 +# Swtich to Golang 1.16.3 or Golang 1.18.4 +git checkout -b go${GO_VERSION} tags/go${GO_VERSION} # Apply the patch to adapt Golang to Occlum -git apply ${THIS_DIR}/adapt-golang1.16.3-to-occlum.patch +git apply ${THIS_DIR}/adapt-golang${GO_VERSION}-to-occlum.patch # Build Golang cd src