Skip to content

Commit

Permalink
[DeviceASAN] Fix incorrect symbolizer output for shared libraries (#2229
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zhaomaosu authored Oct 28, 2024
1 parent a5755f1 commit 219e69f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
28 changes: 26 additions & 2 deletions source/loader/layers/sanitizer/linux/symbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#include "common.hpp"
#include "llvm/DebugInfo/Symbolize/DIPrinter.h"
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
#include <link.h>

static llvm::symbolize::PrinterConfig GetPrinterConfig() {
llvm::symbolize::PrinterConfig Config;
Expand All @@ -20,6 +22,28 @@ static llvm::symbolize::PrinterConfig GetPrinterConfig() {
return Config;
}

static uintptr_t GetModuleBase(const char *ModuleName) {
uintptr_t Data = (uintptr_t)ModuleName;
int Result = dl_iterate_phdr(
[](struct dl_phdr_info *Info, size_t, void *Arg) {
uintptr_t *Data = (uintptr_t *)Arg;
const char *ModuleName = (const char *)(*Data);
if (strstr(Info->dlpi_name, ModuleName)) {
*Data = (uintptr_t)Info->dlpi_addr;
return 1;
}
return 0;
},
(void *)&Data);

// If dl_iterate_phdr return 0, it means the module is main executable,
// its base address should be 0.
if (!Result) {
return 0;
}
return Data;
}

extern "C" {

void SymbolizeCode(const char *ModuleName, uint64_t ModuleOffset,
Expand All @@ -38,8 +62,8 @@ void SymbolizeCode(const char *ModuleName, uint64_t ModuleOffset,
llvm::symbolize::LLVMPrinter Printer(OS, EH, Config);

auto ResOrErr = Symbolizer.symbolizeInlinedCode(
ModuleName,
{ModuleOffset, llvm::object::SectionedAddress::UndefSection});
ModuleName, {ModuleOffset - GetModuleBase(ModuleName),
llvm::object::SectionedAddress::UndefSection});

if (!ResOrErr) {
return;
Expand Down
8 changes: 5 additions & 3 deletions source/loader/layers/sanitizer/stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include "stacktrace.hpp"
#include "asan_interceptor.hpp"
#include "ur_sanitizer_layer.hpp"

extern "C" {
Expand Down Expand Up @@ -92,9 +93,10 @@ void StackTrace::print() const {
BacktraceInfo BI = BacktraceSymbols[i];

// Skip runtime modules
if (Contains(BI, "libsycl.so") ||
Contains(BI, "libpi_unified_runtime.so") ||
Contains(BI, "libur_loader.so")) {
if (!getContext()->interceptor->getOptions().Debug &&
(Contains(BI, "libsycl.so") || Contains(BI, "libur_loader.so") ||
Contains(BI, "libomptarget.rtl.unified_runtime.so") ||
Contains(BI, "libomptarget.so"))) {
continue;
}

Expand Down

0 comments on commit 219e69f

Please sign in to comment.