Skip to content

Mono interpreter gets into infinite recursion on filter clause #47624

Open

Description

@vargaz

Testcase:

using System;

class P
{
	static void Main()
	{
		try {
			throw new Exception();
		}
		catch (Exception) when (Throw()) {
                Console.WriteLine("Unreached");
			} catch (Exception) {
			Console.WriteLine("Works as expected");
		}
	}

	static bool Throw() {
		throw new Exception();
	}
}

To reproduce:

mono --interp bug.exe

Expected result:

Unhandled Exception:
System.Exception: Exception of type 'System.Exception' was thrown.

Actual result:

  at (wrapper runtime-invoke) object.runtime_invoke_direct_void (object,intptr,intptr,intptr) <0x000c6>
  at <unknown> <0xffffffff>
  at P/Q.Throw () <0x00028>
  at P/Q.Main () <0x00036>
  at P/Q.Main () <0x00012>
  at (wrapper runtime-invoke) object.runtime_invoke_direct_void (object,intptr,intptr,intptr) <0x000c6>
  at <unknown> <0xffffffff>
....

Activity

  1. ghost added
    untriaged
    on Jan 29, 2021
  2. removed
    untriaged
    on Jan 29, 2021
  3. vargaz commented on Jan 29, 2021

    @vargaz
    ContributorAuthor

    It looks like the try-catch catches the exception thrown by the Throw () from inside the filter.

  4. added this to the 6.0.0 milestone on Feb 1, 2021
  5. BrzVlad commented on Feb 8, 2021

    @BrzVlad
    Member

    @vargaz Did you spend some time thinking about this issue ? I'm not sure it is worth making interp throw like jit since that is wrong behavior. And fixing the behavior altogether seems like a non trivial fix. We need to catch all exceptions thrown from invoking a filter clause. I think we would need a new wrapper that is capable of invoking a filter clause in a method (we would need a new MONO_CEE_ opcode that does this and use it in the wrapper) and runtime invoke into it. While this sounds fair to implement on interpreter, having this working on all JIT targets sounds like a pain. Maybe I can implement this on interp and jit can catch up later with support for this wrapper. Any other better ideas ?

  6. vargaz commented on Feb 8, 2021

    @vargaz
    ContributorAuthor

    I think the problem is that the somehow the catch/filter clause catches the exception thrown from the filter itself i.e. the EH code thinks execution is still in the try block.

  7. BrzVlad commented on Feb 8, 2021

    @BrzVlad
    Member

    Exceptions thrown by filters should be ignored. So even if interp doesn't catch the exception by mistake there, the exception will still bubble up and kill the application which is incorrect behavior (this is what JIT is doing now)

  8. vargaz commented on Feb 8, 2021

    @vargaz
    ContributorAuthor

    In .net, it looks like if a filter clause throws, it's treated as returning false.

  9. BrzVlad commented on Feb 8, 2021

    @BrzVlad
    Member

    This is what ECMA says about endfilter: If an exception is thrown inside the filter block, it is intercepted and a value of exception_continue_search is returned.. So our current implementation is wrong anyway, both on interpreter and jit.

  10. modified the milestones: 6.0.0, Future on Jun 2, 2021
  11. modified the milestones: Future, 7.0.0 on Jun 14, 2021
  12. modified the milestones: 7.0.0, 8.0.0 on Aug 1, 2022
  13. SamMonoRT commented on Aug 1, 2022

    @SamMonoRT
    Member

    We'll work on this, but not targeting 7.0.0

  14. modified the milestones: 8.0.0, 9.0.0 on Jul 31, 2023
  15. modified the milestones: 9.0.0, Future on Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions