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

Commit 6d82c8a4 authored by Yifan Hong's avatar Yifan Hong
Browse files

binderRpcTest: pick ephermeral port

Test: while htest binderRpcTest; do date; done

Fixes: 186468965

Change-Id: I17f0eddda1b6c55c0f500f721fd4b755d05580c5
parent b00ff661
Loading
Loading
Loading
Loading
+41 −10
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
#include <BnBinderRpcSession.h>
#include <BnBinderRpcSession.h>
#include <BnBinderRpcTest.h>
#include <BnBinderRpcTest.h>
#include <aidl/IBinderRpcTest.h>
#include <aidl/IBinderRpcTest.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android/binder_auto_utils.h>
#include <android/binder_auto_utils.h>
#include <android/binder_libbinder.h>
#include <android/binder_libbinder.h>
@@ -176,14 +177,27 @@ public:
};
};
sp<IBinder> MyBinderRpcTest::mHeldBinder;
sp<IBinder> MyBinderRpcTest::mHeldBinder;


class Pipe {
public:
    Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); }
    Pipe(Pipe&&) = default;
    android::base::borrowed_fd readEnd() { return mRead; }
    android::base::borrowed_fd writeEnd() { return mWrite; }

private:
    android::base::unique_fd mRead;
    android::base::unique_fd mWrite;
};

class Process {
class Process {
public:
public:
    Process(const std::function<void()>& f) {
    Process(Process&&) = default;
    Process(const std::function<void(Pipe*)>& f) {
        if (0 == (mPid = fork())) {
        if (0 == (mPid = fork())) {
            // racey: assume parent doesn't crash before this is set
            // racey: assume parent doesn't crash before this is set
            prctl(PR_SET_PDEATHSIG, SIGHUP);
            prctl(PR_SET_PDEATHSIG, SIGHUP);


            f();
            f(&mPipe);
        }
        }
    }
    }
    ~Process() {
    ~Process() {
@@ -191,9 +205,11 @@ public:
            kill(mPid, SIGKILL);
            kill(mPid, SIGKILL);
        }
        }
    }
    }
    Pipe* getPipe() { return &mPipe; }


private:
private:
    pid_t mPid = 0;
    pid_t mPid = 0;
    Pipe mPipe;
};
};


static std::string allocateSocketAddress() {
static std::string allocateSocketAddress() {
@@ -215,6 +231,7 @@ struct ProcessConnection {
    // whether connection should be invalidated by end of run
    // whether connection should be invalidated by end of run
    bool expectInvalid = false;
    bool expectInvalid = false;


    ProcessConnection(ProcessConnection&&) = default;
    ~ProcessConnection() {
    ~ProcessConnection() {
        rootBinder = nullptr;
        rootBinder = nullptr;
        EXPECT_NE(nullptr, connection);
        EXPECT_NE(nullptr, connection);
@@ -238,6 +255,7 @@ struct BinderRpcTestProcessConnection {
    // pre-casted root object
    // pre-casted root object
    sp<IBinderRpcTest> rootIface;
    sp<IBinderRpcTest> rootIface;


    BinderRpcTestProcessConnection(BinderRpcTestProcessConnection&&) = default;
    ~BinderRpcTestProcessConnection() {
    ~BinderRpcTestProcessConnection() {
        if (!proc.expectInvalid) {
        if (!proc.expectInvalid) {
            int32_t remoteBinders = 0;
            int32_t remoteBinders = 0;
@@ -286,11 +304,11 @@ public:


        std::string addr = allocateSocketAddress();
        std::string addr = allocateSocketAddress();
        unlink(addr.c_str());
        unlink(addr.c_str());
        static unsigned int port = 3456;
        static unsigned int vsockPort = 3456;
        port++;
        vsockPort++;


        auto ret = ProcessConnection{
        auto ret = ProcessConnection{
                .host = Process([&] {
                .host = Process([&](Pipe* pipe) {
                    sp<RpcServer> server = RpcServer::make();
                    sp<RpcServer> server = RpcServer::make();


                    server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
                    server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
@@ -304,12 +322,17 @@ public:
                            break;
                            break;
#ifdef __BIONIC__
#ifdef __BIONIC__
                        case SocketType::VSOCK:
                        case SocketType::VSOCK:
                            CHECK(connection->setupVsockServer(port));
                            CHECK(connection->setupVsockServer(vsockPort));
                            break;
                            break;
#endif // __BIONIC__
#endif // __BIONIC__
                        case SocketType::INET:
                        case SocketType::INET: {
                            CHECK(connection->setupInetServer(port));
                            unsigned int outPort = 0;
                            CHECK(connection->setupInetServer(0, &outPort));
                            CHECK_NE(0, outPort);
                            CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort,
                                                            sizeof(outPort)));
                            break;
                            break;
                        }
                        default:
                        default:
                            LOG_ALWAYS_FATAL("Unknown socket type");
                            LOG_ALWAYS_FATAL("Unknown socket type");
                    }
                    }
@@ -327,6 +350,13 @@ public:
                .connection = RpcConnection::make(),
                .connection = RpcConnection::make(),
        };
        };


        unsigned int inetPort = 0;
        if (socketType == SocketType::INET) {
            CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &inetPort,
                                           sizeof(inetPort)));
            CHECK_NE(0, inetPort);
        }

        // create remainder of connections
        // create remainder of connections
        for (size_t i = 0; i < numThreads; i++) {
        for (size_t i = 0; i < numThreads; i++) {
            for (size_t tries = 0; tries < 5; tries++) {
            for (size_t tries = 0; tries < 5; tries++) {
@@ -337,11 +367,12 @@ public:
                        break;
                        break;
#ifdef __BIONIC__
#ifdef __BIONIC__
                    case SocketType::VSOCK:
                    case SocketType::VSOCK:
                        if (ret.connection->addVsockClient(VMADDR_CID_LOCAL, port)) goto success;
                        if (ret.connection->addVsockClient(VMADDR_CID_LOCAL, vsockPort))
                            goto success;
                        break;
                        break;
#endif // __BIONIC__
#endif // __BIONIC__
                    case SocketType::INET:
                    case SocketType::INET:
                        if (ret.connection->addInetClient("127.0.0.1", port)) goto success;
                        if (ret.connection->addInetClient("127.0.0.1", inetPort)) goto success;
                        break;
                        break;
                    default:
                    default:
                        LOG_ALWAYS_FATAL("Unknown socket type");
                        LOG_ALWAYS_FATAL("Unknown socket type");