AZO234 / NP2kai

Neko Project II kai
http://domisan.sakura.ne.jp/article/np2kai/np2kai.html
MIT License
246 stars 61 forks source link

Savestates crashing the libretro core after a shutdown #138

Open eadmaster opened 3 years ago

eadmaster commented 3 years ago

When you load a savestate made in a previous session, the core instantly crashes.

jedenastka commented 3 years ago

The same problem happens with SDL (at least on Linux).

~ $ LANG= ~/gry/emulacja/emulatory/np2kai/bin/xnp21kai

(xnp21kai:6034): Gtk-WARNING **: 13:16:00.425: Unable to locate theme engine in module_path: "adwaita",
zsh: segmentation fault  LANG= ~/gry/emulacja/emulatory/np2kai/bin/xnp21kai

EDIT: I launched a debug version in GDB and I found out that this problem is caused by the line 1474 in sound/fmgen/fmgen_opna.cpp.

This line frees some memory that has something to do with rhythm samples.

Commenting out this line (and the line above it, because it's an "armless" if) "fixes" this bug, but then the program doesn't free the memory until the OS demands it back (when you close the program), so it's only a workaround. I'll try to inspect it further.

In the meantime, could you try this "fix" with libretro, so we are sure it's the same issue? If you don't want to compile it, let me know - I can provide you with the binary (at least if you are on 64bit Linux).

This can be related to #96, however that issue should be fixed long ago...

bslenul commented 2 years ago

Just learned about this issue on Reddit today, this is still happening with latest commit with the Libretro core (at least on Windows 10, haven't tried any other device/OS atm). No issue when loading a state during the same session, but as soon as you close content, then next time you'll boot the game again loading state will crash RetroArch.

GDB output (+ few last lines before the crash, just in case):

interrupt: num = 0x14, intrtype = external, errorp = off, error_code = 00000000
interrupt: num = 0x17, intrtype = external, errorp = off, error_code = 00000000
interrupt: num = 0x14, intrtype = external, errorp = off, error_code = 00000000
interrupt: numwarning: Invalid parameter passed to C runtime function.
warning: Invalid parameter passed to C runtime function.
warning: Invalid parameter passed to C runtime function.
warning: Invalid parameter passed to C runtime function.

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007ffbe9d95bb6 in ntdll!RtlGetCurrentServiceSessionId ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
(gdb) bt full
#0  0x00007ffbe9d95bb6 in ntdll!RtlGetCurrentServiceSessionId ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
No symbol table info available.
#1  0x00007ffbe9d947b1 in ntdll!RtlFreeHeap ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
No symbol table info available.
#2  0x00007ffbe9279c9c in msvcrt!free () from C:\WINDOWS\System32\msvcrt.dll
No symbol table info available.
#3  0x00007ffb3e99af28 in FM::OPNA::LoadRhythmSample (this=0x24a4f437b80,
    path=0xe7769ff450 "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\")
    at ../sound/fmgen/fmgen_opna.cpp:1476
        file = {_vptr.FileIO = 0x7ffb3ec56d20 <vtable for FileIO+16>,
          pfile = 0x7ffbe92efb20 <msvcrt!_iob+288>, flags = 3, lorigin = 0,
          error = 1116950416,
          path = "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\2608_BD.WAV", '\000' <repeats 209 times>}
        fsize = 9488
        buf = "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\2608_BD.WAV", '\000' <repeats 209 times>
        subchunkname = "data"
        whdr = {chunksize = 16, tag = 1, nch = 1, rate = 44100,
          avgbytes = 88200, align = 2, bps = 16, size = 24932}
        rhythmname = {
          0x7ffb3ec080ec <FM::OPNBase::SetPrescaler(unsigned int)::table2+1284> "BD",
          0x7ffb3ec080ef <FM::OPNBase::SetPrescaler(unsigned int)::table2+1287> "SD",
          0x7ffb3ec080f2 <FM::OPNBase::SetPrescaler(unsigned int)::table2+1290> "TOP", 0x7ffb3ec080f6 <FM::OPNBase::SetPrescaler(unsigned int)::table2+1294> "HH",
          0x7ffb3ec080f9 <FM::OPNBase::SetPrescaler(unsigned int)::table2+1297> "TOM",
          0x7ffb3ec080fd <FM::OPNBase::SetPrescaler(unsigned int)::table2+1301> "RIM"}
        i = 0
#4  0x00007ffb3e994785 in OPNA_LoadRhythmSample (OPNA=0x24a4f437b80,
    str=0xe7769ff450 "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\")
    at ../sound/fmgen/fmgen_fmgwrap.cpp:49
No locals.
#5  0x00007ffb3ea5ee8d in opna_sfload (opna=0x7ffb3f4ffa40 <g_opna>,
    sfh=0x24a4f02eca0, tbl=0x7ffb3ec09360 <np2tbl+1440>)
    at ../sound/opna.c:697
        path = "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\\000\231=▒>▒\177\000\000▒▒▒", '\000' <repeats 13 times>, "sound.rom\000m", '\000' <repeats 12 times>, "\017x▒O?▒\177\000\000▒▒▒v\000@\000\000 ▒▒BJ\002\000\000▒▒▒v▒\000\000\000mf▒>▒\177\000\000\000▒\f\000▒", '\000' <repeats 11 times>, "▒▒\002O\006\000\000\000\070\aP?▒\177\000\000\060▒▒v▒\000\000\000~\035▒>▒\177\000\000\177\033▒>▒\177\000\000▒▒▒>▒\177\000\000\070\000\000\200▒\177\000\000"...
        buf = 0x24a4f49f2d0
        ret = 0
#6  0x00007ffb3e9a6bc9 in flagload_fm (sfh=0x24a4f02eca0,
    tbl=0x7ffb3ec09360 <np2tbl+1440>) at ../statsave.c:1013
        ret = 0
        nSoundID = SOUNDID_PC_9801_86
        nSaveFlags = 130
        i = 0
#7  0x00007ffb3e9a7e2f in statsave_load_d (
    filename=0x7ffb3ebd10a0 <curpath> "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\temp_.sxx") at ../statsave.c:1713
        sffh = 0x24a4f02eca0
        ret = 0
        done = 0
        tbl = 0x7ffb3ec09360 <np2tbl+1440>
        tblterm = 0x7ffb3ec09500 <str_updated>
        i = 586
#8  0x00007ffb3eaded0f in retro_unserialize (data=0x24a53bce050,
    size=10747056) at ../sdl/libretro/libretro.c:1840
        ret = <optimized out>
        path = 0x7ffb3ebd10a0 <curpath> "G:\\msys64\\home\\B-S\\build\\system\\np2kai\\temp_.sxx"
        fh = 0x24a4f02ee20
#9  0x00007ff74cede87c in core_unserialize (info=0xe7769ff6d0)
    at runloop.c:8124
        runloop_st = 0x7ff74ded0600 <runloop_state>
#10 0x00007ff74cf027a5 in content_load_rastate1 (
    input=0x24a53bce050 "Neko Project II", size=10747080)
    at tasks/task_save.c:1065
        serial_info = {data_const = 0x24a53bce050, data = 0xa3fcc8,
          size = 10747056}
        block_size = 10747056
        stop = 0x24a5460dd08 ""
        marker = 0x24a53bce048 "MEM ▒▒▒"
        seen_core = false
        seen_cheevos = false
#11 0x00007ff74cf028e7 in content_deserialize_state (
    serialized_data=0x24a53bce040, serialized_size=10747080)
    at tasks/task_save.c:1117
        input = 0x24a53bce040 "RASTATE\001MEM ▒▒▒"
#12 0x00007ff74cf02d2f in content_load_state_cb (task=0x24a4f022670,
    task_data=0x24a4ee22f80, user_data=0x0, error=0x0)
    at tasks/task_save.c:1239
        i = 0
        ret = false
        load_data = 0x24a4ee22f80
        size = 10747080
        num_blocks = 0
        buf = 0x24a53bce040
        blocks = 0x0
        settings = 0x24a4297b3e0
        block_sram_overwrite = false
#13 0x00007ff74cef8711 in retro_task_internal_gather ()
    at libretro-common/queues/task_queue.c:179
        task = 0x24a4f022670
#14 0x00007ff74cef8d2d in retro_task_threaded_gather ()
    at libretro-common/queues/task_queue.c:410
        task = 0x0
#15 0x00007ff74cef9433 in task_queue_check ()
    at libretro-common/queues/task_queue.c:672
        current_threaded = true
        want_threaded = true
#16 0x00007ff74cec86f9 in rarch_main (argc=1, argv=0x24a42934f90, data=0x0)
    at retroarch.c:3826
        ret = 0
        app_exit = false
        p_rarch = 0x7ff74dec7060 <rarch_st>
        runloop_st = 0x7ff74ded0600 <runloop_state>
        video_st = 0x7ff74df49a40 <video_driver_st>
#17 0x00007ff74d0bfa5d in SDL_main (argc=1, argv=0x24a42934f90)
    at ui/drivers/ui_qt.cpp:4315
No locals.
#18 0x00007ff74d610eda in main_getcmdline ()
No symbol table info available.
#19 0x00007ff74cec13c1 in __tmainCRTStartup ()
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:321
        lock_free = <optimized out>
        fiberid = <optimized out>
        nested = <optimized out>
        lpszCommandLine = <optimized out>
        StartupInfo = {cb = 104, lpReserved = 0x24a40e559d0 "",
          lpDesktop = 0x24a40e432f0 "Winsta0\\Default",
          lpTitle = 0x24a40e54430 "G:\\msys64\\home\\B-S\\build\\retroarch_debug.exe", dwX = 0, dwY = 0, dwXSize = 0, dwYSize = 0, dwXCountChars = 0,
          dwYCountChars = 0, dwFillAttribute = 0, dwFlags = 0,
          wShowWindow = 0, cbReserved2 = 0, lpReserved2 = 0x0,
          hStdInput = 0xffffffffffffffff, hStdOutput = 0xffffffffffffffff,
          hStdError = 0xffffffffffffffff}
        inDoubleQuote = <optimized out>
#20 0x00007ff74cec14d6 in WinMainCRTStartup ()
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:176
        ret = 255

I tried what was mentioned in the previous comment, I commented these 2 lines: https://github.com/AZO234/NP2kai/blob/2b09ea6a589cdcae27bca27160b3f82638fbb45d/sound/fmgen/fmgen_opna.cpp#L1475-L1476

And indeed the state loaded properly.

kanjieater commented 2 years ago

I have the auto save on exit and auto load on start set in Retroarch - so unfortunately, with that setting you can't run your games more than once, due to this bug. I tried to temporarily work around it by setting retroarch\config\Neko Project II kai\Neko Project II kai.cfg global.savestate_auto_load = "false"

like I have with other cores on settings like: global.video_scale_integer = "false"

But unfortunately it's not working on the latest retroarch version & NP2Kai core.

So in the meantime I am disabling it everywhere in the global retroarch.cfg: savestate_auto_load = "false"

dhwz commented 1 year ago

@AZO234 any news on that issue, latest core version is still unusable on RetroArch as loading savestates crashes the core.

eadmaster commented 1 year ago

I've tested it again recently, and it was loading fine in Ubuntu Linux x64, while on Android is still crashing. So maybe it is something OS-related.

DReaper commented 1 year ago

I can confirm it is still crashing, but on Windows.

Unusually, I can use save states within the same game session. It's only after Retroarch closes that loading any past save states will crash.

YonKuma commented 1 year ago

Just want to confirm that the solution in @bslenul 's comment works. Commenting out those two lines causes save states to be loaded correctly.

AZO234 commented 1 year ago

I fixed rythum buffer to bslenul saied.