Skip to content

Commit

Permalink
[string16] Prepare //sandbox for base::string16 switch
Browse files Browse the repository at this point in the history
This change prepares //sandbox/win for the upcoming base::string16
switch to std::u16string by replacing existing base::string16 and base::char16
usages with std::wstring and wchar_t.

Furthermore, it introduces explicit conversions to and from base::string16
at API boundaries where necessary.

Bug: 911896
Change-Id: I96ae433a94d133d16c10b15d6db650eafeaece04
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1816409
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Will Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712012}
  • Loading branch information
jdoerrie authored and Commit Bot committed Nov 2, 2019
1 parent f35959d commit 4e88a3c
Show file tree
Hide file tree
Showing 81 changed files with 466 additions and 468 deletions.
14 changes: 7 additions & 7 deletions sandbox/win/sandbox_poc/main_ui_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ INT_PTR CALLBACK MainUIWindow::SpawnTargetWndProc(HWND dialog,
HWND edit_box_dll_name = ::GetDlgItem(dialog, IDC_DLL_NAME);
wchar_t current_dir[MAX_PATH];
if (GetCurrentDirectory(MAX_PATH, current_dir)) {
base::string16 dll_path = base::string16(current_dir) +
base::string16(kDefaultDll_);
std::wstring dll_path =
std::wstring(current_dir) + std::wstring(kDefaultDll_);
::SetWindowText(edit_box_dll_name, dll_path.c_str());
}

Expand Down Expand Up @@ -205,7 +205,7 @@ INT_PTR CALLBACK MainUIWindow::SpawnTargetWndProc(HWND dialog,
return static_cast<INT_PTR>(TRUE);
} else if (LOWORD(wparam) == IDC_BROWSE_DLL) {
// If the user presses the Browse button to look for a DLL
base::string16 dll_path = host->OnShowBrowseForDllDlg(dialog);
std::wstring dll_path = host->OnShowBrowseForDllDlg(dialog);
if (dll_path.length() > 0) {
// Initialize the window text for Log File edit box
HWND edit_box_dll_path = ::GetDlgItem(dialog, IDC_DLL_NAME);
Expand All @@ -214,7 +214,7 @@ INT_PTR CALLBACK MainUIWindow::SpawnTargetWndProc(HWND dialog,
return static_cast<INT_PTR>(TRUE);
} else if (LOWORD(wparam) == IDC_BROWSE_LOG) {
// If the user presses the Browse button to look for a log file
base::string16 log_path = host->OnShowBrowseForLogFileDlg(dialog);
std::wstring log_path = host->OnShowBrowseForLogFileDlg(dialog);
if (log_path.length() > 0) {
// Initialize the window text for Log File edit box
HWND edit_box_log_file = ::GetDlgItem(dialog, IDC_LOG_FILE);
Expand Down Expand Up @@ -350,7 +350,7 @@ bool MainUIWindow::OnLaunchDll(HWND dialog) {
}

// store these values in the member variables for use in SpawnTarget
log_file_ = base::string16(L"\"") + log_file + base::string16(L"\"");
log_file_ = std::wstring(L"\"") + log_file + std::wstring(L"\"");
dll_path_ = dll_path;
entry_point_ = entry_point;

Expand Down Expand Up @@ -571,7 +571,7 @@ bool MainUIWindow::SpawnTarget() {
return return_value;
}

base::string16 MainUIWindow::OnShowBrowseForDllDlg(HWND owner) {
std::wstring MainUIWindow::OnShowBrowseForDllDlg(HWND owner) {
wchar_t filename[MAX_PATH];
wcscpy_s(filename, MAX_PATH, L"");

Expand All @@ -591,7 +591,7 @@ base::string16 MainUIWindow::OnShowBrowseForDllDlg(HWND owner) {
return L"";
}

base::string16 MainUIWindow::OnShowBrowseForLogFileDlg(HWND owner) {
std::wstring MainUIWindow::OnShowBrowseForLogFileDlg(HWND owner) {
wchar_t filename[MAX_PATH];
wcscpy_s(filename, MAX_PATH, L"");

Expand Down
13 changes: 6 additions & 7 deletions sandbox/win/sandbox_poc/main_ui_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <string>

#include "base/macros.h"
#include "base/strings/string16.h"

namespace sandbox {
class BrokerServices;
Expand Down Expand Up @@ -126,11 +125,11 @@ class MainUIWindow {

// Shows a standard File Open dialog and returns the DLL filename selected or
// blank string if the user cancelled (or an error occurred).
base::string16 OnShowBrowseForDllDlg(HWND owner);
std::wstring OnShowBrowseForDllDlg(HWND owner);

// Shows a standard Save As dialog and returns the log filename selected or
// blank string if the user cancelled (or an error occurred).
base::string16 OnShowBrowseForLogFileDlg(HWND owner);
std::wstring OnShowBrowseForLogFileDlg(HWND owner);

// Formats a message using the supplied format string and prints it in the
// listview in the main UI window. Passing a NULL param in 'fmt' results in
Expand Down Expand Up @@ -166,20 +165,20 @@ class MainUIWindow {

// This is essentially a command line to a target executable that the
// broker will spawn and ask to load the DLL.
base::string16 spawn_target_;
std::wstring spawn_target_;

// A handle to the current instance of the app. Passed in to this class
// through CreateMainWindowAndLoop.
HINSTANCE instance_handle_;

// A path to the DLL that the target should load once it executes.
base::string16 dll_path_;
std::wstring dll_path_;

// The name of the entry point the target should call after it loads the DLL.
base::string16 entry_point_;
std::wstring entry_point_;

// The name of the log file to use.
base::string16 log_file_;
std::wstring log_file_;

// This is a static handle to the list view that fills up the entire main
// UI window. The list view is used to display debugging information to the
Expand Down
3 changes: 1 addition & 2 deletions sandbox/win/sandbox_poc/pocdll/spyware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <string>

#include "base/strings/string16.h"
#include "sandbox/win/sandbox_poc/pocdll/exports.h"
#include "sandbox/win/sandbox_poc/pocdll/utils.h"

Expand All @@ -25,7 +24,7 @@ void POCDLL_API TestSpyKeys(HANDLE log) {

fprintf(output, "[INFO] Logging keystrokes for 15 seconds\r\n");
fflush(output);
base::string16 logged;
std::wstring logged;
DWORD tick = ::GetTickCount() + 15000;
while (tick > ::GetTickCount()) {
for (int i = 0; i < 256; ++i) {
Expand Down
14 changes: 7 additions & 7 deletions sandbox/win/sandbox_poc/sandbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// Prototype allowed for functions to be called in the POC
typedef void(__cdecl *lpfnInit)(HANDLE);

bool ParseCommandLine(wchar_t * command_line,
std::string * dll_name,
std::string * entry_point,
base::string16 * log_file) {
bool ParseCommandLine(wchar_t* command_line,
std::string* dll_name,
std::string* entry_point,
std::wstring* log_file) {
DCHECK(dll_name);
DCHECK(entry_point);
DCHECK(log_file);
Expand All @@ -39,8 +39,8 @@ bool ParseCommandLine(wchar_t * command_line,
return false;
}

base::string16 entry_point_wide = arg_list[1];
base::string16 dll_name_wide = arg_list[2];
std::wstring entry_point_wide = arg_list[1];
std::wstring dll_name_wide = arg_list[2];
*entry_point = std::string(entry_point_wide.begin(), entry_point_wide.end());
*dll_name = std::string(dll_name_wide.begin(), dll_name_wide.end());
*log_file = arg_list[3];
Expand Down Expand Up @@ -128,7 +128,7 @@ int APIENTRY _tWinMain(HINSTANCE instance, HINSTANCE, wchar_t* command_line,

// Parse the command line to find out what we need to call
std::string dll_name, entry_point;
base::string16 log_file;
std::wstring log_file;
if (!ParseCommandLine(GetCommandLineW(),
&dll_name,
&entry_point,
Expand Down
4 changes: 2 additions & 2 deletions sandbox/win/src/app_container_profile_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool AppContainerProfileBase::GetFolderPath(base::FilePath* file_path) {
GetModuleHandle(L"userenv"), "GetAppContainerFolderPath"));
if (!get_app_container_folder_path)
return false;
base::string16 sddl_str;
std::wstring sddl_str;
if (!package_sid_.ToSddlString(&sddl_str))
return false;
base::win::ScopedCoMem<wchar_t> path_str;
Expand All @@ -189,7 +189,7 @@ bool AppContainerProfileBase::GetFolderPath(base::FilePath* file_path) {

bool AppContainerProfileBase::GetPipePath(const wchar_t* pipe_name,
base::FilePath* pipe_path) {
base::string16 sddl_str;
std::wstring sddl_str;
if (!package_sid_.ToSddlString(&sddl_str))
return false;
*pipe_path = base::FilePath(base::StringPrintf(L"\\\\.\\pipe\\%ls\\%ls",
Expand Down
2 changes: 1 addition & 1 deletion sandbox/win/src/app_container_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SECURITY_ATTRIBUTES_SDDL : public SECURITY_ATTRIBUTES {
};

std::wstring CreateSddlWithSid(const Sid& sid) {
base::string16 sddl_string;
std::wstring sddl_string;
if (!sid.ToSddlString(&sddl_string))
return L"";
std::wstring base_sddl = L"D:(A;;GA;;;WD)(A;;GA;;;";
Expand Down
2 changes: 1 addition & 1 deletion sandbox/win/src/broker_services.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ ResultCode BrokerServicesBase::SpawnTarget(const wchar_t* exe_path,
std::vector<HANDLE> inherited_handle_list;
DWORD child_process_creation = PROCESS_CREATION_CHILD_PROCESS_RESTRICTED;

base::string16 desktop = policy_base->GetAlternateDesktop();
std::wstring desktop = policy_base->GetAlternateDesktop();
if (!desktop.empty()) {
startup_info.startup_info()->lpDesktop =
const_cast<wchar_t*>(desktop.c_str());
Expand Down
11 changes: 6 additions & 5 deletions sandbox/win/src/crosscall_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ bool CrossCallParamsEx::GetParameterVoidPtr(uint32_t index, void** param) {

// Covers the common case of reading a string. Note that the string is not
// scanned for invalid characters.
bool CrossCallParamsEx::GetParameterStr(uint32_t index,
base::string16* string) {
bool CrossCallParamsEx::GetParameterStr(uint32_t index, std::wstring* string) {
DCHECK(string->empty());
uint32_t size = 0;
ArgType type;
Expand All @@ -260,14 +259,16 @@ bool CrossCallParamsEx::GetParameterStr(uint32_t index,

// Check if this is an empty string.
if (size == 0) {
*string = base::WideToUTF16(L"");
*string = std::wstring();
return true;
}

if (!start || ((size % sizeof(wchar_t)) != 0))
return false;
return base::WideToUTF16(reinterpret_cast<wchar_t*>(start),
size / sizeof(wchar_t), string);

string->assign(reinterpret_cast<const wchar_t*>(start),
size / sizeof(wchar_t));
return true;
}

bool CrossCallParamsEx::GetParameterPtr(uint32_t index,
Expand Down
3 changes: 1 addition & 2 deletions sandbox/win/src/crosscall_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "base/callback.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "sandbox/win/src/crosscall_params.h"
#include "sandbox/win/src/ipc_tags.h"

Expand Down Expand Up @@ -118,7 +117,7 @@ class CrossCallParamsEx : public CrossCallParams {

// Gets a parameter that is a string. Returns false if the parameter does not
// exist.
bool GetParameterStr(uint32_t index, base::string16* string);
bool GetParameterStr(uint32_t index, std::wstring* string);

// Gets a parameter that is an in/out buffer. Returns false is the parameter
// does not exist or if the size of the actual parameter is not equal to the
Expand Down
46 changes: 23 additions & 23 deletions sandbox/win/src/file_policy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ SBOX_TESTS_COMMAND int File_Win32Create(int argc, wchar_t** argv) {
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
}

base::string16 full_path = MakePathToSys(argv[0], false);
std::wstring full_path = MakePathToSys(argv[0], false);
if (full_path.empty()) {
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ SBOX_TESTS_COMMAND int File_CreateSys32(int argc, wchar_t** argv) {
if (argc != 1)
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;

base::string16 file(argv[0]);
std::wstring file(argv[0]);
if (0 != _wcsnicmp(file.c_str(), kNTDevicePrefix, kNTDevicePrefixLen))
file = MakePathToSys(argv[0], true);

Expand Down Expand Up @@ -148,7 +148,7 @@ SBOX_TESTS_COMMAND int File_OpenSys32(int argc, wchar_t** argv) {
if (argc != 1)
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;

base::string16 file = MakePathToSys(argv[0], true);
std::wstring file = MakePathToSys(argv[0], true);
UNICODE_STRING object_name;
RtlInitUnicodeString(&object_name, file.c_str());

Expand All @@ -172,7 +172,7 @@ SBOX_TESTS_COMMAND int File_OpenSys32(int argc, wchar_t** argv) {
}

SBOX_TESTS_COMMAND int File_GetDiskSpace(int argc, wchar_t** argv) {
base::string16 sys_path = MakePathToSys(L"", false);
std::wstring sys_path = MakePathToSys(L"", false);
if (sys_path.empty()) {
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ SBOX_TESTS_COMMAND int File_QueryAttributes(int argc, wchar_t** argv) {
bool expect_directory = (L'd' == argv[1][0]);

UNICODE_STRING object_name;
base::string16 file = MakePathToSys(argv[0], true);
std::wstring file = MakePathToSys(argv[0], true);
RtlInitUnicodeString(&object_name, file.c_str());

OBJECT_ATTRIBUTES obj_attributes = {};
Expand Down Expand Up @@ -262,8 +262,8 @@ SBOX_TESTS_COMMAND int File_QueryAttributes(int argc, wchar_t** argv) {
// Tries to create a backup of calc.exe in system32 folder. This should fail
// with ERROR_ACCESS_DENIED if everything is working as expected.
SBOX_TESTS_COMMAND int File_CopyFile(int argc, wchar_t** argv) {
base::string16 calc_path = MakePathToSys(L"calc.exe", false);
base::string16 calc_backup_path = MakePathToSys(L"calc.exe.bak", false);
std::wstring calc_path = MakePathToSys(L"calc.exe", false);
std::wstring calc_backup_path = MakePathToSys(L"calc.exe.bak", false);

if (::CopyFile(calc_path.c_str(), calc_backup_path.c_str(), FALSE))
return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
Expand Down Expand Up @@ -296,8 +296,8 @@ TEST(FilePolicyTest, AllowNtCreateCalc) {
}

TEST(FilePolicyTest, AllowNtCreateWithNativePath) {
base::string16 calc = MakePathToSys(L"calc.exe", false);
base::string16 nt_path;
std::wstring calc = MakePathToSys(L"calc.exe", false);
std::wstring nt_path;
ASSERT_TRUE(GetNtPathFromWin32Path(calc, &nt_path));
TestRunner runner;
runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, nt_path.c_str());
Expand Down Expand Up @@ -592,9 +592,9 @@ TEST(FilePolicyTest, TestReparsePoint) {
ASSERT_TRUE(::CreateDirectory(temp_file_name, nullptr));

// Create a temporary file in the subfolder.
base::string16 subfolder = temp_file_name;
base::string16 temp_file_title = subfolder.substr(subfolder.rfind(L"\\") + 1);
base::string16 temp_file = subfolder + L"\\file_" + temp_file_title;
std::wstring subfolder = temp_file_name;
std::wstring temp_file_title = subfolder.substr(subfolder.rfind(L"\\") + 1);
std::wstring temp_file = subfolder + L"\\file_" + temp_file_title;

HANDLE file = ::CreateFile(temp_file.c_str(), FILE_ALL_ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
Expand All @@ -603,21 +603,21 @@ TEST(FilePolicyTest, TestReparsePoint) {
ASSERT_TRUE(::CloseHandle(file));

// Create a temporary file in the temp directory.
base::string16 temp_dir = temp_directory;
base::string16 temp_file_in_temp = temp_dir + L"file_" + temp_file_title;
std::wstring temp_dir = temp_directory;
std::wstring temp_file_in_temp = temp_dir + L"file_" + temp_file_title;
file = ::CreateFile(temp_file_in_temp.c_str(), FILE_ALL_ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
CREATE_ALWAYS, 0, nullptr);
ASSERT_TRUE(INVALID_HANDLE_VALUE != file);
ASSERT_TRUE(::CloseHandle(file));

// Give write access to the temp directory.
base::string16 temp_dir_wildcard = temp_dir + L"*";
std::wstring temp_dir_wildcard = temp_dir + L"*";
EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY,
temp_dir_wildcard.c_str()));

// Prepare the command to execute.
base::string16 command_write;
std::wstring command_write;
command_write += L"File_Create Write \"";
command_write += temp_file;
command_write += L"\"";
Expand All @@ -632,7 +632,7 @@ TEST(FilePolicyTest, TestReparsePoint) {
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
EXPECT_TRUE(INVALID_HANDLE_VALUE != dir);

base::string16 temp_dir_nt;
std::wstring temp_dir_nt;
temp_dir_nt += L"\\??\\";
temp_dir_nt += temp_dir;
EXPECT_TRUE(SetReparsePoint(dir, temp_dir_nt.c_str()));
Expand All @@ -656,25 +656,25 @@ TEST(FilePolicyTest, TestReparsePoint) {
}

TEST(FilePolicyTest, CheckExistingNTPrefixEscape) {
base::string16 name = L"\\??\\NAME";
std::wstring name = L"\\??\\NAME";

base::string16 result = FixNTPrefixForMatch(name);
std::wstring result = FixNTPrefixForMatch(name);

EXPECT_STREQ(result.c_str(), L"\\/?/?\\NAME");
}

TEST(FilePolicyTest, CheckEscapedNTPrefixNoEscape) {
base::string16 name = L"\\/?/?\\NAME";
std::wstring name = L"\\/?/?\\NAME";

base::string16 result = FixNTPrefixForMatch(name);
std::wstring result = FixNTPrefixForMatch(name);

EXPECT_STREQ(result.c_str(), name.c_str());
}

TEST(FilePolicyTest, CheckMissingNTPrefixEscape) {
base::string16 name = L"C:\\NAME";
std::wstring name = L"C:\\NAME";

base::string16 result = FixNTPrefixForMatch(name);
std::wstring result = FixNTPrefixForMatch(name);

EXPECT_STREQ(result.c_str(), L"\\/?/?\\C:\\NAME");
}
Expand Down

0 comments on commit 4e88a3c

Please sign in to comment.