Taritsyn / JavaScriptEngineSwitcher

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines. This library allows you to quickly and easily switch to using of another JavaScript engine.
Apache License 2.0
446 stars 49 forks source link

System.AccessViolationException: Attempted to read or write protected memory. #87

Closed xtroncode closed 1 year ago

xtroncode commented 4 years ago

Hi, I am using ReactJS.net (v5.2.5) with JavaScriptEngineSwitcher.ChakraCore (v3.7.1) in .netcore 3.1 and I am getting the following exception when there is an error in my script.

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.NativeMethods.JsGetAndClearExceptionWithMetadata(JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue ByRef)
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.NativeMethods.JsGetAndClearExceptionWithMetadata(JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue ByRef)
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorCode)
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsContext.RunScript(System.String, JavaScriptEngineSwitcher.ChakraCore.JsRt.JsSourceContext, System.String, JavaScriptEngineSwitcher.ChakraCore.JsRt.JsParseScriptAttributes ByRef)
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine+<>c__DisplayClass21_0.<InnerEvaluate>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher+ScriptTaskWithResult`1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Run()
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.StartThread()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

I tried using JavaScriptEngineSwitcher.V8 and it works with V8 and proper js error is reported. I looked at other issues related to the exception but most of them are for older versions and seem to have been fixed. Let me know if should move this issue to ReactJS.net.

Thanks.

Taritsyn commented 4 years ago

Hello, Meet!

Very similar to the old issue (“System.AccessViolationException in JavascriptEngineSwitcher.ChakraCore”). This problem is most likely caused by an error in the native assembly.

Try rolling back the JavaScriptEngineSwitcher.ChakraCore and JavaScriptEngineSwitcher.ChakraCore.Native.* packages to version 3.5.6.

I tried using JavaScriptEngineSwitcher.V8 and it works with V8 and proper js error is reported.

Could you show the full error message when using the JavaScriptEngineSwitcher.V8 module.

xtroncode commented 4 years ago

Thanks for the promt reply !

I have the same issue on v3.5.6

Here's the error reported when I use JavaScriptEngineSwitcher.V8

ERROR [46] ReactServer ? - TypeError: Cannot read property 'heading' of undefined
   at MileageHeader (main.js:189926:25) ->             throw err;
   at processChild (main.js:58907:14)
   at resolve (main.js:58830:5)
   at render (main.js:59253:22)
   at read (main.js:59212:29)
   at renderToString (main.js:59771:27)
   at Script [5]:1:16 - JavaScriptEngineSwitcher.Core.JsRuntimeException: TypeError: Cannot read property 'heading' of undefined
   at MileageHeader (main.js:189926:25) ->             throw err;
   at processChild (main.js:58907:14)
   at resolve (main.js:58830:5)
   at render (main.js:59253:22)
   at read (main.js:59212:29)
   at renderToString (main.js:59771:27)
   at Script [5]:1:16

Engine name: V8JsEngine
Engine version: 8.3.110.9
Category: Runtime error
Description: Cannot read property 'heading' of undefined
Type: TypeError
Document name: main.js
Line number: 189926
Column number: 25
Source fragment:             throw err;
Call stack:
   at MileageHeader (main.js:189926:25)
   at processChild (main.js:58907:14)
   at resolve (main.js:58830:5)
   at render (main.js:59253:22)
   at read (main.js:59212:29)
   at renderToString (main.js:59771:27)
   at Script [5]:1:16 ---> Microsoft.ClearScript.ScriptEngineException: TypeError: Cannot read property 'heading' of undefined
   at V8Exception.ThrowScriptEngineException(V8Exception* )
   at Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(UniqueDocumentInfo documentInfo, String gcCode, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteRaw(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteInternal(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass100_0.<Execute>b__0()
   at Microsoft.ClearScript.ScriptEngine.ScriptInvokeInternal[T](Func`1 func)
   at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__DisplayClass106_0`1.<ScriptInvoke>b__0()
   at Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[T](Func`1 func)
   at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.ScriptEngine.Evaluate(UniqueDocumentInfo documentInfo, String code, Boolean marshalResult)
   at Microsoft.ClearScript.ScriptEngine.Evaluate(DocumentInfo documentInfo, String code)
   at Microsoft.ClearScript.ScriptEngine.Evaluate(String documentName, Boolean discard, String code)
   at JavaScriptEngineSwitcher.V8.V8JsEngine.InnerEvaluate(String expression, String documentName)
   --- Script error details follow ---
   TypeError: Cannot read property 'heading' of undefined
       at MileageHeader (main.js:189926:25) ->             throw err;
       at processChild (main.js:58907:14)
       at resolve (main.js:58830:5)
       at render (main.js:59253:22)
       at read (main.js:59212:29)
       at renderToString (main.js:59771:27)
       at Script [5]:1:16
   at JavaScriptEngineSwitcher.V8.V8JsEngine.InnerEvaluate(String expression, String documentName)
   at JavaScriptEngineSwitcher.V8.V8JsEngine.InnerEvaluate[T](String expression, String documentName)
   at JavaScriptEngineSwitcher.V8.V8JsEngine.InnerEvaluate[T](String expression)
   at JavaScriptEngineSwitcher.Core.JsEngineBase.Evaluate[T](String expression)
   at JSPool.PooledJsEngine.Evaluate[T](String expression)
   at React.ReactEnvironment.Execute[T](String code)
   at React.ReactComponent.RenderHtml(TextWriter writer, Boolean renderContainerOnly, Boolean renderServerOnly, Action`3 exceptionHandler, IRenderFunctions renderFunctions)

In case of ChakraCore the exception does not reach the exception handler passed to ReactJS.net, the error is just logged to console and the application exits (running in debug mode on VSCode).

Also this is the log for ChakraCore when run Release mode on linux

ERROR [4] ReactServer ? - A fatal exception has occurred in a JavaScript runtime - JavaScriptEngineSwitcher.Core.JsFatalException: A fatal exception has occurred in a JavaScript runtime

Engine name: ChakraCoreJsEngine
Engine version: 1.11.21
Category: Fatal error
Description: A fatal exception has occurred in a JavaScript runtime ---> JavaScriptEngineSwitcher.ChakraCore.JsRt.JsFatalException: A fatal exception has occurred in a JavaScript runtime
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JsErrorCode errorCode)
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsContext.RunScript(String script, JsSourceContext sourceContext, String sourceUrl, JsParseScriptAttributes& parseAttributes)
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass21_0.<InnerEvaluate>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass21_0.<InnerEvaluate>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.ScriptTaskWithResult`1.Run()
--- End of stack trace from previous location where exception was thrown ---
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.Invoke[T](Func`1 func)
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.InnerEvaluate[T](String expression, String documentName)
   at React.ReactComponent.RenderHtml(TextWriter writer, Boolean renderContainerOnly, Boolean renderServerOnly, Action`3 exceptionHandler, IRenderFunctions renderFunctions)

Thanks.

Taritsyn commented 4 years ago

Try making a demo project that reproduces this error and send me a link to it.

xtroncode commented 4 years ago

Hi Taritsyn,

I was able to reproduce the issue here jsengineswitcher-chakracore-issue The application breaks with System.AccessViolationException as soon as the home page is hit.

I was able to reproduce the issue by setting devtools: false in the webpack configuration, removing it resolves the issue. Same is the case with my project in which I first faced this issue.

Please let me know if more info is needed.

Thanks.

Taritsyn commented 4 years ago

Hello, Meet!

Thank you for the provided demo project!

This problem is related to the pre-compilation of scripts. You can disable a pre-compilation of scripts in the Startup.cs file:

public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
    // Initialise ReactJS.NET. Must be before static files.
    app.UseReact(config =>
    {
        config
            …
            .SetAllowJavaScriptPrecompilation(false)
            …

An error occurs in the JsGetAndClearExceptionWithMetadata method of ChakraCore library when the value is returned from the ChakraCorePrecompiledScript.LoadScriptSourceCode method of JavaScriptEngineSwitcher.ChakraCore module. I cannot say anything about the reasons for the error yet.

When I have free time, I will deal with this problem.