Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
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
35 changes: 24 additions & 11 deletions src/rt/backtrace/dwarf.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,44 @@ int traceHandlerOpApplyImpl(const void*[] callstack, scope int delegate(ref size
int ret = 0;
foreach (size_t i; 0 .. callstack.length)
{
char[1536] buffer = void; buffer[0] = 0;
char[256] addressBuffer = void; addressBuffer[0] = 0;
char[1536] buffer = void;
size_t bufferLength = 0;

void appendToBuffer(Args...)(const(char)* format, Args args)
{
const count = snprintf(buffer.ptr + bufferLength, buffer.length - bufferLength, format, args);
assert(count >= 0);
bufferLength += count;
if (bufferLength >= buffer.length)
bufferLength = buffer.length - 1;
}

if (locations.length > 0 && locations[i].line != -1)
snprintf(addressBuffer.ptr, addressBuffer.length, "%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
{
appendToBuffer("%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
}
else
addressBuffer[] = "??:? \0";
{
buffer[0 .. 5] = "??:? ";
bufferLength = 5;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix 1:

addressBuffer[] = "??:? \0";

=> object.Error@(0): Array lengths don't match for copy https://run.dlang.io/is/4RQZWh

Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance we can add a test for this to prevent future regressions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's pretty bad with -release, causing a segfault as long as the destination buffer is sufficiently large: https://run.dlang.io/is/NEqZOB. Without -release an exception is thrown during stacktrace generation, which is also pretty bad.

The latter is how I stumbled on this. LLVM 6.0 led to DWARF v4 debuginfos, rt.backtrace.dwarf doesn't support that version, so no file/line infos => "??:? \0" + exception (debug druntime only; release druntime compiled with -release happily reads beyond the string constant, causing no segfault on x86 apparently). The exception with debug-druntime is swallowed in my test case, it only manifests itself in an incomplete stack trace (single entry).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wilzbach: There's no debug druntime (i.e., compiled with enabled bounds checks) available for CI, right? Then it's untestable.


char[1024] symbolBuffer = void;
int bufferLength;
auto symbol = getDemangledSymbol(frameList[i][0 .. strlen(frameList[i])], symbolBuffer);
if (symbol.length > 0)
bufferLength = snprintf(buffer.ptr, buffer.length, "%s%.*s ", addressBuffer.ptr, cast(int) symbol.length, symbol.ptr);
else
bufferLength = snprintf(buffer.ptr, buffer.length, "%s", addressBuffer.ptr);
appendToBuffer("%.*s ", cast(int) symbol.length, symbol.ptr);

assert(bufferLength >= 0);
const addressLength = 20;
const maxBufferLength = buffer.length - addressLength;
if (bufferLength > maxBufferLength)
{
buffer[maxBufferLength-4 .. maxBufferLength] = "... ";
bufferLength = maxBufferLength;
buffer[$-4-addressLength..$-addressLength] = "... ";
}
bufferLength += snprintf(buffer.ptr + bufferLength, buffer.length, "[0x%x]", callstack[i]);
static if (size_t.sizeof == 8)
appendToBuffer("[0x%llx]", callstack[i]);
else
appendToBuffer("[0x%x]", callstack[i]);
Copy link
Contributor Author

@kinke kinke Mar 25, 2018

Choose a reason for hiding this comment

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

Fix 2: don't restrict to lower 32 bits of address on 64-bit platforms, e.g., now ??:? __libc_start_main [0x7f7bb141182f].


auto output = buffer[0 .. bufferLength];
auto pos = i;
Expand Down