Skip to content

AssemblyLoadContext incorrectly validates name of dynamic assembly when returned from Load #53181

Closed

Description

@vitek-karas

AssemblyLoadContext validates that the assembly returned from Load has the same name as the name which was asked for. This validation incorrectly handles dynamic assemblies and will always fail for them.

This is the cause because ValidateAssemblyNameWithSimpleName simply casts the returned assembly to RuntimeAssembly:

RuntimeAssembly? rtLoadedAssembly = assembly as RuntimeAssembly;

But this doesn't work for assemblies created by AssemblyBuilder.

The code should call GetRuntimeAssembly instead, which handles this case correctly.

Full repro for both affected public APIs (Load and Resolving) is here:
https://gist.github.com/vitek-karas/3c8f938dc15730ad00a375d8d745f36a

This is basically a derivative issue from the bug fix in #49387.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on May 24, 2021
  2. Ap0k commented on May 25, 2021

    @Ap0k
    Contributor

    @vitek-karas
    This error can be easily treated. But in the process of fixing it, I found the following case.

    // Reflection emitted assemblies will not have a domain assembly.

    If I understood everything correctly, then the dynamic assembly is not domain-specific and the code execution goes along the specified branch with the subsequent generation of an exception.

    If fixing the definition of ALC for a dynamic assembly is not urgent, then I can investigate the above problem in more detail within the framework of this task. Otherwise, I propose to create a separate ticket for the error in the definition of ALC, merge it, merge the fixes of the tests (#48579) and then tackle the problem of this ticket tightly.

  3. vitek-karas commented on May 25, 2021

    @vitek-karas
    MemberAuthor

    Honestly I was expecting this - that dynamic assemblies will be prevented from actually succeeding in Load. I still think it's worth fixing the name validation logic, mainly because it's inconsistent and fails with really bad error.

    The potential support for dynamic assemblies in load resolution is probably a much larger work item (I'm not even sure what are all the potential problems with it for example), so I would definitely track that separately. There's also the question if it's actually worth going after at all.

  4. Ap0k commented on May 25, 2021

    @Ap0k
    Contributor

    Okay, I fill in both fixes, but without the ValidateAssemblyNameWithSimpleName tests

  5. added a commit that references this issue on May 25, 2021
    f9056b8
  6. ghost locked as resolved and limited conversation to collaborators on Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-AssemblyLoader-coreclronly use for closed issuesuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions