Skip to content

System.BadImageFormatException on attempt to compare a voidptr and IntPtr.Zero.ToPointer() #15254

@ForNeVeR

Description

@ForNeVeR

Consider this F# program:

open System

[<Struct>]
type Image =
    val data: voidptr

let LoadTextureFromMemory(): bool =
    let image = Unchecked.defaultof<Image>
    image.data = IntPtr.Zero.ToPointer()

let foo = LoadTextureFromMemory()

Expected behavior
This program should work and put true into the foo variable.

Actual behavior

Unhandled exception. System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)
   at Program.LoadTextureFromMemory()
   at <StartupCode$ConsoleApp11>.$Program.main@() in T:\Temp\ConsoleApp11\ConsoleApp11\Program.fs:line 13

See the repro on sharplab as well.

Known workarounds

// open Microsoft.FSharp.NativeInterop
NativePtr.ofVoidPtr<byte> image.data = NativePtr.nullPtr

Related information

  • Operating system: Windows 11
  • .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET SDK 7.0.302
  • Editing Tools (e.g. Visual Studio Version, Visual Studio): should not matter

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions