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 19, 2023
1 parent 870ce3e commit 40c55c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Mono.Cecil.Cil/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ void ComputeHeader ()
}

body.code_size = offset;
body.max_stack_size = max_stack;
if (body.max_stack_size == 0) {
body.max_stack_size = max_stack;
}
}

void ComputeExceptionHandlerStackSize (ref Dictionary<Instruction, int> stack_sizes)
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 40c55c1

Please sign in to comment.