Bug 41280

Summary: System.MethodAccessException is thrown from wrong frame
Product: [Mono] Runtime Reporter: Marek Safar <masafa>
Component: JITAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, mono-bugs+runtime, vargaz
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: y.exe
y-lib.dll

Description Marek Safar 2016-05-24 09:51:45 UTC
Created attachment 16083 [details]
y.exe

Execute enclosed test

It fails with

Unhandled Exception:
System.MethodAccessException: Method `Ex:Private ()' is inaccessible from method `X/<Foo>c__async0:MoveNext ()'

  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) <0x9320e8 + 0x000d3> in <filename unknown>:0
  at X.Foo () <0x92f290 + 0x000bb> in <filename unknown>:0
  at M.Main () <0x92f188 + 0x00027> in <filename unknown>:0

which is wrong because 'System.MethodAccessException' does not happen within "::Start" method but inside "::MoveNext" method.

.NET handles this correctly and throws

System.AggregateException: One or more errors occurred. ---> System.MethodAccessException: Attempt by method 'X+<Foo>c__async0.MoveNext()' to access method 'Ex.Private()' failed.
   at X.<Foo>c__async0.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at M.Main()

Why this matters is that Mono at the end throws different exception than .net
Comment 1 Marek Safar 2016-05-24 09:52:12 UTC
Created attachment 16084 [details]
y-lib.dll
Comment 2 Zoltan Varga 2016-05-29 04:27:46 UTC
Fixed in mono master.