Skip to content

Produce consistent signatures and attributes for hook stubs#274

Open
AlpinePastoralist wants to merge 1 commit into
sunnamed434:mainfrom
AlpinePastoralist:ConsistentSigAndAttributesHookStubs
Open

Produce consistent signatures and attributes for hook stubs#274
AlpinePastoralist wants to merge 1 commit into
sunnamed434:mainfrom
AlpinePastoralist:ConsistentSigAndAttributesHookStubs

Conversation

@AlpinePastoralist
Copy link
Copy Markdown

Fix DotNetHook: produce consistent signature and attributes for hook stubs

When hooking an instance method, the generated dummy stub on reused callingMethod.Signature (which carries HasThis=true) while forcing IsStatic=true via an object initializer. AsmResolver rejects the resulting metadata with:

"Method is static but its signature has the HasThis flag set."

Under .NET 10 / ASP.NET Core, where the protection touches hundreds of call sites in real-world assemblies, this surfaces as 500+ WriteModule errors and the obfuscated module is never written.

This change:

  1. Rebuilds the stub signature as truly static via MethodSignature.CreateStatic. For instance methods, the declaring type is prepended as a regular first parameter so the IL call stack at the caller stays the same.

  2. Sets MethodAttributes.Assembly | MethodAttributes.Static directly in the MethodDefinition constructor (after stripping the inherited visibility/static-related bits). Setting IsStatic=true later via an object initializer was too late, because AsmResolver verifies the attributes against the signature inside the constructor, throwing:

    "An instance method requires a signature with the HasThis flag set."

  3. Adds an explicit "this" ParameterDefinition for the prepended slot and shifts the sequence numbers of the cloned parameters by one.

  4. Skips generic methods. The cloned signature has GenericParameterCount > 0 but no GenericParameter definitions are attached to the stub, which AsmResolver rejects with:

    "Method defines 0 generic parameters but its signature defines N parameters."

    Fully cloning the generic parameters (including their constraints) is non-trivial, so we conservatively skip these call sites.

…stubs

When hooking an instance method, the generated dummy stub on <Module>
reused callingMethod.Signature (which carries HasThis=true) while
forcing IsStatic=true via an object initializer. AsmResolver rejects
the resulting metadata with:

  "Method is static but its signature has the HasThis flag set."

Under .NET 10 / ASP.NET Core, where the protection touches hundreds of
call sites in real-world assemblies, this surfaces as 500+ WriteModule
errors and the obfuscated module is never written.

This change:

1. Rebuilds the stub signature as truly static via
   MethodSignature.CreateStatic. For instance methods, the declaring
   type is prepended as a regular first parameter so the IL call stack
   at the caller stays the same.

2. Sets MethodAttributes.Assembly | MethodAttributes.Static directly in
   the MethodDefinition constructor (after stripping the inherited
   visibility/static-related bits). Setting IsStatic=true later via an
   object initializer was too late, because AsmResolver verifies the
   attributes against the signature inside the constructor, throwing:

   "An instance method requires a signature with the HasThis flag set."

3. Adds an explicit "this" ParameterDefinition for the prepended slot
   and shifts the sequence numbers of the cloned parameters by one.

4. Skips generic methods. The cloned signature has
   GenericParameterCount > 0 but no GenericParameter definitions are
   attached to the stub, which AsmResolver rejects with:

   "Method defines 0 generic parameters but its signature defines N
    parameters."

   Fully cloning the generic parameters (including their constraints)
   is non-trivial, so we conservatively skip these call sites.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant