Commit graph

51 commits

Author SHA1 Message Date
刘皓
1a275a1866
Properly set assumingRubyGC when deserializing bitmaps from libretro save states 2025-07-06 11:08:44 -04:00
刘皓
d1cdd55566
Avoid pointer arithmetic overflow in sandbox_ptr_unaligned in big-endian libretro builds 2025-07-05 19:33:18 -04:00
刘皓
e22b2c6d5a
Fix some bugs and safety issues in sandbox memory manipulation functions 2025-07-03 22:04:29 -04:00
刘皓
1824a0fc06
Change type of sb()->fiber_list elements from std::pair<key_t, struct fiber> to struct fiber 2025-07-03 13:21:29 -04:00
刘皓
7fa8b8ef16
Fix libretro save states created in the same frame one was loaded being corrupted 2025-07-03 12:39:29 -04:00
刘皓
d1a6d53892
Enforce deterministic iteration order for sandbox fibers
I've changed the sandbox fibers in libretro builds to be stored in a
linked list, with an unordered map mapping fiber keys to linked list
nodes for efficient lookup. The original implementation was just having
all the fibers in an unordered map.

The new implementation has the benefit that fibers are always iterated
in the same order on every platform, which allows save state creation to
be more deterministic.
2025-07-03 09:16:55 -04:00
刘皓
81cb43ef37
Prevent unaligned memory accesses in libretro builds
These are undefined behaviour. All memory accesses need to be aligned.
For unaligned memory accesses, we have to use memcpy.
2025-07-02 20:46:54 -04:00
刘皓
7f0d5b3960
Serialize which Ruby fiber is currently active in libretro save states 2025-07-02 18:24:02 -04:00
刘皓
2aa09e8529
Prevent std::memcpy from being called on null pointer in binding-sandbox/binding-base.h 2025-07-02 10:35:38 -04:00
刘皓
ef64eaa4e5
Implement reinitializing OpenGL objects in libretro builds
Libretro provides a way to detect when the OpenGL context is destroyed,
like on Android and possibly also iOS when switching between apps. This
commit implements reinitializing all OpenGL objects when this happens so
that the graphics continue to function in this case.
2025-06-10 16:30:44 -04:00
刘皓
6bddd82618
Change sandbox_str() to return a guard object instead of a pointer to a static string in big-endian libretro builds 2025-06-03 13:14:51 -04:00
刘皓
afb0748ffa
Use a priority deque to allocate object keys/WASI file descriptors 2025-06-03 10:59:30 -04:00
刘皓
175a210532
Implement deserializing libretro save states of the opposite endianness 2025-06-02 17:39:30 -04:00
刘皓
7980cd440a
Handle change watches when deserializing save states in libretro builds 2025-05-29 10:41:19 -04:00
刘皓
b664b3c8c7
Disconnect dispose watches before deserializing save states in libretro builds 2025-05-28 16:01:32 -04:00
刘皓
6f472fb732
Handle disposal when deserializing save states in libretro builds 2025-05-28 15:32:43 -04:00
刘皓
1f5d90822c
Remove designated initializers
Apparently these are not valid until C++20.
2025-05-27 21:28:29 -04:00
刘皓
8ca753d85d
Continue implementing save state deserialization in libretro builds 2025-05-27 16:59:41 -04:00
刘皓
d383889d4e
Start implementing save state deserialization in libretro builds 2025-05-25 16:07:40 -04:00
刘皓
7558f3fbcb
Add utilities for deserializing save states in libretro builds 2025-05-24 22:16:26 -04:00
刘皓
b1ba7e1606
Fix coroutine state being changed on read in libretro builds 2025-05-24 17:05:11 -04:00
刘皓
0b27c24bd5
Implement save state serialization for stack pointer and Asyncify state/data in libretro builds 2025-05-24 13:25:40 -04:00
刘皓
2896ec5abd
Implement save state serialization for C++ objects owned by the bindings in libretro builds 2025-05-22 12:49:01 -04:00
刘皓
39567dd0ea
Add fiber stack indices into libretro save state 2025-05-20 18:34:55 -04:00
刘皓
80b3833fff
Start implementing save state serialization in libretro builds 2025-05-20 17:42:35 -04:00
刘皓
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