Produce consistent signatures and attributes for hook stubs#274
Open
AlpinePastoralist wants to merge 1 commit into
Open
Produce consistent signatures and attributes for hook stubs#274AlpinePastoralist wants to merge 1 commit into
AlpinePastoralist wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.
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."
Adds an explicit "this" ParameterDefinition for the prepended slot and shifts the sequence numbers of the cloned parameters by one.
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.