Files
carbon-lang/common/exe_path_test.cpp
T
Jon Ross-Perkins 3070e5cfc6 Try using getMainExecutable to address argv[0] limitations (#5643)
When finding an executable, this validates that the returned binary is a
symlink back to the same thing as /proc/self/exe, also using that as a
fallback for different things.

Looking back at #3912, we started using `findProgramByName` in order to
avoid path canonicalization done by `GetMainExecutable`. That created
issues as in #5096, wherein an `argv[0]` that's not explicit enough
(`llvm-symbolizer` instead of the full path, done in [LLVM's
Signals.cpp](https://github.com/llvm/llvm-project/blob/4f60f45130c6bd96c79e468fe9927a29af760f56/llvm/lib/Support/Signals.cpp#L198))
leads to incorrect results (finding an `llvm-symbolizer` in `$PATH`).

One option to fix this would be to patch LLVM to provide an absolute
path for `llvm-symbolizer`. However, I'll suggest that passing a
filename in `argv[0]` is not terribly uncommon, and could be a migration
limitation if we force it. The failure mode is also opaque; for example:

```
$ /bin/sh -c "exec -a llvm-symbolizer ./bazel-bin/toolchain/carbon"
error: expected carbon-busybox symlink at `/usr/lib/llvm-19/bin/llvm-symbolizer`
```

Combined with the `setenv` of `LLVM_SYMBOLIZER_PATH` in
`busybox_main.cpp`, this is intended to fix #5096.
2025-06-12 16:05:11 +00:00

58 lines
1.8 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/exe_path.h"
#include <gtest/gtest.h>
#include <string>
#include <system_error>
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
namespace Carbon {
namespace {
TEST(ExePath, FailureFallback) {
static int static_for_main_addr;
std::string running_binary =
llvm::sys::fs::getMainExecutable("exe_path_test", &static_for_main_addr);
llvm::SmallString<128> path = llvm::StringRef(getenv("TEST_TMPDIR"));
llvm::sys::path::append(path, "non_existant_binary");
std::string exe_path = FindExecutablePath(path.c_str());
EXPECT_EQ(running_binary, exe_path);
}
TEST(ExePath, Symlink) {
static int static_for_main_addr;
std::string running_binary =
llvm::sys::fs::getMainExecutable("exe_path_test", &static_for_main_addr);
llvm::SmallString<128> path = llvm::StringRef(getenv("TEST_TMPDIR"));
llvm::sys::path::append(path, "test_binary");
std::error_code ec;
std::filesystem::create_symlink(running_binary, path.c_str(), ec);
ASSERT_TRUE(!ec) << "Error code: " << ec;
std::string exe_path = FindExecutablePath(path.c_str());
EXPECT_EQ(path, exe_path);
}
TEST(ExePath, PathLookup) {
// TODO: This is not likely to work well on Windows (outside of WSL). But some
// of that may be hidden by Bazel's test environment. Regardless, we should
// revisit this when we have good coverage of Windows build with something
// appropriate for that platform.
std::string exe_path = FindExecutablePath("bash");
EXPECT_NE(exe_path, "bash");
EXPECT_TRUE(llvm::sys::fs::exists(exe_path));
EXPECT_TRUE(llvm::sys::fs::can_execute(exe_path));
}
} // namespace
} // namespace Carbon