robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

多线程的时候会报错 #511

Open DokiDoki1103 opened 9 months ago

DokiDoki1103 commented 9 months ago

var vm = otto.New()

// func xx(){}
func TestJS(t *testing.T) {

    const numTasks = 10000
    const numWorkers = 2

    tasks := make(chan int, numTasks)
    results := make(chan int, numTasks)
    var wg sync.WaitGroup

    // 启动 goroutine 处理任务
    for i := 0; i < numWorkers; i++ {
        wg.Add(1)
        go worker(tasks, results, &wg)
    }

    // 提交任务到通道中
    go func() {
        for i := 0; i < numTasks; i++ {
            tasks <- i
        }
        close(tasks) // 关闭任务通道,告知所有的 worker 已经没有任务了
    }()

    // 处理结果,等待所有 worker 完成任务
    go func() {
        wg.Wait()      // 等待所有 worker 完成任务
        close(results) // 关闭结果通道
    }()

    // 读取结果
    for res := range results {
        fmt.Printf("结果 %d 完成\n", res)
    }

    fmt.Println("所有任务完成")

}

func worker(tasks <-chan int, results chan<- int, wg *sync.WaitGroup) {
    defer wg.Done()
    for task := range tasks {
        // 这里执行任务的逻辑,示例中只是打印任务号
        // fmt.Printf("完成任务 %d\n", task)
        vm.Run(`//这个是具体的加密函数 zstring魔改的压缩算法
function _0x56d97c(_0x8df421) {
  // console.log(_0x8df421)
  if (_0x8df421 == null) {
    return '';
  }

  var _0x3219b6 = "DGi0YA7BemWnQjCl4+bR3f8SKIF9tUz/xhr2oEOgPpac=61ZqwTudLkM5vHyNXsVJ";

  function _0x1637ab(_0x329955) {
    return _0x3219b6["charAt"](_0x329955);
  }

  if (_0x8df421 == null) {
    return '';
  }

  var _0x2ba560;

  var _0x1a3125;

  var _0x56a845 = {};
  var _0x568d8a = {};
  var _0x17ecc2 = '';
  var _0x4b03fb = '';
  var _0x127752 = '';
  var _0x55a847 = 2;
  var _0x48b33e = 3;
  var _0x318c3 = 2;
  var _0x24e447 = [];
  var _0x841ee6 = 0;
  var _0x25adf9 = 0;

  var _0x4afbc9;

  for (_0x4afbc9 = 0; _0x4afbc9 < _0x8df421["length"]; _0x4afbc9 += 1) {
    _0x17ecc2 = _0x8df421["charAt"](_0x4afbc9);

    if (!Object["prototype"]["hasOwnProperty"]["call"](_0x56a845, _0x17ecc2)) {
      _0x56a845[_0x17ecc2] = _0x48b33e++;
      _0x568d8a[_0x17ecc2] = true;
    }

    _0x4b03fb = _0x127752 + _0x17ecc2;

    if (Object["prototype"]["hasOwnProperty"]["call"](_0x56a845, _0x4b03fb)) {
      _0x127752 = _0x4b03fb;
    } else {
      if (Object["prototype"]["hasOwnProperty"]["call"](_0x568d8a, _0x127752)) {
        if (_0x127752["charCodeAt"](0) < 256) {
          for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
            _0x841ee6 = _0x841ee6 << 1;

            if (_0x25adf9 == 5) {
              _0x25adf9 = 0;

              _0x24e447["push"](_0x1637ab(_0x841ee6));

              _0x841ee6 = 0;
            } else {
              _0x25adf9++;
            }
          }

          _0x1a3125 = _0x127752["charCodeAt"](0);

          for (_0x2ba560 = 0; _0x2ba560 < 8; _0x2ba560++) {
            _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

            if (_0x25adf9 == 5) {
              _0x25adf9 = 0;

              _0x24e447["push"](_0x1637ab(_0x841ee6));

              _0x841ee6 = 0;
            } else {
              _0x25adf9++;
            }

            _0x1a3125 = _0x1a3125 >> 1;
          }
        } else {
          _0x1a3125 = 1;

          for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
            _0x841ee6 = _0x841ee6 << 1 | _0x1a3125;

            if (_0x25adf9 == 5) {
              _0x25adf9 = 0;

              _0x24e447["push"](_0x1637ab(_0x841ee6));

              _0x841ee6 = 0;
            } else {
              _0x25adf9++;
            }

            _0x1a3125 = 0;
          }

          _0x1a3125 = _0x127752["charCodeAt"](0);

          for (_0x2ba560 = 0; _0x2ba560 < 16; _0x2ba560++) {
            _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

            if (_0x25adf9 == 5) {
              _0x25adf9 = 0;

              _0x24e447["push"](_0x1637ab(_0x841ee6));

              _0x841ee6 = 0;
            } else {
              _0x25adf9++;
            }

            _0x1a3125 = _0x1a3125 >> 1;
          }
        }

        _0x55a847--;

        if (_0x55a847 == 0) {
          _0x55a847 = Math["pow"](2, _0x318c3);
          _0x318c3++;
        }

        delete _0x568d8a[_0x127752];
      } else {
        _0x1a3125 = _0x56a845[_0x127752];

        for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
          _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

          if (_0x25adf9 == 5) {
            _0x25adf9 = 0;

            _0x24e447["push"](_0x1637ab(_0x841ee6));

            _0x841ee6 = 0;
          } else {
            _0x25adf9++;
          }

          _0x1a3125 = _0x1a3125 >> 1;
        }
      }

      _0x55a847--;

      if (_0x55a847 == 0) {
        _0x55a847 = Math["pow"](2, _0x318c3);
        _0x318c3++;
      }

      _0x56a845[_0x4b03fb] = _0x48b33e++;
      _0x127752 = String(_0x17ecc2);
    }
  }

  if (_0x127752 !== '') {
    if (Object["prototype"]["hasOwnProperty"]["call"](_0x568d8a, _0x127752)) {
      if (_0x127752["charCodeAt"](0) < 256) {
        for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
          _0x841ee6 = _0x841ee6 << 1;

          if (_0x25adf9 == 5) {
            _0x25adf9 = 0;

            _0x24e447["push"](_0x1637ab(_0x841ee6));

            _0x841ee6 = 0;
          } else {
            _0x25adf9++;
          }
        }

        _0x1a3125 = _0x127752["charCodeAt"](0);

        for (_0x2ba560 = 0; _0x2ba560 < 8; _0x2ba560++) {
          _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

          if (_0x25adf9 == 5) {
            _0x25adf9 = 0;

            _0x24e447["push"](_0x1637ab(_0x841ee6));

            _0x841ee6 = 0;
          } else {
            _0x25adf9++;
          }

          _0x1a3125 = _0x1a3125 >> 1;
        }
      } else {
        _0x1a3125 = 1;

        for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
          _0x841ee6 = _0x841ee6 << 1 | _0x1a3125;

          if (_0x25adf9 == 5) {
            _0x25adf9 = 0;

            _0x24e447["push"](_0x1637ab(_0x841ee6));

            _0x841ee6 = 0;
          } else {
            _0x25adf9++;
          }

          _0x1a3125 = 0;
        }

        _0x1a3125 = _0x127752["charCodeAt"](0);

        for (_0x2ba560 = 0; _0x2ba560 < 16; _0x2ba560++) {
          _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

          if (_0x25adf9 == 5) {
            _0x25adf9 = 0;

            _0x24e447["push"](_0x1637ab(_0x841ee6));

            _0x841ee6 = 0;
          } else {
            _0x25adf9++;
          }

          _0x1a3125 = _0x1a3125 >> 1;
        }
      }

      _0x55a847--;

      if (_0x55a847 == 0) {
        _0x55a847 = Math["pow"](2, _0x318c3);
        _0x318c3++;
      }

      delete _0x568d8a[_0x127752];
    } else {
      _0x1a3125 = _0x56a845[_0x127752];

      for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
        _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

        if (_0x25adf9 == 5) {
          _0x25adf9 = 0;

          _0x24e447["push"](_0x1637ab(_0x841ee6));

          _0x841ee6 = 0;
        } else {
          _0x25adf9++;
        }

        _0x1a3125 = _0x1a3125 >> 1;
      }
    }

    _0x55a847--;

    if (_0x55a847 == 0) {
      _0x55a847 = Math["pow"](2, _0x318c3);
      _0x318c3++;
    }
  }

  _0x1a3125 = 2;

  for (_0x2ba560 = 0; _0x2ba560 < _0x318c3; _0x2ba560++) {
    _0x841ee6 = _0x841ee6 << 1 | _0x1a3125 & 1;

    if (_0x25adf9 == 5) {
      _0x25adf9 = 0;

      _0x24e447["push"](_0x1637ab(_0x841ee6));

      _0x841ee6 = 0;
    } else {
      _0x25adf9++;
    }

    _0x1a3125 = _0x1a3125 >> 1;
  }

  while (true) {
    _0x841ee6 = _0x841ee6 << 1;

    if (_0x25adf9 == 5) {
      _0x24e447["push"](_0x1637ab(_0x841ee6));

      break;
    } else {
      _0x25adf9++;
    }
  }

  return _0x24e447["join"]('');
}
//这个是压缩前置
function formatParams(base_url, data) {
  var sinData = base_url + data;
  sinData = encodeURIComponent(sinData);
  var _0x318558 = 0;
  for (var i = 0; i < sinData.length; i++) {
    var _0x44becc = sinData.charCodeAt(i);
    _0x318558 = (_0x318558 << 7) - _0x318558 + 398 + _0x44becc;
    _0x318558 |= _0x318558;
  }
  if ( !data) {
    return _0x318558 + "|0|" + new Date().getTime();
  }
  return _0x318558 + "|0|" + new Date().getTime() + "|1";
}
//n4Ah7KBKDIZG8FD/IG7D9D0gDmrXBzbDREbfbD
//n4Ah7KBKDIZG8FD/IG7D9D0gDmgIDRog+ddrfbD
(formatParams("https://wx.gzhotelgroup.com/api/MyReservation/query", "{\"start\":0,\"length\":1000,\"statuses\":[0,1,2]}"));
(_0x56d97c(formatParams("https://wx.gzhotelgroup.com/api/MyReservation/query", "{\"start\":0,\"length\":1000,\"statuses\":[0,1,2]}")));`)
        results <- task // 将任务号放入结果通道
    }
}

这是报错日志

fatal error: concurrent map read and map write

goroutine 42 [running]:
github.com/robertkrimen/otto.(*_dclStash).getBinding(0x140001395c0, {0x14000119a6c, 0x7}, 0x0)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/stash.go:203 +0x50
github.com/robertkrimen/otto.(*_stashReference).getValue(0x140001fe300?)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_reference.go:68 +0x34
github.com/robertkrimen/otto.Value.resolve(...)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/value.go:483
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeDotExpression(0x140001fe300, 0x14000138ea0)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:253 +0x70
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0x140001fe300, {0x100fdf098?, 0x14000138ea0})
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:49 +0x19c
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeBinaryExpression_comparison(0x1483543f8?, 0x14000138ed0)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:159 +0x94
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0x140001fe300, {0x100fdeff8?, 0x14000138ed0})
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:34 +0xec
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeForStatement(0x140001fe300, 0x140000daa50?)
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:251 +0x17c
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0x140001fe300, {0x100fdee78?, 0x140000daa50})
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:67 +0x458
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatementList(0x100d54d77?, {0x140001b21e0?, 0x6, 0x100fdea78?})
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:119 +0x90
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0x140001fe300, {0x100fded98?, 0x14000136d38})
    /Users/zhangxiaoyuan/go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:29 +0xe4
github.com/robertkrimen/otto.(*_runtime).cmpl_call_nodeFunction(0x140001fe300, 0x1400013e120, 0x140001395c0, 0x14000584800, {0x100fbb680?, {0x14000102480?, 0x1400012d378?}}, {0x14000139590, 0x2, 0x30?})
stevenh commented 9 months ago

Otto is not safe to use for concurrent access, consider using multiple instances it that works for your use case.

If you need safe concurrent access, we'd be willing to consider a PR if it doesn't negatively impact the single access use case which is the common use for Otto.

DokiDoki1103 commented 9 months ago

Thank you, I think you need to consider this PR