Skip to content

Commit 7e28063

Browse files
test(e2e): a link and a pack under a caller's XLINGS_SUBOS_LD_PATHS keep the run path mcpp derived
xlings' ld wrapper appends -rpath "$XLINGS_SUBOS_LIB" to a link of a non-system loader unless XLINGS_SUBOS_LD_PATHS=0, which mcpp declares for everything it starts. Two cases state what that holds from mcpp's side, with the caller's XLINGS_SUBOS_LD_PATHS=1 and a XLINGS_SUBOS_LIB of its own: - the programs the link starts (observed through gcc's -wrapper, which sees collect2, the linker's parent) and a build program read XLINGS_SUBOS_LD_PATHS=0, and the artifact's run path equals the -Wl,-rpath entries of the link line, without the caller's directory; - every ELF object a pack bundles names only $ORIGIN directories. The declaration is the one cli.cppm makes for the invocation; no spawn dropped it, so no code changes. Without the declaration the first case fails on both observations. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 11824df commit 7e28063

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// The linker's run-path rule from mcpp's side (xlings design 2026-11-27 §8.3, C1): xlings' `ld`
2+
// wrapper appends `-rpath "$XLINGS_SUBOS_LIB"` to a link of a non-system loader unless
3+
// XLINGS_SUBOS_LD_PATHS=0. mcpp declares the 0 for everything it starts, whatever its caller set,
4+
// so an artifact's run path is the one mcpp itself derived from the binding it selected, and a
5+
// pack holds no path of the machine that built it.
6+
//
7+
// What these cases hold without the wrapper (binutils before its r1 carries none): the variable
8+
// each program mcpp starts receives, and the run path the artifact ends with. With the wrapper
9+
// installed the second would fail were the declaration missing.
10+
import std;
11+
import mcpplibs.mtest;
12+
import mcpp.test.support;
13+
14+
using namespace mcpplibs::mtest;
15+
namespace s = mcpp::test::support;
16+
namespace fs = std::filesystem;
17+
18+
namespace {
19+
20+
std::vector<std::string> split(std::string_view text, char sep) {
21+
std::vector<std::string> out;
22+
for (auto part : std::views::split(text, sep))
23+
if (part.begin() != part.end()) out.emplace_back(part.begin(), part.end());
24+
return out;
25+
}
26+
27+
// The first build.ninja of the project's target, outside the build program's tree.
28+
std::string graph_of(const s::project& p) {
29+
for (const auto& f : s::find_files(p.at("target"), R"(build\.ninja)"))
30+
if (f.generic_string().find("/.build-mcpp/") == std::string::npos) return s::read_file(f);
31+
expect(fatal(false)) << "no build.ninja generated";
32+
return {};
33+
}
34+
35+
// The directories the link line gives the linker as run path (`-Wl,-rpath,<dir>`), as the linker
36+
// reads them (ninja's `$$` is a `$`).
37+
std::set<std::string> rpath_of_graph(const std::string& graph) {
38+
std::set<std::string> out;
39+
static const std::regex rpath{R"(-Wl,-rpath,([^\s"']+))"};
40+
for (std::sregex_iterator it{graph.begin(), graph.end(), rpath}, end; it != end; ++it) {
41+
std::string dir = (*it)[1].str();
42+
for (auto at = dir.find("$$"); at != std::string::npos; at = dir.find("$$", at + 1)) dir.erase(at, 1);
43+
out.insert(std::move(dir));
44+
}
45+
return out;
46+
}
47+
48+
std::set<std::string> rpath_of_artifact(const fs::path& file) {
49+
const auto parts = split(s::read_header(file).runpath, ':');
50+
return {parts.begin(), parts.end()};
51+
}
52+
53+
std::string joined(const std::set<std::string>& entries) {
54+
std::string out;
55+
for (const auto& e : entries) out += (out.empty() ? "" : ":") + e;
56+
return out;
57+
}
58+
59+
std::optional<fs::path> program_under(const fs::path& root, std::string_view name) {
60+
for (const auto& f : s::find_files(root, std::string(name)))
61+
if (f.generic_string().find("/.build-mcpp/") == std::string::npos && f.generic_string().find("/bin/") != std::string::npos)
62+
return f;
63+
return std::nullopt;
64+
}
65+
66+
std::string output_of(const fs::path& program) {
67+
auto out = s::run(program).out;
68+
while (out.ends_with('\n') || out.ends_with('\r')) out.pop_back();
69+
return out;
70+
}
71+
72+
// The regular files under `root`, not the links.
73+
std::vector<fs::path> plain_files(const fs::path& root) {
74+
std::vector<fs::path> out;
75+
std::error_code ec;
76+
if (fs::is_directory(root, ec))
77+
for (const auto& e : fs::recursive_directory_iterator(root))
78+
if (!e.is_symlink() && e.is_regular_file()) out.push_back(e.path());
79+
std::ranges::sort(out);
80+
return out;
81+
}
82+
83+
// What a caller that meets the wrapper without mcpp's declaration would give it: a directory of
84+
// the caller's own as the active SubOS's library directory, and the variable that lets the
85+
// wrapper add it.
86+
std::vector<std::pair<std::string, std::optional<std::string>>> callers_env(const fs::path& subos_lib) {
87+
return {{"XLINGS_SUBOS_LIB", subos_lib.string()}, {"XLINGS_SUBOS_LD_PATHS", std::string("1")}};
88+
}
89+
90+
E2E linker_declaration{"linker_declaration", s::home_fixture, [] {
91+
"1177 a link under a caller's SubOS keeps the run path mcpp derived"_test[s::gcc][s::elf][s::unix_shell] = [] {
92+
// The caller's XLINGS_SUBOS_LD_PATHS=1 and XLINGS_SUBOS_LIB do not reach the link as
93+
// they were given: the programs the link starts (the compiler driver, and what it starts
94+
// for the link) and a build program read XLINGS_SUBOS_LD_PATHS=0, and the artifact's run
95+
// path is exactly the `-Wl,-rpath,` of the link line, without the caller's directory.
96+
const auto tmp = s::case_dir();
97+
const auto subos_lib = tmp / "callers-subos" / "lib";
98+
fs::create_directories(subos_lib);
99+
const auto log = tmp / "driver-env.log";
100+
s::write_executable(tmp / "observe.sh", std::format(R"sh(#!/bin/sh
101+
# gcc's -wrapper runs each program of the link (collect2, which starts the linker) as `$@`.
102+
printf '%s LD_PATHS=%s LIB=%s\n' "$(basename "$1")" "${{XLINGS_SUBOS_LD_PATHS-unset}}" "${{XLINGS_SUBOS_LIB-unset}}" >> '{}'
103+
exec "$@"
104+
)sh", log.string()));
105+
s::project p{tmp / "p", {{"mcpp.toml", std::format(R"([package]
106+
name = "rp"
107+
version = "0.1.0"
108+
109+
[build]
110+
ldflags = ["-wrapper", "{}"]
111+
)", (tmp / "observe.sh").string())},
112+
{"src/main.cpp", R"cpp(#include <cstdio>
113+
int main() { std::printf("%d\n", LD_PATHS_SEEN_BY_BUILD_PROGRAM); }
114+
)cpp"},
115+
{"build.mcpp", R"cpp(import mcpp;
116+
import std;
117+
int main() {
118+
const char* v = std::getenv("XLINGS_SUBOS_LD_PATHS");
119+
mcpp::cxxflag(std::format("-DLD_PATHS_SEEN_BY_BUILD_PROGRAM={}", v && *v == '0' && !v[1] ? 0 : 7).c_str());
120+
}
121+
)cpp"}}};
122+
auto r = p.mcpp({"build"}, {.env = callers_env(subos_lib)});
123+
expect(fatal(e2e::succeeded(r))) << "the build failed\n" << s::text(r);
124+
const auto bin = program_under(p.at("target"), "rp");
125+
expect(fatal(bin.has_value())) << "no program was produced";
126+
expect(output_of(*bin) == "0") << "the build program saw XLINGS_SUBOS_LD_PATHS other than 0 (7 is anything else, the caller's 1 included): " << output_of(*bin);
127+
128+
const auto seen = s::read_file(log);
129+
expect(fatal(e2e::contains(seen, "collect2 "))) << "the compiler driver started no collect2 under -wrapper: the link was not observed\n" << seen;
130+
for (const auto& l : split(seen, '\n')) {
131+
expect(e2e::contains(l, "LD_PATHS=0 ")) << "a program of the link was not told XLINGS_SUBOS_LD_PATHS=0: " << l;
132+
expect(e2e::contains(l, "LIB=" + subos_lib.string())) << "the caller's XLINGS_SUBOS_LIB was dropped on the way (the case would prove nothing about the opt-out): " << l;
133+
}
134+
135+
const auto intent = rpath_of_graph(graph_of(p));
136+
const auto actual = rpath_of_artifact(*bin);
137+
expect(fatal(actual == intent)) << "the artifact's run path is not the one mcpp put on the link line\n link line: " << joined(intent) << "\n artifact: " << joined(actual);
138+
expect(fatal(!joined(actual).contains(subos_lib.string()))) << "the caller's SubOS directory is in the run path: " << joined(actual);
139+
expect(fatal(!joined(actual).contains("callers-subos"))) << "the caller's SubOS is in the run path: " << joined(actual);
140+
};
141+
142+
"1178 a pack holds no run path outside its bundle"_test[s::pack][s::gcc][s::elf] = [] {
143+
// Under a caller's XLINGS_SUBOS_LD_PATHS=1 and XLINGS_SUBOS_LIB, no ELF object of the
144+
// packed program names a directory other than one under its own $ORIGIN: not the
145+
// caller's SubOS, not the home's store, not the registry's farm.
146+
const auto tmp = s::case_dir();
147+
const auto subos_lib = tmp / "callers-subos" / "lib";
148+
fs::create_directories(subos_lib);
149+
auto app = s::project::scaffold("bundled");
150+
auto r = app.mcpp({"pack"}, {.env = callers_env(subos_lib)});
151+
expect(fatal(e2e::succeeded(r))) << "pack failed\n" << s::text(r);
152+
std::optional<fs::path> tarball;
153+
for (const auto& f : s::find_files(app.at("target/dist"), R"(.*\.tar\.gz)")) tarball = f;
154+
expect(fatal(tarball.has_value())) << "no tarball\n" << s::text(r);
155+
s::tar_extract(*tarball, tmp / "x");
156+
std::size_t objects = 0;
157+
for (const auto& obj : plain_files(tmp / "x")) {
158+
const auto h = s::read_header(obj);
159+
if (h.kind != s::format::elf || !h.wide) continue;
160+
++objects;
161+
const auto rel = fs::relative(obj, tmp / "x").generic_string();
162+
for (const auto& entry : split(h.runpath, ':'))
163+
expect(entry.starts_with("$ORIGIN")) << rel << " names a run-path directory outside the bundle: " << entry << " (run path " << h.runpath << ")";
164+
}
165+
expect(fatal(objects > 0u)) << "the bundle holds no ELF object: nothing was checked";
166+
};
167+
}};
168+
169+
} // namespace

0 commit comments

Comments
 (0)