Skip to content

Fix calling convention support in Swift (breaks 32-bit Windows API calls). #85263

@al45tair

Description

@al45tair

Description

On 32-bit Windows, attempting to call Windows APIs directly will cause a crash, because the Win32 API generally uses the stdcall calling convention, in which the callee is responsible for popping its arguments off the stack (using the ret nn instruction), but Swift drops the calling convention on the floor and performs the call as if the Win32 API was using cdecl — the upshot being that both Swift and the Win32 API will pop the arguments from the stack, which leads to stack corruption and hard to debug crashes.

Reproduction

func foo() {
  let hStdin = GetStdHandle(STD_INPUT_HANDLE)
  print(hStdin)
}

foo()

should be sufficient to cause a crash.

Expected behavior

Swift should respect calling conventions on imported C APIs.

Environment

Swift 6.2. This actually affects more than just 32-bit Windows — it's just that that platform relies most heavily on foreign calling conventions that aren't compatible with cdecl or swiftcc.

Additional information

No response

Metadata

Metadata

Assignees

Labels

IRGenLLVM IR generationbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.clang importerArea → compiler: The clang importer

Type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions