Skip to content

Commit

Permalink
Move some //base winternl functions to ntdll.lib
Browse files Browse the repository at this point in the history
"Documented" functions in winternl.h can be linked to ntdll.lib
rather than getprocaddressed. This does this for those functions
in //base where this makes sense.

Bug: 1282011
Change-Id: I1edad2db3f9194bbca1a4143930a0e8ed750468e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4230192
Reviewed-by: Tushar Agarwal <agarwaltushar@google.com>
Reviewed-by: Will Harris <wfh@chromium.org>
Commit-Queue: Alex Gough <ajgo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1104656}
  • Loading branch information
quidity authored and Chromium LUCI CQ committed Feb 13, 2023
1 parent 736ed6e commit da69b90
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 41 deletions.
3 changes: 3 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ component("base") {

libs += [
"cfgmgr32.lib",
"ntdll.lib",
"powrprof.lib",
"propsys.lib",
"setupapi.lib",
Expand Down Expand Up @@ -3458,6 +3459,8 @@ test("base_unittests") {
if (enable_cet_shadow_stack) {
sources += [ "win/cet_shadow_stack_unittest.cc" ]
}

libs = [ "ntdll.lib" ]
}

if (is_linux || is_chromeos) {
Expand Down
12 changes: 3 additions & 9 deletions base/process/process_metrics_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,14 @@ Value::Dict SystemPerformanceInfo::ToDict() const {
// Retrieves performance counters from the operating system.
// Fills in the provided |info| structure. Returns true on success.
BASE_EXPORT bool GetSystemPerformanceInfo(SystemPerformanceInfo* info) {
static const auto query_system_information_ptr =
reinterpret_cast<decltype(&::NtQuerySystemInformation)>(GetProcAddress(
GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation"));
if (!query_system_information_ptr)
return false;

SYSTEM_PERFORMANCE_INFORMATION counters = {};
{
// The call to NtQuerySystemInformation might block on a lock.
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
BlockingType::MAY_BLOCK);
if (query_system_information_ptr(::SystemPerformanceInformation, &counters,
sizeof(SYSTEM_PERFORMANCE_INFORMATION),
nullptr) != STATUS_SUCCESS) {
if (::NtQuerySystemInformation(::SystemPerformanceInformation, &counters,
sizeof(SYSTEM_PERFORMANCE_INFORMATION),
nullptr) != STATUS_SUCCESS) {
return false;
}
}
Expand Down
25 changes: 5 additions & 20 deletions base/profiler/suspendable_thread_delegate_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "base/check.h"
#include "base/debug/alias.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/profiler/native_unwinder_win.h"
#include "build/build_config.h"

Expand Down Expand Up @@ -75,34 +75,19 @@ const TEB* GetThreadEnvironmentBlock(PlatformThreadId thread_id,
if (thread_id == ::GetCurrentThreadId())
return reinterpret_cast<TEB*>(NtCurrentTeb());

// Define the internal types we need to invoke NtQueryInformationThread.
enum THREAD_INFORMATION_CLASS { ThreadBasicInformation };

struct CLIENT_ID {
HANDLE UniqueProcess;
HANDLE UniqueThread;
};

// Define types not in winternl.h needed to invoke NtQueryInformationThread().
constexpr auto ThreadBasicInformation = static_cast<THREADINFOCLASS>(0);
struct THREAD_BASIC_INFORMATION {
NTSTATUS ExitStatus;
raw_ptr<TEB> Teb;
RAW_PTR_EXCLUSION TEB* Teb; // Filled in by the OS so cannot use raw_ptr<>.
CLIENT_ID ClientId;
KAFFINITY AffinityMask;
LONG Priority;
LONG BasePriority;
};

using NtQueryInformationThreadFunction =
NTSTATUS(WINAPI*)(HANDLE, THREAD_INFORMATION_CLASS, PVOID, ULONG, PULONG);

static const auto nt_query_information_thread =
reinterpret_cast<NtQueryInformationThreadFunction>(::GetProcAddress(
::GetModuleHandle(L"ntdll.dll"), "NtQueryInformationThread"));
if (!nt_query_information_thread)
return nullptr;

THREAD_BASIC_INFORMATION basic_info = {0};
NTSTATUS status = nt_query_information_thread(
NTSTATUS status = ::NtQueryInformationThread(
thread_handle, ThreadBasicInformation, &basic_info,
sizeof(THREAD_BASIC_INFORMATION), nullptr);
if (status != 0)
Expand Down
6 changes: 1 addition & 5 deletions base/win/scoped_handle_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ TEST_F(ScopedHandleTest, ScopedHandle) {
TEST_F(ScopedHandleDeathTest, HandleVerifierTrackedHasBeenClosed) {
HANDLE handle = ::CreateMutex(nullptr, false, nullptr);
ASSERT_NE(HANDLE(nullptr), handle);
using NtCloseFunc = decltype(&::NtClose);
NtCloseFunc ntclose = reinterpret_cast<NtCloseFunc>(
GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtClose"));
ASSERT_NE(nullptr, ntclose);

ASSERT_DEATH(
{
base::win::ScopedHandle handle_holder(handle);
ntclose(handle);
::NtClose(handle);
// Destructing a ScopedHandle with an illegally closed handle should
// fail.
},
Expand Down
9 changes: 2 additions & 7 deletions base/win/security_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,9 @@ void AppendSidVector(std::vector<Sid>& base_sids,
}

absl::optional<ACCESS_MASK> GetGrantedAccess(HANDLE handle) {
static const auto query_object = reinterpret_cast<decltype(NtQueryObject)*>(
GetProcAddress(::GetModuleHandle(L"ntdll.dll"), "NtQueryObject"));
if (!query_object) {
return absl::nullopt;
}
PUBLIC_OBJECT_BASIC_INFORMATION basic_info = {};
if (!NT_SUCCESS(query_object(handle, ObjectBasicInformation, &basic_info,
sizeof(basic_info), nullptr))) {
if (!NT_SUCCESS(::NtQueryObject(handle, ObjectBasicInformation, &basic_info,
sizeof(basic_info), nullptr))) {
return absl::nullopt;
}
return basic_info.GrantedAccess;
Expand Down

0 comments on commit da69b90

Please sign in to comment.