Skip to content

Commit

Permalink
Fixed explicitly applied maxstack being ignored.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Jan 20, 2023
1 parent 870ce3e commit 5debb46
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Mono.Cecil.Cil/CodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void ReadMethodBody ()
switch (flags & 0x3) {
case 0x2: // tiny
body.code_size = flags >> 2;
body.MaxStackSize = 8;
body.max_stack_size = 8;
ReadCode ();
break;
case 0x3: // fat
Expand Down
3 changes: 2 additions & 1 deletion Mono.Cecil.Cil/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ void ComputeHeader ()
}

body.code_size = offset;
body.max_stack_size = max_stack;
body.max_stack_size = body.is_max_stack_size_set_explicitly
? body.max_stack_size : max_stack;
}

void ComputeExceptionHandlerStackSize (ref Dictionary<Instruction, int> stack_sizes)
Expand Down
6 changes: 5 additions & 1 deletion Mono.Cecil.Cil/MethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class MethodBody {

internal ParameterDefinition this_parameter;
internal int max_stack_size;
internal bool is_max_stack_size_set_explicitly;
internal int code_size;
internal bool init_locals;
internal MetadataToken local_var_token;
Expand All @@ -35,7 +36,10 @@ public MethodDefinition Method {

public int MaxStackSize {
get { return max_stack_size; }
set { max_stack_size = value; }
set {
max_stack_size = value;
is_max_stack_size_set_explicitly = true;
}
}

public int CodeSize {
Expand Down
27 changes: 27 additions & 0 deletions Test/Mono.Cecil.Tests/MethodBodyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;

using Mono.Cecil;
Expand Down Expand Up @@ -449,5 +450,31 @@ public void RemoveInstruction ()
Assert.AreEqual (first, third.Previous);
Assert.IsNull (third.Next);
}

[Test]
public void ApplyExplicitMaxStack ()
{
var path = Path.GetTempFileName ();
var module = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);

var method = new MethodDefinition ("foo", MethodAttributes.Static, module.TypeSystem.Void);
var body = method.Body;

body.MaxStackSize = 100;

var il = body.GetILProcessor ();

var ret = il.Create (OpCodes.Ret);
body.Instructions.Add (ret);

var type = new TypeDefinition ("foo", "foo", TypeAttributes.Public | TypeAttributes.Class, module.TypeSystem.Object);
type.Methods.Add (method);
module.Types.Add (type);

module.Write (path);

using (var read_module = ModuleDefinition.ReadModule (path))
Assert.AreEqual (100, read_module.Types [1].Methods[0].Body.MaxStackSize);
}
}
}

0 comments on commit 5debb46

Please sign in to comment.