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

Commit f036afb6 authored by android-build-team Robot's avatar android-build-team Robot
Browse files

Snap for 6964653 from 9b1191fb to sc-release

Change-Id: I81306f0b5bab4568abdb3753ffa361ce383df3e1
parents cbe6d703 9b1191fb
Loading
Loading
Loading
Loading
+21 −42
Original line number Diff line number Diff line
@@ -84,25 +84,6 @@ void logArguments(int argc, char** argv) {
    }
}

template<typename T>
void tryThreadOrError(SocketClient* cli, T* handler) {
    cli->incRef();

    const int rval = netdutils::threadLaunch(handler);
    if (rval == 0) {
        // SocketClient decRef() happens in the handler's run() method.
        return;
    }

    char* msg = nullptr;
    asprintf(&msg, "%s (%d)", strerror(-rval), -rval);
    cli->sendMsg(ResponseCode::OperationFailed, msg, false);
    free(msg);

    delete handler;
    cli->decRef();
}

bool checkAndClearUseLocalNameserversFlag(unsigned* netid) {
    if (netid == nullptr || ((*netid) & NETID_USE_LOCAL_NAMESERVERS) == 0) {
        return false;
@@ -563,10 +544,23 @@ DnsProxyListener::DnsProxyListener() : FrameworkListener(SOCKET_NAME) {
    registerCmd(new GetDnsNetIdCommand());
}

void DnsProxyListener::Handler::spawn() {
    const int rval = netdutils::threadLaunch(this);
    if (rval == 0) {
        return;
    }

    char* msg = nullptr;
    asprintf(&msg, "%s (%d)", strerror(-rval), -rval);
    mClient->sendMsg(ResponseCode::OperationFailed, msg, false);
    free(msg);
    delete this;
}

DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(SocketClient* c, char* host, char* service,
                                                         addrinfo* hints,
                                                         const android_net_context& netcontext)
    : mClient(c), mHost(host), mService(service), mHints(hints), mNetContext(netcontext) {}
    : Handler(c), mHost(host), mService(service), mHints(hints), mNetContext(netcontext) {}

DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
    free(mHost);
@@ -763,7 +757,6 @@ void DnsProxyListener::GetAddrInfoHandler::run() {
    reportDnsEvent(INetdEventListener::EVENT_GETADDRINFO, mNetContext, latencyUs, rv, event, mHost,
                   ip_addrs, total_ip_addr_count);
    freeaddrinfo(result);
    mClient->decRef();
}

std::string DnsProxyListener::GetAddrInfoHandler::threadName() {
@@ -841,9 +834,7 @@ int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
        hints->ai_protocol = ai_protocol;
    }

    DnsProxyListener::GetAddrInfoHandler* handler =
            new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netcontext);
    tryThreadOrError(cli, handler);
    (new GetAddrInfoHandler(cli, name, service, hints, netcontext))->spawn();
    return 0;
}

@@ -888,19 +879,13 @@ int DnsProxyListener::ResNSendCommand::runCommand(SocketClient* cli, int argc, c
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
    }

    DnsProxyListener::ResNSendHandler* handler =
            new DnsProxyListener::ResNSendHandler(cli, argv[3], flags, netcontext);
    tryThreadOrError(cli, handler);
    (new ResNSendHandler(cli, argv[3], flags, netcontext))->spawn();
    return 0;
}

DnsProxyListener::ResNSendHandler::ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags,
                                                   const android_net_context& netcontext)
    : mClient(c), mMsg(std::move(msg)), mFlags(flags), mNetContext(netcontext) {}

DnsProxyListener::ResNSendHandler::~ResNSendHandler() {
    mClient->decRef();
}
    : Handler(c), mMsg(std::move(msg)), mFlags(flags), mNetContext(netcontext) {}

void DnsProxyListener::ResNSendHandler::run() {
    LOG(DEBUG) << "ResNSendHandler::run: " << mFlags << " / {" << mNetContext.app_netid << " "
@@ -1090,15 +1075,13 @@ int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
    }

    DnsProxyListener::GetHostByNameHandler* handler =
            new DnsProxyListener::GetHostByNameHandler(cli, name, af, netcontext);
    tryThreadOrError(cli, handler);
    (new GetHostByNameHandler(cli, name, af, netcontext))->spawn();
    return 0;
}

DnsProxyListener::GetHostByNameHandler::GetHostByNameHandler(SocketClient* c, char* name, int af,
                                                             const android_net_context& netcontext)
    : mClient(c), mName(name), mAf(af), mNetContext(netcontext) {}
    : Handler(c), mName(name), mAf(af), mNetContext(netcontext) {}

DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
    free(mName);
@@ -1190,7 +1173,6 @@ void DnsProxyListener::GetHostByNameHandler::run() {
    const int total_ip_addr_count = extractGetHostByNameAnswers(hp, &ip_addrs);
    reportDnsEvent(INetdEventListener::EVENT_GETHOSTBYNAME, mNetContext, latencyUs, rv, event,
                   mName, ip_addrs, total_ip_addr_count);
    mClient->decRef();
}

std::string DnsProxyListener::GetHostByNameHandler::threadName() {
@@ -1242,16 +1224,14 @@ int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
    }

    DnsProxyListener::GetHostByAddrHandler* handler = new DnsProxyListener::GetHostByAddrHandler(
            cli, addr, addrLen, addrFamily, netcontext);
    tryThreadOrError(cli, handler);
    (new GetHostByAddrHandler(cli, addr, addrLen, addrFamily, netcontext))->spawn();
    return 0;
}

DnsProxyListener::GetHostByAddrHandler::GetHostByAddrHandler(SocketClient* c, void* address,
                                                             int addressLen, int addressFamily,
                                                             const android_net_context& netcontext)
    : mClient(c),
    : Handler(c),
      mAddress(address),
      mAddressLen(addressLen),
      mAddressFamily(addressFamily),
@@ -1351,7 +1331,6 @@ void DnsProxyListener::GetHostByAddrHandler::run() {

    reportDnsEvent(INetdEventListener::EVENT_GETHOSTBYADDR, mNetContext, latencyUs, rv, event,
                   (hp && hp->h_name) ? hp->h_name : "null", {}, 0);
    mClient->decRef();
}

std::string DnsProxyListener::GetHostByAddrHandler::threadName() {
+33 −21
Original line number Diff line number Diff line
@@ -38,6 +38,23 @@ class DnsProxyListener : public FrameworkListener {
    static constexpr const char* SOCKET_NAME = "dnsproxyd";

  private:
    class Handler {
      public:
        Handler(SocketClient* c) : mClient(c) { mClient->incRef(); }
        virtual ~Handler() { mClient->decRef(); }
        void operator=(const Handler&) = delete;

        // Attept to spawn the worker thread, or return an error to the client.
        // The Handler instance will self-delete in either case.
        void spawn();

        virtual void run() = 0;
        virtual std::string threadName() = 0;

        SocketClient* mClient;  // ref-counted
    };

    /* ------ getaddrinfo ------*/
    class GetAddrInfoCmd : public FrameworkCommand {
      public:
        GetAddrInfoCmd();
@@ -45,21 +62,19 @@ class DnsProxyListener : public FrameworkListener {
        int runCommand(SocketClient* c, int argc, char** argv) override;
    };

    /* ------ getaddrinfo ------*/
    class GetAddrInfoHandler {
    class GetAddrInfoHandler : public Handler {
      public:
        // Note: All of host, service, and hints may be NULL
        GetAddrInfoHandler(SocketClient* c, char* host, char* service, addrinfo* hints,
                           const android_net_context& netcontext);
        ~GetAddrInfoHandler();
        ~GetAddrInfoHandler() override;

        void run();
        std::string threadName();
        void run() override;
        std::string threadName() override;

      private:
        void doDns64Synthesis(int32_t* rv, addrinfo** res, NetworkDnsEventReported* event);

        SocketClient* mClient;  // ref counted
        char* mHost;            // owned. TODO: convert to std::string.
        char* mService;         // owned. TODO: convert to std::string.
        addrinfo* mHints;       // owned
@@ -74,20 +89,19 @@ class DnsProxyListener : public FrameworkListener {
        int runCommand(SocketClient* c, int argc, char** argv) override;
    };

    class GetHostByNameHandler {
    class GetHostByNameHandler : public Handler {
      public:
        GetHostByNameHandler(SocketClient* c, char* name, int af,
                             const android_net_context& netcontext);
        ~GetHostByNameHandler();
        ~GetHostByNameHandler() override;

        void run();
        std::string threadName();
        void run() override;
        std::string threadName() override;

      private:
        void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
                              NetworkDnsEventReported* event);

        SocketClient* mClient;  // ref counted
        char* mName;            // owned. TODO: convert to std::string.
        int mAf;
        android_net_context mNetContext;
@@ -101,20 +115,19 @@ class DnsProxyListener : public FrameworkListener {
        int runCommand(SocketClient* c, int argc, char** argv) override;
    };

    class GetHostByAddrHandler {
    class GetHostByAddrHandler : public Handler {
      public:
        GetHostByAddrHandler(SocketClient* c, void* address, int addressLen, int addressFamily,
                             const android_net_context& netcontext);
        ~GetHostByAddrHandler();
        ~GetHostByAddrHandler() override;

        void run();
        std::string threadName();
        void run() override;
        std::string threadName() override;

      private:
        void doDns64ReverseLookup(hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
                                  NetworkDnsEventReported* event);

        SocketClient* mClient;  // ref counted
        void* mAddress;         // address to lookup; owned
        int mAddressLen;        // length of address to look up
        int mAddressFamily;     // address family
@@ -129,17 +142,16 @@ class DnsProxyListener : public FrameworkListener {
        int runCommand(SocketClient* c, int argc, char** argv) override;
    };

    class ResNSendHandler {
    class ResNSendHandler : public Handler {
      public:
        ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags,
                        const android_net_context& netcontext);
        ~ResNSendHandler();
        ~ResNSendHandler() override = default;

        void run();
        std::string threadName();
        void run() override;
        std::string threadName() override;

      private:
        SocketClient* mClient;  // ref counted
        std::string mMsg;
        uint32_t mFlags;
        android_net_context mNetContext;
+1 −2
Original line number Diff line number Diff line
@@ -28,8 +28,7 @@ bool resolv_init(const ResolverNetdCallbacks* callbacks) {
    android::base::InitLogging(/*argv=*/nullptr);
    android::base::SetDefaultTag("libnetd_resolv");
    LOG(INFO) << __func__ << ": Initializing resolver";
    // TODO(b/170539625): restore log level to WARNING after clarifying flaky tests.
    resolv_set_log_severity(isUserDebugBuild() ? android::base::DEBUG : android::base::WARNING);
    resolv_set_log_severity(android::base::WARNING);
    using android::net::gApiLevel;
    gApiLevel = getApiLevel();
    using android::net::gResNetdCallbacks;
+55 −2
Original line number Diff line number Diff line
@@ -184,8 +184,8 @@ class ResolverTest : public ::testing::Test {
        // service.

        AIBinder* binder = AServiceManager_getService("dnsresolver");
        ndk::SpAIBinder resolvBinder = ndk::SpAIBinder(binder);
        auto resolvService = aidl::android::net::IDnsResolver::fromBinder(resolvBinder);
        sResolvBinder = ndk::SpAIBinder(binder);
        auto resolvService = aidl::android::net::IDnsResolver::fromBinder(sResolvBinder);
        ASSERT_NE(nullptr, resolvService.get());

        // Subscribe the death recipient to the service IDnsResolver for detecting Netd death.
@@ -371,11 +371,16 @@ class ResolverTest : public ::testing::Test {
    // Use a shared static death recipient to monitor the service death. The static death
    // recipient could monitor the death not only during the test but also between tests.
    static AIBinder_DeathRecipient* sResolvDeathRecipient;  // Initialized in SetUpTestSuite.

    // The linked AIBinder_DeathRecipient will be automatically unlinked if the binder is deleted.
    // The binder needs to be retained throughout tests.
    static ndk::SpAIBinder sResolvBinder;
};

// Initialize static member of class.
std::shared_ptr<DnsMetricsListener> ResolverTest::sDnsMetricsListener;
AIBinder_DeathRecipient* ResolverTest::sResolvDeathRecipient;
ndk::SpAIBinder ResolverTest::sResolvBinder;

TEST_F(ResolverTest, GetHostByName) {
    constexpr char nonexistent_host_name[] = "nonexistent.example.com.";
@@ -2705,6 +2710,54 @@ TEST_F(ResolverTest, Async_VerifyQueryID) {
    EXPECT_EQ(1U, GetNumQueries(dns, host_name));
}

// Run a large number of DNS queries through asynchronous API to create
// thousands of threads in resolver to simulate the failure of thread creation
// when memory per process is exhausted. (The current critical value is about
// 84xx threads.)
TEST_F(ResolverTest, Async_OutOfMemory) {
    constexpr char host_name[] = "howdy.example.com.";
    constexpr size_t AMOUNT_OF_UIDS = 40;
    constexpr size_t MAX_QUERIES_PER_UID = 256;
    constexpr size_t NUM_OF_QUERIES = AMOUNT_OF_UIDS * MAX_QUERIES_PER_UID;

    test::DNSResponder dns;
    StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.4"}});
    ASSERT_TRUE(mDnsClient.SetResolversForNetwork());
    dns.setDeferredResp(true);

    std::vector<int> fds;
    fds.reserve(NUM_OF_QUERIES);
    bool send_query = true;
    for (size_t i = 0; i < AMOUNT_OF_UIDS && send_query; i++) {
        ScopedChangeUID scopedChangeUID(TEST_UID - i);
        for (size_t j = 0; j < MAX_QUERIES_PER_UID; j++) {
            int fd = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0);
            if (fd >= 0) {
                fds.emplace_back(fd);
            } else {
                send_query = false;
                break;
            }
        }
    }

    dns.setDeferredResp(false);
    EXPECT_EQ(NUM_OF_QUERIES, fds.size());
    // TODO: AIBinder_DeathRecipient_new does not work (b/172178636), which
    // should be fixed. Fortunately, netd crash is still detectable at the point
    // of DumpResolverService() in TearDown(), where accesses mDnsClient. Also,
    // the fds size will be less than NUM_OF_QUERIES in that case.

    uint8_t buf[MAXPACKET];
    int rcode;
    for (auto fd : fds) {
        memset(buf, 0, MAXPACKET);
        getAsyncResponse(fd, &rcode, buf, MAXPACKET);
        // The results of each DNS query are not examined, since they won't all
        // succeed or all fail. Here we only focus on netd is crashed or not.
    }
}

// This test checks that the resolver should not generate the request containing OPT RR when using
// cleartext DNS. If we query the DNS server not supporting EDNS0 and it reponds with
// FORMERR_ON_EDNS, we will fallback to no EDNS0 and try again. If the server does no response, we
+0 −5
Original line number Diff line number Diff line
@@ -47,8 +47,3 @@ inline uint64_t getApiLevel() {
            android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
    return std::max(buildVersionSdk + !!buildVersionPreviewSdk, firstApiLevel);
}

// It's the identical strategy as frameworks/base/core/java/android/os/Build.java did.
inline bool isUserDebugBuild() {
    return (android::base::GetProperty("ro.build.type", "user") == "userdebug");
}