Skip to content

Commit

Permalink
Bug 1881437 - Use a thread local __res_state for HTTPS res_nquery r=n…
Browse files Browse the repository at this point in the history
…ecko-reviewers,kershaw

This makes it so each thread has its own __res_state instead of using
the global _res which could cause data races.

Differential Revision: https://phabricator.services.mozilla.com/D202400
  • Loading branch information
valenting committed Feb 22, 2024
1 parent 2b68cf4 commit c8b1958
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions netwerk/dns/GetAddrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ nsresult GetAddrInfoInit();
*/
nsresult GetAddrInfoShutdown();

void DNSThreadShutdown();

/**
* Resolves a HTTPS record. Will check overrides before calling the
* native OS implementation.
Expand Down
2 changes: 2 additions & 0 deletions netwerk/dns/PlatformDNSAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,6 @@ nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, uint16_t aFlags,
return NS_OK;
}

void DNSThreadShutdown() {}

} // namespace mozilla::net
35 changes: 32 additions & 3 deletions netwerk/dns/PlatformDNSUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "nsIDNSService.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPrefs_network.h"
#include "mozilla/ThreadLocal.h"

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -18,6 +19,10 @@

namespace mozilla::net {

#if defined(HAVE_RES_NINIT)
MOZ_THREAD_LOCAL(struct __res_state*) sThreadRes;
#endif

#define LOG(msg, ...) \
MOZ_LOG(gGetAddrInfoLog, LogLevel::Debug, ("[DNS]: " msg, ##__VA_ARGS__))

Expand All @@ -33,16 +38,28 @@ nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, uint16_t aFlags,
return NS_ERROR_UNKNOWN_HOST;
}

#if defined(HAVE_RES_NINIT)
if (!sThreadRes.get()) {
UniquePtr<struct __res_state> resState(new struct __res_state);
memset(resState.get(), 0, sizeof(struct __res_state));
if (int ret = res_ninit(resState.get())) {
LOG("res_ninit failed: %d", ret);
return NS_ERROR_UNKNOWN_HOST;
}
sThreadRes.set(resState.release());
}
#endif

LOG("resolving %s\n", host.get());
// Perform the query
rv = packet.FillBuffer(
[&](unsigned char response[DNSPacket::MAX_SIZE]) -> int {
int len = 0;
#if defined(XP_LINUX)
len = res_nquery(&_res, host.get(), ns_c_in,
#if defined(HAVE_RES_NINIT)
len = res_nquery(sThreadRes.get(), host.get(), ns_c_in,
nsIDNSService::RESOLVE_TYPE_HTTPSSVC, response,
DNSPacket::MAX_SIZE);
#elif defined(XP_MACOSX)
#else
len =
res_query(host.get(), ns_c_in, nsIDNSService::RESOLVE_TYPE_HTTPSSVC,
response, DNSPacket::MAX_SIZE);
Expand All @@ -60,4 +77,16 @@ nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, uint16_t aFlags,
return ParseHTTPSRecord(host, packet, aResult, aTTL);
}

void DNSThreadShutdown() {
#if defined(HAVE_RES_NINIT)
auto* res = sThreadRes.get();
if (!res) {
return;
}

sThreadRes.set(nullptr);
res_nclose(res);
#endif
}

} // namespace mozilla::net
2 changes: 2 additions & 0 deletions netwerk/dns/PlatformDNSWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ nsresult ResolveHTTPSRecordImpl(const nsACString& aHost, uint16_t aFlags,
return NS_OK;
}

void DNSThreadShutdown() {}

} // namespace mozilla::net
21 changes: 21 additions & 0 deletions netwerk/dns/nsHostResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsIThreadPool.h"
#if defined(HAVE_RES_NINIT)
# include <sys/types.h>
# include <netinet/in.h>
Expand Down Expand Up @@ -137,6 +138,24 @@ class nsResState {

#endif // RES_RETRY_ON_FAILURE

class DnsThreadListener final : public nsIThreadPoolListener {
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSITHREADPOOLLISTENER
private:
virtual ~DnsThreadListener() = default;
};

NS_IMETHODIMP
DnsThreadListener::OnThreadCreated() { return NS_OK; }

NS_IMETHODIMP
DnsThreadListener::OnThreadShuttingDown() {
DNSThreadShutdown();
return NS_OK;
}

NS_IMPL_ISUPPORTS(DnsThreadListener, nsIThreadPoolListener)

//----------------------------------------------------------------------------

static const char kPrefGetTtl[] = "network.dns.get-ttl";
Expand Down Expand Up @@ -252,6 +271,8 @@ nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
MOZ_ALWAYS_SUCCEEDS(
threadPool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize));
MOZ_ALWAYS_SUCCEEDS(threadPool->SetName("DNS Resolver"_ns));
nsCOMPtr<nsIThreadPoolListener> listener = new DnsThreadListener();
threadPool->SetListener(listener);
mResolverThreads = ToRefPtr(std::move(threadPool));

return NS_OK;
Expand Down

0 comments on commit c8b1958

Please sign in to comment.