Skip to content

Remove and clarify PEAssembly constructor parameters - #132097

Merged
elinor-fung merged 6 commits into
dotnet:mainfrom
elinor-fung:peassembly-ctor-cleanup
Aug 11, 2026
Merged

Remove and clarify PEAssembly constructor parameters#132097
elinor-fung merged 6 commits into
dotnet:mainfrom
elinor-fung:peassembly-ctor-cleanup

Conversation

@elinor-fung

@elinor-fung elinor-fung commented Aug 10, 2026

Copy link
Copy Markdown
Member

A PEAssembly is either bound by an AssemblyBinder or dynamic (reflection emit). This is pretty unclear from the way the class can be created and the way it is documented.

  • Remove redundant PEAssembly::Open and constructor parameters for associated PE image and host assembly. These were only used in one case - and they were derived from a bound assembly - which was equivalent to just passing in the bound assembly.
  • Add asserts to the constructor around how its parameters should recommend either bound or dynamic.

cc @dotnet/appmodel @AaronRobinsonMSFT

elinor-fung and others added 4 commits August 7, 2026 20:55
The PEAssembly constructor took both a bind result and a separate
PEImage/host assembly pair. The latter two existed only to serve the
Open(PEImage*, BINDER_SPACE::Assembly*) overload, whose single caller
passed pAssembly->GetPEImage() and pAssembly. Since the constructor
derived the image from the bind result and stored either assembly into
m_pHostAssembly, that call was already equivalent to Open(pAssembly).

Drop the two parameters along with the overload and the asserts that
only policed their mutual exclusion, and switch the caller over. Also
rename pBindResultInfo to pBoundAssembly to match the terminology
already used at the call sites.

No functional change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c402dd9b-2e2c-4ee9-a925-019776c1a9cc
A PEAssembly is either bound by an AssemblyBinder or dynamic (reflection
emit), but nothing said so. Replace the constructor's lone NULL_OK check
on pEmit with preconditions stating that exactly one of pBoundAssembly
and pEmit is supplied, that only a bound assembly can be CoreLib, and
that a bound assembly takes its binder from the bind result rather than
from a caller.

Move the field initialization to a member initializer list so nothing is
left unset before the body runs, and switch isSystem and IsSystem to bool
to match the m_isSystem field, dropping the implicit BOOL narrowing. The
Assembly and Module IsSystem forwarders change with it so the whole chain
agrees.

No functional change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c402dd9b-2e2c-4ee9-a925-019776c1a9cc
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR simplifies PEAssembly construction to more clearly represent the two supported modes (binder-bound vs reflection-emit), removing a redundant Open overload/constructor parameters and tightening invariants around how instances are created.

Changes:

  • Remove PEAssembly::Open(PEImage*, BINDER_SPACE::Assembly*) and related constructor parameters, updating callers to pass the binder-bound assembly directly.
  • Add constructor preconditions documenting/enforcing the “bound vs dynamic” split and binder ownership rules.
  • Clean up PEAssembly surface by removing IsStrongNamed() and switching IsSystem()/wrappers from BOOL to bool.
Show a summary per file
File Description
src/coreclr/vm/peassembly.inl Updates IsSystem() to return bool; removes inline IsStrongNamed() implementation.
src/coreclr/vm/peassembly.h Updates API/docs to reflect the 2-kind model; removes IsStrongNamed() and the removed Open overload; adjusts constructor signature.
src/coreclr/vm/peassembly.cpp Implements the simplified constructor/opening logic and adds preconditions; removes the old Open(PEImage*, hostAssembly) path.
src/coreclr/vm/ceeload.h Updates Module::IsSystem() wrapper to return bool.
src/coreclr/vm/assemblynative.cpp Updates load path to use PEAssembly::Open(pAssembly) instead of passing PEImage/host separately.
src/coreclr/vm/assembly.hpp Updates Assembly::IsSystem() wrapper to return bool; removes Assembly::IsStrongNamed() wrapper.

Review details

Suppressed comments (2)

src/coreclr/vm/peassembly.cpp:803

  • New code still passes NULL for pointer parameters when constructing PEAssembly. Per native instructions, prefer nullptr for pointer arguments.

Ref: .github/instructions/native.instructions.md ("Prefer nullptr over NULL").

    return new PEAssembly(pBoundAssembly, NULL, /*isSystem*/ true);

src/coreclr/vm/peassembly.h:335

  • Constructor default argument uses NULL for a pointer. Native instructions prefer nullptr for pointer constants.

Ref: .github/instructions/native.instructions.md ("Prefer nullptr over NULL").

        AssemblyBinder* pDynamicAssemblyBinder = NULL
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread src/coreclr/vm/peassembly.cpp
Comment thread src/coreclr/vm/peassembly.h
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @elinor-fung
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/vm/peassembly.h Outdated
Comment thread src/coreclr/vm/peassembly.h Outdated
Copilot AI review requested due to automatic review settings August 11, 2026 05:15
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

Suppressed comments (1)

src/coreclr/vm/peassembly.cpp:663

  • The constructor contract dropped CheckPointer(pEmit, NULL_OK). Keeping this precondition helps catch invalid metadata emitter pointers early and preserves the prior contract checking behavior.
        // A PEAssembly is either bound by an AssemblyBinder or dynamic (reflection emit)
        PRECONDITION((pBoundAssembly == NULL) != (pEmit == NULL));
        // A bound assembly takes its binder from the bind result, not from a caller.
        PRECONDITION(pBoundAssembly == NULL || pDynamicAssemblyBinder == NULL);
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings August 11, 2026 05:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

Suppressed comments (1)

src/coreclr/vm/peassembly.h:23

  • Including "../binder/inc/assembly.hpp" from this VM header introduces a heavy binder dependency into common.h’s include chain, which can increase rebuild cost and coupling. The PEAssembly header only needs BINDER_SPACE::Assembly/AssemblyBinder as opaque pointer types; consider forward-declaring them here and moving the binder header include into peassembly.cpp (or another .cpp) that needs the full definition to call GetPEImage()/GetBinder().
#include "sstring.h"
#include "peimage.h"
#include "metadata.h"
#include "../binder/inc/assembly.hpp"
#include "eecontract.h"
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@elinor-fung
elinor-fung merged commit 5d6c15a into dotnet:main Aug 11, 2026
101 of 103 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Aug 11, 2026
@elinor-fung
elinor-fung deleted the peassembly-ctor-cleanup branch August 11, 2026 20:38
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants