Bug 55681

Summary: System.Reflection.Emit.ModuleBuilder.build_metadata bug when running FAKE's test suite
Product: [Mono] Runtime Reporter: Jason Imison <jason.imison>
Component: ReflectionAssignee: Aleksey Kliger <aleksey>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, matthi.d, mono-bugs+runtime, v-rosman
Priority: ---    
Version: 5.0 (2017-02)   
Target Milestone: 15.2   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Jason Imison 2017-04-27 10:45:09 UTC
I see the following error when running ./build.sh from https://github.com/fsharp/FAKE using Mono 5.0.0.76. Will try and come up with a smaller repro.


when running script
requested token for ConstructorBuilder





Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Reflection.Emit.ModuleBuilder.build_metadata (System.Reflection.Emit.ModuleBuilder) [0x00000] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.ModuleBuilder.Save () [0x001ad] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.AssemblyBuilder.Save (string,System.Reflection.PortableExecutableKinds,System.Reflection.ImageFileMachine) [0x0022b] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.AssemblyBuilder.Save (string) [0x00000] in <5357e6452d21439088fd8589dfcf1134>:0
  at Fake.FSIHelper.handleCaching<a_REF> (bool,Yaaf.FSharp.Scripting.IFsiSession,a_REF,System.IO.DirectoryInfo,Fake.FSIHelper/CacheInfo) [0x00046] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.runScriptUncached (bool,string,System.Collections.Generic.IEnumerable`1<string>,bool,Fake.FSIHelper/CacheInfo,System.IO.TextWriter,System.IO.TextWriter) [0x00236] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.runFAKEScriptWithFsiArgsAndRedirectMessages (bool,Fake.FSIHelper/FsiArgs,System.Collections.Generic.IEnumerable`1<System.Tuple`2<string, string>>,Microsoft.FSharp.Core.FSharpFunc`2<string, Microsoft.FSharp.Core.Unit>,Microsoft.FSharp.Core.FSharpFunc`2<string, Microsoft.FSharp.Core.Unit>,bool) [0x0015d] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.executeBuildScriptWithArgsAndFsiArgsAndReturnMessages (string,string[],string[],bool) [0x00038] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Test.FAKECore.when_running_script.RunExplicitWithResult (string,string[],string[],bool) [0x00023] in <4e523b3abfd44163b8ac5d7c21429614>:0
  at Test.FAKECore.when_running_script/<>c.<.ctor>b__25_1 () [0x0006c] in <4e523b3abfd44163b8ac5d7c21429614>:0
Comment 1 matthi.d 2017-04-27 12:05:39 UTC
Whatever this bug is, it is related to emiting (via Reflection.Emit) the output of the F# compiler for this code. This might help while analysing this.


open System
type MyAttr() =
  inherit Attribute()
  do failwith "test"

[<MyAttr()>]
type Test () =
  do ignore ()
[<assembly: MyAttr()>]
do ()


This is a test-case we have in FAKE test-suite.
Comment 2 Jason Imison 2017-04-27 12:19:27 UTC
FAKE has since been updated to detect Mono 5.0.0 and not do assembly cache writing https://github.com/fsharp/FAKE/pull/1536/files?w=1
Comment 3 Jason Imison 2017-04-27 13:22:36 UTC
@matthid: I can't repro with this isolated script. Any ideas?

#r "FSharp.Compiler.Service.12.0.5/lib/net45/FSharp.Compiler.Service.dll"
open Microsoft.FSharp.Compiler.Interactive.Shell

open System.IO
open System.Text

// Intialize output and input streams
let sbOut = new StringBuilder()
let sbErr = new StringBuilder()
let inStream = new StringReader("")
let outStream = new StringWriter(sbOut)
let errStream = new StringWriter(sbErr)

// Build command line arguments & start FSI session
let argv = [| "C:\\fsi.exe" |]
let allArgs = Array.append argv [|"--noninteractive"|]

let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)  
let script =
    """
    open System
    type MyAttr() =
      inherit Attribute()
      do failwith "test"
    
    [<MyAttr()>]
    type Test () =
      do ignore ()
    [<assembly: MyAttr()>]
    do ()
    """
fsiSession.EvalScript script
let builder =
    match fsiSession.DynamicAssembly with
    | :? System.Reflection.Emit.AssemblyBuilder as builder -> builder
    | _ -> failwith "The DynamicAssembly property is no AssemblyBuilder!"
builder.Save("FSI-ASSEMBLY.dll")
Comment 4 Jason Imison 2017-04-27 17:08:36 UTC
I've tried and failed to come with a repro that doesn't involve using the full FAKE .exe

You should be able to repro with the following steps though

Create a file named build.fsx with the following contents

```
open System
type MyAttr() =
  inherit Attribute()
  do failwith "test"

[<MyAttr()>]
type Test () =
  do ignore ()
[<assembly: MyAttr()>]
do ()
```

nuget install FAKE -Version 4.9.6
mono FAKE.4.9.6/tools/FAKE.exe build.fsx
Comment 5 Aleksey Kliger 2017-04-27 18:51:00 UTC
In mono_image_add_cattrs () we call mono_image_create_token on a ConstructorBuilder object, but that function expects to only be called on System.Reflection objects, not on System.Reflection.Emit objects. (It assumes calls are coming from managed code, and there everything is routed through ModuleBuilder.GetToken(MemberInfo) which handles all the Builders in managed code).

I should have C# testcases shortly.  (I think there are actually two bad cases here. The one like the F# code in Comment 4 where the custom attribute is applied to the assembly that it is defined in, and another one where we have two assembly builders going and one of them makes an attribute that's applied to the other).
Comment 6 Jason Imison 2017-04-29 14:20:17 UTC
Fixed by https://github.com/mono/mono/pull/4769 Thanks Aleksey!
Comment 8 Roshan Mankani 2017-05-09 09:40:59 UTC
Hello @Jason Imison,
Can you please provide proper steps or description with expected condition, for verify this bug.
Comment 9 Aleksey Kliger 2017-05-09 14:58:51 UTC
Roshan,

See Jason's Comment 4 for the reproduction steps.

The expected output is that the last step "mono FAKE.4.9.6/tools/FAKE.exe build.fsx" exits without an error.