Skip to content

Commit

Permalink
[lldb] Fix TestSettings.test_pass_host_env_vars on windows
Browse files Browse the repository at this point in the history
Summary:
A defensive check in ProcessLauncherWindows meant that we would never
attempt to launch a process with a completely empty environment -- the
host environment would be used instead. Instead, I make the function add
an extra null wchar_t at the end of an empty environment. The
documentation on this is a bit fuzzy, but it seems to be what is needed
to make windows accept these kinds of environments.

Reviewers: amccarth, friss

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D76835
  • Loading branch information
labath committed Mar 30, 2020
1 parent 703a1b8 commit 908f78f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lldb/source/Host/windows/ProcessLauncherWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ using namespace lldb_private;
namespace {
void CreateEnvironmentBuffer(const Environment &env,
std::vector<char> &buffer) {
if (env.size() == 0)
return;

// Environment buffer is a null terminated list of null terminated strings
// The buffer is a list of null-terminated UTF-16 strings, followed by an
// extra L'\0' (two bytes of 0). An empty environment must have one
// empty string, followed by an extra L'\0'.
for (const auto &KV : env) {
std::wstring warg;
if (llvm::ConvertUTF8toWide(Environment::compose(KV), warg)) {
Expand All @@ -38,6 +37,9 @@ void CreateEnvironmentBuffer(const Environment &env,
// One null wchar_t (to end the block) is two null bytes
buffer.push_back(0);
buffer.push_back(0);
// Insert extra two bytes, just in case the environment was empty.
buffer.push_back(0);
buffer.push_back(0);
}

bool GetFlattenedWindowsCommandString(Args args, std::string &command) {
Expand Down Expand Up @@ -94,8 +96,7 @@ ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info,

LPVOID env_block = nullptr;
::CreateEnvironmentBuffer(launch_info.GetEnvironment(), environment);
if (!environment.empty())
env_block = environment.data();
env_block = environment.data();

executable = launch_info.GetExecutableFile().GetPath();
GetFlattenedWindowsCommandString(launch_info.GetArguments(), commandLine);
Expand Down
1 change: 0 additions & 1 deletion lldb/test/API/commands/settings/TestSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ def do_test_run_args_and_env_vars(self, use_launchsimple):
"Environment variable 'MY_ENV_VAR' successfully passed."])

@skipIfRemote # it doesn't make sense to send host env to remote target
@skipIf(oslist=["windows"])
def test_pass_host_env_vars(self):
"""Test that the host env vars are passed to the launched process."""
self.build()
Expand Down

0 comments on commit 908f78f

Please sign in to comment.