Commit graph

38 commits

Author SHA1 Message Date
刘皓
0b27c24bd5
Implement save state serialization for stack pointer and Asyncify state/data in libretro builds 2025-05-24 13:25:40 -04:00
刘皓
f9aeef915f
Fix type of mkxp_sandbox_cwd in libretro/ruby-bindings.h 2025-05-24 10:11:21 -04:00
刘皓
80b3833fff
Start implementing save state serialization in libretro builds 2025-05-20 17:42:35 -04:00
刘皓
134e08ce6a
Allow the entire codebase to build with -fno-exceptions -fno-rtti in libretro builds 2025-05-16 21:59:54 -04:00
刘皓
28fb36af98
Capitalize /Dist, /Game, /Save and /System in libretro builds for consistency 2025-05-13 17:53:59 -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
刘皓
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
刘皓
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
刘皓
3e3df818aa
Use C getcwd() instead of Ruby Dir.pwd() to get CWD in libretro builds 2025-05-06 18:02:03 -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
刘皓
41fc9bf7c7
Pin wasm2c version in libretro builds
To prevent incompatibilities between wasm2c and the custom wasm-rt
implementation.
2025-05-01 00:53:26 -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
刘皓
0d07aff3e2
Remove exceptions from the sandbox implementation in libretro builds 2025-04-25 13:31:32 -04:00
刘皓
52d65cfe96
Decrease Ruby stack size in libretro builds from 16 MiB to 2 MiB
I highly doubt any game needs that much stack space.
2025-04-21 13:05:17 -04:00
刘皓
46bd055c0a
Fix memory leak in cont_restore_thread in Ruby in libretro builds 2025-04-21 11:16:21 -04:00
刘皓
23affbe96f
Remove unnecessary cleanup code from rb_ec_tag_jump() 2025-04-20 14:21:41 -04:00
刘皓
f00cfdbcf5
Rework the patches for Ruby WASI memory leaks 2025-04-20 11:56:50 -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
刘皓
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
刘皓
911cfc29f5
Fix sandbox-bindgen.rb not generating bindings for functions that take no arguments 2025-04-13 22:48:58 -04:00
刘皓
35455e17dc
Apply patches from https://github.com/mkxp-z/ruby in libretro builds 2025-04-10 14:00:32 -04:00
刘皓
12f12d91a1
Apply patch from ruby/ruby#12995 in libretro builds 2025-03-31 12:28:30 -04:00
刘皓
6f6d0044ff
Decrease size of Ruby in libretro builds
This decreases the size of the libretro core by around 10% without
having a discernable effect on game compatibility.
2025-03-27 22:00:00 -04:00
刘皓
941ee73f1d
Don't set saved_ec.tag to NULL in cont_init() 2025-03-27 11:25:48 -04:00
刘皓
fac72be163
Fix a second Ruby memory leak
Okay, I think that fixes all the memory leaks. Finally! I'll submit a
pull request to Ruby to fix both of the memory leaks.
2025-03-27 00:28:37 -04:00
刘皓
82444abed2
Fix stack pointer leak in libretro builds
There's currently a memory leak in libretro builds where memory usage
increases by several kilobytes every frame you hold down an arrow key
while the player character is visible on the map in KNight-Blade.

This fixes a bug where parts of the WebAssembly stack are leaked when an
exception is caught in Ruby. There seems to be another source of memory
leaking, though, because memory usage still increases by several
kilobytes every frame like before.

I'm pretty sure this bug I fixed is an internal Ruby bug that I should
be creating a pull request to fix. I'll create one once I fully fix the
memory leak, since there might be more Ruby bugs that I have yet to
find.
2025-03-25 16:19:08 -04:00
刘皓
322d61b604
Allow adjusting Ruby GC parameters in libretro builds
There seems to be a memory leak somewhere. I'm tweaking the GC
parameters to be more conservative to help track down the leak.
2025-03-21 11:56:45 -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
刘皓
359203805c
Fix typo in libretro/Makefile 2025-03-03 18:45:31 -05:00
刘皓
20f806abef
Remove dependency on xxd 2025-02-27 23:32:30 -05:00
刘皓
ba68b8fa5b
Move libretro headers and SDL headers to meson.build 2025-02-27 20:39:17 -05:00
刘皓
5124baf23e
Retrieve RetroArch Nintendo Switch patch from GitHub 2025-02-24 01:08:00 -05:00
刘皓
9ed11ecffa
Use libretro's PlayStation 3 Docker image instead of ScummVM's 2025-02-22 16:04:51 -05:00
刘皓
71fa2453e8
Fix libretro compilation error when using GCC < 8.1.0 2025-02-21 17:56:32 -05: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
刘皓
7c066a2b24
Change some of the nomenclature in libretro builds 2025-02-14 12:05:30 -05:00