justjake / quickjs-emscripten

Safely execute untrusted Javascript in your Javascript, and execute synchronous code that uses async functions
https://www.npmjs.com/package/quickjs-emscripten
Other
1.18k stars 94 forks source link

Determinism of the library #153

Open angrymouse opened 4 months ago

angrymouse commented 4 months ago

Hey! Is it possible to create fully deterministic sandbox from this library? (So that same code will always give same result with same input, even if code authors try to get different results). Assuming exposing only asyncified function (no callbacks or promises) and not exposing any non-deterministic functions, would there be some other ways to produce non-determinism?

justjake commented 4 months ago

I don't think the Javascript bits of the library introduce non-determinism into the VM. You should double-check the Emscripten module initialization code for your preferred variant.

As far as behavior of the upstream quickjs interpreter, I don't understand the underlying code enough to give a confident answer. You can try asking folks at quickjs-ng quickjs-ng issues, the upstream bellard/quickjs issues or the mailing list

angrymouse commented 4 months ago

Thank you! Will do.

Lohann commented 1 week ago

@angrymouse I think just for the fact that all numbers in javascript are IEEE-754 float, I don't think is possible to guarantee determinism across different machines without emulate all numbers (which can make it very slow): https://gafferongames.com/post/floating_point_determinism/