Skip to content

Commit bf7811d

Browse files
authored
Merge pull request #295 from DataObjects-NET/6.0-weaver-ctor-processing-bug
Weaver: Fixes broken catch leaving to final ret operation
2 parents 5fb154d + e94f8d4 commit bf7811d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ChangeLog/6.0.11_dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
[postgresql] Dedicated exception when an extracting schema doesn't exist or it belongs to another user
1+
[postgresql] Dedicated exception when an extracting schema doesn't exist or it belongs to another user
2+
[weaver] Fixed ctor processing with try...catch where there was broken catch section leaving

Weaver/Xtensive.Orm.Weaver/Tasks/ImplementInitializablePatternTask.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2013 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2013-2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2013.08.19
66

@@ -20,6 +20,7 @@ public override ActionResult Execute(ProcessorContext context)
2020
{
2121
var body = constructor.Body;
2222
var il = body.GetILProcessor();
23+
var originalLastRet = body.Instructions.Reverse().FirstOrDefault(i => i != null && i.OpCode.Code == Code.Ret);
2324
var leavePlaceholder = il.Create(OpCodes.Nop);
2425

2526
var initializeCall = EmitInitializeCall(context, il);
@@ -36,6 +37,12 @@ public override ActionResult Execute(ProcessorContext context)
3637
var ret = il.Create(OpCodes.Ret);
3738
il.Append(ret);
3839
il.Replace(leavePlaceholder, il.Create(OpCodes.Leave, ret));
40+
if (body.ExceptionHandlers.Count != 0) {
41+
if (originalLastRet != null)
42+
foreach (var eHandler in body.ExceptionHandlers) {
43+
FixCatchLeave(eHandler.HandlerStart, eHandler.HandlerEnd, originalLastRet, initializeCall);
44+
}
45+
}
3946

4047
body.InitLocals = true;
4148
var handler = new ExceptionHandler(ExceptionHandlerType.Catch) {
@@ -61,6 +68,19 @@ private void ReplaceRetWithBr(ILProcessor il, Instruction start, Instruction end
6168
}
6269
}
6370

71+
private void FixCatchLeave(Instruction start, Instruction end, Instruction oldRetTarget, Instruction newTarget)
72+
{
73+
var current = start;
74+
while (current != end && current != null) {
75+
var next = current.Next;
76+
var code = current.OpCode.Code;
77+
if ((code == Code.Leave || code == Code.Leave_S) && current.Operand == oldRetTarget) {
78+
current.Operand = newTarget;
79+
}
80+
current = next;
81+
}
82+
}
83+
6484
private Instruction GetStartInstruction(ILProcessor il)
6585
{
6686
var instructions = constructor.Body.Instructions;

0 commit comments

Comments
 (0)