Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tools/dotnet-linker/AppBundleRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ internal bool ImplementConstructNSObjectFactoryMethod (Tuner.DerivedLinkContext
il.Emit (OpCodes.Newobj, ctor);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();

// make sure the trimmer doesn't trim it away if the type is kept
if (context.App.Registrar == RegistrarMode.TrimmableStatic) {
Expand Down Expand Up @@ -2088,7 +2088,7 @@ internal bool ImplementConstructINativeObjectFactoryMethod (Tuner.DerivedLinkCon
throw new UnreachableException ();
}

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();

// make sure the trimmer doesn't trim it away if the type is kept
if (context.App.Registrar == RegistrarMode.TrimmableStatic) {
Expand Down
17 changes: 15 additions & 2 deletions tools/dotnet-linker/CecilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;

using Xamarin.Bundler;

Expand Down Expand Up @@ -76,8 +77,20 @@ public static MethodBody CreateBody (this MethodDefinition self, out ILProcessor
return body;
}

public static void GenerateILOffsets (this MethodBody body)
// Use the macro/short form of instructions whenever possible. This makes the generated IL
// smaller, and it also works around a bug in the CoreCLR interpreter, which reads the operand
// of the long form of the ldloc/stloc instructions at the wrong offset (the bug was fixed in
// https://github.com/dotnet/runtime/pull/131547).
public static void OptimizeGeneratedBody (this MethodBody body)
{
body.OptimizeMacros ();
}

// Call this method once a generated method body is complete.
public static void FinalizeGeneratedBody (this MethodBody body)
{
body.OptimizeGeneratedBody ();

// This does not compute precise offsets, it just assigns a unique number to each instruction
// The trimmer relies on unique offsets to identify instructions
int instructionOffset = 0;
Expand Down Expand Up @@ -164,7 +177,7 @@ public static MethodDefinition AddDefaultConstructor (this TypeDefinition type,
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Call, abr.System_Object__ctor);
il.Emit (OpCodes.Ret);
body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
return defaultCtor;
}

Expand Down
12 changes: 12 additions & 0 deletions tools/dotnet-linker/Steps/InlineDlfcnMethodsStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ MethodDefinition GetOrCreateGetNativeFieldMethod (MethodDefinition callingMethod
}
il.Append (il.Create (OpCodes.Ret));

// See the comment on CecilExtensions.OptimizeGeneratedBody for why this is needed.
body.OptimizeGeneratedBody ();

return rv;
}

Expand Down Expand Up @@ -504,6 +507,9 @@ MethodDefinition GetOrCreateSetNativeFieldMethod (MethodDefinition callingMethod
}
il.Append (il.Create (OpCodes.Ret));

// See the comment on CecilExtensions.OptimizeGeneratedBody for why this is needed.
body.OptimizeGeneratedBody ();

return rv;
}

Expand Down Expand Up @@ -558,6 +564,9 @@ MethodDefinition GetOrCreateSetNativeStringMethod (MethodDefinition callingMetho
il.Append (il.Create (OpCodes.Stind_I));
il.Append (il.Create (OpCodes.Ret));

// See the comment on CecilExtensions.OptimizeGeneratedBody for why this is needed.
body.OptimizeGeneratedBody ();

return rv;
}

Expand Down Expand Up @@ -909,6 +918,9 @@ protected override bool ProcessMethod (MethodDefinition method)
il.Append (il.Create (OpCodes.Ldind_I));
il.Append (il.Create (OpCodes.Ret));

// See the comment on CecilExtensions.OptimizeGeneratedBody for why this is needed.
method.Body.OptimizeGeneratedBody ();

modified = true;
return modified; // we replace the whole method body, so no need to continue processing the method
}
Expand Down
16 changes: 8 additions & 8 deletions tools/dotnet-linker/Steps/ManagedRegistrarLookupTablesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void GenerateLookupTypeId (AssemblyTrampolineInfo infos, TypeDefinition registra
il.Emit (OpCodes.Ldc_I4_M1);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

void GenerateLookupType (AssemblyTrampolineInfo infos, TypeDefinition registrarType, List<TypeData> types)
Expand Down Expand Up @@ -331,7 +331,7 @@ void GenerateLookupType (AssemblyTrampolineInfo infos, TypeDefinition registrarT
il.Emit (OpCodes.Ldloc, temporary);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

void GenerateConstructNSObject (TypeDefinition registrarType)
Expand Down Expand Up @@ -388,7 +388,7 @@ void GenerateConstructNSObject (TypeDefinition registrarType)
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

void GenerateConstructINativeObject (TypeDefinition registrarType)
Expand Down Expand Up @@ -446,7 +446,7 @@ void GenerateConstructINativeObject (TypeDefinition registrarType)
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

// We need to preserve the constructor because it might not be used anywhere else.
Expand Down Expand Up @@ -502,7 +502,7 @@ void GenerateRegisterWrapperTypes (TypeDefinition type)

il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

void GenerateLookupUnmanagedFunction (TypeDefinition registrar_type, IList<TrampolineInfo> trampolineInfos)
Expand Down Expand Up @@ -536,7 +536,7 @@ void GenerateLookupUnmanagedFunction (TypeDefinition registrar_type, IList<Tramp
il.Emit (OpCodes.Call, lookupMethods);
}
il.Emit (OpCodes.Ret);
body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

// If WrappedLook is true we'll wrap the ldftn instruction in a separate method, which can be useful for debugging,
Expand Down Expand Up @@ -584,7 +584,7 @@ MethodDefinition GenerateLookupMethods (TypeDefinition type, IList<TrampolineInf
var wrappedBody = wrappedLookup.CreateBody (out var wrappedIl);
wrappedIl.Emit (OpCodes.Ldftn, mr);
wrappedIl.Emit (OpCodes.Ret);
wrappedBody.GenerateILOffsets ();
wrappedBody.FinalizeGeneratedBody ();

targets [i] = Instruction.Create (OpCodes.Call, wrappedLookup);
} else {
Expand Down Expand Up @@ -656,7 +656,7 @@ MethodDefinition GenerateLookupMethods (TypeDefinition type, IList<TrampolineInf
il.Emit (OpCodes.Conv_I);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();

return method;
}
Expand Down
10 changes: 5 additions & 5 deletions tools/dotnet-linker/Steps/ManagedRegistrarStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ void EmitGenericReflectionDispatch (MethodDefinition method, MethodDefinition ca
}
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

// Recursively substitutes generic parameters in a type reference according to the given map.
Expand Down Expand Up @@ -929,7 +929,7 @@ public void EmitCallToProxyMethod (MethodDefinition method, MethodDefinition cal
il.Emit (OpCodes.Callvirt, proxyInterfaceMethod);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

public void EmitCallToExportedMethod (MethodDefinition method, MethodDefinition callback)
Expand Down Expand Up @@ -1243,7 +1243,7 @@ public void EmitCallToExportedMethod (MethodDefinition method, MethodDefinition
instr.Operand = leaveTryInstructionOperand;
eh.HandlerEnd = (Instruction) leaveEHInstruction.Operand;

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}

void AddExceptionHandler (ILProcessor il, VariableDefinition? returnVariable, Instruction placeholderNextInstruction, out ExceptionHandler eh, out Instruction leaveEHInstruction)
Expand Down Expand Up @@ -2039,7 +2039,7 @@ MethodDefinition CloneConstructorWithNativeHandle (MethodDefinition ctor)
il.Emit (OpCodes.Call, ctor);
il.Emit (OpCodes.Ret);

body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();

return clonedCtor;
}
Expand All @@ -2059,7 +2059,7 @@ void ImplementNSObjectRegisterToggleRefMethodStub ()

var body = registerToggleRef!.CreateBody (out var il);
il.Emit (OpCodes.Ret);
body.GenerateILOffsets ();
body.FinalizeGeneratedBody ();
}
}
}
Expand Down
Loading