Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions sys/src/cmd/ld/dwarf.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,11 @@ addhistfile(char *zentry)
return histfilesize - 1;
}

// Go's runtime C sources are sane, and Go sources nest only 1 level,
// so 16 should be plenty.
// APE headers have deep transitive include chains; 256 covers real-world use.
static struct {
int file;
vlong line;
} includestack[64];
} includestack[256];
static int includetop;
static vlong absline;

Expand All @@ -542,9 +541,9 @@ checknesting(void)
errorexit();
}
if (includetop >= nelem(includestack)) {
diag("nesting too deep");
for (i = 0; i < nelem(includestack); i++)
diag("%s", histfile[includestack[i].file]); errorexit();
if(includetop == nelem(includestack))
diag("include nesting too deep, truncating DWARF line info");
includetop = nelem(includestack) - 1;
Comment on lines +544 to +546

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The clamping logic introduced here has two significant issues:

  1. Diagnostic Spam: Since includetop is reset to nelem(includestack) - 1, the next push will increment it back to nelem, triggering the diag message again. This will repeat for every include level and line change beyond the limit, flooding the output.

  2. Stack Underflow / Desynchronization: Clamping the stack pointer includetop causes it to lose sync with the actual nesting depth. When the nesting level decreases (pops), includetop will be decremented from the clamped value. Once the nesting returns to zero, includetop will become negative, likely triggering a fatal error (stack underflow) later in the execution. This defeats the purpose of making the limit non-fatal.

To fix the spam, use a static flag. It is also recommended to keep the stack trace (the loop over histfile) inside the if(!warned) block to help identify the cause of the deep nesting. To properly handle the nesting without aborting, you should ideally allow includetop to track the real depth but guard the array accesses to use a clamped index.

		static int warned;
		if(!warned){
			int i;
			diag("include nesting too deep, truncating DWARF line info");
			for(i = 0; i < nelem(includestack); i++)
				diag("%s", histfile[includestack[i].file]);
			warned = 1;
		}
		includetop = nelem(includestack) - 1;

}
}

Expand Down
Loading