Skip to content

Commit b62921c

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:11b9ec5f240e into amd-gfx:121ddb486b15
Local branch amd-gfx 121ddb4 Merged main:a5a008ff4f59 into amd-gfx:40bdeb2e3eb6 Remote branch main 11b9ec5 [sanitizer] Add more internal symbolizer tests
2 parents 121ddb4 + 11b9ec5 commit b62921c

File tree

40 files changed

+885
-235
lines changed

40 files changed

+885
-235
lines changed

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,8 @@ Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
31003100
llvm::opt::ArgStringList &CC1Args) const {
31013101
const Driver &D = getDriver();
31023102
std::string SysRoot = computeSysRoot();
3103+
if (SysRoot.empty())
3104+
SysRoot = llvm::sys::path::get_separator();
31033105

31043106
auto AddIncludePath = [&](StringRef Path, bool TargetDirRequired = false) {
31053107
std::string Version = detectLibcxxVersion(Path);

clang/unittests/Driver/ToolChainTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
#include "clang/Driver/Driver.h"
2020
#include "clang/Frontend/CompilerInstance.h"
2121
#include "llvm/ADT/ArrayRef.h"
22+
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/MC/TargetRegistry.h"
2324
#include "llvm/Support/TargetSelect.h"
2425
#include "llvm/Support/VirtualFileSystem.h"
2526
#include "llvm/Support/raw_ostream.h"
27+
#include "gmock/gmock.h"
2628
#include "gtest/gtest.h"
2729
#include <memory>
2830

@@ -316,6 +318,52 @@ TEST(ToolChainTest, VFSSolarisMultiGCCInstallation) {
316318
}
317319
}
318320

321+
MATCHER_P(jobHasArgs, Substr, "") {
322+
const driver::Command &C = arg;
323+
std::string Args = "";
324+
llvm::ListSeparator Sep(" ");
325+
for (const char *Arg : C.getArguments()) {
326+
Args += Sep;
327+
Args += Arg;
328+
}
329+
if (is_style_windows(llvm::sys::path::Style::native))
330+
std::replace(Args.begin(), Args.end(), '\\', '/');
331+
if (llvm::StringRef(Args).contains(Substr))
332+
return true;
333+
*result_listener << "whose args are '" << Args << "'";
334+
return false;
335+
}
336+
337+
TEST(ToolChainTest, VFSGnuLibcxxPathNoSysroot) {
338+
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
339+
340+
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
341+
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
342+
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
343+
new llvm::vfs::InMemoryFileSystem);
344+
345+
const char *EmptyFiles[] = {
346+
"foo.cpp",
347+
"/bin/clang",
348+
"/usr/include/c++/v1/cstdio",
349+
};
350+
351+
for (const char *Path : EmptyFiles)
352+
InMemoryFileSystem->addFile(Path, 0,
353+
llvm::MemoryBuffer::getMemBuffer("\n"));
354+
355+
{
356+
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
357+
Driver TheDriver("/bin/clang", "x86_64-unknown-linux-gnu", Diags,
358+
"clang LLVM compiler", InMemoryFileSystem);
359+
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(
360+
{"/bin/clang", "-fsyntax-only", "-stdlib=libc++", "foo.cpp"}));
361+
ASSERT_TRUE(C);
362+
EXPECT_THAT(C->getJobs(), testing::ElementsAre(jobHasArgs(
363+
"-internal-isystem /usr/include/c++/v1")));
364+
}
365+
}
366+
319367
TEST(ToolChainTest, DefaultDriverMode) {
320368
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
321369

compiler-rt/lib/hwasan/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ function(add_hwasan_runtimes arch use_aliases)
164164
RTSanitizerCommonLibc
165165
RTSanitizerCommonCoverage
166166
RTSanitizerCommonSymbolizer
167-
# FIXME: disable tagging when in symbolizer.
168-
# RTSanitizerCommonSymbolizerInternal
167+
RTSanitizerCommonSymbolizerInternal
169168
RTLSanCommon
170169
RTUbsan
171170
CFLAGS ${hwasan_rtl_flags}
@@ -203,8 +202,7 @@ function(add_hwasan_runtimes arch use_aliases)
203202
RTSanitizerCommonLibc
204203
RTSanitizerCommonCoverage
205204
RTSanitizerCommonSymbolizer
206-
# FIXME: disable tagging when in symbolizer.
207-
# RTSanitizerCommonSymbolizerInternal
205+
RTSanitizerCommonSymbolizerInternal
208206
RTLSanCommon
209207
RTUbsan
210208
RTUbsan_cxx

compiler-rt/lib/hwasan/hwasan_allocator.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,28 +234,23 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
234234
}
235235

236236
void *user_ptr = allocated;
237-
// Tagging can only be skipped when both tag_in_malloc and tag_in_free are
238-
// false. When tag_in_malloc = false and tag_in_free = true malloc needs to
239-
// retag to 0.
240237
if (InTaggableRegion(reinterpret_cast<uptr>(user_ptr)) &&
241-
(flags()->tag_in_malloc || flags()->tag_in_free) &&
242-
atomic_load_relaxed(&hwasan_allocator_tagging_enabled)) {
243-
if (flags()->tag_in_malloc && malloc_bisect(stack, orig_size)) {
244-
tag_t tag = t ? t->GenerateRandomTag() : kFallbackAllocTag;
245-
uptr tag_size = orig_size ? orig_size : 1;
246-
uptr full_granule_size = RoundDownTo(tag_size, kShadowAlignment);
247-
user_ptr =
248-
(void *)TagMemoryAligned((uptr)user_ptr, full_granule_size, tag);
249-
if (full_granule_size != tag_size) {
250-
u8 *short_granule =
251-
reinterpret_cast<u8 *>(allocated) + full_granule_size;
252-
TagMemoryAligned((uptr)short_granule, kShadowAlignment,
253-
tag_size % kShadowAlignment);
254-
short_granule[kShadowAlignment - 1] = tag;
255-
}
256-
} else {
257-
user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, size, 0);
238+
atomic_load_relaxed(&hwasan_allocator_tagging_enabled) &&
239+
flags()->tag_in_malloc && malloc_bisect(stack, orig_size)) {
240+
tag_t tag = t ? t->GenerateRandomTag() : kFallbackAllocTag;
241+
uptr tag_size = orig_size ? orig_size : 1;
242+
uptr full_granule_size = RoundDownTo(tag_size, kShadowAlignment);
243+
user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, full_granule_size, tag);
244+
if (full_granule_size != tag_size) {
245+
u8 *short_granule = reinterpret_cast<u8 *>(allocated) + full_granule_size;
246+
TagMemoryAligned((uptr)short_granule, kShadowAlignment,
247+
tag_size % kShadowAlignment);
248+
short_granule[kShadowAlignment - 1] = tag;
258249
}
250+
} else {
251+
// Tagging can not be completely skipped. If it's disabled, we need to tag
252+
// with zeros.
253+
user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, size, 0);
259254
}
260255

261256
Metadata *meta =
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Test that disabling/enabling tagging does not trigger false reports on
2+
// allocations happened in a different state.
3+
4+
// RUN: %clang_hwasan -O1 %s -o %t && %run %t 2>&1
5+
6+
#include <assert.h>
7+
#include <sanitizer/hwasan_interface.h>
8+
#include <stdlib.h>
9+
10+
enum {
11+
COUNT = 5,
12+
SZ = 10,
13+
};
14+
void *x[COUNT];
15+
16+
int main() {
17+
__hwasan_enable_allocator_tagging();
18+
for (unsigned i = 0; i < COUNT; ++i) {
19+
x[i] = malloc(SZ);
20+
assert(__hwasan_test_shadow(x[i], SZ) == -1);
21+
}
22+
for (unsigned i = 0; i < COUNT; ++i)
23+
free(x[i]);
24+
25+
__hwasan_disable_allocator_tagging();
26+
for (unsigned i = 0; i < COUNT; ++i) {
27+
x[i] = malloc(SZ);
28+
assert(__hwasan_tag_pointer(x[i], 0) == x[i]);
29+
assert(__hwasan_test_shadow(x[i], SZ) == -1);
30+
}
31+
for (unsigned i = 0; i < COUNT; ++i)
32+
free(x[i]);
33+
34+
__hwasan_enable_allocator_tagging();
35+
for (unsigned i = 0; i < COUNT; ++i) {
36+
x[i] = malloc(SZ);
37+
assert(__hwasan_test_shadow(x[i], SZ) == -1);
38+
}
39+
for (unsigned i = 0; i < COUNT; ++i)
40+
free(x[i]);
41+
return 0;
42+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// RUN: %clangxx %s -o %t && %run %t
2+
3+
// REQUIRES: internal_symbolizer
4+
5+
#include <assert.h>
6+
#include <dlfcn.h>
7+
#include <link.h>
8+
#include <sanitizer/msan_interface.h>
9+
#include <string.h>
10+
11+
#include <string>
12+
#include <vector>
13+
14+
extern "C" {
15+
bool __sanitizer_symbolize_code(const char *ModuleName, uint64_t ModuleOffset,
16+
char *Buffer, int MaxLength,
17+
bool SymbolizeInlineFrames);
18+
bool __sanitizer_symbolize_data(const char *ModuleName, uint64_t ModuleOffset,
19+
char *Buffer, int MaxLength);
20+
void __sanitizer_print_stack_trace();
21+
bool __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
22+
int MaxLength);
23+
}
24+
25+
struct ScopedInSymbolizer {
26+
#if defined(__has_feature)
27+
# if __has_feature(memory_sanitizer)
28+
ScopedInSymbolizer() { __msan_scoped_disable_interceptor_checks(); }
29+
~ScopedInSymbolizer() { __msan_scoped_enable_interceptor_checks(); }
30+
# endif
31+
#endif
32+
};
33+
34+
struct FrameInfo {
35+
int line;
36+
std::string file;
37+
std::string function;
38+
void *address;
39+
};
40+
41+
__attribute__((noinline)) void *GetPC() { return __builtin_return_address(0); }
42+
43+
__attribute__((always_inline)) FrameInfo InlineFunction() {
44+
void *address = GetPC();
45+
return {__LINE__ - 1, __FILE__, __FUNCTION__,
46+
reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(address) - 1)};
47+
}
48+
49+
__attribute__((noinline)) FrameInfo NoInlineFunction() {
50+
void *address = GetPC();
51+
return {__LINE__ - 1, __FILE__, __FUNCTION__,
52+
reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(address) - 1)};
53+
}
54+
55+
template <int N> struct A {
56+
template <class T> FrameInfo RecursiveTemplateFunction(const T &t);
57+
};
58+
59+
template <int N>
60+
template <class T>
61+
__attribute__((noinline)) FrameInfo A<N>::RecursiveTemplateFunction(const T &) {
62+
std::vector<T> t;
63+
return A<N - 1>().RecursiveTemplateFunction(t);
64+
}
65+
66+
template <>
67+
template <class T>
68+
__attribute__((noinline)) FrameInfo A<0>::RecursiveTemplateFunction(const T &) {
69+
return NoInlineFunction();
70+
}
71+
72+
__attribute__((no_sanitize_memory)) std::pair<const char *, uint64_t>
73+
GetModuleAndOffset(void *address) {
74+
Dl_info di;
75+
link_map *lm = nullptr;
76+
assert(
77+
dladdr1(address, &di, reinterpret_cast<void **>(&lm), RTLD_DL_LINKMAP));
78+
return {di.dli_fname, reinterpret_cast<uint64_t>(address) - lm->l_addr};
79+
}
80+
81+
std::string Symbolize(FrameInfo frame) {
82+
auto modul_offset = GetModuleAndOffset(frame.address);
83+
char buffer[1024] = {};
84+
ScopedInSymbolizer in_symbolizer;
85+
__sanitizer_symbolize_code(modul_offset.first, modul_offset.second, buffer,
86+
std::size(buffer), true);
87+
return buffer;
88+
}
89+
90+
std::string GetRegex(const FrameInfo &frame) {
91+
return frame.function + "[^\\n]*\\n[^\\n]*" + frame.file + ":" +
92+
std::to_string(frame.line);
93+
}
94+
95+
void TestInline() {
96+
auto frame = InlineFunction();
97+
fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
98+
}
99+
100+
void TestNoInline() {
101+
auto frame = NoInlineFunction();
102+
fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
103+
}
104+
105+
void TestLongFunctionNames() {
106+
auto frame = A<10>().RecursiveTemplateFunction(0);
107+
fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
108+
}
109+
110+
std::string SymbolizeStaticVar() {
111+
static int var = 1;
112+
auto modul_offset = GetModuleAndOffset(&var);
113+
char buffer[1024] = {};
114+
ScopedInSymbolizer in_symbolizer;
115+
__sanitizer_symbolize_data(modul_offset.first, modul_offset.second, buffer,
116+
std::size(buffer));
117+
return buffer;
118+
}
119+
120+
void TestData() {
121+
fprintf(stderr, "%s: %s\n", __FUNCTION__, SymbolizeStaticVar().c_str());
122+
}
123+
124+
void TestDemangle() {
125+
char out[128];
126+
assert(!__sanitizer_symbolize_demangle("1A", out, sizeof(out)));
127+
128+
const char name[] = "_Z3fooi";
129+
for (int i = 1; i < sizeof(out); ++i) {
130+
memset(out, 1, sizeof(out));
131+
assert(__sanitizer_symbolize_demangle(name, out, i) == (i > 8));
132+
assert(i < 9 || 0 == strncmp(out, "foo(int)", i - 1));
133+
}
134+
}
135+
136+
int main() {
137+
TestInline();
138+
TestNoInline();
139+
TestLongFunctionNames();
140+
TestData();
141+
TestDemangle();
142+
}

compiler-rt/test/sanitizer_common/TestCases/demangle_internal.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

llvm/docs/Coroutines.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ Metadata
17791779
========
17801780

17811781
'``coro.outside.frame``' Metadata
1782-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1782+
---------------------------------
17831783

17841784
``coro.outside.frame`` metadata may be attached to an alloca instruction to
17851785
to signify that it shouldn't be promoted to the coroutine frame, useful for

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 475679
19+
#define LLVM_MAIN_REVISION 475700
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ SizeOffsetType ObjectSizeOffsetVisitor::combineSizeOffset(SizeOffsetType LHS,
988988
return (getSizeWithOverflow(LHS).eq(getSizeWithOverflow(RHS))) ? LHS
989989
: unknown();
990990
case ObjectSizeOpts::Mode::ExactUnderlyingSizeAndOffset:
991-
return LHS == RHS && LHS.second.eq(RHS.second) ? LHS : unknown();
991+
return LHS == RHS ? LHS : unknown();
992992
}
993993
llvm_unreachable("missing an eval mode");
994994
}

llvm/lib/CodeGen/LLVMTargetMachine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static cl::opt<bool>
3737
EnableTrapUnreachable("trap-unreachable", cl::Hidden,
3838
cl::desc("Enable generating trap for unreachable"));
3939

40+
static cl::opt<bool> EnableNoTrapAfterNoreturn(
41+
"no-trap-after-noreturn", cl::Hidden,
42+
cl::desc("Do not emit a trap instruction for 'unreachable' IR instructions "
43+
"after noreturn calls, even if --trap-unreachable is set."));
44+
4045
void LLVMTargetMachine::initAsmInfo() {
4146
MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
4247
assert(MRI && "Unable to create reg info");
@@ -95,6 +100,8 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
95100

96101
if (EnableTrapUnreachable)
97102
this->Options.TrapUnreachable = true;
103+
if (EnableNoTrapAfterNoreturn)
104+
this->Options.NoTrapAfterNoreturn = true;
98105
}
99106

100107
TargetTransformInfo

0 commit comments

Comments
 (0)