Skip to content

Commit b4195cb

Browse files
Merge pull request #2006 from zhaomaosu/devsan-fix-cxxlib-mismatch
[DeviceSanitizer] Fix libstdc++ and libc++ mismatch problem
2 parents 5482621 + ab77d02 commit b4195cb

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

source/loader/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ if(UR_ENABLE_SANITIZER)
167167
)
168168
target_include_directories(ur_loader PRIVATE ${LLVM_INCLUDE_DIRS})
169169
target_link_libraries(ur_loader PRIVATE LLVMSupport LLVMSymbolize)
170+
# In in-tree build, if LLVM is built with libc++, we also need to build
171+
# symbolizer.cpp with libc++ abi and link libc++ in.
172+
if(NOT UR_STANDALONE_BUILD AND LLVM_LIBCXX_USED)
173+
execute_process(
174+
COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=libc++.a
175+
OUTPUT_VARIABLE LIBCXX_PATH
176+
OUTPUT_STRIP_TRAILING_WHITESPACE)
177+
execute_process(
178+
COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=libc++abi.a
179+
OUTPUT_VARIABLE LIBCXX_ABI_PATH
180+
OUTPUT_STRIP_TRAILING_WHITESPACE)
181+
set_property(SOURCE
182+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/linux/symbolizer.cpp
183+
APPEND_STRING PROPERTY COMPILE_FLAGS
184+
" -stdlib=libc++ ")
185+
if(NOT EXISTS ${LIBCXX_PATH} OR NOT EXISTS ${LIBCXX_ABI_PATH})
186+
message(FATAL_ERROR "libc++ is required but can't find the libraries")
187+
endif()
188+
target_link_libraries(ur_loader PRIVATE ${LIBCXX_PATH} ${LIBCXX_ABI_PATH})
189+
endif()
170190
endif()
171191

172192
target_include_directories(ur_loader PRIVATE

source/loader/layers/sanitizer/linux/symbolizer.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ llvm::symbolize::PrinterConfig GetPrinterConfig() {
3131

3232
extern "C" {
3333

34-
bool SymbolizeCode(const std::string ModuleName, uint64_t ModuleOffset,
35-
std::string &Result) {
34+
void SymbolizeCode(const char *ModuleName, uint64_t ModuleOffset,
35+
char *ResultString, size_t ResultSize, size_t *RetSize) {
36+
std::string Result;
3637
llvm::raw_string_ostream OS(Result);
3738
llvm::symbolize::Request Request{ModuleName, ModuleOffset};
3839
llvm::symbolize::PrinterConfig Config =
@@ -51,10 +52,16 @@ bool SymbolizeCode(const std::string ModuleName, uint64_t ModuleOffset,
5152
{ModuleOffset, llvm::object::SectionedAddress::UndefSection});
5253

5354
if (!ResOrErr) {
54-
return false;
55+
return;
5556
}
5657
Printer->print(Request, *ResOrErr);
5758
ur_sanitizer_layer::GetSymbolizer()->pruneCache();
58-
return true;
59+
if (RetSize) {
60+
*RetSize = Result.size() + 1;
61+
}
62+
if (ResultString) {
63+
std::strncpy(ResultString, Result.c_str(), ResultSize);
64+
ResultString[ResultSize - 1] = '\0';
65+
}
5966
}
6067
}

source/loader/layers/sanitizer/stacktrace.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
extern "C" {
1717

18-
__attribute__((weak)) bool SymbolizeCode(const std::string ModuleName,
18+
__attribute__((weak)) void SymbolizeCode(const char *ModuleName,
1919
uint64_t ModuleOffset,
20-
std::string &Result);
20+
char *ResultString, size_t ResultSize,
21+
size_t *RetSize);
2122
}
2223

2324
namespace ur_sanitizer_layer {
@@ -49,7 +50,7 @@ void ParseBacktraceInfo(BacktraceInfo BI, std::string &ModuleName,
4950
// Parse symbolizer output in the following formats:
5051
// <function_name>
5152
// <file_name>:<line_number>[:<column_number>]
52-
SourceInfo ParseSymbolizerOutput(std::string Output) {
53+
SourceInfo ParseSymbolizerOutput(const std::string &Output) {
5354
SourceInfo Info;
5455
// Parse function name
5556
size_t End = Output.find_first_of('\n');
@@ -98,8 +99,14 @@ void StackTrace::print() const {
9899
std::string ModuleName;
99100
uptr Offset;
100101
ParseBacktraceInfo(BI, ModuleName, Offset);
101-
if (SymbolizeCode(ModuleName, Offset, Result)) {
102-
SourceInfo SrcInfo = ParseSymbolizerOutput(std::move(Result));
102+
size_t ResultSize = 0;
103+
SymbolizeCode(ModuleName.c_str(), Offset, nullptr, 0, &ResultSize);
104+
if (ResultSize) {
105+
std::vector<char> ResultVector(ResultSize);
106+
SymbolizeCode(ModuleName.c_str(), Offset, ResultVector.data(),
107+
ResultSize, nullptr);
108+
std::string Result((char *)ResultVector.data());
109+
SourceInfo SrcInfo = ParseSymbolizerOutput(Result);
103110
if (SrcInfo.file != "??") {
104111
getContext()->logger.always(" #{} in {} {}:{}:{}", index,
105112
SrcInfo.function, SrcInfo.file,
@@ -109,10 +116,10 @@ void StackTrace::print() const {
109116
SrcInfo.function, ModuleName,
110117
(void *)Offset);
111118
}
112-
continue;
113119
}
120+
} else {
121+
getContext()->logger.always(" #{} {}", index, BI);
114122
}
115-
getContext()->logger.always(" #{} {}", index, BI);
116123
++index;
117124
}
118125
getContext()->logger.always("");

0 commit comments

Comments
 (0)