Skip to content

Commit

Permalink
parser-gcc: fix incomplete file paths in UBSAN key events
Browse files Browse the repository at this point in the history
UBSAN may only use the base name for the file path in its key event.
However, the top of the backtrace, if present, always contains the whole
path to this file.
  • Loading branch information
lzaoral committed Sep 12, 2023
1 parent 0e4db39 commit be20b75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ include_directories(lib)

# link cslib.a and boost libraries
link_libraries(cs
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_REGEX_LIBRARY})

Expand All @@ -67,13 +68,11 @@ add_executable(cshtml cshtml.cc)
add_executable(cslinker cslinker.cc)
add_executable(cssort cssort.cc)
target_link_libraries(cshtml
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})

# experimental
add_executable(cstrans-df-run cstrans-df-run.cc)
target_link_libraries(cstrans-df-run
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})

# declare what 'make install' should install
Expand Down
29 changes: 27 additions & 2 deletions src/lib/parser-gcc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "regex.hh"

#include <algorithm>
#include <boost/filesystem.hpp>

namespace GccParserImpl {

Expand Down Expand Up @@ -639,8 +640,32 @@ void GccPostProcessor::Private::transUbsan(Defect *pDef) const

// UBSAN always uses 'runtime error' for its key event
DefEvent &keyEvt = pDef->events[pDef->keyEventIdx];
if (keyEvt.event == "runtime error")
pDef->checker = "UBSAN_WARNING";
if (keyEvt.event != "runtime error")
return;

pDef->checker = "UBSAN_WARNING";

// UBSAN may only use the base name for the file path in its key event.
// However, the top of the backtrace, if present, always contains the
// whole path to this file.
if (!keyEvt.fileName.empty() && keyEvt.fileName.front() == '/')
return;

using path = boost::filesystem::path;
const auto &keyFileName = path(keyEvt.fileName).filename();

for (const auto &evt : pDef->events) {
const auto &evtFileName = path(evt.fileName).filename();

// check that the basenames and line numbers match
if (&keyEvt != &evt
&& keyFileName == evtFileName
&& keyEvt.line == evt.line)
{
keyEvt.fileName = evt.fileName;
break;
}
}
}

void GccPostProcessor::Private::polishGccAnal(Defect *pDef) const
Expand Down
4 changes: 2 additions & 2 deletions tests/csgrep/0112-gcc-parser-ubsan-bt-stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Error: UBSAN_WARNING:
byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for type 'const uint32_t', which requires 4 byte alignment
/builddir/build/BUILD/rsync-3.2.3/byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for type 'const uint32_t', which requires 4 byte alignment
0x556e3e877805: note: pointer points here
# b5 21 00 00 6c 00 00 07 ff 65 a0 b8 03 05 2f 74 65 78 74 0e 70 d6 f0 d2 4d 97 21 a4 81 00 00 a0
# ^
Expand All @@ -24,7 +24,7 @@ byteorder.h:83:9: runtime error: load of misaligned address 0x556e3e877805 for t
/builddir/build/BUILD/rsync-3.2.3/rsync: note: _start() at 0x556e3dc0f324

Error: UBSAN_WARNING:
test.c:2:23: runtime error: load of null pointer of type 'char'
/home/lukas/csdiff/tests/csgrep/test.c:2:23: runtime error: load of null pointer of type 'char'
/home/lukas/csdiff/tests/csgrep/test.c:2: note: main() at 0x401147
/lib64/libc.so.6: note: __libc_start_call_main() at 0x7f7851249b49
/lib64/libc.so.6: note: __libc_start_main_alias_2() at 0x7f7851249c0a
Expand Down

0 comments on commit be20b75

Please sign in to comment.