Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-15188] Calling-convention mismatch violations between runtime, stdlib, and emitted code #57511

Closed
kateinoigakukun opened this issue Sep 12, 2021 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. platform support runtime The Swift Runtime standard library Area: Standard library umbrella WebAssembly Platform: WebAssembly

Comments

@kateinoigakukun
Copy link
Member

Previous ID SR-15188
Radar rdar://problem/83154000
Original Reporter @kateinoigakukun
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement
Assignee @kateinoigakukun
Priority Medium

md5: a717f3fd621f1634ebd1eeb736ab0650

Issue Description:

The current Swift implementation has 3 types of calling-convention mismatch in runtime, stdlib, and code emitted by the compiler.

These violations are one of the blockers of porting to WebAssembly because swiftcc and C-cc are not compatible on WebAssembly.
In our SwiftWasm fork, we applied a hacky patch to use the same calling convention for each function. However, it may break ABI on Darwin, so I want to ask compiler folks before submitting patches to the upstream.

The violations can be categorized into 4 types:

1. Defined as a C-cc in runtime but published as a Swift-cc public API in stdlib.

For example, `swift_retainCount` is declared and defined in runtime without explicit cc, so it's defined as a C-cc function. But it's published as a Swift-cc public API here in stdlib.
This means some external users can call the function assuming Swift-cc, but the actual implementation in runtime uses C-cc.

The other violating functions in the same way are:

  • swift_retainCount, swift_unownedRetainCount, swift_weakRetainCount, _swift_getObjectRuntimeFunctionCounters, _swift_getGlobalRuntimeFunctionCounters, and other RuntimeInvocationsTracking things

2. Emitted as a C-cc from user code, but called as a Swift-cc by stdlib indirectly.

For example, `keypath_get_arg_layout` are emitted as a C-cc function by the compiler, but it's called as a Swift-cc function in the stdlib.

  • Compiler emits the function here:

    auto layoutFn = llvm::Function::Create(fnTy,
    llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule());
    layoutFn->setAttributes(IGM.constructInitialAttributes());

  • stdlib calls the function here:

    internal typealias KeyPathComputedArgumentLayoutFn = @convention(thin)
    (_ patternArguments: UnsafeRawPointer?) -> (size: Int, alignmentMask: Int)

Other same cases:

  • keypath_get_arg_layout, keypath_destroy, keypath_copy, keypath_arg_init

3. Defined as a C-cc in runtime, but called as a Swift-cc in stdlib

For example, `_isClassType` is mainly called by compiler-emitted code and defined as a C-cc function. But it's also called by stdlib.
The call-site of stdlib uses @_silgen_name to link the function, so it's called through swiftcc.
Fortunately, the API is not exposed from Swift stdlib, so no external user calls it as a Swift-cc.

4. Defined as a C-cc in runtime, but called as a Swift-cc by test binary

`swift_demangle` is defined as a C-cc function in runtime, but called as a Swift-cc function from test case.

@typesanitizer
Copy link

@swift-ci create

@aschwaighofer
Copy link
Member

I don't see how any of these violations are not bugs.

We get away with them because the Swift Calling Convention matches the C calling convention very closely on darwin archs especially if only pointer sized arguments and no swift specific parameter attributes are used.

Changing the definition to swift cc should be a NFC for the cases you referenced on darwin archs, if they were not we would have issues today.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@kateinoigakukun
Copy link
Member Author

#66273 #69352 #69351 #39119 fixed all of those violations.

@AnthonyLatsis AnthonyLatsis added standard library Area: Standard library umbrella runtime The Swift Runtime platform support WebAssembly Platform: WebAssembly bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. and removed compiler The Swift compiler in itself improvement labels Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. platform support runtime The Swift Runtime standard library Area: Standard library umbrella WebAssembly Platform: WebAssembly
Projects
None yet
Development

No branches or pull requests

4 participants