Skip to content

Regression in 16.10.0 release results in System.BadImageFormatException #11620

Closed

Description

@kentcb

Since the 16.10.0 VS update, the below code produces a BadImageFormatException. Prior to that release, it works as expected.

Repro steps

module Repro

type Data = interface end
and Service<'Data when 'Data :> Data>() = class end

type IThing = interface end
and Thing<'T> = { Metadata: 'T } with interface IThing

let createService metadata = (Service<'Data>())

let getCreateServiceCallback<'T> (thing: IThing) =
    let upcastThing =
        thing
        :?> Thing<'T>
    let getService () = createService upcastThing.Metadata
    (fun () -> getService)

[<EntryPoint>]
let main _ =
    let dummyThing : Thing<int> = { Thing.Metadata = 42 }
    // crash occurs on the following line
    let callback = getCreateServiceCallback<int> dummyThing
    let resolvedService = callback ()
    printfn "Resolved service: %A" resolvedService
    System.Console.ReadKey() |> ignore
    0

Expected behavior

Code should work as it did prior to 16.10.0, or at least produce a compiler error.

Actual behavior

Crashes with BadImageFormatException on the indicated line.

Known workarounds

  • Marking the getService function as inline
  • Explicitly annotating the getService return type as Service<Data>
  • Run in release mode

Related information

  • Windows 10
  • .NET 5
  • Visual Studio 2019

Activity

  1. added
    Impact-High
    on Jun 3, 2021
  2. added this to the Backlog milestone on Jun 3, 2021
  3. cartermp commented on Jun 3, 2021

    @cartermp
    Contributor

    Thanks for the repro, I can confirm it. I suspect we did regress somewhere here given that it still works in release mode. cc @TIHan

  4. TIHan commented on Jun 4, 2021

    @TIHan
    Contributor

    Testing this, in Release, it does work however, I can't open the binary in ILSpy or ILDASM.
    I'm not sure yet why this is occurring.

  5. TIHan commented on Jun 4, 2021

    @TIHan
    Contributor

    peverify just tells us that it has bad headers.

  6. TIHan commented on Jun 4, 2021

    @TIHan
    Contributor

    So, getService is type unit -> Service<#Data> - so being explicit with the return type via unit -> Service<Data> makes it a different type.

    unit -> Service<#Data> tells us it found a free type variable due to #Data. This means getService is a forall type.
    With it being captured in a closure, this might be the source of the issue. In release, this closure is probably getting erased.

  7. TIHan commented on Jun 4, 2021

    @TIHan
    Contributor

    I can confirm: This regression occurred when this was merged - #11349 .

    However, the initial fix df4969b works fine. It was only until subsequent changes to the PR where we tried to simplify the local type functions does this regression occur.
    cc @dsyme

  8. modified the milestones: Backlog, 16.11 on Jun 4, 2021
  9. dsyme commented on Jul 7, 2021

    @dsyme
    Contributor

    This was fixed by #11755 .

    @TIHan @jonsequitur Don't forget we may need to apply this as a servicing fix

  10. dungpa commented on Sep 1, 2021

    @dungpa
    Contributor

    @dsyme @TIHan Is the fix available in Visual Studio 16.11.2?

    I saw a similar bug and upgrading to Visual Studio 16.11.2 didn't help.
    I don't see this being mentioned in https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes either.

  11. dsyme commented on Sep 1, 2021

    @dsyme
    Contributor

    @dsyme @TIHan Is the fix available in Visual Studio 16.11.2?

    No, it isn't. It will be in VS2022. There is still discussion about whether it will qualify for a servicing fix. If you have a public-facing repro for this bug (especially in Release code) please document it below. Either way we would like to give good guidance about what code correction to make.

  12. dungpa commented on Sep 1, 2021

    @dungpa
    Contributor

    I still experience this in Release mode.
    In certain cases, inlining functions could help.

    This is from some private code; I'll try to find an independent repro.

  13. dsyme commented on Sep 1, 2021

    @dsyme
    Contributor

    In certain cases, inlining functions could help.

    The thing that will help is lifting certain kinds of generic functions to no longer be inner-functions, or else type annotating them to be non-generic. This can be very subtle, which is why I want this serviced.

    In release code this kind of lifting is often done automatically by the "lambda lifting" optimization. However there are restrictions on when that applies, which can make it even harder to determine.

  14. dungpa commented on Sep 1, 2021

    @dungpa
    Contributor

    @dsyme Agree.
    In this codebase, we have heavy use of generics; lambda lifting is not always applicable.

    It would be great if the fix goes through a service release.
    We have added workarounds for a few cases, but still frequently encounter this defect in some pull requests (compiled in Release mode in CI).

    We rolled back to Visual Studio 16.9 in some machines a while ago because of the defect.
    It seems that we can't upgrade to 16.11 either.
    I've got a wrong impression since the fix is included in 16.11 milestone https://github.com/dotnet/fsharp/milestone/41?closed=1.

  15. dsyme commented on Sep 1, 2021

    @dsyme
    Contributor

    I've got a wrong impression since the fix is included in 16.11 milestone https://github.com/dotnet/fsharp/milestone/41?closed=1.

    The criteria for 16.11 tightened and we didn't fully analyse this in time for a deadline, partly due to change of staff. So a combination of factors. But my intuition is that this will need to go into a servicing release.

    You can in theory workaround the problem by using .NET SDK before this was introduced, however the fsc.exe that ships with 16.10 and 16.11 has this problem as things currently stand.

  16. dungpa commented on Sep 1, 2021

    @dungpa
    Contributor

    You can in theory workaround the problem by using .NET SDK before this was introduced, however the fsc.exe that ships with 16.10 and 16.11 has this problem as things currently stand.

    I tried that but couldn't make it work properly.
    Moreover, we strive to keep up-to-date with Visual Studio versions; there are so many good things being added 😀.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Impact-HighRegression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions