Skip to content

[LLVM] Fix an ASAN error in lookupLLVMIntrinsicByName #147444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions llvm/include/llvm/ADT/StringTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ class StringTable {
"Last byte must be a null byte.");
}

// Get a string from the table starting with the provided offset. The returned
// `StringRef` is in fact null terminated, and so can be converted safely to a
// C-string if necessary for a system API.
constexpr StringRef operator[](Offset O) const {
// Returns the raw C string from the table starting with the provided offset.
// The returned string is null terminated.
constexpr const char *getCString(Offset O) const {
assert(O.value() < Table.size() && "Out of bounds offset!");
return Table.data() + O.value();
}

// Get a string from the table starting with the provided offset. The returned
// `StringRef` is in fact null terminated, and so can be converted safely to a
// C-string if necessary for a system API.
constexpr StringRef operator[](Offset O) const { return getCString(O); }

/// Returns the byte size of the table.
constexpr size_t size() const { return Table.size(); }

Expand Down
24 changes: 12 additions & 12 deletions llvm/lib/IR/Intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,20 +660,20 @@ static int lookupLLVMIntrinsicByName(ArrayRef<unsigned> NameOffsetTable,
// `equal_range` requires the comparison to work with either side being an
// offset or the value. Detect which kind each side is to set up the
// compared strings.
StringRef LHSStr;
if constexpr (std::is_integral_v<decltype(LHS)>) {
LHSStr = IntrinsicNameTable[LHS];
} else {
const char *LHSStr;
if constexpr (std::is_integral_v<decltype(LHS)>)
LHSStr = IntrinsicNameTable.getCString(LHS);
else
LHSStr = LHS;
}
StringRef RHSStr;
if constexpr (std::is_integral_v<decltype(RHS)>) {
RHSStr = IntrinsicNameTable[RHS];
} else {

const char *RHSStr;
if constexpr (std::is_integral_v<decltype(RHS)>)
RHSStr = IntrinsicNameTable.getCString(RHS);
else
RHSStr = RHS;
}
return strncmp(LHSStr.data() + CmpStart, RHSStr.data() + CmpStart,
CmpEnd - CmpStart) < 0;

return strncmp(LHSStr + CmpStart, RHSStr + CmpStart, CmpEnd - CmpStart) <
0;
};
LastLow = Low;
std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
Expand Down
22 changes: 22 additions & 0 deletions llvm/unittests/IR/IntrinsicsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ TEST(IntrinsicNameLookup, Basic) {
EXPECT_EQ(memcpy_inline, lookupIntrinsicID("llvm.memcpy.inline.p0.p0.i1024"));
}

TEST(IntrinsicNameLookup, NonNullterminatedStringRef) {
using namespace Intrinsic;
// This reproduces an issue where lookupIntrinsicID() can access memory beyond
// the bounds of the passed in StringRef. For ASAN to catch this as an error,
// create a StringRef using heap allocated memory and make it not null
// terminated.

// ASAN will report a "AddressSanitizer: heap-buffer-overflow" error in
// `lookupLLVMIntrinsicByName` when LLVM is built with these options:
// -DCMAKE_BUILD_TYPE=Debug
// -DLLVM_USE_SANITIZER=Address
// -DLLVM_OPTIMIZE_SANITIZED_BUILDS=OFF

// Make an intrinsic name "llvm.memcpy.inline" on the heap.
std::string Name = "llvm.memcpy.inline";
assert(Name.size() == 18);
std::unique_ptr<char[]> Data = std::make_unique<char[]>(Name.size());
std::strncpy(Data.get(), Name.data(), Name.size());
StringRef S(Data.get(), Name.size());
EXPECT_EQ(memcpy_inline, lookupIntrinsicID(S));
}

// Tests to verify getIntrinsicForClangBuiltin.
TEST(IntrinsicNameLookup, ClangBuiltinLookup) {
using namespace Intrinsic;
Expand Down
Loading