main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "github.com/langgenius/dify-sandbox/internal/static/python_syscall"
  10. )
  11. const (
  12. SYSCALL_NUMS = 400
  13. )
  14. func run(allowed_syscalls []int) {
  15. nums := []string{}
  16. for _, syscall := range allowed_syscalls {
  17. nums = append(nums, strconv.Itoa(syscall))
  18. }
  19. os.Setenv("ALLOWED_SYSCALLS", strings.Join(nums, ","))
  20. _, err := exec.Command("python3", "cmd/test/fuzz_python/test.py").Output()
  21. if err == nil {
  22. //fmt.Println(string(p))
  23. } else {
  24. fmt.Println(err)
  25. }
  26. }
  27. func find_syscall(syscall int, syscalls []int) int {
  28. for i, s := range syscalls {
  29. if s == syscall {
  30. return i
  31. }
  32. }
  33. return -1
  34. }
  35. func main() {
  36. original := python_syscall.ALLOW_SYSCALLS
  37. original = append(original, python_syscall.ALLOW_NETWORK_SYSCALLS...)
  38. // generate task list
  39. list := make([][]int, SYSCALL_NUMS)
  40. for i := 0; i < SYSCALL_NUMS; i++ {
  41. list[i] = make([]int, len(original))
  42. copy(list[i], original)
  43. // add i
  44. if find_syscall(i, original) == -1 {
  45. list[i] = append(list[i], i)
  46. }
  47. for j := 49; j < 50; j++ {
  48. if find_syscall(j, list[i]) == -1 {
  49. list[i] = append(list[i], j)
  50. }
  51. }
  52. // for j := 293; j < 294; j++ {
  53. // if find_syscall(j, list[i]) == -1 {
  54. // list[i] = append(list[i], j)
  55. // }
  56. // }
  57. // for j := 220; j < 220; j++ {
  58. // if find_syscall(j, list[i]) == -1 {
  59. // list[i] = append(list[i], j)
  60. // }
  61. // }
  62. // for j := 100; j < 100; j++ {
  63. // if find_syscall(j, list[i]) == -1 {
  64. // list[i] = append(list[i], j)
  65. // }
  66. // }
  67. }
  68. lock := sync.Mutex{}
  69. wg := sync.WaitGroup{}
  70. i := 0
  71. // run 4 tasks concurrently
  72. for j := 0; j < 4; j++ {
  73. wg.Add(1)
  74. go func() {
  75. defer wg.Done()
  76. for {
  77. lock.Lock()
  78. if i >= len(list) {
  79. lock.Unlock()
  80. return
  81. }
  82. task := list[i]
  83. i++
  84. lock.Unlock()
  85. run(task)
  86. }
  87. }()
  88. }
  89. // wait for all tasks to finish
  90. wg.Wait()
  91. }