Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74a54454 authored by Andrei Homescu's avatar Andrei Homescu
Browse files

libbinder: build on Trusty

Adds a new trusty/ subdirectory containing all the files
needed to build Binder RPC on Trusty, including:
* A RpcServerTrusty class used to create Trusty Binder services
* The underlying RpcTransportTipcTrusty transport that interfaces
  between libbinder and TIPC
* Trusty implementations of some OS-specific functionality, like logging
* Make-based build file for libbinder in the Trusty build system

Bug: 224644083
Test: build Trusty
Change-Id: I25b97736d41489d20c2dd266e8e110764215378c
parent 66aaab47
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -209,9 +209,10 @@ void RpcServer::join() {

        {
            RpcMutexLockGuard _l(mLock);
            RpcMaybeThread thread = RpcMaybeThread(&RpcServer::establishConnection,
                                                   sp<RpcServer>::fromExisting(this),
                                                   std::move(clientFd), addr, addrLen);
            RpcMaybeThread thread =
                    RpcMaybeThread(&RpcServer::establishConnection,
                                   sp<RpcServer>::fromExisting(this), std::move(clientFd), addr,
                                   addrLen, RpcSession::join);

            auto& threadRef = mConnectingThreads[thread.get_id()];
            threadRef = std::move(thread);
@@ -294,8 +295,10 @@ size_t RpcServer::numUninitializedSessions() {
    return mConnectingThreads.size();
}

void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd,
                                    std::array<uint8_t, kRpcAddressSize> addr, size_t addrLen) {
void RpcServer::establishConnection(
        sp<RpcServer>&& server, base::unique_fd clientFd, std::array<uint8_t, kRpcAddressSize> addr,
        size_t addrLen,
        std::function<void(sp<RpcSession>&&, RpcSession::PreJoinSetupResult&&)>&& joinFn) {
    // mShutdownTrigger can only be cleared once connection threads have joined.
    // It must be set before this thread is started
    LOG_ALWAYS_FATAL_IF(server->mShutdownTrigger == nullptr);
@@ -478,7 +481,7 @@ void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clie
    // avoid strong cycle
    server = nullptr;

    RpcSession::join(std::move(session), std::move(setupResult));
    joinFn(std::move(session), std::move(setupResult));
}

status_t RpcServer::setupSocketServer(const RpcSocketAddress& addr) {
+6 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
namespace android {

class FdTrigger;
class RpcServerTrusty;
class RpcSocketAddress;

/**
@@ -189,6 +190,7 @@ public:
    ~RpcServer();

private:
    friend RpcServerTrusty;
    friend sp<RpcServer>;
    explicit RpcServer(std::unique_ptr<RpcTransportCtx> ctx);

@@ -196,8 +198,10 @@ private:
    void onSessionIncomingThreadEnded() override;

    static constexpr size_t kRpcAddressSize = 128;
    static void establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd,
                                    std::array<uint8_t, kRpcAddressSize> addr, size_t addrLen);
    static void establishConnection(
            sp<RpcServer>&& server, base::unique_fd clientFd,
            std::array<uint8_t, kRpcAddressSize> addr, size_t addrLen,
            std::function<void(sp<RpcSession>&&, RpcSession::PreJoinSetupResult&&)>&& joinFn);
    [[nodiscard]] status_t setupSocketServer(const RpcSocketAddress& address);

    const std::unique_ptr<RpcTransportCtx> mCtx;
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ namespace android {

class Parcel;
class RpcServer;
class RpcServerTrusty;
class RpcSocketAddress;
class RpcState;
class RpcTransport;
@@ -202,6 +203,7 @@ public:
private:
    friend sp<RpcSession>;
    friend RpcServer;
    friend RpcServerTrusty;
    friend RpcState;
    explicit RpcSession(std::unique_ptr<RpcTransportCtx> ctx);

+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 <openssl/rand.h>

#include "../OS.h"

using android::base::Result;

namespace android {

Result<void> setNonBlocking(android::base::borrowed_fd fd) {
    // Trusty IPC syscalls are all non-blocking by default.
    return {};
}

status_t getRandomBytes(uint8_t* data, size_t size) {
    int res = RAND_bytes(data, size);
    return res == 1 ? OK : UNKNOWN_ERROR;
}

} // namespace android
+39 −0
Original line number Diff line number Diff line
# Binder for Trusty

This is the Trusty port of the libbinder library.
To build it, take the following steps:

* Check out copies of the Trusty and AOSP repositories.
* Apply the patches from the `trusty_binder` topic on both repositories.
* Build Trusty normally using `build.py`.
* Run the sample AIDL test for Trusty:
  ```shell
  $ ./build-root/.../run --headless --boot-test com.android.trusty.aidl.test
  ```

To run the Android-Trusty IPC test, do the following:

* Build AOSP for the `qemu_trusty_arm64-userdebug` target:
  ```shell
  $ lunch qemu_trusty_arm64-userdebug
  $ m
  ```
* In the Trusty directory, run the emulator with the newly built Android:
  ```shell
  $ ./build-root/.../run --android /path/to/aosp
  ```
* Using either `adb` or the shell inside the emulator itself, run the Trusty
  Binder test as root:
  ```shell
  # /data/nativetest64/vendor/trusty_binder_test/trusty_binder_test
  ```

## Running the AIDL compiler
For now, you will need to run the AIDL compiler manually to generate the C++
source code for Trusty clients and services. The general syntax is:
```shell
$ aidl --lang=cpp -o <output directory> -h <output header directory> <AIDL files...>
```

The compiler will emit some `.cpp` files in the output directory and their
corresponding `.h` files in the header directory.
Loading