BizHawk icon indicating copy to clipboard operation
BizHawk copied to clipboard

[🐧] Lua in Mono megathread

Open YoshiRulz opened this issue 2 years ago • 35 comments

¯\_(ツ)_/¯

YoshiRulz avatar Oct 02 '21 23:10 YoshiRulz

On a fresh build of Bizhawk, upon opening the Lua editor, bizhawk crashes with terminal output

warning: File "/usr/bin/mono-sgen-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
	add-auto-load-safe-path /usr/bin/mono-sgen-gdb.py
line to your configuration file "/home/{my username}/.gdbinit".
To completely disable this security protection add
	set auto-load safe-path /
line to your configuration file "/home/will/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
	info "(gdb)Auto-loading safe path"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
0x00007f126e972a2f in wait4 () from /usr/lib/libc.so.6

and then a bunch of thread dumps.

I don't know if this is a configuration error on my part, but if it isn't, and you'd like more details, I can open a new issue.

Dsm0 avatar Oct 03 '21 19:10 Dsm0

The actual error message:

* Assertion: should not be reached at metadata.c:3041

Looks like this was fixed by mono/mono#19434, and all Mono releases from 6.12.0.151 are unaffected. ~~Unfortunately that version isn't available in the Manjaro repos so I'll have to wait.~~ I built from source (see below) and confirmed that the fix works.

~~I can't offer you a workaround beyond deleting your config every time you launch EmuHawk, as that seems to avoid the crash somehow.~~ edit: New, more helpful workaround: change Lua engine to "NLua+KopiLua". From 2.9 (https://github.com/TASEmulators/BizHawk/commit/41de03e31c2df9ea92f9f05df33a7e86d31eca7f) this will be the default on Linux.

YoshiRulz avatar Oct 03 '21 22:10 YoshiRulz

In #1430 was cited that the "ugliness" was kinda fixed in 62f90f1. I don't know how bad it was before, but i think it is still pretty bad:

printscreen

It is at a unusable point, texts inside the gradient in status bar, and also bright text in bright background are so hard, or impossible to read. I'm sorry if i missed some configuration step or if it's my computer messing things up, but i searched for a solution without success...

If it is something easily fixable, please tell me how. I am really glad someone is trying to make the software available in Linux, thank you even if it hasn't a solution yet.

imsamuka avatar Oct 21 '21 23:10 imsamuka

Fixing that should be as simple as adding more overrides to LinuxColorTable like in the commit you linked. Hit me up on Discord if you want help with that.

YoshiRulz avatar Oct 22 '21 08:10 YoshiRulz

About the crash on mono < 6.12.151, Is there any other workaround besides deleting the config every time? I tried building mono (6.12.0.158, the libgdiplus dependency on gtest is a problem, set -Wno-error=maybe-uninitialized in CXXFLAGS) and setting up a parallel environment for this, but the error persists, although instead of a plain crash it gives this (displayed in a dialog).


Using OpenTK 3 for host input (keyboard + gamepads)
Method '<Module>:_getFiberPtrId ()' in assembly '/home/magpie/Downloads/BizHawk-2.7-linux-x64/dll/lua51.dll' contains native code that cannot be executed by Mono on this platform. The assembly was probably created using C++/CLI.

System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
 ---> System.MissingMethodException: Method contains unsupported native code assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) <Module>._getFiberPtrId()
  at <Module>.<CrtImplementationDetails>.LanguageSupport._Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00024] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00028] in <01e64c95deda41b0ac7590707eabc864>:0 
   --- End of inner exception stack trace ---
  at <Module>.<CrtImplementationDetails>.ThrowModuleLoadException (System.String errorMessage, System.Exception innerException) [0x00007] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00041] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>..cctor () [0x00008] in <01e64c95deda41b0ac7590707eabc864>:0 
   --- End of inner exception stack trace ---
  at NLua.Lua.init () [0x00000] in <33fe4f6b9b274d9dab88987da2521403>:0 
  at NLua.Lua..ctor () [0x00027] in <33fe4f6b9b274d9dab88987da2521403>:0 
  at BizHawk.Client.EmuHawk.Win32LuaLibraries..ctor (BizHawk.Client.Common.LuaFileList scriptList, BizHawk.Client.Common.LuaFunctionList registeredFuncList, BizHawk.Emulation.Common.IEmulatorServiceProvider serviceProvider, BizHawk.Client.EmuHawk.MainForm mainForm, BizHawk.Client.Common.DisplayManagerBase displayManager, BizHawk.Client.Common.InputManager inputManager, BizHawk.Client.Common.Config config, BizHawk.Emulation.Common.IEmulator emulator, BizHawk.Emulation.Common.IGameInfo game) [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.LuaConsole.Restart () [0x0012f] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.ToolManager.Load[T] (System.Boolean focus, System.String toolPath) [0x00173] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.MainForm.OpenLuaConsole () [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.MainForm.LuaConsoleMenuItem_Click (System.Object sender, System.EventArgs e) [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at System.Windows.Forms.ToolStripItem.OnClick (System.EventArgs e) [0x00019] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripMenuItem.OnClick (System.EventArgs e) [0x00090] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripMenuItem.HandleClick (System.Int32 mouse_clicks, System.EventArgs e) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs e, System.Windows.Forms.ToolStripItemEventType met) [0x00054] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs,System.Windows.Forms.ToolStripItemEventType)
  at System.Windows.Forms.ToolStrip.OnMouseUp (System.Windows.Forms.MouseEventArgs mea) [0x00048] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripDropDown.OnMouseUp (System.Windows.Forms.MouseEventArgs mea) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00078] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x001b4] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStrip.WndProc (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripDropDown.WndProc (System.Windows.Forms.Message& m) [0x00017] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x0000b] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00085] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
BizHawk has completed its shutdown routines, killing process...

Seems more informative than the backtraces I was getting with the system-wide version of Mono, but hm. What can be done? It's so close to working (and it does indeed work when deleting config.ini) but only runs once before having to reconfigure all the emulator and keys, so it's not very useful, sadly. Do I need to build bizhawk with this Mono install for it to work? I don't mind that on principle, but it's going to be a giant amount of development packages just for one thing and that bothers me at a fundamental level (unless it's sure to work, then it's okay)

magpie514 avatar Nov 13 '21 14:11 magpie514

If you'd care to join me down the rabbit-hole, I've added Nix build scripts to the repo for both EmuHawk and the required version of Mono. That build is definitely immune from the crash. Unfortunately, I had to uninstall the older Mono and have broken my host OS such that I can't reinstall it, so I can't help look for another workaround.

YoshiRulz avatar Nov 17 '21 16:11 YoshiRulz

Nix, huh? I've been meaning to look into it, but won't have time until the weekend. That said, I'll give it a spin then, maybe I can set up a builder VM.

magpie514 avatar Nov 17 '21 16:11 magpie514

If you'd care to join me down the rabbit-hole, I've added Nix build scripts to the repo for both EmuHawk and the required version of Mono. That build is definitely immune from the crash.

The necessary mono versions (6.12.0.151+) aren't available yet in the ubuntu package repos either. Never used nix before now, but I took a stab at it.

After installing nix, I cloned the bizhawk repo, and ran nix-build in the repo's root directory. However after running for awhile it failed the build. Here's the full output from nix, wasn't sure which parts were relevant to bizhawk/mono, so figured I'd include everything. Every following build attempt shows this.

bigbass1997 avatar Nov 18 '21 05:11 bigbass1997

Hmm I thought it might be unstable because it includes /.git, but it's the same after I pushed a commit... guess that was just a typo on my part. You could also build the cloned copy of the source with --arg useCWDAsSource true which obviously will bypass the hash check.

YoshiRulz avatar Nov 18 '21 08:11 YoshiRulz

That seemed to get a little farther, but now there's another issue. CLI output here. And the full log mentioned in the error.

bigbass1997 avatar Nov 18 '21 09:11 bigbass1997

That's the same problem GitLab CI had on fresh clones, which I fixed by building BizHawk.Version first. I could add that to the Nix expression, or you could do it manually (in nix-shell to use the same SDK). But this is straying further off-topic, so if we could move to Discord that'd be appreciated.

edit: fixed in https://github.com/TASEmulators/BizHawk/commit/9448e56f0a8770c7631ff5be1c71246bb9c7a8c8

YoshiRulz avatar Nov 18 '21 10:11 YoshiRulz

That's the same problem GitLab CI had on fresh clones, which I fixed by building BizHawk.Version first. I could add that to the Nix expression, or you could do it manually (in nix-shell to use the same SDK). But this is straying further off-topic, so if we could move to Discord that'd be appreciated.

Please post the results of that conversation if something comes up, I'm interested in setting up in a couple days.

magpie514 avatar Nov 18 '21 17:11 magpie514

I succeeded in building it. Wow, Nix is really interesting, now I really want to learn more about it. image You can see it opens fine when a configuration is already existing in there. Thank you very much, YoshiRulz, this was a very functional and educative solution. How does this work when updating, though? Do I just run the same commands after a git pull?

magpie514 avatar Nov 20 '21 19:11 magpie514

By the config.json I assume you copied it out of the Nix store or something? I actually got it working in the store, just run emuhawk-monort-2.7. It should be available in $PATH if you run nix-env -f default.nix -iA emuhawk from the repo (or do it declaratively—I haven't looked into that yet). As for updating, pass --arg useCWDAsSource true to nix-build/nix-env to make a dev build from the checked-out commit. By default it will grab the 2.7 source tarball instead. The dev build can be "installed" to your profile alongside 2.7, but only one dev build at a time, as they are all version 2.7.1-local.

YoshiRulz avatar Nov 20 '21 20:11 YoshiRulz

Oh, I meant to say it ran (and exited) once, generated a config and then ran again fine, meaning the crash doesn't happen anymore with this method. My bad. This builds works perfectly as far as my basic tests of the Lua interface go. Thanks for the updating tips, I'll stick to this method as things go forward, until it can run with standard distro packages.

magpie514 avatar Nov 20 '21 20:11 magpie514

I've been trying to get this working with no success for a few days now.

I've tried on two seperate systems, one running Fedora and one running Arch.

System mono provides similar results to @magpie514, with the same mono regression (crash on subsequent runs of the Lua Console)

Building mono from source (from latest master) exhibits similar behavior, though it creates an unhandled exception instead: Mono exception output

Running it through Nix has been unsuccessful on both Arch and Fedora, which seem to be attached to Nix: LibGL/driver failures on Fedora, and build failures on Arch. I don't feel like fighting with Nix, at least not right now.

Please let me know if any additional information is necessary.

TauAkiou avatar Dec 05 '21 00:12 TauAkiou

I have a patch for debugging that NRE as another user had reported it (probably want to manually copy diff): https://gitlab.com/TASVideos/BizHawk/-/commit/8798cbcc7dc99b2270730fd4ff530380a268451d IIRC it will only ever print "... is null" in the case of a use-after-free, which the runtime shouldn't be doing in safe managed code.

I'm very interested in debugging Nix failures though. You can DM me the logs at YoshiRulz#4472 on Discord or wherever.

YoshiRulz avatar Dec 05 '21 02:12 YoshiRulz

it turns out that this is the same error that @magpie514 has, pretty much exactly.

This still happens on the latest git release & the latest nightly build from the official repos which is mono 6.13.0 and by most accounts should be a later version then the version of mono with this bug.

Even using the Preview build, which is 6.12.0.158 exhibits the same issue.

TauAkiou avatar Dec 05 '21 13:12 TauAkiou

@TauAkiou I solved that with the Nix method, but I honestly can't tell why it's failing for you. For whatever it's worth I did the process on Ubuntu and using the nVidia GL setup. I only ran into a minor issue, I had to use --arg useCWDAsSource true on the cloned repo for it to build right. Can you try that on Arch since that's the one that failed to build?

magpie514 avatar Dec 06 '21 20:12 magpie514

The issue with Nix on Arch is related to it not finding autoconf or libtool in the Nix environment, which more refers to a problem with setup on Arch or, more likely, me not knowing how Nix works particularly well and not setting it up properly.

What's more confusing, though is that it works perfectly fine on Fedora, but Fedora doesn't have OpenGL configured properly, and that's a separate issue.

I'm more confused as to why BizHawk is exhibiting the same problem despite being run on versions that shouldn't be vulnerable to the bug. My guess is that somehow, the metadata fixes either aren't working or aren't applied in these builds for some bizarre reason.

TauAkiou avatar Dec 07 '21 01:12 TauAkiou

On Arch, are you using nix-build --pure? Is Nix up-to-date?

On Fedora, pull (to https://github.com/TASEmulators/BizHawk/commit/54e7bf25425bdf1da308c92eddd61f0b70778ae3) and try again. I borrowed a Fedora machine and added the missing paths to the wrapper script.

edit: btw we resolved this (mostly)

YoshiRulz avatar Dec 07 '21 04:12 YoshiRulz

What is the intended value for the setting "Config -> Customize -> Advanced > Lua Core"? For me, "Lua + LuaInterface" was preselected, and it crashes. "NLua + KopiLua" doesn't crash.

raphaelr avatar Dec 31 '21 11:12 raphaelr

LuaInterface uses actual C Lua, while NLua reimplements it in C#.

vadosnaprimer avatar Dec 31 '21 12:12 vadosnaprimer

The intended value is the default of Lua + LuaInterface. I didn't change it for Mono because it doesn't crash on my machine, though I've now had two reports of crashes. https://github.com/TASEmulators/BizHawk/blob/b20fdb8e8580780b87964be46ff11dac878bef14/src/BizHawk.Client.Common/config/Config.cs#L144

YoshiRulz avatar Dec 31 '21 21:12 YoshiRulz

I've had issues with a script using luasockets (Specifically, it fails to find the library). Is that a known issue? Script in Question is here.

Still, thanks for getting this far already :D

N00byKing avatar Jan 05 '22 14:01 N00byKing

Apparently, BizHawk is having difficulty finding package.path and package.cpath on Debian distros, at least for lua-socket.

BizHawk's LUA console looks for it at usr/local/share/lua and usr/local/lib/lua however Ubuntu and similar distros put it in usr/share/lua and usr/lib/x86_64-linux-gnu/lua respectively.

This results, unsurprisingly, in failing to find the library (probably previous user's issue as well). I tried to include package.path and package.cpath directions as a quick workaround before calling , but unsurprisingly dynamic libraries are a no-go.

Other than that, since Ubuntu's repositories finally uploaded to use MONO >151, everything else seems to be working.

ShneekeyTheLost avatar May 26 '22 03:05 ShneekeyTheLost

Are you able to use EmuHawk's socket implementation comm.socket* instead?

YoshiRulz avatar May 26 '22 05:05 YoshiRulz

Are you able to use EmuHawk's socket implementation comm.socket* instead?

How would I go about doing that?

ShneekeyTheLost avatar May 26 '22 14:05 ShneekeyTheLost

Honestly not sure. I think the relevant functions are comm.socketServerSetIp, comm.socketServerIsConnected, comm.socketServerSetTimeout, and comm.socketServerResponse.

YoshiRulz avatar May 27 '22 06:05 YoshiRulz

Hello, I am running into an issue when adding the ironmon tracking script. The error I get is Ironmon-Tracker (Gen 3): v0.6.2c NLua.Exceptions.LuaScriptException: [string "main"]:81: 'popen' not supported I am running an Arch linux build and running bizhawk from source through Rider. I have also run it through the provided EmuHawkMono.sh script. I have verified that lua exists and runs on the system. The io.popen("cd"):read() command runs through the lua field. I would be happy to do more research or even debug through Rider, but I am not sure where exactly to start.

thechucklingatom avatar Sep 02 '22 22:09 thechucklingatom