Commit graph

228 commits

Author SHA1 Message Date
刘皓
b55f6dd1f4
Add more error checking to sandbox bindings
* Added checks for when an incorrect number of arguments is passed to a
  function
* Added checks for when an argument of object type has mismatching type
  (arguments of numeric and string types were already checked for type
  mismatches before this commit)
* Added checks for trying to read certain properties of a disposed
  object
2025-08-18 14:04:09 -04:00
刘皓
0307f2ed18
Explicitly cast integers before passing to log_printf in wasm-rt.cpp 2025-08-18 11:13:53 -04:00
刘皓
643a6bc14f
Properly catch exceptions in Graphics.update in libretro builds 2025-08-18 11:02:54 -04:00
刘皓
dc741f455f
Fix crash when performing zero-length vectored file operations in libretro builds 2025-08-13 09:49:54 -04:00
刘皓
19fe3f955d
Add bounds checking to operations involving arrays in sandbox memory 2025-08-10 11:27:39 -04:00
刘皓
c6e33a9ff1
Move sandbox coroutine destructors into an end() method
Follow-up to 9b3240f7b3.

This allows us to choose whether or not to run the destructor when
destroying the coroutines in a way that doesn't cause memory leaks.
2025-07-25 21:21:29 -04:00
刘皓
03a26a941c
Fix memory leak in stack frame destructors in libretro builds
We need to call `delete` on the coroutines to actually deallocate their
memory. Calling `->~T()` only runs the destructor without deallocating
the memory afterwards.
2025-07-25 20:40:39 -04:00
刘皓
9b3240f7b3
Revert "Don't run stack frame destructors when deserializing libretro save states"
This reverts commit 33153783e8.

Not calling stack frame destructors would leak their memory, silly.
2025-07-25 20:38:20 -04:00
刘皓
fa05f440af
Only allow disposable objects to be marked as disposed in libretro save states 2025-07-25 11:51:58 -04:00
刘皓
33153783e8
Don't run stack frame destructors when deserializing libretro save states 2025-07-25 11:22:31 -04:00
刘皓
4ada800de3
Refactor libretro core initialization to not need RETRO_SERIALIZATION_QUIRK_MUST_INITIALIZE
This commit moves some of the initialization around so that the core can
handle save states immediately after initialization instead of needing
to run for one frame before save states will work.
2025-07-23 13:47:58 -04:00
刘皓
8fed0ee847
Properly handle RETRO_ENVIRONMENT_GET_USERNAME returning null in libretro builds 2025-07-14 22:32:00 -04:00
刘皓
accb5948ea
Update libretro-common and add Irish language in binding-sandbox.cpp 2025-07-13 20:59:33 -04:00
刘皓
eb8794874e
Don't reallocate memory in wasm_rt_replace_memory unless the memory capacity has changed 2025-07-06 13:38:20 -04:00
刘皓
f98d868336
Implement Kernel.save_data in libretro builds 2025-07-06 11:48:11 -04:00
刘皓
5dc71864bb
Implement optional second argument for Kernel.load_data in libretro builds 2025-07-06 11:43:24 -04:00
刘皓
1a275a1866
Properly set assumingRubyGC when deserializing bitmaps from libretro save states 2025-07-06 11:08:44 -04:00
刘皓
e587768f9c
Fix WASI file descriptor leak during libretro save state deserialization 2025-07-06 10:53:02 -04:00
刘皓
d1cdd55566
Avoid pointer arithmetic overflow in sandbox_ptr_unaligned in big-endian libretro builds 2025-07-05 19:33:18 -04:00
刘皓
ca5cfcbca9
Fix some more bugs in sandbox memory manipulation functions 2025-07-03 22:20:56 -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
刘皓
29c0621771
Call Font::initDefaultDynAttribs() before initializing shared state in libretro builds 2025-07-02 14:38:09 -04:00
刘皓
00c688309b
Fix edge case in pointer unswizzling algorithm for libretro save state serialization
If there are two Ruby objects, A and B, that are both no longer used
(i.e. they're able to be freed by the Ruby garbage collector), and A
references B, then it's possible for the Ruby garbage collector to free
B but not also free A in the same garbage collection cycle.

The pointer unswizzling algorithm currently crashes when the garbage
collector does this because the object A will still be in
`sb()->objects` but B will not be, and so save state serialization will
still try to serialize A. When the pointer unswizzling algorithm tries
to serialize all the other objects A references and cannot find the
referenced object B, the program crashes.
2025-07-02 13:32:06 -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
刘皓
880fdeca43
Fix bug in PCG implementation in wasi.cpp 2025-07-02 09:10:19 -04:00
刘皓
0d18b9e422
Handle initialize being called multiple times in binding-sandbox/font-binding.cpp 2025-07-01 15:14:41 -04:00
刘皓
529c0cd905
Fix bug in initialize_copy in binding-sandbox/font-binding.cpp 2025-07-01 11:24:16 -04:00
刘皓
84d5851aec
Serialize WASI file descriptor positions in libretro save states 2025-06-28 11:43:18 -04:00
刘皓
8858f9dd90
Remove unnecessary slot from libretro Graphics.update binding 2025-06-12 17:57:13 -04:00
刘皓
e6404179ea
Fix a small bug in the previous commit 2025-06-11 16:07:48 -04:00
刘皓
ee31cccb81
Serialize PRNG state in libretro save states
Save states in libretro builds now also contain the state of the
pseudorandom number generator used to implement the WASI `random_get`
function for better save state determinism.

I've also changed the PRNG from MT19937 to PCG.
2025-06-11 15:54:21 -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
刘皓
41557aba6e
Add a log message when loading libretro save state made with incompatible mkxp-z version 2025-06-04 11:32:32 -04:00
刘皓
0ca9e7ef3d
Don't include .UTF-8 at the ends of locale names in libretro builds
This is for consistency with standalone builds, which report locale
names without the encoding and with an underscore between the language
and region, e.g. "en_US".
2025-06-04 09:29:38 -04:00
刘皓
c61ef1cb60
Fix typo in sandbox bindings: "default_font_family" should be "default_font_family=" 2025-06-03 23:14:49 -04:00
刘皓
6092f4a6fc
Change exception_raise in sandbox bindings to take const Exception & instead of Exception & as argument 2025-06-03 23:11:06 -04:00
刘皓
b7b5e23e18
Implement system bindings in libretro builds 2025-06-03 23:06:05 -04:00
刘皓
784e62d1c5
Cast struct sandbox_str_guard to const char * before passing to variadic functions 2025-06-03 14:26:48 -04:00
刘皓
a71746bca0
Revert "Remove WASI_DEBUG macro"
This reverts commit d62d6aef3e.

Now that `sandbox_str()` is safe to use, I should put this macro back
in.
2025-06-03 13:30:22 -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
刘皓
addf927922
Merge branch 'misc-fixes' into libretro 2025-06-03 11:36:20 -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
刘皓
3c244cc7ea
Refactor libretro save state serialization to not require extra objects 2025-05-31 19:06:57 -04:00
刘皓
bb4dd64d8d
Add some safety checks to libretro save state deserialization 2025-05-31 16:34:15 -04:00