Skip to content

The generic type 'System.Tuple`2' was used with an invalid instantiation (likely byref issue) #3362

@ForNeVeR

Description

@ForNeVeR

Repro steps

  1. Create an empty F# project on .NET Core:

    $ dotnet new console -lang F#
    $ dotnet restore
  2. Write the following code in Program.fs:

    open System
    open System.Runtime.InteropServices
    
    module private SetupAPI =
        // note that's not the actual signature from SetupAPI.dll, but it's already enough to crash the program
        [<DllImport("Setupapi")>]
        extern bool SetupDiGetDeviceInterfaceDetail(
            nativeint DeviceInfoSet,
            uint32& RequiredSize)
    
    let private setupDiGetDeviceInterfaceDetail args : unit =
        let result = SetupAPI.SetupDiGetDeviceInterfaceDetail args
        ignore result
    
    let sizeFromSetupDiGetDeviceInterfaceDetail (deviceInfoSet : nativeint) : uint32 =
        let mutable requiredSize = 0u
        setupDiGetDeviceInterfaceDetail(IntPtr.Zero,
                                        &requiredSize)
        requiredSize
    
    [<EntryPoint>]
    let main argv =
        let x = sizeFromSetupDiGetDeviceInterfaceDetail IntPtr.Zero
        0
  3. Start the program:

    $ dotnet run
    
    Unhandled Exception: System.TypeLoadException: The generic type 'System.Tuple`2' was used with an invalid instantiation in assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
    at Program.setupDiGetDeviceInterfaceDetail(IntPtr args_0, UInt32& args_1)
    at Program.sizeFromSetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet) in T:\Temp\testproject\Program.fs:line 16
    at Program.main(String[] argv) in T:\Temp\testproject\Program.fs:line 22

Expected behavior

The program should not crash with TypeLoadException.

Actual behavior

The program crashes.

Known workarounds

Replace setupDiGetDeviceInterfaceDetail call with SetupAPI.SetupDiGetDeviceInterfaceDetail (e.g. remove the intermediate function that passes around the argument tuple).

Related information

Probably linked with #862. I found a similar issue #558 but it's closed.

$ dotnet --info
.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.15063
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\1.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions