Skip to content

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

@grynspan

Description

@grynspan
Contributor

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

Activity

added
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.
triage neededThis issue needs more specific labels
on Mar 18, 2024
changed the title [-]WASI/WASM: `withVaList(_:)` produces a va_list with the wrong type[/-] [+]WASI/Wasm: `withVaList(_:)` produces a va_list with the wrong type[/+] on Mar 18, 2024
MaxDesiatov

MaxDesiatov commented on Mar 18, 2024

@MaxDesiatov
Contributor
kateinoigakukun

kateinoigakukun commented on Mar 18, 2024

@kateinoigakukun
Member

We might need to re-consider #31692

added
clang importerArea → compiler: The clang importer
and removed
triage neededThis issue needs more specific labels
on Mar 18, 2024
MaxDesiatov

MaxDesiatov commented on Mar 18, 2024

@MaxDesiatov
Contributor

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

grynspan

grynspan commented on Mar 18, 2024

@grynspan
ContributorAuthor
MaxDesiatov

MaxDesiatov commented on Mar 18, 2024

@MaxDesiatov
Contributor

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

grynspan commented on Mar 18, 2024

@grynspan
ContributorAuthor

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

al45tair

al45tair commented on Mar 22, 2024

@al45tair
Contributor

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

grynspan commented on Mar 25, 2024

@grynspan
ContributorAuthor

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

turbolent

turbolent commented on May 11, 2024

@turbolent

@grynspan How did you work around the issue?

grynspan

grynspan commented on May 13, 2024

@grynspan
ContributorAuthor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    WebAssemblyPlatform: WebAssemblybugA deviation from expected or documented behavior. Also: expected but undesirable behavior.clang importerArea → compiler: The clang importerstandard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @turbolent@MaxDesiatov@al45tair@grynspan@kateinoigakukun

        Issue actions

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