Skip to content
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
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Tests;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -177,6 +178,207 @@ private static void ThrowException()
throw new Exception("Boom!");
}

[Theory]
[InlineData(nameof(ArgumentAfterFilteredCatch), "name: InvalidDataException")]
[InlineData(nameof(ArgumentAfterPlainCatch), "name: InvalidDataException")]
[InlineData(nameof(ExceptionFilterProbe.LocalsSetInFilteredCatch), "0 0 True")]
[InlineData(nameof(LocalBeforeFilteredCatch), 28)]
[InlineData(nameof(IntegerArgumentAfterFilteredCatch), 42)]
[InlineData(nameof(ArgumentInFilter), true)]
[InlineData(nameof(ArgumentInsideFilteredCatch), "name: InvalidDataException")]
[InlineData(nameof(ArgumentInOuterFilteredCatch), "name x")]
public static void ExceptionFilter_PreservesArgumentsAndLocals(string scenario, object expected)
{
object actual = scenario switch
{
nameof(ArgumentAfterFilteredCatch) => ArgumentAfterFilteredCatch("name"),
nameof(ArgumentAfterPlainCatch) => ArgumentAfterPlainCatch("name"),
nameof(ExceptionFilterProbe.LocalsSetInFilteredCatch) => new ExceptionFilterProbe { Flag = true }.LocalsSetInFilteredCatch(),
nameof(LocalBeforeFilteredCatch) => LocalBeforeFilteredCatch(7),
nameof(IntegerArgumentAfterFilteredCatch) => IntegerArgumentAfterFilteredCatch(42),
nameof(ArgumentInFilter) => ArgumentInFilter("x"),
nameof(ArgumentInsideFilteredCatch) => ArgumentInsideFilteredCatch("name"),
nameof(ArgumentInOuterFilteredCatch) => ArgumentInOuterFilteredCatch("name"),
_ => throw new ArgumentException($"Unknown scenario: {scenario}", nameof(scenario))
};

Assert.Equal(expected, actual);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public static void ExceptionFilter_PreservesArgumentChanges(bool matches)
{
Assert.Equal("49: name-filter", ChangedArgumentsInFilter(42, "name", matches));
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowForFilter() => throw new InvalidDataException("x");

[MethodImpl(MethodImplOptions.NoInlining)]
private static string ArgumentAfterFilteredCatch(string name)
{
string result;
try
{
ThrowForFilter();
result = "ok";
}
catch (Exception e) when (e is InvalidDataException or NotSupportedException)
{
result = e.GetType().Name;
}
return name + ": " + result;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static string ArgumentAfterPlainCatch(string name)
{
string result;
try
{
ThrowForFilter();
result = "ok";
}
catch (Exception e)
{
if (e is not InvalidDataException)
throw;
result = e.GetType().Name;
}
return name + ": " + result;
}

private sealed class ExceptionFilterProbe
{
public bool Flag { get; set; }

[MethodImpl(MethodImplOptions.NoInlining)]
public string LocalsSetInFilteredCatch()
{
List<int> a;
List<int> b;
bool lost = false;
try
{
ThrowForFilter();
a = [1];
b = [2];
}
catch (InvalidDataException) when (Flag)
{
(a, b, lost) = ([], [], true);
}
return $"{a.Count} {b.Count} {lost}";
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static int LocalBeforeFilteredCatch(int value)
{
int local = value * 3;
try
{
ThrowForFilter();
}
catch (Exception e) when (e is InvalidDataException)
{
}
return local + value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static int IntegerArgumentAfterFilteredCatch(int value)
{
try
{
ThrowForFilter();
}
catch (Exception e) when (e is InvalidDataException)
{
}
return value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool ArgumentInFilter(string value)
{
try
{
ThrowForFilter();
return false;
}
catch (Exception) when (value == "x")
{
return true;
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static string ArgumentInsideFilteredCatch(string name)
{
try
{
ThrowForFilter();
return "";
}
catch (Exception e) when (e is InvalidDataException or NotSupportedException)
{
return $"{name}: {e.GetType().Name}";
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static string ArgumentInOuterFilteredCatch(string name)
{
try
{
return RejectedInnerFilter();
}
catch (Exception e) when (e is InvalidDataException)
{
return name + " " + e.Message;
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static string RejectedInnerFilter()
{
try
{
ThrowForFilter();
return "";
}
catch (Exception e) when (e is NotSupportedException)
{
return "wrong";
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static string ChangedArgumentsInFilter(int value, string name, bool matches)
{
try
{
ThrowForFilter();
}
catch (InvalidDataException) when (ChangeFilterArguments(ref value, ref name, matches))
{
}
catch (InvalidDataException)
{
}
return $"{value}: {name}";
}

private static bool ChangeFilterArguments(ref int value, ref string name, bool matches)
{
value += 7;
name += "-filter";
return matches;
}

[Fact]
public static void FirstCatch_ThrowFromCatch_HandledByOuterCatch()
{
Expand Down
7 changes: 5 additions & 2 deletions src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8510,9 +8510,12 @@ interp_run_clause_with_il_state (gpointer il_state_ptr, int clause_index, MonoOb
if (clause_type == MONO_EXCEPTION_CLAUSE_FILTER && !filtered)
clause_type = MONO_EXCEPTION_CLAUSE_NONE;

if (clause_type == MONO_EXCEPTION_CLAUSE_FILTER)
stackval filter_retval = {0};
if (clause_type == MONO_EXCEPTION_CLAUSE_FILTER) {
/* Keep the filter result out of the argument slots written back below. */
frame.retval = &filter_retval;
clause_args.start_with_ip = (const guint16*)ei->data.filter;
else
} else
clause_args.start_with_ip = (const guint16*)ei->handler_start;
if (clause_type == MONO_EXCEPTION_CLAUSE_NONE || clause_type == MONO_EXCEPTION_CLAUSE_FILTER) {
/* Run until the end */
Expand Down
Loading