Skip to content

[Distributed] thread-safety also for parameter type metadata #81123

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 19 additions & 36 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2907,25 +2907,6 @@ static NodePointer getParameterList(NodePointer funcType) {
return parameterContainer;
}

static const Metadata *decodeType(TypeDecoder<DecodedMetadataBuilder> &decoder,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely removing this method.

NodePointer type) {
assert(type->getKind() == Node::Kind::Type);

auto builtTypeOrError = decoder.decodeMangledType(type);

if (builtTypeOrError.isError()) {
auto err = builtTypeOrError.getError();
char *errStr = err->copyErrorString();
err->freeErrorString(errStr);
return nullptr;
}

if (!builtTypeOrError.getType().isMetadata())
return nullptr;

return builtTypeOrError.getType().getMetadata();
}

SWIFT_CC(swift)
SWIFT_RUNTIME_STDLIB_SPI
unsigned swift_func_getParameterCount(const char *typeNameStart,
Expand Down Expand Up @@ -3009,8 +2990,21 @@ swift_func_getParameterTypeInfo(

SubstGenericParametersFromMetadata substFn(genericEnv, genericArguments);

DecodedMetadataBuilder builder(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no other distributed impl funcs use this, so I'm hoping we're good.

The only other thing swift_func_getParameterCount does not seem like it has any of these problems.

demangler,
// for each parameter (TupleElement), store it into the provided buffer
for (unsigned index = 0; index != typesLength; ++index) {
auto nodePointer = parameterList->getChild(index);

if (nodePointer->getKind() == Node::Kind::TupleElement) {
assert(nodePointer->getNumChildren() == 1);
nodePointer = nodePointer->getFirstChild();
}
assert(nodePointer->getKind() == Node::Kind::Type);

auto request = MetadataRequest(MetadataState::Complete);

auto typeInfoOrErr = swift_getTypeByMangledNode(
request, demangler, nodePointer,
/*arguments=*/genericArguments,
/*substGenericParam=*/
[&substFn](unsigned depth, unsigned index) {
return substFn.getMetadata(depth, index).Ptr;
Expand All @@ -3019,24 +3013,13 @@ swift_func_getParameterTypeInfo(
[&substFn](const Metadata *type, unsigned index) {
return substFn.getWitnessTable(type, index);
});
TypeDecoder<DecodedMetadataBuilder> decoder(builder);

// for each parameter (TupleElement), store it into the provided buffer
for (unsigned index = 0; index != typesLength; ++index) {
auto *parameter = parameterList->getChild(index);

if (parameter->getKind() == Node::Kind::TupleElement) {
assert(parameter->getNumChildren() == 1);
parameter = parameter->getFirstChild();
}

assert(parameter->getKind() == Node::Kind::Type);

auto type = decodeType(decoder, parameter);
if (!type)
if (typeInfoOrErr.isError()) {
return -3; // Failed to decode a type.
}

types[index] = type;
auto typeInfo = typeInfoOrErr.getType();
types[index] = typeInfo.getMetadata();
} // end foreach parameter

return typesLength;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -target %target-swift-5.7-abi-triple %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name dist -target %target-swift-5.7-abi-triple -parse-as-library -j2 -parse-as-library -plugin-path %swift-plugin-dir -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out

// RUN: %target-run %t/a.out PARAMETER_TYPE
// RUN: %target-run %t/a.out RETURN_TYPE

// REQUIRES: executable_test
// REQUIRES: concurrency
Expand All @@ -17,7 +18,6 @@

import Darwin
import Distributed
import FakeDistributedActorSystems
import Foundation

typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
Expand Down Expand Up @@ -1052,20 +1052,39 @@ struct BigGeneric<T>: Codable {
}

distributed actor D {
public distributed func getBigGeneric(_ value: BigGeneric<TypeXXXX>) {}
public distributed func getBigGeneric() -> BigGeneric<TypeXXXX> {
return BigGeneric()
}
return BigGeneric()
}
}

func attempt(n: Int) {
var fname = "$s4dist1DC13getBigGenericAA0cD0VyAA8Type\(n)VGyF"
func attempt(_ mode: Mode, n: Int) {
var funcName_returnType = "$s4dist1DC13getBigGenericAA0cD0VyAA8Type\(n)VGyF"
var funcName_param = "$s4dist1DC13getBigGenericyyAA0cD0VyAA8Type\(n)VGF"

func tryLookup() {
let t = fname.withUTF8 {
__getReturnTypeInfo($0.baseAddress!, UInt($0.count), nil, nil)
}
let t: (any Any.Type)? =
switch mode {
case .returnType:
funcName_returnType.withUTF8 {
__getReturnTypeInfo($0.baseAddress!, UInt($0.count), nil, nil)
}

case .paramType:
funcName_param.withUTF8 { nameBuf in
var type: Any.Type?
withUnsafeMutablePointer(to: &type) { typePtr in
let ret = __getParameterTypeInfo(nameBuf.baseAddress!, UInt(nameBuf.count), nil, nil, typePtr._rawValue, 1)
if ret != 1 {
fatalError("Decoded \(ret) parameters, expected 1")
}
}
return type
}
}

guard let t else {
print("couldn't look up type for: \(fname)")
print("couldn't look up type for mode: \(mode)")
exit(1)
}
func examineType<T>(_t: T.Type) {
Expand All @@ -1081,10 +1100,23 @@ func attempt(n: Int) {
}
}

enum Mode: String {
case returnType = "RETURN_TYPE"
case paramType = "PARAMETER_TYPE"
}

@main struct Main {
static func main() {
if CommandLine.arguments.count < 2 {
fatalError("Expected explicit mode in command line arguments, was: \(CommandLine.arguments)")
}

let mode = Mode.init(rawValue: CommandLine.arguments.dropFirst().first!)!
print("Checking in mode: \(mode)...")

for i in 1000...1999 {
attempt(n: i)
attempt(mode, n: i)
}
print("Passed in mode: \(mode)!")
}
}
}