Skip to content
Open
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
2 changes: 1 addition & 1 deletion doc/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Each `ServerField` invokes `PassField`, which:

`ServerRet` invokes the next layer (typically `ServerCall`), stores the result, and calls `BuildField` to serialize it into the `Results::Builder`.

`ServerCall` uses the generated `ProxyMethod<MethodParams>::impl` pointer-to-member to invoke the actual C++ method on the wrapped implementation object.
`ServerCall` uses the generated `ProxyMethod<Interface, MethodParams>::impl` pointer-to-member to invoke the actual C++ method on the wrapped implementation object.

```mermaid
sequenceDiagram
Expand Down
5 changes: 3 additions & 2 deletions include/mp/proxy-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ struct ServerCall
return TryFinally(
[&]() -> decltype(auto) {
return ProxyServerMethodTraits<
typename std::remove_reference_t<decltype(server_context.proxy_server)>::Interface,
typename decltype(server_context.call_context.getParams())::Reads
>::invoke(server_context, std::forward<Args>(args)...);
},
Expand Down Expand Up @@ -750,7 +751,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel

auto request = (proxy_client.m_client.*get_request)(nullptr);
using Request = CapRequestTraits<decltype(request)>;
using FieldList = typename ProxyClientMethodTraits<typename Request::Params>::Fields;
using FieldList = typename ProxyClientMethodTraits<typename ProxyClient::Interface, typename Request::Params>::Fields;
invoke_context.emplace(*proxy_client.m_context.connection, thread_context);
IterateFields().handleChain(*invoke_context, request, FieldList(), typename FieldObjs::BuildParams{&fields}...);
MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Debug)
Expand Down Expand Up @@ -836,7 +837,7 @@ kj::Promise<void> serverInvoke(Server& server, CallContext& call_context, Fn fn)

try {
using ServerContext = ServerInvokeContext<Server, CallContext>;
using ArgList = typename ProxyClientMethodTraits<typename Params::Reads>::Params;
using ArgList = typename ProxyClientMethodTraits<typename Server::Interface, typename Params::Reads>::Params;
ServerContext server_context{server, call_context, req};
// ReplaceVoid is used to support fn.invoke implementations that
// execute asynchronously and return promises, as well as
Expand Down
39 changes: 22 additions & 17 deletions include/mp/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ template <typename Interface> struct ProxyClient; // IWYU pragma: export
//! Mapping from capnp interface type to proxy server implementation (specializations are generated by
//! proxy-codegen.cpp).
template <typename Interface> struct ProxyServer; // IWYU pragma: export
//! Mapping from capnp method params type to method traits (specializations are generated by proxy-codegen.cpp).
template <typename Params> struct ProxyMethod; // IWYU pragma: export
//! Mapping from capnp interface and method params types to method traits
//! (specializations are generated by proxy-codegen.cpp). The interface type is
//! the wrapped interface handling the call, which may differ from the
//! interface declaring the method if the method is inherited.
template <typename Interface, typename Params> struct ProxyMethod; // IWYU pragma: export
//! Mapping from capnp struct type to struct traits (specializations are generated by proxy-codegen.cpp).
template <typename Struct> struct ProxyStruct; // IWYU pragma: export
//! Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen.cpp).
Expand Down Expand Up @@ -247,16 +250,18 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)>
//! an additional invoke() method that calls the C++ method which is being
//! proxied, forwarding any arguments.
//!
//! The template argument should be the InterfaceName::MethodNameParams class
//! (generated by Cap'n Proto) associated with the method.
//! The template arguments should be the wrapped InterfaceName class handling the
//! call and the MethodNameParams class (generated by Cap'n Proto) associated
//! with the method. If the method is inherited, the params class is declared by
//! the base interface, e.g. BaseInterfaceName::MethodNameParams.
//!
//! Note: The class definition here is just the fallback definition used when
//! the other specialization below doesn't match. The fallback is only used for
//! capnp methods which do not have corresponding C++ methods, which in practice
//! is just the two special construct() and destroy() methods described in \ref
//! ProxyClientBase. These methods don't have any C++ parameters or return
//! types, so the trait information below reflects that.
template <typename MethodParams, typename Enable = void>
template <typename Interface, typename MethodParams, typename Enable = void>
struct ProxyMethodTraits
{
using Params = TypeList<>;
Expand All @@ -270,39 +275,39 @@ struct ProxyMethodTraits
};

//! Specialization of above for proxy methods that have a
//! ProxyMethod<InterfaceName::MethodNameParams>::impl pointer-to-method
//! constant defined by generated code. This includes all functions defined in
//! the capnp interface except any construct() or destroy() methods, that are
//! ProxyMethod<InterfaceName, MethodParams>::impl pointer-to-method constant
//! defined by generated code. This includes all functions defined or inherited
//! by the capnp interface except any construct() or destroy() methods, that are
//! assumed not to correspond to real member functions in the C++ class, and
//! will use the fallback traits definition above. The generated code this
//! specialization relies on looks like:
//!
//! struct ProxyMethod<InterfaceName::MethodNameParams>
//! struct ProxyMethod<InterfaceName, InterfaceName::MethodNameParams>
//! {
//! static constexpr auto impl = &ClassName::methodName;
//! };
template <typename MethodParams>
struct ProxyMethodTraits<MethodParams, Require<decltype(ProxyMethod<MethodParams>::impl)>>
: public FunctionTraits<decltype(ProxyMethod<MethodParams>::impl)>
template <typename Interface, typename MethodParams>
struct ProxyMethodTraits<Interface, MethodParams, Require<decltype(ProxyMethod<Interface, MethodParams>::impl)>>
: public FunctionTraits<decltype(ProxyMethod<Interface, MethodParams>::impl)>
{
template <typename ServerContext, typename... Args>
static decltype(auto) invoke(ServerContext& server_context, Args&&... args)
{
return (server_context.proxy_server.m_impl.get()->*ProxyMethod<MethodParams>::impl)(std::forward<Args>(args)...);
return (server_context.proxy_server.m_impl.get()->*ProxyMethod<Interface, MethodParams>::impl)(std::forward<Args>(args)...);
}
};

//! Customizable (through template specialization) traits class used in generated ProxyClient implementations from
//! proxy-codegen.cpp.
template <typename MethodParams>
struct ProxyClientMethodTraits : public ProxyMethodTraits<MethodParams>
template <typename Interface, typename MethodParams>
struct ProxyClientMethodTraits : public ProxyMethodTraits<Interface, MethodParams>
{
};

//! Customizable (through template specialization) traits class used in generated ProxyServer implementations from
//! proxy-codegen.cpp.
template <typename MethodParams>
struct ProxyServerMethodTraits : public ProxyMethodTraits<MethodParams>
template <typename Interface, typename MethodParams>
struct ProxyServerMethodTraits : public ProxyMethodTraits<Interface, MethodParams>
{
};

Expand Down
13 changes: 9 additions & 4 deletions src/mp/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,14 @@ static void Generate(kj::StringPtr src_prefix,
}
fields.mergeFields();

if (!is_construct && !is_destroy && (&method_interface == &interface)) {
// Generate a ProxyMethod specialization for every method,
// including inherited methods, so each wrapped interface maps
// the methods it handles to its own C++ class. The interface
// declaring an inherited method may be wrapped by a different
// C++ class, or not be wrapped at all.
if (!is_construct && !is_destroy) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In commit 08f72b0: mpgen: Key ProxyMethod and method traits by interface type

I was wondering what happens if the c++ subclass adds an overload of an inherited method.

FooCallback already declares call(int). I added these two lines to ExtendedCallback in test/foo.h

using FooCallback::call;
int call(double arg) { return static_cast<int>(arg); }

This makes both FooCallback::call(int) and ExtendedCallback::call(double) visible through ExtendedCallback.

Previously, the inherited method used &FooCallback::call, which identifies call(int). The new mapping uses &ExtendedCallback::call, where both overloads are visible.

With these lines added mptest fails to build with:

error: unable to deduce ‘const auto’ from
‘& mp::test::ExtendedCallback::call’

Could we select the c++ class before taking the method pointer? I worked up this possible solution in gen.cpp

index e27b1d2..bb5f5ef 100644
--- i/src/mp/gen.cpp
+++ w/src/mp/gen.cpp
@@ -551,7 +551,19 @@ static void Generate(kj::StringPtr src_prefix,
                     methods << "template<>\n";
                     methods << "struct ProxyMethod<" << message_namespace << "::" << node_name << ", " << method_prefix << "Params>\n";
                     methods << "{\n";
-                    methods << "    static constexpr auto impl = &" << proxied_class_type
+                    kj::StringPtr method_class_type = proxied_class_type;
+                    kj::StringPtr declaring_class_type;
+                    if (&method_interface != &interface &&
+                        GetAnnotationText(method_interface.getProto(), WRAP_ANNOTATION_ID, &declaring_class_type) &&
+                        declaring_class_type.size()) {
+                        // Preserve the declaring C++ base's lookup when possible,
+                        // so overloads added by the derived class do not make it ambiguous.
+                        methods << "    using Impl = std::conditional_t<std::is_convertible_v<"
+                                << proxied_class_type << "*, " << declaring_class_type << "*>, "
+                                << declaring_class_type << ", " << proxied_class_type << ">;\n";
+                        method_class_type = "Impl";
+                    }
+                    methods << "    static constexpr auto impl = &" << method_class_type
                             << "::" << proxied_method_name << ";\n";
                     methods << "};\n\n";

There might be a better solution to this, but the reasoning here is that, where the receiving c++ class derives publicly and unambiguously from the declaring class, we can preserve the old base lookup. Here, Impl becomes FooCallback, avoiding the additional overload.

For unrelated c++ classes, the condition is false and we retain the receiving class’s lookup. This selection would only be emitted for inherited methods with a wrapped base and unwrapped bases keep the direct lookup.

With the proposed fix, mptest builds with the added overload. I haven’t found a current Bitcoin Core interface affected by this, and the ambiguity doesn’t arise when the inherited and added methods have distinct c++ names. But the PR does break a previously working libmultiprocess arrangement

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

re: #377 (comment)

FooCallback already declares call(int). I added these two lines to ExtendedCallback in test/foo.h

using FooCallback::call;
int call(double arg) { return static_cast<int>(arg); }

Wow this is an interesting thing to test. My first instinct is that it's good for this to trigger an error because interfaces wrapped by libmultiprocess already have a bunch of restrictions and one listed there is "Interface methods should not be overloaded" (because capnproto doesn't support method overloading, and this helps it be more language-independent).

But maybe overloading could be good to support in a derived class, and I"m thinking in particular of cases where base class may be wrapped and exposed over IPC and derived class is an unwrapped normal c++ class inheriting from some implementation of the interface. This type of class should be free to have its own methods without the same restrictions. I'm assuming this does not work today, so would not want to add support for that here, but I think the change you suggested could make sense as a followup especially if there is a motivating use-case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for pointing out that restriction. I hadn’t accounted for it when testing the wrapped ExtendedCallback case. Agreed that this doesn’t need to expand the scope of this PR.

I also tried the unwrapped subclass case you mentioned; a normal c++ subclass inheriting from an implementation, adding a local overload, and being passed through the wrapped base interface. That builds and passes the ipc test, so that particular arrangement already appears to work.

methods << "template<>\n";
methods << "struct ProxyMethod<" << method_prefix << "Params>\n";
methods << "struct ProxyMethod<" << message_namespace << "::" << node_name << ", " << method_prefix << "Params>\n";
methods << "{\n";
methods << " static constexpr auto impl = &" << proxied_class_type
<< "::" << proxied_method_name << ";\n";
Expand Down Expand Up @@ -611,8 +616,8 @@ static void Generate(kj::StringPtr src_prefix,
const std::string super_str{is_construct || is_destroy ? "Super& super" : ""};
const std::string self_str{is_construct || is_destroy ? "super" : "*this"};

client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << method_prefix
<< "Params>;\n";
client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << message_namespace
<< "::" << node_name << ", " << method_prefix << "Params>;\n";
client << " " << static_str << "typename M" << method_ordinal << "::Result " << method_name << "("
<< super_str << client_args.str() << ")";
client << ";\n";
Expand Down
1 change: 1 addition & 0 deletions test/mp/test/foo-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace mp {
namespace test {
namespace messages {
struct ExtendedCallback; // IWYU pragma: export
struct FlatCallback; // IWYU pragma: export
struct FooCallback; // IWYU pragma: export
struct FooFn; // IWYU pragma: export
struct FooInterface; // IWYU pragma: export
Expand Down
12 changes: 12 additions & 0 deletions test/mp/test/foo.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") {
passDataPointers @22 (arg :List(Data)) -> (result :List(Data));
listBars @25 (context :Proxy.Context, n :Int32) -> (result :List(BarInterface));
callMessageAsync @26 (context :Proxy.Context) -> (result :FooMessage);
callbackFlat @27 (context :Proxy.Context, callback :FlatCallback, arg: Int32) -> (result :Int32);
}

interface FooInit $Proxy.wrap("mp::test::FooInit") {
Expand All @@ -54,6 +55,17 @@ interface ExtendedCallback extends(FooCallback) $Proxy.wrap("mp::test::ExtendedC
callExtended @0 (context :Proxy.Context, arg :Int32) -> (result :Int32);
}

# Interface without a $Proxy.wrap annotation, so no C++ class corresponds to
# it. Its methods are handled by C++ classes wrapping derived interfaces.
interface CallbackBase {
destroy @0 (context :Proxy.Context) -> ();
call @1 (context :Proxy.Context, arg :Int32) -> (result :Int32);
}

interface FlatCallback extends(CallbackBase) $Proxy.wrap("mp::test::FlatCallback") {
callExtended @0 (context :Proxy.Context, arg :Int32) -> (result :Int32);
}

interface BarInterface $Proxy.wrap("mp::test::Bar") {
destroy @0 (context :Proxy.Context) -> ();
value @1 (context :Proxy.Context) -> (result :Int32);
Expand Down
12 changes: 12 additions & 0 deletions test/mp/test/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ class ExtendedCallback : public FooCallback
virtual int callExtended(int arg) = 0;
};

//! C++ class wrapped by the FlatCallback capnp interface, which inherits its
//! call method from the CallbackBase capnp interface. Unlike ExtendedCallback,
//! there is no C++ base class corresponding to the capnp base interface.
class FlatCallback
{
public:
virtual ~FlatCallback() = default;
virtual int call(int arg) = 0;
virtual int callExtended(int arg) = 0;
};

class FooInit
{
};
Expand Down Expand Up @@ -104,6 +115,7 @@ class FooImplementation
void saveCallback(std::shared_ptr<FooCallback> callback) { m_callback = std::move(callback); }
int callbackSaved(int arg) { return m_callback->call(arg); }
int callbackExtended(ExtendedCallback& callback, int arg) { return callback.callExtended(arg); }
int callbackFlat(FlatCallback& callback, int arg) { return callback.call(arg) + callback.callExtended(arg); }
FooCustom passCustom(FooCustom foo) { return foo; }
FooEmpty passEmpty(FooEmpty foo) { return foo; }
FooData passData(FooData foo) { return foo; }
Expand Down
9 changes: 9 additions & 0 deletions test/mp/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ KJ_TEST("Call FooInterface methods")
KJ_EXPECT(saved.use_count() == 1);
KJ_EXPECT(foo->callbackExtended(callback, 11) == 12);

class FlatCallbackImpl : public FlatCallback
{
public:
int call(int arg) override { return arg + 1; }
int callExtended(int arg) override { return arg * 10; }
};
FlatCallbackImpl flat_callback;
KJ_EXPECT(foo->callbackFlat(flat_callback, 2) == 23);

FooCustom custom_in;
custom_in.v1 = "v1";
custom_in.v2 = 5;
Expand Down
Loading