Skip to content
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

Add support for exception regions #62

Merged
merged 3 commits into from
Oct 14, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Basic
{
[Kept]
public class ExceptionRegions
{
[Kept]
static void Main()
{
try
{
A();
}
catch (CustomException ce)
{
Console.WriteLine(ce.Message);
try
{
B();
}
catch (Exception e) when (e.InnerException != null)
{
Console.WriteLine(e.Message);
}
}
finally
{
C();
}
}

[Kept]
static void A() { }

[Kept]
static void B() { }

[Kept]
static void C() { }
}

[Kept]
[KeptBaseType(typeof(Exception))]
class CustomException : Exception
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFacto
if (!bodyBlock.LocalSignature.IsNil)
yield return new DependencyListEntry(factory.StandaloneSignature(_module, bodyBlock.LocalSignature), "Signatures of local variables");

var exceptionRegions = bodyBlock.ExceptionRegions;
if (!bodyBlock.ExceptionRegions.IsEmpty)
{
foreach (var exceptionRegion in exceptionRegions)
{
if (exceptionRegion.Kind != ExceptionRegionKind.Catch)
continue;

yield return new DependencyListEntry(factory.GetNodeForToken(_module, exceptionRegion.CatchType), "Catch type of exception region");
}
}

ILReader ilReader = new(bodyBlock.GetILBytes());
while (ilReader.HasNext)
{
Expand Down Expand Up @@ -123,10 +135,23 @@ public int Write(ModuleWritingContext writeContext)
return -1;

MethodBodyBlock bodyBlock = _module.PEReader.GetMethodBody(rva);
var exceptionRegions = bodyBlock.ExceptionRegions;

// TODO: need to rewrite token references in the exception regions
// This would need ControlFlowBuilder and setting up labels and such.
// All doable, just more code.
// Use small exception regions when the code size of the try block and
// the handler code are less than 256 bytes and offsets smaller than 65536 bytes.
bool useSmallExceptionRegions = ExceptionRegionEncoder.IsSmallRegionCount(exceptionRegions.Length);
if (useSmallExceptionRegions)
{
foreach (var exceptionRegion in exceptionRegions)
{
if (!ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.TryOffset, exceptionRegion.TryLength) ||
!ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.HandlerOffset, exceptionRegion.HandlerLength))
{
useSmallExceptionRegions = false;
break;
}
}
}

BlobBuilder outputBodyBuilder = writeContext.GetSharedBlobBuilder();
byte[] bodyBytes = bodyBlock.GetILBytes();
Expand Down Expand Up @@ -201,11 +226,45 @@ public int Write(ModuleWritingContext writeContext)
MethodBodyStreamEncoder.MethodBody bodyEncoder = writeContext.MethodBodyEncoder.AddMethodBody(
outputBodyBuilder.Count,
bodyBlock.MaxStack,
exceptionRegionCount: 0,
hasSmallExceptionRegions: false,
exceptionRegionCount: exceptionRegions.Length,
hasSmallExceptionRegions: useSmallExceptionRegions,
(StandaloneSignatureHandle)writeContext.TokenMap.MapToken(bodyBlock.LocalSignature),
bodyBlock.LocalVariablesInitialized ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None);
BlobWriter instructionsWriter = new(bodyEncoder.Instructions);

ExceptionRegionEncoder exceptionRegionEncoder = bodyEncoder.ExceptionRegions;
foreach (var exceptionRegion in exceptionRegions)
{
switch (exceptionRegion.Kind)
{
case ExceptionRegionKind.Catch:
exceptionRegionEncoder.AddCatch(
exceptionRegion.TryOffset,
exceptionRegion.TryLength,
exceptionRegion.HandlerOffset,
exceptionRegion.HandlerLength,
writeContext.TokenMap.MapToken(exceptionRegion.CatchType));
break;

case ExceptionRegionKind.Filter:
exceptionRegionEncoder.AddFilter(
exceptionRegion.TryOffset,
exceptionRegion.TryLength,
exceptionRegion.HandlerOffset,
exceptionRegion.HandlerLength,
exceptionRegion.FilterOffset);
break;

case ExceptionRegionKind.Finally:
exceptionRegionEncoder.AddFinally(
exceptionRegion.TryOffset,
exceptionRegion.TryLength,
exceptionRegion.HandlerOffset,
exceptionRegion.HandlerLength);
break;
}
}

outputBodyBuilder.WriteContentTo(ref instructionsWriter);

return bodyEncoder.Offset;
Expand Down