From 0210cc2ca4c4e44f967d0f80eda81344e9990f18 Mon Sep 17 00:00:00 2001 From: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:28:45 +0800 Subject: [PATCH] fix: operation not permitted with python/amd64 (#8) * fix: amd64 sometimes operation not permitted * fix: tests * fix: tests * fix: tests --- .github/workflows/tests-amd64.yml | 2 +- .github/workflows/tests-arm64.yml | 2 +- cmd/test/fuzz_python_amd64/main.go | 6 -- cmd/test/fuzz_python_amd64/test.py | 5 +- .../static/python_syscall/syscalls_amd64.go | 1 + tests/integration_tests/base.go | 9 ++ .../integration_tests/nodejs_feature_test.go | 60 ++++++------- .../integration_tests/python_feature_test.go | 84 ++++++++++--------- 8 files changed, 92 insertions(+), 77 deletions(-) create mode 100644 tests/integration_tests/base.go diff --git a/.github/workflows/tests-amd64.yml b/.github/workflows/tests-amd64.yml index ff3aec7..1853bc9 100644 --- a/.github/workflows/tests-amd64.yml +++ b/.github/workflows/tests-amd64.yml @@ -52,6 +52,6 @@ jobs: run: pip install httpx requests jinja2 - name: Run Intgeration tests - run: sudo go test -v github.com/langgenius/dify-sandbox/tests/integration_tests/... + run: sudo go test -timeout 120s -v ./tests/integration_tests/... env: PYTHON_PATH: /usr/bin/python3.10 diff --git a/.github/workflows/tests-arm64.yml b/.github/workflows/tests-arm64.yml index c46e395..3529582 100644 --- a/.github/workflows/tests-arm64.yml +++ b/.github/workflows/tests-arm64.yml @@ -50,6 +50,6 @@ jobs: run: sudo ln -s "$(which go)" /usr/local/bin/go - name: Run Intgeration tests - run: sudo go test -v github.com/langgenius/dify-sandbox/tests/integration_tests/... + run: sudo go test -timeout 120s -v ./tests/integration_tests/... env: PYTHON_PATH: /usr/bin/python3.10 diff --git a/cmd/test/fuzz_python_amd64/main.go b/cmd/test/fuzz_python_amd64/main.go index 9d93f68..93b8f30 100644 --- a/cmd/test/fuzz_python_amd64/main.go +++ b/cmd/test/fuzz_python_amd64/main.go @@ -50,12 +50,6 @@ func main() { if find_syscall(i, original) == -1 { list[i] = append(list[i], i) } - - // for j := 217; j < 218; j++ { - // if find_syscall(j, list[i]) == -1 { - // list[i] = append(list[i], j) - // } - // } } lock := sync.Mutex{} diff --git a/cmd/test/fuzz_python_amd64/test.py b/cmd/test/fuzz_python_amd64/test.py index 097924e..f72368e 100644 --- a/cmd/test/fuzz_python_amd64/test.py +++ b/cmd/test/fuzz_python_amd64/test.py @@ -31,8 +31,9 @@ def excepthook(type, value, tb): # declare main function here def main() -> dict: - import requests - print(requests.get("https://www.google.com").text) + return { + "message": [1, 2, 3] + } from json import loads, dumps from base64 import b64decode diff --git a/internal/static/python_syscall/syscalls_amd64.go b/internal/static/python_syscall/syscalls_amd64.go index 3ea0e2e..f4cf9da 100644 --- a/internal/static/python_syscall/syscalls_amd64.go +++ b/internal/static/python_syscall/syscalls_amd64.go @@ -27,6 +27,7 @@ var ALLOW_SYSCALLS = []int{ syscall.SYS_EXIT, syscall.SYS_EXIT_GROUP, syscall.SYS_TGKILL, syscall.SYS_RT_SIGACTION, syscall.SYS_IOCTL, syscall.SYS_SCHED_YIELD, + syscall.SYS_SET_ROBUST_LIST, SYS_RSEQ, // time syscall.SYS_CLOCK_GETTIME, syscall.SYS_GETTIMEOFDAY, syscall.SYS_NANOSLEEP, diff --git a/tests/integration_tests/base.go b/tests/integration_tests/base.go new file mode 100644 index 0000000..a071b5a --- /dev/null +++ b/tests/integration_tests/base.go @@ -0,0 +1,9 @@ +package integrationtests_test + +import "testing" + +func runMultipleTestings(t *testing.T, iterations int, testFunc func(*testing.T)) { + for i := 0; i < iterations; i++ { + testFunc(t) + } +} diff --git a/tests/integration_tests/nodejs_feature_test.go b/tests/integration_tests/nodejs_feature_test.go index 705acad..c0ad1cd 100644 --- a/tests/integration_tests/nodejs_feature_test.go +++ b/tests/integration_tests/nodejs_feature_test.go @@ -10,41 +10,45 @@ import ( func TestNodejsBase64(t *testing.T) { // Test case for base64 - resp := service.RunNodeJsCode(` + runMultipleTestings(t, 30, func(t *testing.T) { + resp := service.RunNodeJsCode(` const base64 = Buffer.from("hello world").toString("base64"); console.log(Buffer.from(base64, "base64").toString()); - `, "", &types.RunnerOptions{ - EnableNetwork: true, + `, "", &types.RunnerOptions{ + EnableNetwork: true, + }) + if resp.Code != 0 { + t.Fatal(resp) + } + + if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") { + t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) + } + + if resp.Data.(*service.RunCodeResponse).Stderr != "" { + t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) + } }) - if resp.Code != 0 { - t.Error(resp) - } - - if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") { - t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) - } - - if resp.Data.(*service.RunCodeResponse).Stderr != "" { - t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) - } } func TestNodejsJSON(t *testing.T) { // Test case for json - resp := service.RunNodeJsCode(` + runMultipleTestings(t, 30, func(t *testing.T) { + resp := service.RunNodeJsCode(` console.log(JSON.stringify({"hello": "world"})); - `, "", &types.RunnerOptions{ - EnableNetwork: true, + `, "", &types.RunnerOptions{ + EnableNetwork: true, + }) + if resp.Code != 0 { + t.Error(resp) + } + + if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) { + t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) + } + + if resp.Data.(*service.RunCodeResponse).Stderr != "" { + t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) + } }) - if resp.Code != 0 { - t.Error(resp) - } - - if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello":"world"}`) { - t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) - } - - if resp.Data.(*service.RunCodeResponse).Stderr != "" { - t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) - } } diff --git a/tests/integration_tests/python_feature_test.go b/tests/integration_tests/python_feature_test.go index 66b4881..0caf9a6 100644 --- a/tests/integration_tests/python_feature_test.go +++ b/tests/integration_tests/python_feature_test.go @@ -10,63 +10,69 @@ import ( func TestPythonBase64(t *testing.T) { // Test case for base64 - resp := service.RunPython3Code(` + runMultipleTestings(t, 50, func(t *testing.T) { + resp := service.RunPython3Code(` import base64 print(base64.b64decode(base64.b64encode(b"hello world")).decode()) - `, "", &types.RunnerOptions{ - EnableNetwork: true, - }) - if resp.Code != 0 { - t.Error(resp) - } + `, "", &types.RunnerOptions{ + EnableNetwork: true, + }) + if resp.Code != 0 { + t.Fatal(resp) + } - if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") { - t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) - } + if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "hello world") { + t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) + } - if resp.Data.(*service.RunCodeResponse).Stderr != "" { - t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) - } + if resp.Data.(*service.RunCodeResponse).Stderr != "" { + t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) + } + }) } func TestPythonJSON(t *testing.T) { - // Test case for json - resp := service.RunPython3Code(` + runMultipleTestings(t, 50, func(t *testing.T) { + // Test case for json + resp := service.RunPython3Code(` import json print(json.dumps({"hello": "world"})) - `, "", &types.RunnerOptions{ - EnableNetwork: true, - }) - if resp.Code != 0 { - t.Error(resp) - } + `, "", &types.RunnerOptions{ + EnableNetwork: true, + }) + if resp.Code != 0 { + t.Fatal(resp) + } - if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) { - t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) - } + if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, `{"hello": "world"}`) { + t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) + } - if resp.Data.(*service.RunCodeResponse).Stderr != "" { - t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) - } + if resp.Data.(*service.RunCodeResponse).Stderr != "" { + t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) + } + }) } func TestPythonHttp(t *testing.T) { // Test case for http - resp := service.RunPython3Code(` + runMultipleTestings(t, 10, func(t *testing.T) { + resp := service.RunPython3Code(` import requests print(requests.get("https://www.bilibili.com").content) `, "", &types.RunnerOptions{ - EnableNetwork: true, - }) - if resp.Code != 0 { - t.Error(resp) - } + EnableNetwork: true, + }) + if resp.Code != 0 { + t.Fatal(resp) + } - if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") { - t.Errorf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) - } + if !strings.Contains(resp.Data.(*service.RunCodeResponse).Stdout, "bilibili") { + t.Fatalf("unexpected output: %s\n", resp.Data.(*service.RunCodeResponse).Stdout) + } - if resp.Data.(*service.RunCodeResponse).Stderr != "" { - t.Errorf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) - } + if resp.Data.(*service.RunCodeResponse).Stderr != "" { + t.Fatalf("unexpected error: %s\n", resp.Data.(*service.RunCodeResponse).Stderr) + } + }) }