<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/125694>125694</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [asan] ReportErrorSummary should symbolicate at the exception PC, not the previous instruction
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yjugl
      </td>
    </tr>
</table>

<pre>
    Below is an example ASAN x64 report that sparked confusion [in a Mozilla bug entry](https://bugzilla.mozilla.org/show_bug.cgi?id=1945246):

```
==9540==ERROR: AddressSanitizer: breakpoint on unknown address 0x7ffa2a4c53b5 (pc 0x7ffa2a4c53b5 bp 0x00c5df9fcf70 sp 0x00c5df9fcee0 T0)
#0 0x7ffa2a4c53b4 in AnnotateMozCrashReason /builds/worker/workspace/obj-build/dist/include/mozilla/Assertions.h:55
#1 0x7ffa2a4c53b4 in MOZ_Crash /builds/worker/workspace/obj-build/dist/include/mozilla/Assertions.h:336
#2 0x7ffa2a4c53b4 in mozilla::net::CheckForBrokenChromeURL(class nsILoadInfo *, class nsIURI *) /builds/worker/checkouts/gecko/netwerk/base/nsNetUtil.cpp:4105
#3 0x7ffa2b924414 in nsJARChannel::OnOpenLocalFileComplete(enum nsresult, bool) /builds/worker/checkouts/gecko/modules/libjar/nsJARChannel.cpp:460
#4 0x7ffa2aee9f6f in mozilla::detail::RunnableMethodArguments<unsigned int,bool>::apply<mozilla::net::HttpBackgroundChannelParent,bool (mozilla::net::HttpBackgroundChannelParent::*)(unsigned int, bool)>::<lambda_1>::operator() /builds/worker/workspace/obj-build/dist/include/nsThreadUtils.h:1085
....
```

In this example, the first 3 "stack" addresses (`0x7ffa2a4c53b4`) refer to the exception PC (minus 1), while the rest refer to the return addresses (minus 1) that were found on the stack. The symbols shown next to the addresses were obtained by symbolicating at the addresses printed (so -1 compared to the actual addresses found).

While it can make sense to symbolicate at -1 for the return addresses in order to symbolicate at the call site rather than the return site, I would argue that:
(a) it makes no sense to symbolicate at -1 for the exception PC, and it can get people confused about where the exception happened exactly (see linked Mozilla bug entry);
(b) it is confusing to print these -1 addresses in the output as they will typically point to invalid code, other tools would just print the actual exception PC and return addresses found.

This issue is mostly about (a) though. Below are some of the code paths that I think are involved.

In `lib/sanitizer_common/sanitizer_stacktrace.cpp`  we don't distinguish the exception pc from a return address when we store it in `trace_buffer`:

```c++
void BufferedStackTrace::UnwindFast(uptr pc, uptr bp, uptr stack_top,
                                    uptr stack_bottom, u32 max_depth) {
  // ...
  trace_buffer[0] = pc;
  // ...
}
```

In `lib/sanitizer_common/sanitizer_symbolizer_report.cpp` we iterate overy the stack and symbolicate the instruction at the previous PC for all entries, including those derived from the exception PC:

```c++
void ReportErrorSummary(const char *error_type, const StackTrace *stack,
 const char *alt_tool_name) {
  // ...
    uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[i]);
 SymbolizedStackHolder symbolized_stack(
 Symbolizer::GetOrInit()->SymbolizePC(pc));
    if (const SymbolizedStack *frame = symbolized_stack.get()) {
      if (const SymbolizedStack *summary_frame = SkipInternalFrames(frame)) {
 ReportErrorSummary(error_type, summary_frame->info, alt_tool_name);
 return;
      }
    }
  }
  // ...
}
```


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V1tv2zoS_jXMyyCGTPn64Afbqfd40Z4WSYsF9sWgpJHEmCIFkorj_vrFkLIdO9k9OQ9bGAhLzeXjNxcOhXOy0ogLNl6x8cOd6Hxt7OL43FXqLjPFcbFCZQ4gHQgN-CqaViEsn5Z_wutkBBZbYz34WnhwrbB7LCA3uuycNBrYeCU1CPhmfkulBGRdBai9PbLxA-Oz2vvWsXTJ-IbxTdZVQWrQROmBsRXjG1ebwy7rqkFeSZZuZMHSh-F8NOajCeNz0k7Cb5L0v2TJ0geWPszHoySuvjw-fn9k6RKWRWHRuSehpZe_0dJeZlHsWyO1B6O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