Skip to content

4.1 compiler generates invalid programs for structs which use measure types #2373

@BillHally

Description

@BillHally

The combination of a struct, a measure type and no optimization can create incorrect programs which fail verification by peverify, and raise InvalidProgramException when run.

Repro steps

  1. Take the following code:

    type AStruct<[<Measure>]'u> = 
        struct
        end
    
    [<EntryPoint>]
    let main argv = 
        let x = AStruct()
        0
    
  2. Compile the code without optimization using version 4.1 of the F# compiler:

    PS> &"C:\Program Files (x86)\Microsoft SDKs\F#\4.1\Framework\v4.0\Fsc.exe" --optimize- .\Program.fs
    Microsoft (R) F# Compiler version 4.1
    Copyright (c) Microsoft Corporation. All Rights Reserved.
    
  3. Run the code:

    PS> .\Program.exe
    
    Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.
       at [email protected]()
       at Program.main(String[] argv)
    
  4. Verify the code:

    PS> peverify.exe Program.exe
    
    Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.0
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    [IL]: Error: [...\Program.exe : Program::main][offset 0x00000005][found ref 'Program+x@7'][expected value 'Program+AStruct'] Unexpected type on the stack.
    [IL]: Error: [...\Program.exe : Program+x@7::.ctor][offset 0x00000009] Return from .ctor when this is uninitialized.
    2 Error(s) Verifying Program.exe
    

Expected behavior

Program runs without crashing, and verification with peverify completes without reporting any errors.

Actual behavior

The program crashes with an InvalidProgramException, and verification with peverify reports 2 errors.

Known workarounds

Either:

  1. Use a class instead of a struct
  2. don't use a measure type
  3. Turn on optimization

Related information

  • Operating system: Windows 10 (Anniversary Update)
  • .NET Runtime, CoreCLR or Mono Version: .Net 4.6.2
  • Indications of severity: prevents use of measure types with structs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions