From 1440e20231c8efc67806145fa79c5a26f5043043 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 14 Nov 2026 23:42:47 +0100 Subject: [PATCH] test(external): a sibling's pointers to its own Configuration hold SiblingOwnPointers loads a fixture through LocalProject, on a fresh copy outside the checkout: a library whose project file declares a test with cfg.AddTest("OwnTest").Dependencies({ &cfg }). The one dependency of that test has to be the Configuration LocalProject returns. The test reads the dependency out of that Configuration and compares the two addresses, without following the dependency. Against the loader as it was before the fix the test fails: the dependency is not that Configuration. --- project.cpp | 2 + tests/SiblingOwnPointers/fixture/project.cpp | 20 ++++++++ .../fixture/tests/OwnTest/main.cpp | 6 +++ tests/SiblingOwnPointers/main.cpp | 49 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 tests/SiblingOwnPointers/fixture/project.cpp create mode 100644 tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp create mode 100644 tests/SiblingOwnPointers/main.cpp diff --git a/project.cpp b/project.cpp index 3b33cee..2aceae7 100644 --- a/project.cpp +++ b/project.cpp @@ -334,6 +334,8 @@ extern "C" Configuration CrafterBuildProject(std::span a .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("DependencyNotes").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); + cfg.AddTest("SiblingOwnPointers").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobGroup").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobLimit").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/SiblingOwnPointers/fixture/project.cpp b/tests/SiblingOwnPointers/fixture/project.cpp new file mode 100644 index 0000000..b766a0c --- /dev/null +++ b/tests/SiblingOwnPointers/fixture/project.cpp @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// A library whose test depends on it: the test's dependency is a pointer to +// the Configuration this function returns. A library with no sources is +// enough, since the test that loads this file reads the pointer and builds +// nothing. +extern "C" Configuration CrafterBuildProject(std::span) { + Configuration cfg; + cfg.path = "./"; + cfg.name = "own"; + cfg.outputName = "own"; + cfg.type = ConfigurationType::LibraryStatic; + cfg.AddTest("OwnTest").Dependencies({ &cfg }); + return cfg; +} diff --git a/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp b/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp new file mode 100644 index 0000000..97eb9af --- /dev/null +++ b/tests/SiblingOwnPointers/fixture/tests/OwnTest/main.cpp @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +int main() { + return 0; +} diff --git a/tests/SiblingOwnPointers/main.cpp b/tests/SiblingOwnPointers/main.cpp new file mode 100644 index 0000000..60348cb --- /dev/null +++ b/tests/SiblingOwnPointers/main.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +namespace fs = std::filesystem; +using namespace Crafter; + +// A project file may keep pointers to the Configuration it returns: a test +// that links the library the file describes is declared with +// cfg.AddTest("T").Dependencies({ &cfg }). Loaded by another project through +// LocalProject or GitProject, that Configuration has to stay where the +// project file built it. +// +// The loader moved it: out of the call's return value, and then into its +// cache. The test's dependency was left pointing at an object that no +// longer existed, and whatever followed it read freed memory. +// `crafter-build lint` follows it for a sibling inside the project's own +// directory, when it works out the compile command of each of the sibling's +// tests for a rule that needs an AST. +int main() { + const fs::path installedHome = GetCrafterBuildHome(); + if (!fs::exists(installedHome / "Crafter.Build.cppm")) { + std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string()); + return 77; + } + + // Staged because the load drops a build/ tree next to the project file. + const fs::path scratch = fs::temp_directory_path() / "crafter-build-sibling-own-pointers"; + std::error_code ec; + fs::remove_all(scratch, ec); + fs::copy(fs::current_path() / "tests" / "SiblingOwnPointers" / "fixture", scratch, fs::copy_options::recursive); + + Configuration* sibling = nullptr; + try { + sibling = LocalProject({ .projectFile = scratch / "project.cpp" }); + } catch (const std::exception& e) { + std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what()); + return 1; + } + // Compared, never followed: where the two differ, the pointer leads + // nowhere. + if (sibling->tests.size() != 1 || sibling->tests[0].config.dependencies != std::vector{sibling}) { + std::println(std::cerr, "FAIL: the pointer a sibling's project file took to the Configuration it returns is not the Configuration its consumers get"); + return 1; + } + fs::remove_all(scratch, ec); + return 0; +}