Skip to content

Weaver: Fixes broken catch leaving to final ret operation #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion ChangeLog/6.0.11_dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[postgresql] Dedicated exception when an extracting schema doesn't exist or it belongs to another user
[postgresql] Dedicated exception when an extracting schema doesn't exist or it belongs to another user
[weaver] Fixed ctor processing with try...catch where there was broken catch section leaving
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2013 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Copyright (C) 2013-2022 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Denis Krjuchkov
// Created: 2013.08.19

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

var initializeCall = EmitInitializeCall(context, il);
Expand All @@ -36,6 +37,12 @@ public override ActionResult Execute(ProcessorContext context)
var ret = il.Create(OpCodes.Ret);
il.Append(ret);
il.Replace(leavePlaceholder, il.Create(OpCodes.Leave, ret));
if (body.ExceptionHandlers.Count != 0) {
if (originalLastRet != null)
foreach (var eHandler in body.ExceptionHandlers) {
FixCatchLeave(eHandler.HandlerStart, eHandler.HandlerEnd, originalLastRet, initializeCall);
}
}

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

private void FixCatchLeave(Instruction start, Instruction end, Instruction oldRetTarget, Instruction newTarget)
{
var current = start;
while (current != end && current != null) {
var next = current.Next;
var code = current.OpCode.Code;
if ((code == Code.Leave || code == Code.Leave_S) && current.Operand == oldRetTarget) {
current.Operand = newTarget;
}
current = next;
}
}

private Instruction GetStartInstruction(ILProcessor il)
{
var instructions = constructor.Body.Instructions;
Expand Down