Check for cycles in object files' node graphs to avoid infinite loops in error messages (#2256)

This commit is contained in:
Rangi authored and GitHub committed 2026-11-26 17:01:16 -05:00
1 parent d5812eb438
commit 84a7a69506
4 files changed
+56

No files matched your search

+29
View File
@@ -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<FileStackNode> const &fileNodes, char const *fileName) {
enum DfsState : uint8_t { DFS_UNVISITED, DFS_VISITED, DFS_TERMINATES };
std::vector<DfsState> 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<size_t>(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<size_t>(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<Symbol> &fileSymbols = symbolLists.emplace_front(nbSymbols);
+2
View File
@@ -0,0 +1,2 @@
SECTION "s", ROM0
db 42
+2
View File
@@ -0,0 +1,2 @@
FATAL: <object>: Node #0 has cyclic parent ID #0
Linking aborted with 1 error
+23
View File
@@ -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 "<object>" 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/<object>/g" -e "s/$subst2/<object>/g" "$outtemp" >"$outtemp2"
else
subst="$(printf '%s\n' "$gbtemp2" | sed 's:[][\/.^$*]:\\&:g')"
sed "s/$subst/<object>/g" "$outtemp" >"$outtemp2"
fi
tryDiff "$test"/out.err "$outtemp2"
evaluateTest
test="export-all"
startTest
"$RGBASM" -E -o "$otemp" "$test"/a.asm