From 84a7a69506455ac8bc8da33b6463594e73624681 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:01:16 -0500 Subject: [PATCH] Check for cycles in object files' node graphs to avoid infinite loops in error messages (#2256) --- src/link/object.cpp | 29 +++++++++++++++++++++++++++++ test/link/cyclic-nodes/a.asm | 2 ++ test/link/cyclic-nodes/out.err | 2 ++ test/link/test.sh | 23 +++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 test/link/cyclic-nodes/a.asm create mode 100644 test/link/cyclic-nodes/out.err diff --git a/src/link/object.cpp b/src/link/object.cpp index d9aaa1a4..f3dabb0f 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -498,6 +498,34 @@ static void readAssertion( ); } +// Validates that there are no cycles in the graph of parent nodes with a three-color DFS. +// Any cycles from an invalid object file would cause printing backtraces to infinitely loop. +static void checkFileStackNodes(std::vector const &fileNodes, char const *fileName) { + enum DfsState : uint8_t { DFS_UNVISITED, DFS_VISITED, DFS_TERMINATES }; + std::vector states(fileNodes.size(), DFS_UNVISITED); + for (size_t nodeID = 0; nodeID < fileNodes.size(); ++nodeID) { + for (size_t i = nodeID; states[i] != DFS_TERMINATES;) { + if (states[i] == DFS_VISITED) { + fatal("%s: Node #%zu has cyclic parent ID #%zu", fileName, nodeID, i); + } + states[i] = DFS_VISITED; + if (FileStackNode const *parent = fileNodes[i].parent; parent != nullptr) { + i = static_cast(parent - fileNodes.data()); + } else { + break; + } + } + for (size_t i = nodeID; states[i] == DFS_VISITED;) { + states[i] = DFS_TERMINATES; + if (FileStackNode const *parent = fileNodes[i].parent; parent != nullptr) { + i = static_cast(parent - fileNodes.data()); + } else { + break; + } + } + } +} + void obj_ReadFile(std::string const &filePath, size_t fileID) { FILE *file; char const *fileName = filePath.c_str(); @@ -582,6 +610,7 @@ void obj_ReadFile(std::string const &filePath, size_t fileID) { for (uint32_t nodeID = nbNodes; nodeID--;) { readFileStackNode(file, nodes[fileID], nodeID, fileName); } + checkFileStackNodes(nodes[fileID], fileName); // This file's symbols, kept to link sections to them std::vector &fileSymbols = symbolLists.emplace_front(nbSymbols); diff --git a/test/link/cyclic-nodes/a.asm b/test/link/cyclic-nodes/a.asm new file mode 100644 index 00000000..6e195d01 --- /dev/null +++ b/test/link/cyclic-nodes/a.asm @@ -0,0 +1,2 @@ +SECTION "s", ROM0 +db 42 diff --git a/test/link/cyclic-nodes/out.err b/test/link/cyclic-nodes/out.err new file mode 100644 index 00000000..1dc1285c --- /dev/null +++ b/test/link/cyclic-nodes/out.err @@ -0,0 +1,2 @@ +FATAL: : Node #0 has cyclic parent ID #0 +Linking aborted with 1 error diff --git a/test/link/test.sh b/test/link/test.sh index 6dad3656..9b53f204 100755 --- a/test/link/test.sh +++ b/test/link/test.sh @@ -180,6 +180,29 @@ tryDiff "$test"/ref.out.sym "$outtemp2" tryCmpRom "$test"/ref.out.bin evaluateTest +test="cyclic-nodes" +startTest +"$RGBASM" -o "$otemp" "$test"/a.asm +"$RGBASM" -o "$gbtemp2" "$test"/a.asm +# Patch one node's parent ID to be itself, #0 +dd if=/dev/zero "of=$gbtemp2" bs=1 count=4 seek=20 conv=notrunc >/dev/null +continueTest +rgblinkQuiet -o "$gbtemp" "$otemp" "$gbtemp2" 2>"$outtemp" +# Replace the file name with "" to match changed output +fixed_output="$input" +if type -t cygpath >/dev/null; then + # MinGW needs the Windows path substituted but with forward slash separators; + # Cygwin has `cygpath` but just needs the original path substituted. + subst1="$(printf '%s\n' "$gbtemp2" | sed 's:[][\/.^$*]:\\&:g')" + subst2="$(printf '%s\n' "$(cygpath -w "$gbtemp2")" | sed -e 's:\\:/:g' -e 's:[][\/.^$*]:\\&:g')" + sed -e "s/$subst1//g" -e "s/$subst2//g" "$outtemp" >"$outtemp2" +else + subst="$(printf '%s\n' "$gbtemp2" | sed 's:[][\/.^$*]:\\&:g')" + sed "s/$subst//g" "$outtemp" >"$outtemp2" +fi +tryDiff "$test"/out.err "$outtemp2" +evaluateTest + test="export-all" startTest "$RGBASM" -E -o "$otemp" "$test"/a.asm