diff options
author | Andrei Homescu <ahomescu@google.com> | 2024-03-22 22:52:43 +0000 |
---|---|---|
committer | Andrei Homescu <ahomescu@google.com> | 2024-04-11 06:25:46 +0000 |
commit | a800ac7b2c26426e62e23a2e65325ebe3207e2a4 (patch) | |
tree | 37de22fef2971e3f4241f0bc9b713bcbb314b8ce | |
parent | ab2daeca28b23a164a8bb5100e3e39a4092a7689 (diff) | |
download | native-a800ac7b2c26426e62e23a2e65325ebe3207e2a4.tar.gz |
libbinder: Add ARpcServerTrusty API
Add an API for Rust code to create and call into
RpcServerTrusty objects.
Bug: 242243245
Test: trusty/vendor/google/aosp/scripts/build.py \
--test "boot-test:com.android.trusty.rust.binder_rpc_test.test" \
qemu-generic-arm64-test-debug
Change-Id: Iaaf255d9df59c62923dcc894d365e93beaec6c66
6 files changed, 229 insertions, 0 deletions
diff --git a/libs/binder/trusty/include/binder/ARpcServerTrusty.h b/libs/binder/trusty/include/binder/ARpcServerTrusty.h new file mode 100644 index 0000000000..c82268b47b --- /dev/null +++ b/libs/binder/trusty/include/binder/ARpcServerTrusty.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include <lib/tipc/tipc_srv.h> + +#if defined(__cplusplus) +extern "C" { +#endif + +struct AIBinder; +struct ARpcServerTrusty; + +struct ARpcServerTrusty* ARpcServerTrusty_newPerSession(struct AIBinder* (*)(const void*, size_t, + char*), + char*, void (*)(char*)); +void ARpcServerTrusty_delete(struct ARpcServerTrusty*); +int ARpcServerTrusty_handleConnect(struct ARpcServerTrusty*, handle_t, const struct uuid*, void**); +int ARpcServerTrusty_handleMessage(void*); +void ARpcServerTrusty_handleDisconnect(void*); +void ARpcServerTrusty_handleChannelCleanup(void*); + +#if defined(__cplusplus) +} +#endif diff --git a/libs/binder/trusty/include/binder/RpcServerTrusty.h b/libs/binder/trusty/include/binder/RpcServerTrusty.h index bd1d90f588..fe44ea5e28 100644 --- a/libs/binder/trusty/include/binder/RpcServerTrusty.h +++ b/libs/binder/trusty/include/binder/RpcServerTrusty.h @@ -16,6 +16,7 @@ #pragma once +#include <binder/ARpcServerTrusty.h> #include <binder/IBinder.h> #include <binder/RpcServer.h> #include <binder/RpcSession.h> @@ -100,6 +101,16 @@ private: return rpcServer; } + friend struct ::ARpcServerTrusty; + friend ::ARpcServerTrusty* ::ARpcServerTrusty_newPerSession(::AIBinder* (*)(const void*, size_t, + char*), + char*, void (*)(char*)); + friend void ::ARpcServerTrusty_delete(::ARpcServerTrusty*); + friend int ::ARpcServerTrusty_handleConnect(::ARpcServerTrusty*, handle_t, const uuid*, void**); + friend int ::ARpcServerTrusty_handleMessage(void*); + friend void ::ARpcServerTrusty_handleDisconnect(void*); + friend void ::ARpcServerTrusty_handleChannelCleanup(void*); + // The Rpc-specific context maintained for every open TIPC channel. struct ChannelContext { sp<RpcSession> session; diff --git a/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/ARpcServerTrusty.cpp b/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/ARpcServerTrusty.cpp new file mode 100644 index 0000000000..451383a90a --- /dev/null +++ b/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/ARpcServerTrusty.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <android/binder_libbinder.h> +#include <binder/RpcServer.h> +#include <binder/RpcServerTrusty.h> +#include <binder/RpcSession.h> +#include <binder/RpcTransportTipcTrusty.h> + +using android::RpcServer; +using android::RpcServerTrusty; +using android::RpcSession; +using android::RpcTransportCtxFactoryTipcTrusty; +using android::sp; +using android::wp; + +struct ARpcServerTrusty { + sp<RpcServer> mRpcServer; + + ARpcServerTrusty() = delete; + ARpcServerTrusty(sp<RpcServer> rpcServer) : mRpcServer(std::move(rpcServer)) {} +}; + +ARpcServerTrusty* ARpcServerTrusty_newPerSession(AIBinder* (*cb)(const void*, size_t, char*), + char* cbArg, void (*cbArgDeleter)(char*)) { + std::shared_ptr<char> cbArgSp(cbArg, cbArgDeleter); + + auto rpcTransportCtxFactory = RpcTransportCtxFactoryTipcTrusty::make(); + if (rpcTransportCtxFactory == nullptr) { + return nullptr; + } + + auto ctx = rpcTransportCtxFactory->newServerCtx(); + if (ctx == nullptr) { + return nullptr; + } + + auto rpcServer = RpcServerTrusty::makeRpcServer(std::move(ctx)); + if (rpcServer == nullptr) { + return nullptr; + } + + rpcServer->setPerSessionRootObject( + [cb, cbArgSp](wp<RpcSession> /*session*/, const void* addrPtr, size_t len) { + auto* aib = (*cb)(addrPtr, len, cbArgSp.get()); + auto b = AIBinder_toPlatformBinder(aib); + + // We have a new sp<IBinder> backed by the same binder, so we can + // finally release the AIBinder* from the callback + AIBinder_decStrong(aib); + + return b; + }); + + return new (std::nothrow) ARpcServerTrusty(std::move(rpcServer)); +} + +void ARpcServerTrusty_delete(ARpcServerTrusty* rstr) { + delete rstr; +} + +int ARpcServerTrusty_handleConnect(ARpcServerTrusty* rstr, handle_t chan, const uuid* peer, + void** ctx_p) { + return RpcServerTrusty::handleConnectInternal(rstr->mRpcServer.get(), chan, peer, ctx_p); +} + +int ARpcServerTrusty_handleMessage(void* ctx) { + return RpcServerTrusty::handleMessageInternal(ctx); +} + +void ARpcServerTrusty_handleDisconnect(void* ctx) { + RpcServerTrusty::handleDisconnectInternal(ctx); +} + +void ARpcServerTrusty_handleChannelCleanup(void* ctx) { + RpcServerTrusty::handleChannelCleanup(ctx); +} diff --git a/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/rules.mk b/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/rules.mk new file mode 100644 index 0000000000..6def6343fa --- /dev/null +++ b/libs/binder/trusty/rust/binder_rpc_server_bindgen/cpp/rules.mk @@ -0,0 +1,29 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_DIR := $(GET_LOCAL_DIR) +LIBBINDER_DIR := $(LOCAL_DIR)/../../../.. + +MODULE := $(LOCAL_DIR) + +MODULE_SRCS := \ + $(LOCAL_DIR)/ARpcServerTrusty.cpp \ + +MODULE_LIBRARY_DEPS += \ + $(LIBBINDER_DIR)/trusty \ + $(LIBBINDER_DIR)/trusty/ndk \ + trusty/user/base/lib/libstdc++-trusty \ + +include make/library.mk diff --git a/libs/binder/trusty/rust/binder_rpc_server_bindgen/lib.rs b/libs/binder/trusty/rust/binder_rpc_server_bindgen/lib.rs new file mode 100644 index 0000000000..2e8b3ecb03 --- /dev/null +++ b/libs/binder/trusty/rust/binder_rpc_server_bindgen/lib.rs @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! Generated Rust bindings to binder_rpc_server + +#[allow(bad_style)] +mod sys { + include!(env!("BINDGEN_INC_FILE")); +} + +pub use sys::*; diff --git a/libs/binder/trusty/rust/binder_rpc_server_bindgen/rules.mk b/libs/binder/trusty/rust/binder_rpc_server_bindgen/rules.mk new file mode 100644 index 0000000000..4ee333f5be --- /dev/null +++ b/libs/binder/trusty/rust/binder_rpc_server_bindgen/rules.mk @@ -0,0 +1,37 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_DIR := $(GET_LOCAL_DIR) +LIBBINDER_DIR := $(LOCAL_DIR)/../../.. + +MODULE := $(LOCAL_DIR) + +MODULE_SRCS := $(LOCAL_DIR)/lib.rs + +MODULE_CRATE_NAME := binder_rpc_server_bindgen + +MODULE_LIBRARY_DEPS += \ + $(LOCAL_DIR)/cpp \ + trusty/user/base/lib/libstdc++-trusty \ + trusty/user/base/lib/trusty-sys \ + +MODULE_BINDGEN_SRC_HEADER := \ + $(LIBBINDER_DIR)/trusty/include/binder/ARpcServerTrusty.h + +MODULE_BINDGEN_FLAGS += \ + --allowlist-type="ARpcServerTrusty" \ + --allowlist-function="ARpcServerTrusty_.*" \ + +include make/library.mk |