Skip to content

[mono][bug] mono_alc_from_gchandle return wrong value? #62052

Closed
@srxqds

Description

@srxqds

Description

I am embadding monovm, I write a function mono_alc_load_assembly_with_partial_name extend from mono_assembly_load_with_partial_name with alc argument

when call mono_image_open_from_data_alc to load MonoImage the mono_alc_from_gchandle get the wrong MonoAssemblyLoadContext value

Reproduction Steps

// I implemented AssemblyLoadContext:
public class PluginLoadContext : AssemblyLoadContext
  {
      
      public PluginLoadContext(string? name, bool isCollectible) : base(name, isCollectible)
      {
      }

      protected override Assembly? Load(AssemblyName assemblyName)
      {
          return PluginLoader.LoadAssembly(assemblyName.Name);
      }
  }
// like mono_assembly_load_with_partial_name just pass our ALC, not use default alc
MonoAssembly* 
mono_alc_load_assembly_with_partial_name (void* native_alc, const char *name, MonoImageOpenStatus *status)
{
	MonoAssembly *result;
	MONO_ENTER_GC_UNSAFE;
	MonoImageOpenStatus def_status;
	if (!status)
		status = &def_status;
	MonoAssemblyLoadContext* alc = (MonoAssemblyLoadContext*)native_alc;
	if (!alc)
		alc = mono_alc_get_default();
	result = mono_assembly_load_with_partial_name_internal (name, alc, status);
	MONO_EXIT_GC_UNSAFE;
	return result;
}

// get MonoAssemblyLoadContext though `_nativeAssemblyLoadContext` field
// the internal call
MonoObject* ScriptingManager::LoadAssembly(MonoObject* alc, MonoString* assemblyName)
{
	void* NativeALC = GetNativeALC(alc);
	if (!NativeALC)
		return nullptr;
	MonoImageOpenStatus LoadStatus = MonoImageOpenStatus::MONO_IMAGE_OK;
	const char* AssemblyName = mono_string_to_utf8(assemblyName);
	MonoAssembly* Assembly = mono_alc_load_assembly_with_partial_name(NativeALC, AssemblyName, &LoadStatus);
	mono_free((void*)AssemblyName);
	return (MonoObject*)mono_assembly_get_object(mono_domain_get(), Assembly);
}

our code call step

// in c#

PluginLoaderContent alc = new PluginLoaderContent ("Engine", true);
// call internal call ScriptingManager::LoadAssembly 
LoadAssembly(alc, "UnrealEngine");

image

the native stack variable

  1. new PluginLoaderContent create native ALC, and the alc native address is 0x000001c4bfdf51a0

image

  1. in the ScriptingManager::LoadAssembly, we can see the MonoObject* alc and native alc value is correct.
    image

  2. but in the mono_alc_from_gchandle the value is wong:
    image

  3. the full call statck:
    image

Expected behavior

work correctly

Actual behavior

crash

Regression?

No response

Known Workarounds

No response

Configuration

build the latest version of release/6.0 branch
x64 windows desktop

Other information

no

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions