Recognize a VB exception filter's type test

vbc builds an exception filter as a single non-short-circuiting
expression: `isinst`, then the user's `when` conditions, combined with
`and`. DetectCatchWhenConditionBlocks only knew the branch chain csc
emits, so the type test stayed inside the filter and the handler kept the
`object` variable it has in IL:

    catch (object obj) when ((obj is Exception) & (num2 != 0) & (num == 0))
    {
        ProjectData.SetProjectError((Exception)obj);

A catch type has to derive from Exception, so that does not compile.

The conjunction form is matched too now, lifting the test to the catch
type as the block form already does. The other conjuncts stop running for
a non-matching exception once the test moves, so the filter has to be
pure for this to be invisible; PropagateExceptionVariable then drops the
castclass in the handler:

    catch (Exception ex) when ((num2 != 0) & (num == 0))
    {
        ProjectData.SetProjectError(ex);

Closes #3659
This commit is contained in:
dualfroz committed 2026-09-11 23:55:33 +02:00
1 parent 8563d1b747
commit d5dfe13b26
4 files changed
+249

No files matched your search

@@ -69,11 +69,80 @@ namespace ICSharpCode.Decompiler.IL.Transforms
container.SortBlocks(deleteUnreachableBlocks: true);
}
PropagateExceptionVariable(context, catchBlock);
}
else if (catchBlock.Filter is BlockContainer filterContainer
&& MatchTypeTestConjunction(catchBlock.Variable, filterContainer,
out var conjunctionType, out var typeTest)
&& conjunctionType.GetStackType() == catchBlock.Variable.StackType)
{
context.Step($"Detected catch-when type test for {catchBlock.Variable.Name}", typeTest);
catchBlock.Variable.Type = conjunctionType;
var remainder = typeTest.Right;
typeTest.ReplaceWith(remainder);
context.EndStep(remainder);
PropagateExceptionVariable(context, catchBlock);
}
}
}
/// <summary>
/// BlockContainer {
/// Block entryPoint (incoming: 1) {
/// leave container(bit.and(bit.and(comp(isinst T(ldloc exceptionVar) != ldnull), cond1), cond2))
/// }
/// }
/// The Visual Basic compiler emits the whole filter as one non-short-circuiting
/// expression rather than the block chain csc emits, so the type test sits in a
/// conjunction instead of a branch. On a match, typeTest is the innermost `bit.and`,
/// whose right operand is the filter that remains once the test moves to the catch type.
/// </summary>
bool MatchTypeTestConjunction(ILVariable exceptionVar, BlockContainer container,
out IType exceptionType, out BinaryNumericInstruction typeTest)
{
exceptionType = null;
typeTest = null;
var entryPoint = container.EntryPoint;
if (entryPoint == null || entryPoint.IncomingEdgeCount != 1 || entryPoint.Instructions.Count != 1)
return false;
if (!entryPoint.Instructions[0].MatchLeave(container, out var condition))
return false;
// The conjuncts all run whatever the exception type is; moving the test to the catch
// clause stops the others from running for a non-matching one, which only goes
// unnoticed if none of them can do anything observable.
if (!SemanticHelper.IsPure(condition.Flags))
return false;
// The test is the leftmost operand of a left-nested chain of `&`.
while (condition is BinaryNumericInstruction { Operator: BinaryNumericOperator.BitAnd } and)
{
if (MatchTypeTest(and.Left, exceptionVar, out exceptionType))
{
typeTest = and;
return true;
}
condition = and.Left;
}
return false;
}
/// <summary>
/// comp(isinst T(ldloc exceptionVar) != ldnull), however the compiler spelled the null test.
/// </summary>
static bool MatchTypeTest(ILInstruction condition, ILVariable exceptionVar, out IType exceptionType)
{
exceptionType = null;
if (condition is not Comp comp || !comp.Right.MatchLdNull())
return false;
// `cgt.un x, null` is how both compilers spell `x != null` for a reference.
if (comp.Kind != ComparisonKind.Inequality
&& !(comp.Kind == ComparisonKind.GreaterThan && comp.InputType == StackType.O))
return false;
if (!comp.Left.MatchIsInst(out var argument, out exceptionType))
return false;
return argument.MatchLdLoc(exceptionVar);
}
/// <summary>
/// catch E_189 : 0200007C System.Exception when (BlockContainer {
/// Block IL_0079 (incoming: 1) {