Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebApplication in c# with V8ScriptEngine crashes on Mac x64 #483

Closed
Morpheus121 opened this issue Feb 1, 2023 · 11 comments
Closed

WebApplication in c# with V8ScriptEngine crashes on Mac x64 #483

Morpheus121 opened this issue Feb 1, 2023 · 11 comments
Assignees

Comments

@Morpheus121
Copy link

Hello,
I have a problem with V8 Script engine. Application crashes when I use WebApplication project type in C#.
I work on a Mac with Intel processor.
OS 12.6 Monterey.
IDE Rider 2022.3.1.
.NET 7.0.2

Used nuget packages:
Microsoft.ClearScript.V8 7.3.7
Microsoft.ClearScript.Core 7.3.7
Microsoft.ClearScript.V8.ICUData 7.3.7
Microsoft.ClearScript.V8.Native.osx-x64 7.3.7

When I create WebApplication from template in Rider and paste this example from your examples
using var engine = new V8ScriptEngine(); // expose a host type engine.AddHostType("Console", typeof(Console)); engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

The application crashes when a new object V8ScriptEngine is created with this error message in console:
Fatal error in , line 0
Check failed: 12 == (*__error()).
FailureMessage Object: 0x7ff7bb170820

When I create ConsoleApplication project type and paste example code, everything works.

@Morpheus121
Copy link
Author

I am attaching an example project with a non-working Web Application and a working Console Application
V8Tests.zip

@ClearScriptLib
Copy link
Collaborator

Hello @Morpheus121,

We can't reproduce your issue on macOS 13.0 x64:

image

Does your issue produce a crash report in the Console app?

Thanks!

@Morpheus121
Copy link
Author

Morpheus121 commented Feb 1, 2023

It does not produce a crash report.
All I have is
image

I tried to run it dotnet run on the project WebApplication and I have the same output as you. The problem occurs only when I debug WebApplication. I tried Debug WebApplication in Visual Studio and Rider with the same error

@ClearScriptLib
Copy link
Collaborator

The problem occurs only when I debug WebApplication.

Please share the exact steps that reproduce the crash. Thanks!

@Morpheus121
Copy link
Author

Steps to reproduce:
Open attached project in Visual Studio or Rider.
Choose WebApplication as target for Debug.
Place breakpoint on line using var engine = new V8ScriptEngine();
Run Debug
=> Debugger stops on this line
press go to next line button
=>Crash with error

@ClearScriptLib
Copy link
Collaborator

Open attached project in Visual Studio or Rider.

Just to be clear, are you talking about Visual Studio Code or Visual Studio for Mac?

@Morpheus121
Copy link
Author

Yes, it is Visual Studio for Mac

@ClearScriptLib ClearScriptLib changed the title WebbApplication in c# with V8ScriptEngine crashes on Mac x84 WebbApplication in c# with V8ScriptEngine crashes on Mac x64 Feb 2, 2023
@ClearScriptLib
Copy link
Collaborator

Hi @Morpheus121,

We've reproduced this with Visual Studio for Mac. Oddly, it only happens on macOS x64; Apple Silicon is unaffected.

It looks like V8 is crashing the process here. We're investigating.

Thanks!

@ClearScriptLib ClearScriptLib changed the title WebbApplication in c# with V8ScriptEngine crashes on Mac x64 WebApplication in c# with V8ScriptEngine crashes on Mac x64 Feb 2, 2023
@ClearScriptLib
Copy link
Collaborator

Hi @Morpheus121,

It turns out that we've seen this issue before, but only on Apple Silicon. Maybe something has changed in macOS, .NET, or Visual Studio since then, but now we can only reproduce it on x64.

Anyway, what's happening is that V8's call to mprotect is returning EACCES in the debugging environment. It has something to do with macOS's strict enforcement of just-in-time (JIT) compilation policies.

Our understanding is that JIT compilation is blocked by default but can be enabled via entitlements. Such policies are enforced at the application level, so ClearScript and other libraries can't work around them. However, we've found that libraries carrying a developer signature can bypass some enforcement. That appears intended to streamline developer workflows.

Given all that, we're aware of two ways to work around this issue.

First, if your application can detect that it's being debugged – via command-line flag, configuration setting, or some other means – it can use V8Settings.GlobalFlags to disable JIT compilation. Or, if JavaScript performance isn't critical during development, you can control JIT compilation based on your Debug/Release configuration.

Second, you can use the following procedure to apply a developer signature to your copy of ClearScript's V8 dynamic library:

  1. Locate your cached copy of ClearScriptV8.osx-x64.dylib. Run dotnet nuget locals global-packages --list to display your NuGet cache path. It should be something like ~/.nuget/packages/. On the command line, cd to that path, and then to ./microsoft.clearscript.v8.native.osx-x64/7.3.7/runtimes/osx-x64/native. The file should be there.

  2. Patch the file as follows:
    codesign --sign - --options linker-signed --force ClearScriptV8.osx-x64.dylib

  3. Clean, rebuild, and run your test project.

Please let us know if either workaround works for you.

Good luck!

@Morpheus121
Copy link
Author

Thanks for the workaround!
It works for me.

@ClearScriptLib
Copy link
Collaborator

Please reopen this issue if you have additional thoughts or findings related to this topic. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants