Skip to content

Commit 5debb46

Browse files
committed
Fixed explicitly applied maxstack being ignored.
1 parent 870ce3e commit 5debb46

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

Mono.Cecil.Cil/CodeReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void ReadMethodBody ()
9292
switch (flags & 0x3) {
9393
case 0x2: // tiny
9494
body.code_size = flags >> 2;
95-
body.MaxStackSize = 8;
95+
body.max_stack_size = 8;
9696
ReadCode ();
9797
break;
9898
case 0x3: // fat

Mono.Cecil.Cil/CodeWriter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ void ComputeHeader ()
341341
}
342342

343343
body.code_size = offset;
344-
body.max_stack_size = max_stack;
344+
body.max_stack_size = body.is_max_stack_size_set_explicitly
345+
? body.max_stack_size : max_stack;
345346
}
346347

347348
void ComputeExceptionHandlerStackSize (ref Dictionary<Instruction, int> stack_sizes)

Mono.Cecil.Cil/MethodBody.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public sealed class MethodBody {
2121

2222
internal ParameterDefinition this_parameter;
2323
internal int max_stack_size;
24+
internal bool is_max_stack_size_set_explicitly;
2425
internal int code_size;
2526
internal bool init_locals;
2627
internal MetadataToken local_var_token;
@@ -35,7 +36,10 @@ public MethodDefinition Method {
3536

3637
public int MaxStackSize {
3738
get { return max_stack_size; }
38-
set { max_stack_size = value; }
39+
set {
40+
max_stack_size = value;
41+
is_max_stack_size_set_explicitly = true;
42+
}
3943
}
4044

4145
public int CodeSize {

Test/Mono.Cecil.Tests/MethodBodyTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34

45
using Mono.Cecil;
@@ -449,5 +450,31 @@ public void RemoveInstruction ()
449450
Assert.AreEqual (first, third.Previous);
450451
Assert.IsNull (third.Next);
451452
}
453+
454+
[Test]
455+
public void ApplyExplicitMaxStack ()
456+
{
457+
var path = Path.GetTempFileName ();
458+
var module = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);
459+
460+
var method = new MethodDefinition ("foo", MethodAttributes.Static, module.TypeSystem.Void);
461+
var body = method.Body;
462+
463+
body.MaxStackSize = 100;
464+
465+
var il = body.GetILProcessor ();
466+
467+
var ret = il.Create (OpCodes.Ret);
468+
body.Instructions.Add (ret);
469+
470+
var type = new TypeDefinition ("foo", "foo", TypeAttributes.Public | TypeAttributes.Class, module.TypeSystem.Object);
471+
type.Methods.Add (method);
472+
module.Types.Add (type);
473+
474+
module.Write (path);
475+
476+
using (var read_module = ModuleDefinition.ReadModule (path))
477+
Assert.AreEqual (100, read_module.Types [1].Methods[0].Body.MaxStackSize);
478+
}
452479
}
453480
}

0 commit comments

Comments
 (0)