Commit graph

26 commits

Author SHA1 Message Date
刘皓
b8fb59e558
Make sure stack_index is initialized in binding-sandbox/binding-base.h
This field needs to be initialized when `struct frame` is constructed,
or its value may be random.

I also changed the type of `stack_index` to `wasm_size_t` so that it's
the same size on every platform.
2025-05-20 15:51:50 -04:00
刘皓
cd628048ef
Store destructors in a global table instead of in the object itself in libretro builds 2025-05-19 19:27:30 -04:00
刘皓
1d88c068fb
Fix libretro PlayStation 3 compilation error in binding-sandbox/binding-base.cpp 2025-05-19 15:48:06 -04:00
刘皓
031245491f
Keep track of all C++ objects allocated by bindings in libretro builds
This commit adds `sb()->create_object()`, `sb()->get_object()`,
`sb()->check_object_type()` and `sb()->destroy_object()` in libretro
builds to keep track of all C++ objects allocated by the bindings in
libretro builds. This has some benefits:

* Any C++ objects allocated by the bindings that are still alive when
  the game terminates can now be deallocated instead of being leaked
  like before.
* We now keep track of the types of all objects allocated by the
  bindings, so we will be able to detect when the bindings attempt to
  access objects of mismatching type.
* Keeping track of all allocated objects is required to implement
  libretro save states.
* Objects are now kept track of using numeric keys whose sizes are the
  same on every platform rather than pointers, which helps with making
  save states portable across platforms.
2025-05-19 14:44:44 -04:00
刘皓
3b564efd79
Remove direct dependency on Boost.Core in libretro builds 2025-05-16 15:51:51 -04:00
刘皓
e3cbed2fb3
Remove unused next_func_ptr field of binding_base 2025-05-14 21:16:50 -04:00
刘皓
67e61917a7
Apply big-endian fixes to binding-sandbox/wasi.cpp as well 2025-05-12 12:39:08 -04:00
刘皓
7347afed21
Revert "Make sure sandbox stack grows upwards in big-endian libretro builds"
This reverts commit 58e6f71ef3.

The original behaviour was the correct one. The stack should always grow
downwards.
2025-05-10 23:37:39 -04:00
刘皓
68b2067a45
Remove void pointer arithmetic in binding-sandbox/binding-base.h 2025-05-10 20:44:46 -04:00
刘皓
58e6f71ef3
Make sure sandbox stack grows upwards in big-endian libretro builds 2025-05-10 19:23:43 -04:00
刘皓
3ba12e5672
Fix handling of big-endian platforms in libretro builds
In big-endian libretro builds, the WebAssembly memory is reversed, so no
byte-swapping is required to read from/write to WebAssembly memory
(which is little-endian).

However, that means the ways to get and set values in WebAssembly memory
are endianness-dependent, so I've added the correct such ways for
big-endian platforms.
2025-05-10 18:55:14 -04:00
刘皓
c5747f17c5
Fix minor issues in binding-sandbox/binding-base.h and binding-sandbox/binding-util.h 2025-05-10 09:25:43 -04:00
刘皓
8ec189e3d2
Remove always-false static_assert from binding-sandbox/binding-base.h
This is causing compilation errors with some compilers.
2025-05-09 23:28:17 -04:00
刘皓
b8d785b7e1
Implement growing VM memory in libretro builds
The binding coroutines in libretro builds are constructed on the VM
stack, so reallocating the VM memory would corrupt the memory of any
currently existing coroutines.

I've changed it so that the coroutines are no longer constructed on the
VM stack so that they're unaffected by VM memory reallocations, and
added a "slot" mechanism for storing variables on the VM stack. (Any
Ruby `VALUE`s used by a coroutine have to be stored on the VM stack so
that the Ruby garbage collector doesn't free them while they're being
used, which is why the slot mechanism is necessary.)
2025-05-09 22:49:13 -04:00
刘皓
13c0657691
Handle current working directory properly in libretro builds
Any relative paths that the game tries to access in libretro builds will
now be relative to whatever is the current working directory in the Ruby
sandbox, which will also now be initialized to the game directory during
initialization. Before, all of the bindings that took paths were
hardcoded to prepend the path with the game directory.
2025-05-06 16:55:28 -04:00
刘皓
09bcb1532c
Refactor out the SANDBOX_COROUTINE() macro
Removing this macro allows the correct line numbers to show up in GDB
stack traces.
2025-04-30 19:00:48 -04:00
刘皓
244d88908c
Remove unnecessary reserve() in binding-base.h 2025-04-20 13:40:49 -04:00
刘皓
5a5fcd26c5
Delete copy constructor for stack_frame_guard and stack_frame
The copy constructors are causing problems when the `fiber.stack` vector
gets reallocated when its capacity is full, since when vectors are
reallocated, the elements are moved (or copied if there's no usable move
constructor) to the reallocated memory and then the original elements
are destroyed.

This premature calling of destructors leads to double-free and
use-after-free errors.

I fixed it by deleting the copy constructors and explicitly defining
move constructors.
2025-04-16 21:39:04 -04:00
刘皓
8e9e7700f0
Fix stack pointer corruption in binding-base.h 2025-04-16 19:21:48 -04:00
刘皓
c7f35c96c9
Make sandbox-bindgen allocate varargs buffers on the stack
Not sure why, but this fixes crashes when calling variadic functions in
the Ruby API in libretro builds when Ruby is built without `-DNDEBUG`.
Maybe the previous way of calling varargs functions was undefined
behaviour somehow.
2025-04-14 21:04:14 -04:00
刘皓
c6dd227d54
Improve the stack frame guard implementation in libretro builds 2025-03-25 00:30:54 -04:00
刘皓
06f3c064b7
Make sure WebAssembly stack pointer is aligned in libretro builds 2025-03-22 19:33:16 -04:00
刘皓
eeef9ff943
Don't make sandbox_malloc into a coroutine
Okay, the coroutine implementation of `sandbox_malloc` is clearly
broken. It would be working if Asyncify instrumented the `memory.grow`
WebAssembly instruction, but it doesn't instrument it.

This commit reverts commit 42c4ff9497 and
also increases the default VM memory allocation from 64 MiB to 96 MiB to
account for the lack of ability to increase the memory allocation at run
time. I'll find some new way to implement increasing the memory
allocation later.
2025-03-18 13:49:41 -04:00
刘皓
42c4ff9497
Make sandbox_malloc into a coroutine
According to AddressSanitizer, when `sandbox_malloc` causes the
WebAssembly memory to grow in size, every single coroutine on the
sandbox stack gets corrupted. So if `sandbox_malloc` is going to cause
the memory to grow in size, we need to yield so that there are no
coroutines on the sandbox stack while the reallocation occurs.
2025-02-17 00:38:40 -05:00
刘皓
206e8508b6
Replace WABT's WebAssembly runtime with a custom implementation 2025-02-11 20:40:55 -05:00
刘皓
1c4d65e02e
Refactor the non-autogenerated parts of sandbox-bindgen into separate files 2025-02-05 01:11:23 -05:00