swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.39k stars 10.35k forks source link

WASI/Wasm: `withVaList(_:)` produces a va_list with the wrong type #72398

Open grynspan opened 6 months ago

grynspan commented 6 months ago

Description

swift-testing uses withVaList(_:) in one spot and, when building for WASI/Wasm, runs into this error:

/swift-testing/Sources/Testing/Events/TimeValue.swift:87:79: error: cannot convert value of type 'CVaListPointer' to expected argument type '__isoc_va_list?' (aka 'Optional<UnsafeMutableRawPointer>')
        _ = vsnprintf(buffer.baseAddress!, buffer.count, "%lld.%03d seconds", args)

Looks like either CVaListPointer is typealiased incorrectly or needs to be implemented.

Reproduction

Try to build the following code for WASI/Wasm:

let string = withUnsafeTemporaryAllocation(of: CChar.self, capacity: 512) { buffer in
  withVaList([CInt(123)]) { args in
    _ = vsnprintf(buffer.baseAddress!, buffer.count, "%d", args)
  }
  return String(cString: buffer.baseAddress!)
}
print(string)

Expected behavior

Prints 123.

Environment

SwiftWasm Swift version 5.10-dev (LLVM 5dc9d563e5a6cd2, Swift 3aaeb1a91e1c0a5) Target: arm64-apple-darwin23.4.0

Additional information

No response

MaxDesiatov commented 6 months ago

cc @kateinoigakukun

kateinoigakukun commented 6 months ago

We might need to re-consider https://github.com/apple/swift/pull/31692

MaxDesiatov commented 6 months ago

I'm curious if @al45tair has seen this with Musl previously?

grynspan commented 6 months ago

Is it just a matter of adding a mapping here? https://github.com/apple/swift/blob/main/lib/ClangImporter/MappedTypes.def#L131

MaxDesiatov commented 6 months ago

That's what my previous attempt to fix it was about https://github.com/apple/swift/pull/31692/files#diff-794a8ab7c1a5c80032405934f97c0e1e0d6b6acabd9a308c61c4dbd563dff48d. But I couldn't get the test passing on x86_64 macOS, even though it passed on all other platforms.

grynspan commented 6 months ago

Looks like the runs have been purged from CI… might be worth trying again just to get an up-to-date CI failure.

al45tair commented 6 months ago

I'm curious if @al45tair has seen this with Musl previously?

I haven't, but then I haven't really tried using C varargs functions and I would imagine there isn't that much Swift code out there that does. It's entirely possible that this is broken in the fully static SDK too.

grynspan commented 6 months ago

I worked around the issue in my case, but it'd be nice to not have to.

turbolent commented 4 months ago

@grynspan How did you work around the issue?

grynspan commented 4 months ago

Our particular use case was for custom formatting of time intervals as strings, so we just used String(describing:) instead and traded the custom format for the default one on WASI. This is probably not a general solution.