Commit 1047dd4a authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Remove using directives ("using namespace x") from chrome/chrome_elf/.

Also shortens/cleans up some existing code (no functional change).

Bug: 82078
Change-Id: I4d3ee878d6c4f6e301dcaaa19e5fc7877bb1b5e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1881696
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarCait Phillips <caitkp@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709961}
parent ffc6c925
......@@ -501,12 +501,10 @@ bool GetDataFromImageForTesting(PVOID mapped_image,
} // namespace third_party_dlls
using namespace third_party_dlls;
void DisableHook() {
g_hook_disabled.store(true, std::memory_order_relaxed);
third_party_dlls::g_hook_disabled.store(true, std::memory_order_relaxed);
}
int32_t GetApplyHookResult() {
return g_apply_hook_result.load(std::memory_order_relaxed);
return third_party_dlls::g_apply_hook_result.load(std::memory_order_relaxed);
}
......@@ -273,17 +273,17 @@ void DeinitLogs() {
// - Function definition in logging_api.h
// - Export declared in chrome_elf_[x64|x86].def
//------------------------------------------------------------------------------
using namespace third_party_dlls;
uint32_t DrainLog(uint8_t* buffer,
uint32_t buffer_size,
uint32_t* log_remaining) {
if (!g_log_mutex ||
::WaitForSingleObject(g_log_mutex, kMaxMutexWaitMs) != WAIT_OBJECT_0)
if (!third_party_dlls::g_log_mutex ||
::WaitForSingleObject(third_party_dlls::g_log_mutex,
third_party_dlls::kMaxMutexWaitMs) != WAIT_OBJECT_0)
return 0;
Log& blocked = GetBlockedLog();
Log& allowed = GetAllowedLog();
third_party_dlls::Log& blocked = third_party_dlls::GetBlockedLog();
third_party_dlls::Log& allowed = third_party_dlls::GetAllowedLog();
uint32_t bytes_written = blocked.Drain(buffer, buffer_size);
bytes_written +=
......@@ -299,13 +299,13 @@ uint32_t DrainLog(uint8_t* buffer,
*log_remaining = static_cast<uint32_t>(full_size);
}
::ReleaseMutex(g_log_mutex);
::ReleaseMutex(third_party_dlls::g_log_mutex);
return bytes_written;
}
bool RegisterLogNotification(HANDLE event_handle) {
if (!g_log_mutex)
if (!third_party_dlls::g_log_mutex)
return false;
// Duplicate the new handle, if not clearing with nullptr.
......@@ -317,18 +317,20 @@ bool RegisterLogNotification(HANDLE event_handle) {
}
// Close any existing registered handle.
if (g_notification_event)
::CloseHandle(g_notification_event);
if (third_party_dlls::g_notification_event)
::CloseHandle(third_party_dlls::g_notification_event);
g_notification_event = temp;
third_party_dlls::g_notification_event = temp;
return true;
}
uint32_t GetBlockedModulesCount() {
return g_blocked_modules_count.load(std::memory_order_relaxed);
return third_party_dlls::g_blocked_modules_count.load(
std::memory_order_relaxed);
}
uint32_t GetUniqueBlockedModulesCount() {
return g_unique_blocked_modules_count.load(std::memory_order_relaxed);
return third_party_dlls::g_unique_blocked_modules_count.load(
std::memory_order_relaxed);
}
......@@ -152,8 +152,6 @@ void AddStatusCodeForTesting(ThirdPartyStatus code) {
} // namespace third_party_dlls
using namespace third_party_dlls;
bool IsThirdPartyInitialized() {
return g_third_party_initialized;
return third_party_dlls::g_third_party_initialized;
}
......@@ -260,23 +260,21 @@ TEST_F(ThirdPartyTest, MAYBE_Base) {
// initialization.
base::CommandLine cmd_line1 = base::CommandLine::FromString(kTestExeFilename);
cmd_line1.AppendArgNative(GetBlTestFilePath());
cmd_line1.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestOnlyInitialization));
cmd_line1.AppendArgNative(base::NumberToString16(kTestOnlyInitialization));
int exit_code = 0;
LaunchChildAndWait(cmd_line1, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadSuccess, exit_code);
ASSERT_EQ(kDllLoadSuccess, exit_code);
//----------------------------------------------------------------------------
// 2. Spawn the test process with NO blacklist. Expect successful DLL load.
base::CommandLine cmd_line2 = base::CommandLine::FromString(kTestExeFilename);
cmd_line2.AppendArgNative(GetBlTestFilePath());
cmd_line2.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line2.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line2.AppendArgNative(MakePath(GetExeDir(), kTestDllName1));
LaunchChildAndWait(cmd_line2, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadSuccess, exit_code);
ASSERT_EQ(kDllLoadSuccess, exit_code);
//----------------------------------------------------------------------------
// 3. Spawn the test process with blacklist. Expect failed DLL load.
......@@ -295,12 +293,11 @@ TEST_F(ThirdPartyTest, MAYBE_Base) {
base::CommandLine cmd_line3 = base::CommandLine::FromString(kTestExeFilename);
cmd_line3.AppendArgNative(GetBlTestFilePath());
cmd_line3.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line3.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line3.AppendArgNative(MakePath(GetExeDir(), kTestDllName1));
LaunchChildAndWait(cmd_line3, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
//----------------------------------------------------------------------------
// 4. Spawn the test process with blacklist. Expect failed DLL load.
......@@ -315,13 +312,12 @@ TEST_F(ThirdPartyTest, MAYBE_Base) {
base::CommandLine cmd_line4 = base::CommandLine::FromString(kTestExeFilename);
cmd_line4.AppendArgNative(GetBlTestFilePath());
cmd_line4.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line4.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line4.AppendArgNative(
MakePath(GetScopedTempDirValue(), kTestDllName1MixedCase));
LaunchChildAndWait(cmd_line4, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
}
// Note: The test module used in this unittest has no export table.
......@@ -334,13 +330,12 @@ TEST_F(ThirdPartyTest, WideCharEncoding) {
// 1) Test a successful DLL load with no blacklist.
base::CommandLine cmd_line1 = base::CommandLine::FromString(kTestExeFilename);
cmd_line1.AppendArgNative(GetBlTestFilePath());
cmd_line1.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line1.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line1.AppendArgNative(MakePath(GetScopedTempDirValue(), kChineseUnicode));
int exit_code = 0;
LaunchChildAndWait(cmd_line1, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadSuccess, exit_code);
ASSERT_EQ(kDllLoadSuccess, exit_code);
//----------------------------------------------------------------------------
// 2) Test a failed DLL load with blacklist.
......@@ -360,12 +355,11 @@ TEST_F(ThirdPartyTest, WideCharEncoding) {
base::CommandLine cmd_line2 = base::CommandLine::FromString(kTestExeFilename);
cmd_line2.AppendArgNative(GetBlTestFilePath());
cmd_line2.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line2.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line2.AppendArgNative(MakePath(GetScopedTempDirValue(), kChineseUnicode));
LaunchChildAndWait(cmd_line2, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
}
// Note: The test module used in this unittest has an export table.
......@@ -378,13 +372,12 @@ TEST_F(ThirdPartyTest, WideCharEncodingWithExportDir) {
// 1) Test a successful DLL load with no blacklist.
base::CommandLine cmd_line1 = base::CommandLine::FromString(kTestExeFilename);
cmd_line1.AppendArgNative(GetBlTestFilePath());
cmd_line1.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line1.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line1.AppendArgNative(MakePath(GetScopedTempDirValue(), kChineseUnicode));
int exit_code = 0;
LaunchChildAndWait(cmd_line1, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadSuccess, exit_code);
ASSERT_EQ(kDllLoadSuccess, exit_code);
//----------------------------------------------------------------------------
// 2) Test a failed DLL load with blacklist.
......@@ -409,12 +402,11 @@ TEST_F(ThirdPartyTest, WideCharEncodingWithExportDir) {
base::CommandLine cmd_line2 = base::CommandLine::FromString(kTestExeFilename);
cmd_line2.AppendArgNative(GetBlTestFilePath());
cmd_line2.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line2.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line2.AppendArgNative(MakePath(GetScopedTempDirValue(), kChineseUnicode));
LaunchChildAndWait(cmd_line2, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
// 2b) Only blacklist the new DLL file name, which should be mined out of the
// section by the hook, and the load should be blocked.
......@@ -426,12 +418,11 @@ TEST_F(ThirdPartyTest, WideCharEncodingWithExportDir) {
base::CommandLine cmd_line3 = base::CommandLine::FromString(kTestExeFilename);
cmd_line3.AppendArgNative(GetBlTestFilePath());
cmd_line3.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line3.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line3.AppendArgNative(MakePath(GetScopedTempDirValue(), kChineseUnicode));
LaunchChildAndWait(cmd_line3, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
}
// Note: The test module used in this unittest has no export table.
......@@ -444,14 +435,13 @@ TEST_F(ThirdPartyTest, DeprecatedBlacklistSanityCheck) {
// should trigger a block).
base::CommandLine cmd_line1 = base::CommandLine::FromString(kTestExeFilename);
cmd_line1.AppendArgNative(GetBlTestFilePath());
cmd_line1.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line1.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line1.AppendArgNative(
MakePath(GetScopedTempDirValue(), kOldBlacklistDllName));
int exit_code = 0;
LaunchChildAndWait(cmd_line1, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadFailed, exit_code);
ASSERT_EQ(kDllLoadFailed, exit_code);
}
// Note: This test only sanity checks the two SHA1 libraries used on either side
......@@ -523,14 +513,13 @@ TEST_F(ThirdPartyTest, MAYBE_PathCaseSensitive) {
// for this test.
base::CommandLine cmd_line1 = base::CommandLine::FromString(kTestExeFilename);
cmd_line1.AppendArgNative(GetBlTestFilePath());
cmd_line1.AppendArgNative(
base::NumberToString16(main_unittest_exe::kTestSingleDllLoad));
cmd_line1.AppendArgNative(base::NumberToString16(kTestSingleDllLoad));
cmd_line1.AppendArgNative(
MakePath(GetScopedTempDirValue(), kTestDllName1MixedCase));
int exit_code = 0;
LaunchChildAndWait(cmd_line1, &exit_code);
ASSERT_EQ(main_unittest_exe::kDllLoadSuccess, exit_code);
ASSERT_EQ(kDllLoadSuccess, exit_code);
}
// Test the status-code passing in registry.
......
......@@ -22,8 +22,6 @@
#include "chrome/install_static/install_util.h"
#include "chrome/install_static/product_install_details.h"
using namespace third_party_dlls::main_unittest_exe;
namespace {
// Function object which invokes LocalFree on its parameter, which must be
......@@ -33,13 +31,11 @@ struct LocalFreeDeleter {
};
// Attempt to load a given DLL.
ExitCode LoadDll(std::wstring name) {
third_party_dlls::ExitCode LoadDll(std::wstring name) {
base::FilePath dll_path(name);
base::ScopedNativeLibrary dll(dll_path);
if (!dll.is_valid())
return kDllLoadFailed;
return kDllLoadSuccess;
return dll.is_valid() ? third_party_dlls::kDllLoadSuccess
: third_party_dlls::kDllLoadFailed;
}
// Utility function to protect the local registry.
......@@ -60,10 +56,7 @@ bool MatchPath(const wchar_t* arg_path, const third_party_dlls::LogEntry& log) {
return false;
}
if (drive_path.value().compare(arg_path) != 0)
return false;
return true;
return drive_path.value().compare(arg_path) == 0;
}
} // namespace
......@@ -91,27 +84,26 @@ int main() {
std::unique_ptr<wchar_t*[], LocalFreeDeleter> argv(
::CommandLineToArgvW(::GetCommandLineW(), &argument_count));
if (!argv)
return kBadCommandLine;
return third_party_dlls::kBadCommandLine;
if (IsThirdPartyInitialized())
return kThirdPartyAlreadyInitialized;
return third_party_dlls::kThirdPartyAlreadyInitialized;
install_static::InitializeProductDetailsForPrimaryModule();
install_static::InitializeProcessType();
// Get the required arguments, path to blacklist file and test id to run.
if (argument_count < 3)
return kMissingArgument;
return third_party_dlls::kMissingArgument;
const wchar_t* blacklist_path = argv[1];
if (!blacklist_path || ::wcslen(blacklist_path) == 0) {
return kBadBlacklistPath;
}
if (!blacklist_path || ::wcslen(blacklist_path) == 0)
return third_party_dlls::kBadBlacklistPath;
const wchar_t* arg2 = argv[2];
int test_id = ::_wtoi(arg2);
if (!test_id)
return kUnsupportedTestId;
return third_party_dlls::kUnsupportedTestId;
// Override blacklist path before initializing.
third_party_dlls::OverrideFilePathForTesting(blacklist_path);
......@@ -121,51 +113,49 @@ int main() {
RegRedirect(&rom);
if (!third_party_dlls::Init())
return kThirdPartyInitFailure;
return third_party_dlls::kThirdPartyInitFailure;
// Switch on test id.
switch (test_id) {
// Just initialization.
case kTestOnlyInitialization:
case third_party_dlls::kTestOnlyInitialization:
break;
// Single DLL load.
case kTestSingleDllLoad:
// Single DLL load with log path scrutiny.
case kTestLogPath: {
case third_party_dlls::kTestSingleDllLoad:
case third_party_dlls::kTestLogPath: {
if (argument_count < 4)
return kMissingArgument;
return third_party_dlls::kMissingArgument;
const wchar_t* dll_name = argv[3];
if (!dll_name || ::wcslen(dll_name) == 0)
return kBadArgument;
ExitCode code = LoadDll(dll_name);
return third_party_dlls::kBadArgument;
third_party_dlls::ExitCode code = LoadDll(dll_name);
// Get logging. Ensure the log is as expected.
uint32_t bytes = 0;
DrainLog(nullptr, 0, &bytes);
if (!bytes)
return kEmptyLog;
auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[bytes]);
return third_party_dlls::kEmptyLog;
auto buffer = std::make_unique<uint8_t[]>(bytes);
bytes = DrainLog(&buffer[0], bytes, nullptr);
third_party_dlls::LogEntry* entry =
reinterpret_cast<third_party_dlls::LogEntry*>(&buffer[0]);
if (!bytes || bytes < third_party_dlls::GetLogEntrySize(entry->path_len))
return kBadLogEntrySize;
return third_party_dlls::kBadLogEntrySize;
if ((code == kDllLoadFailed &&
if ((code == third_party_dlls::kDllLoadFailed &&
entry->type != third_party_dlls::kBlocked) ||
(code == kDllLoadSuccess &&
(code == third_party_dlls::kDllLoadSuccess &&
entry->type != third_party_dlls::kAllowed)) {
return kUnexpectedLog;
return third_party_dlls::kUnexpectedLog;
}
if (test_id == kTestLogPath && !MatchPath(dll_name, *entry))
return kUnexpectedSectionPath;
if (test_id == third_party_dlls::kTestLogPath &&
!MatchPath(dll_name, *entry))
return third_party_dlls::kUnexpectedSectionPath;
return code;
}
// Unsupported argument.
default:
return kUnsupportedTestId;
return third_party_dlls::kUnsupportedTestId;
}
return 0;
......
......@@ -6,11 +6,11 @@
#define CHROME_CHROME_ELF_THIRD_PARTY_DLLS_MAIN_UNITTEST_EXE_H_
namespace third_party_dlls {
namespace main_unittest_exe {
enum ExitCode {
kDllLoadSuccess = 0,
kDllLoadFailed = 1,
// Unexpected failures are negative ints:
kBadCommandLine = -1,
kThirdPartyAlreadyInitialized = -2,
......@@ -26,12 +26,11 @@ enum ExitCode {
};
enum TestId {
kTestOnlyInitialization = 1,
kTestSingleDllLoad = 2,
kTestLogPath = 3,
kTestOnlyInitialization = 1, // Just initialization.
kTestSingleDllLoad = 2, // Single DLL load.
kTestLogPath = 3, // Single DLL load with log path scrutiny.
};
} // namespace main_unittest_exe
} // namespace third_party_dlls
#endif // CHROME_CHROME_ELF_THIRD_PARTY_DLLS_MAIN_UNITTEST_EXE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment