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

Cyclic dependency throws System.AccessViolationException from V8 #181

Closed
MikeJohnsonDev opened this issue May 8, 2020 · 6 comments
Closed
Assignees

Comments

@MikeJohnsonDev
Copy link

MikeJohnsonDev commented May 8, 2020

Hi,

If I have a super class, such as:

import { MyClass1 } from "./myClass1";

export class MySuperClass
{
}

And a child class:

import { MySuperClass } from "./mySuperClass";

export class MyClass1 extends MySuperClass
{
}

And a global file to run:

import { MyClass1 } from "./myClass1";

function createMyClass1()
{
	return new MyClass1();
}

Global.createMyClass1 = createMyClass1;

With a .NET Core 3.1 Console Application:

using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using System;
using System.IO;

namespace ClearScriptTest
{
	public class Program
	{
		public static void Main(string[] args)
		{
			using (V8ScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDynamicModuleImports))
			{
				engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading;

				engine.Script.Global = engine.Script;

				string jsFile = File.ReadAllText($"{Environment.CurrentDirectory}\\js\\run.js");

				// Fix import to absolute path
				jsFile = jsFile.Replace("./myClass1",
										$"{Environment.CurrentDirectory.Replace('\\', '/')}" +
										"/js/myClass1.js");

				engine.Execute(new DocumentInfo()
				{
					Category = ModuleCategory.Standard
				}, jsFile);
			}

			Console.ReadKey(true);
		}
	}
}

When I run, I receive:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

And the stack trace:

 	[Managed to Native Transition]	
 	ClearScriptV8-64.dll!Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string gcCode, bool evaluate)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteRaw(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteInternal(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.Execute.AnonymousMethod__0()	Unknown
 	ClearScript.dll!Microsoft.ClearScript.ScriptEngine.ScriptInvokeInternal<object>(System.Func<object> func)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke.AnonymousMethod__0()	Unknown
 	ClearScriptV8-64.dll!<Module>.Microsoft.ClearScript.V8.?A0x9e62bf8d.LockCallback(void* pvArg)	Unknown
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	ClearScriptV8-64.dll!Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(System.Action gcAction)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke<object>(System.Func<object> func)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.Execute(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate)	Unknown
 	ClearScript.dll!Microsoft.ClearScript.ScriptEngine.Execute(Microsoft.ClearScript.DocumentInfo documentInfo, string code)	Unknown
>	ClearScriptTest.dll!ClearScriptTest.Program.Main(string[] args) Line 26	C#

Any help will be appreciated.

Thanks,
Mike

@MikeJohnsonDev
Copy link
Author

MikeJohnsonDev commented May 8, 2020

Here is a more complete stack trace:

>	v8-ia32.dll!v8::internal::ObjectHashTableBase<v8::internal::EphemeronHashTable,v8::internal::EphemeronHashTableShape>::Lookup(v8::internal::Handle<v8::internal::Object> key) Line 7617	C++
 	v8-ia32.dll!v8::internal::SyntheticModule::ResolveExport(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SyntheticModule> module, v8::internal::Handle<v8::internal::String> module_specifier, v8::internal::Handle<v8::internal::String> export_name, v8::internal::MessageLocation loc, bool must_resolve) Line 58	C++
 	v8-ia32.dll!v8::internal::Module::ResolveExport(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::internal::Handle<v8::internal::String> module_specifier, v8::internal::Handle<v8::internal::String> export_name, v8::internal::MessageLocation loc, bool must_resolve, v8::internal::Module::ResolveSet * resolve_set) Line 160	C++
 	v8-ia32.dll!v8::internal::SourceTextModule::ResolveImport(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SourceTextModule> module, v8::internal::Handle<v8::internal::String> name, int module_request, v8::internal::MessageLocation loc, bool must_resolve, v8::internal::Module::ResolveSet * resolve_set) Line 245	C++
 	v8-ia32.dll!v8::internal::SourceTextModule::FinishInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SourceTextModule> module, v8::internal::ZoneForwardList<v8::internal::Handle<v8::internal::SourceTextModule>> * stack, unsigned int * dfs_index, v8::internal::Zone * zone) Line 471	C++
 	v8-ia32.dll!v8::internal::Module::FinishInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::internal::ZoneForwardList<v8::internal::Handle<v8::internal::SourceTextModule>> * stack, unsigned int * dfs_index, v8::internal::Zone * zone) Line 229	C++
 	v8-ia32.dll!v8::internal::SourceTextModule::FinishInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SourceTextModule> module, v8::internal::ZoneForwardList<v8::internal::Handle<v8::internal::SourceTextModule>> * stack, unsigned int * dfs_index, v8::internal::Zone * zone) Line 435	C++
 	[Inline Frame] v8-ia32.dll!v8::internal::Module::FinishInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::internal::ZoneForwardList<v8::internal::Handle<v8::internal::SourceTextModule>> * stack, unsigned int * dfs_index, v8::internal::Zone * zone) Line 229	C++
 	v8-ia32.dll!v8::internal::Module::Instantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 191	C++
 	v8-ia32.dll!v8::Module::InstantiateModule(v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 2328	C++
 	ClearScriptV8-32.dll!V8ContextImpl::ResolveModule(v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> __formal) Line 1338	C++
 	ClearScriptV8-32.dll!V8IsolateImpl::ResolveModule(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> hReferrer) Line 1187	C++
 	ClearScriptV8-32.dll!V8IsolateImpl::ModuleResolveCallback(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> hReferrer) Line 1147	C++
 	v8-ia32.dll!v8::internal::SourceTextModule::PrepareInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SourceTextModule> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 318	C++
 	[Inline Frame] v8-ia32.dll!v8::internal::Module::PrepareInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 212	C++
 	v8-ia32.dll!v8::internal::Module::Instantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 183	C++
 	v8-ia32.dll!v8::Module::InstantiateModule(v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 2328	C++
 	ClearScriptV8-32.dll!V8ContextImpl::ResolveModule(v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> __formal) Line 1338	C++
 	ClearScriptV8-32.dll!V8IsolateImpl::ResolveModule(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> hReferrer) Line 1187	C++
 	ClearScriptV8-32.dll!V8IsolateImpl::ModuleResolveCallback(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> hReferrer) Line 1147	C++
 	v8-ia32.dll!v8::internal::SourceTextModule::PrepareInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::SourceTextModule> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 318	C++
 	[Inline Frame] v8-ia32.dll!v8::internal::Module::PrepareInstantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 212	C++
 	v8-ia32.dll!v8::internal::Module::Instantiate(v8::internal::Isolate * isolate, v8::internal::Handle<v8::internal::Module> module, v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 183	C++
 	v8-ia32.dll!v8::Module::InstantiateModule(v8::Local<v8::Context> context, v8::MaybeLocal<v8::Module>(*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>) callback) Line 2328	C++
 	ClearScriptV8-32.dll!V8ContextImpl::Execute(const V8DocumentInfo & documentInfo, const StdString & code, bool evaluate) Line 485	C++
 	[Managed to Native Transition]	
 	ClearScriptV8-32.dll!Microsoft::ClearScript::V8::V8ContextProxyImpl::Execute(Microsoft::ClearScript::UniqueDocumentInfo^ documentInfo, System::String^ gcCode, bool evaluate) Line 149	C++
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteRaw(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate) Line 1354	C#
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteInternal(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate) Line 1037	C#
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.Execute.AnonymousMethod__0() Line 1326	C#
 	ClearScript.dll!Microsoft.ClearScript.ScriptEngine.ScriptInvokeInternal<object>(System.Func<object> func) Line 1569	C#
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke.AnonymousMethod__0() Line 1387	C#
 	ClearScriptV8-32.dll!Microsoft::ClearScript::V8::LockCallback(void* pvArg) Line 17	C++
 	[Native to Managed Transition]	
 	ClearScriptV8-32.dll!V8ContextImpl::CallWithLock(void(*)(void *) pCallback, void * pvArg) Line 390	C++
 	[Managed to Native Transition]	
 	ClearScriptV8-32.dll!Microsoft::ClearScript::V8::V8ContextProxyImpl::InvokeWithLock(System::Action^ gcAction) Line 93	C++
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke<object>(System.Func<object> func) Line 1387	C#
 	ClearScript.dll!Microsoft.ClearScript.V8.V8ScriptEngine.Execute(Microsoft.ClearScript.UniqueDocumentInfo documentInfo, string code, bool evaluate) Line 1312	C#
 	ClearScript.dll!Microsoft.ClearScript.ScriptEngine.Execute(Microsoft.ClearScript.DocumentInfo documentInfo, string code) Line 1023	C#
 	ClearScriptConsole.dll!Microsoft.ClearScript.Test.Program.Main(string[] args) Line 25	C#
 	[Native to Managed Transition]	
 	hostpolicy.dll!53e446e6()	Unknown
 	hostpolicy.dll![Frames below may be incorrect and/or missing, no symbols loaded for hostpolicy.dll]	Unknown
 	hostpolicy.dll!53e52e1d()	Unknown
 	hostpolicy.dll!53e7147b()	Unknown
 	hostpolicy.dll!53e5259f()	Unknown
 	hostfxr.dll!53edd129()	Unknown
 	hostfxr.dll!53edfa56()	Unknown
 	hostfxr.dll!53edeafe()	Unknown
 	hostfxr.dll!53edcd65()	Unknown
 	hostfxr.dll!53ed93d6()	Unknown
 	ClearScriptConsole.exe!00e9f4ce()	Unknown
 	ClearScriptConsole.exe!00e9f71e()	Unknown
 	ClearScriptConsole.exe!00ea0f6c()	Unknown
 	kernel32.dll!75896359()	Unknown
 	ntdll.dll!77127c24()	Unknown
 	ntdll.dll!77127bf4()	Unknown

And a more correct exception:

Exception thrown at 0x78599A16 (v8-ia32.dll) in ClearScriptConsole.exe: 0xC0000005: Access violation reading location 0x0000000F.

@ClearScriptLib
Copy link
Collaborator

Hi @DarkfireHellspawn,

Thanks for reporting the crash. We're investigating.

By the way, instead of loading and patching the file, you can simply do this:

engine.ExecuteDocument("js/run.js", ModuleCategory.Standard);

Cheers!

@ClearScriptLib ClearScriptLib self-assigned this May 8, 2020
@ClearScriptLib
Copy link
Collaborator

The next ClearScript release will include a fix for this issue. Thanks again!

@MikeJohnsonDev
Copy link
Author

Hi,

Thanks for the fix!

Any idea when it will be available?

Thanks,
Mike

@ClearScriptLib
Copy link
Collaborator

Hi Mike,

It looks like V8 8.3 is scheduled to go stable in a few days. Hopefully we can get a release out about a week after that.

Thanks!

ClearScriptLib added a commit that referenced this issue May 29, 2020
…; added JavaScriptExtensions.ToTask (GitHub Issue #182); added DocumentLoader.MaxCacheSize and DocumentCategory.MaxCacheSize; added code to break event connections on engine disposal (GitHub Issue #183); improved ES6 module support, fixing cycle crash (GitHub Issue #181); added DynamicHostObject (GitHub Issue #180); added BigInt / BigInteger support for V8 (GitHub Issue #176); hardened Assembly.Load call in V8Proxy.cs (GitHub Issue #175); improved V8Update environment isolation to fix some V8 build issues (GitHub Issue #185); updated API documentation. Tested with V8 8.3.110.9.
@ClearScriptLib
Copy link
Collaborator

Fixed in Version 6.0.2.

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