[compiler-rt] [clang] [clang-tools-extra] clang::Linkage #71049 (PR #80064)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 30 13:36:49 PST 2024
https://github.com/harbandana created https://github.com/llvm/llvm-project/pull/80064
None
>From 3bebf2ae6ce886edd5a4f9e989ecef6cbfaddb64 Mon Sep 17 00:00:00 2001
From: Harbandana Kaur <harbandanakaur13 at chromium.org>
Date: Mon, 22 Jan 2024 20:02:12 +0530
Subject: [PATCH 1/5] =?UTF-8?q?problem=C2=A0#77737=20Fix=20a=20possible=20?=
=?UTF-8?q?null=20dereference?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
compiler-rt/lib/hwasan/hwasan_report.cpp | 2196 +++++++++++-----------
1 file changed, 1096 insertions(+), 1100 deletions(-)
diff --git a/compiler-rt/lib/hwasan/hwasan_report.cpp b/compiler-rt/lib/hwasan/hwasan_report.cpp
index 784cfb904aa27..6765888ce7a7a 100644
--- a/compiler-rt/lib/hwasan/hwasan_report.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_report.cpp
@@ -1,1100 +1,1096 @@
-//===-- hwasan_report.cpp -------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file is a part of HWAddressSanitizer.
-//
-// Error reporting.
-//===----------------------------------------------------------------------===//
-
-#include "hwasan_report.h"
-
-#include <dlfcn.h>
-
-#include "hwasan.h"
-#include "hwasan_allocator.h"
-#include "hwasan_globals.h"
-#include "hwasan_mapping.h"
-#include "hwasan_thread.h"
-#include "hwasan_thread_list.h"
-#include "sanitizer_common/sanitizer_allocator_internal.h"
-#include "sanitizer_common/sanitizer_array_ref.h"
-#include "sanitizer_common/sanitizer_common.h"
-#include "sanitizer_common/sanitizer_flags.h"
-#include "sanitizer_common/sanitizer_internal_defs.h"
-#include "sanitizer_common/sanitizer_mutex.h"
-#include "sanitizer_common/sanitizer_report_decorator.h"
-#include "sanitizer_common/sanitizer_stackdepot.h"
-#include "sanitizer_common/sanitizer_stacktrace_printer.h"
-#include "sanitizer_common/sanitizer_symbolizer.h"
-
-using namespace __sanitizer;
-
-namespace __hwasan {
-
-class ScopedReport {
- public:
- explicit ScopedReport(bool fatal) : fatal(fatal) {
- Lock lock(&error_message_lock_);
- error_message_ptr_ = fatal ? &error_message_ : nullptr;
- ++hwasan_report_count;
- }
-
- ~ScopedReport() {
- void (*report_cb)(const char *);
- {
- Lock lock(&error_message_lock_);
- report_cb = error_report_callback_;
- error_message_ptr_ = nullptr;
- }
- if (report_cb)
- report_cb(error_message_.data());
- if (fatal)
- SetAbortMessage(error_message_.data());
- if (common_flags()->print_module_map >= 2 ||
- (fatal && common_flags()->print_module_map))
- DumpProcessMap();
- if (fatal)
- Die();
- }
-
- static void MaybeAppendToErrorMessage(const char *msg) {
- Lock lock(&error_message_lock_);
- if (!error_message_ptr_)
- return;
- error_message_ptr_->Append(msg);
- }
-
- static void SetErrorReportCallback(void (*callback)(const char *)) {
- Lock lock(&error_message_lock_);
- error_report_callback_ = callback;
- }
-
- private:
- InternalScopedString error_message_;
- bool fatal;
-
- static Mutex error_message_lock_;
- static InternalScopedString *error_message_ptr_
- SANITIZER_GUARDED_BY(error_message_lock_);
- static void (*error_report_callback_)(const char *);
-};
-
-Mutex ScopedReport::error_message_lock_;
-InternalScopedString *ScopedReport::error_message_ptr_;
-void (*ScopedReport::error_report_callback_)(const char *);
-
-// If there is an active ScopedReport, append to its error message.
-void AppendToErrorMessageBuffer(const char *buffer) {
- ScopedReport::MaybeAppendToErrorMessage(buffer);
-}
-
-static StackTrace GetStackTraceFromId(u32 id) {
- CHECK(id);
- StackTrace res = StackDepotGet(id);
- CHECK(res.trace);
- return res;
-}
-
-static void MaybePrintAndroidHelpUrl() {
-#if SANITIZER_ANDROID
- Printf(
- "Learn more about HWASan reports: "
- "https://source.android.com/docs/security/test/memory-safety/"
- "hwasan-reports\n");
-#endif
-}
-
-namespace {
-// A RAII object that holds a copy of the current thread stack ring buffer.
-// The actual stack buffer may change while we are iterating over it (for
-// example, Printf may call syslog() which can itself be built with hwasan).
-class SavedStackAllocations {
- public:
- SavedStackAllocations() = default;
-
- explicit SavedStackAllocations(Thread *t) { CopyFrom(t); }
-
- void CopyFrom(Thread *t) {
- StackAllocationsRingBuffer *rb = t->stack_allocations();
- uptr size = rb->size() * sizeof(uptr);
- void *storage =
- MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");
- new (&rb_) StackAllocationsRingBuffer(*rb, storage);
- thread_id_ = t->unique_id();
- }
-
- ~SavedStackAllocations() {
- if (rb_) {
- StackAllocationsRingBuffer *rb = get();
- UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));
- }
- }
-
- const StackAllocationsRingBuffer *get() const {
- return (const StackAllocationsRingBuffer *)&rb_;
- }
-
- StackAllocationsRingBuffer *get() {
- return (StackAllocationsRingBuffer *)&rb_;
- }
-
- u32 thread_id() const { return thread_id_; }
-
- private:
- uptr rb_ = 0;
- u32 thread_id_;
-};
-
-class Decorator: public __sanitizer::SanitizerCommonDecorator {
- public:
- Decorator() : SanitizerCommonDecorator() { }
- const char *Access() { return Blue(); }
- const char *Allocation() const { return Magenta(); }
- const char *Origin() const { return Magenta(); }
- const char *Name() const { return Green(); }
- const char *Location() { return Green(); }
- const char *Thread() { return Green(); }
-};
-} // namespace
-
-static bool FindHeapAllocation(HeapAllocationsRingBuffer *rb, uptr tagged_addr,
- HeapAllocationRecord *har, uptr *ring_index,
- uptr *num_matching_addrs,
- uptr *num_matching_addrs_4b) {
- if (!rb) return false;
-
- *num_matching_addrs = 0;
- *num_matching_addrs_4b = 0;
- for (uptr i = 0, size = rb->size(); i < size; i++) {
- auto h = (*rb)[i];
- if (h.tagged_addr <= tagged_addr &&
- h.tagged_addr + h.requested_size > tagged_addr) {
- *har = h;
- *ring_index = i;
- return true;
- }
-
- // Measure the number of heap ring buffer entries that would have matched
- // if we had only one entry per address (e.g. if the ring buffer data was
- // stored at the address itself). This will help us tune the allocator
- // implementation for MTE.
- if (UntagAddr(h.tagged_addr) <= UntagAddr(tagged_addr) &&
- UntagAddr(h.tagged_addr) + h.requested_size > UntagAddr(tagged_addr)) {
- ++*num_matching_addrs;
- }
-
- // Measure the number of heap ring buffer entries that would have matched
- // if we only had 4 tag bits, which is the case for MTE.
- auto untag_4b = [](uptr p) {
- return p & ((1ULL << 60) - 1);
- };
- if (untag_4b(h.tagged_addr) <= untag_4b(tagged_addr) &&
- untag_4b(h.tagged_addr) + h.requested_size > untag_4b(tagged_addr)) {
- ++*num_matching_addrs_4b;
- }
- }
- return false;
-}
-
-static void PrintStackAllocations(const StackAllocationsRingBuffer *sa,
- tag_t addr_tag, uptr untagged_addr) {
- uptr frames = Min((uptr)flags()->stack_history_size, sa->size());
- bool found_local = false;
- InternalScopedString location;
- for (uptr i = 0; i < frames; i++) {
- const uptr *record_addr = &(*sa)[i];
- uptr record = *record_addr;
- if (!record)
- break;
- tag_t base_tag =
- reinterpret_cast<uptr>(record_addr) >> kRecordAddrBaseTagShift;
- uptr fp = (record >> kRecordFPShift) << kRecordFPLShift;
- uptr pc_mask = (1ULL << kRecordFPShift) - 1;
- uptr pc = record & pc_mask;
- FrameInfo frame;
- if (Symbolizer::GetOrInit()->SymbolizeFrame(pc, &frame)) {
- for (LocalInfo &local : frame.locals) {
- if (!local.has_frame_offset || !local.has_size || !local.has_tag_offset)
- continue;
- if (!(local.name && internal_strlen(local.name)) &&
- !(local.function_name && internal_strlen(local.name)) &&
- !(local.decl_file && internal_strlen(local.decl_file)))
- continue;
- tag_t obj_tag = base_tag ^ local.tag_offset;
- if (obj_tag != addr_tag)
- continue;
- // Guess top bits of local variable from the faulting address, because
- // we only store bits 4-19 of FP (bits 0-3 are guaranteed to be zero).
- uptr local_beg = (fp + local.frame_offset) |
- (untagged_addr & ~(uptr(kRecordFPModulus) - 1));
- uptr local_end = local_beg + local.size;
-
- if (!found_local) {
- Printf("\nPotentially referenced stack objects:\n");
- found_local = true;
- }
-
- uptr offset;
- const char *whence;
- const char *cause;
- if (local_beg <= untagged_addr && untagged_addr < local_end) {
- offset = untagged_addr - local_beg;
- whence = "inside";
- cause = "use-after-scope";
- } else if (untagged_addr >= local_end) {
- offset = untagged_addr - local_end;
- whence = "after";
- cause = "stack-buffer-overflow";
- } else {
- offset = local_beg - untagged_addr;
- whence = "before";
- cause = "stack-buffer-overflow";
- }
- Decorator d;
- Printf("%s", d.Error());
- Printf("Cause: %s\n", cause);
- Printf("%s", d.Default());
- Printf("%s", d.Location());
- StackTracePrinter::GetOrInit()->RenderSourceLocation(
- &location, local.decl_file, local.decl_line, /* column= */ 0,
- common_flags()->symbolize_vs_style,
- common_flags()->strip_path_prefix);
- Printf(
- "%p is located %zd bytes %s a %zd-byte local variable %s [%p,%p) "
- "in %s %s\n",
- untagged_addr, offset, whence, local_end - local_beg, local.name,
- local_beg, local_end, local.function_name, location.data());
- location.clear();
- Printf("%s\n", d.Default());
- }
- frame.Clear();
- }
- }
-
- if (found_local)
- return;
-
- // We didn't find any locals. Most likely we don't have symbols, so dump
- // the information that we have for offline analysis.
- InternalScopedString frame_desc;
- Printf("Previously allocated frames:\n");
- for (uptr i = 0; i < frames; i++) {
- const uptr *record_addr = &(*sa)[i];
- uptr record = *record_addr;
- if (!record)
- break;
- uptr pc_mask = (1ULL << 48) - 1;
- uptr pc = record & pc_mask;
- frame_desc.AppendF(" record_addr:0x%zx record:0x%zx",
- reinterpret_cast<uptr>(record_addr), record);
- SymbolizedStackHolder symbolized_stack(
- Symbolizer::GetOrInit()->SymbolizePC(pc));
- const SymbolizedStack *frame = symbolized_stack.get();
- if (frame) {
- StackTracePrinter::GetOrInit()->RenderFrame(
- &frame_desc, " %F %L", 0, frame->info.address, &frame->info,
- common_flags()->symbolize_vs_style,
- common_flags()->strip_path_prefix);
- }
- Printf("%s\n", frame_desc.data());
- frame_desc.clear();
- }
-}
-
-// Returns true if tag == *tag_ptr, reading tags from short granules if
-// necessary. This may return a false positive if tags 1-15 are used as a
-// regular tag rather than a short granule marker.
-static bool TagsEqual(tag_t tag, tag_t *tag_ptr) {
- if (tag == *tag_ptr)
- return true;
- if (*tag_ptr == 0 || *tag_ptr > kShadowAlignment - 1)
- return false;
- uptr mem = ShadowToMem(reinterpret_cast<uptr>(tag_ptr));
- tag_t inline_tag = *reinterpret_cast<tag_t *>(mem + kShadowAlignment - 1);
- return tag == inline_tag;
-}
-
-// HWASan globals store the size of the global in the descriptor. In cases where
-// we don't have a binary with symbols, we can't grab the size of the global
-// from the debug info - but we might be able to retrieve it from the
-// descriptor. Returns zero if the lookup failed.
-static uptr GetGlobalSizeFromDescriptor(uptr ptr) {
- // Find the ELF object that this global resides in.
- Dl_info info;
- if (dladdr(reinterpret_cast<void *>(ptr), &info) == 0)
- return 0;
- auto *ehdr = reinterpret_cast<const ElfW(Ehdr) *>(info.dli_fbase);
- auto *phdr_begin = reinterpret_cast<const ElfW(Phdr) *>(
- reinterpret_cast<const u8 *>(ehdr) + ehdr->e_phoff);
-
- // Get the load bias. This is normally the same as the dli_fbase address on
- // position-independent code, but can be different on non-PIE executables,
- // binaries using LLD's partitioning feature, or binaries compiled with a
- // linker script.
- ElfW(Addr) load_bias = 0;
- for (const auto &phdr :
- ArrayRef<const ElfW(Phdr)>(phdr_begin, phdr_begin + ehdr->e_phnum)) {
- if (phdr.p_type != PT_LOAD || phdr.p_offset != 0)
- continue;
- load_bias = reinterpret_cast<ElfW(Addr)>(ehdr) - phdr.p_vaddr;
- break;
- }
-
- // Walk all globals in this ELF object, looking for the one we're interested
- // in. Once we find it, we can stop iterating and return the size of the
- // global we're interested in.
- for (const hwasan_global &global :
- HwasanGlobalsFor(load_bias, phdr_begin, ehdr->e_phnum))
- if (global.addr() <= ptr && ptr < global.addr() + global.size())
- return global.size();
-
- return 0;
-}
-
-void ReportStats() {}
-
-constexpr uptr kDumpWidth = 16;
-constexpr uptr kShadowLines = 17;
-constexpr uptr kShadowDumpSize = kShadowLines * kDumpWidth;
-
-constexpr uptr kShortLines = 3;
-constexpr uptr kShortDumpSize = kShortLines * kDumpWidth;
-constexpr uptr kShortDumpOffset = (kShadowLines - kShortLines) / 2 * kDumpWidth;
-
-static uptr GetPrintTagStart(uptr addr) {
- addr = MemToShadow(addr);
- addr = RoundDownTo(addr, kDumpWidth);
- addr -= kDumpWidth * (kShadowLines / 2);
- return addr;
-}
-
-template <typename PrintTag>
-static void PrintTagInfoAroundAddr(uptr addr, uptr num_rows,
- InternalScopedString &s,
- PrintTag print_tag) {
- uptr center_row_beg = RoundDownTo(addr, kDumpWidth);
- uptr beg_row = center_row_beg - kDumpWidth * (num_rows / 2);
- uptr end_row = center_row_beg + kDumpWidth * ((num_rows + 1) / 2);
- for (uptr row = beg_row; row < end_row; row += kDumpWidth) {
- s.Append(row == center_row_beg ? "=>" : " ");
- s.AppendF("%p:", (void *)ShadowToMem(row));
- for (uptr i = 0; i < kDumpWidth; i++) {
- s.Append(row + i == addr ? "[" : " ");
- print_tag(s, row + i);
- s.Append(row + i == addr ? "]" : " ");
- }
- s.AppendF("\n");
- }
-}
-
-template <typename GetTag, typename GetShortTag>
-static void PrintTagsAroundAddr(uptr addr, GetTag get_tag,
- GetShortTag get_short_tag) {
- InternalScopedString s;
- addr = MemToShadow(addr);
- s.AppendF(
- "\nMemory tags around the buggy address (one tag corresponds to %zd "
- "bytes):\n",
- kShadowAlignment);
- PrintTagInfoAroundAddr(addr, kShadowLines, s,
- [&](InternalScopedString &s, uptr tag_addr) {
- tag_t tag = get_tag(tag_addr);
- s.AppendF("%02x", tag);
- });
-
- s.AppendF(
- "Tags for short granules around the buggy address (one tag corresponds "
- "to %zd bytes):\n",
- kShadowAlignment);
- PrintTagInfoAroundAddr(addr, kShortLines, s,
- [&](InternalScopedString &s, uptr tag_addr) {
- tag_t tag = get_tag(tag_addr);
- if (tag >= 1 && tag <= kShadowAlignment) {
- tag_t short_tag = get_short_tag(tag_addr);
- s.AppendF("%02x", short_tag);
- } else {
- s.AppendF("..");
- }
- });
- s.AppendF(
- "See "
- "https://clang.llvm.org/docs/"
- "HardwareAssistedAddressSanitizerDesign.html#short-granules for a "
- "description of short granule tags\n");
- Printf("%s", s.data());
-}
-
-static uptr GetTopPc(const StackTrace *stack) {
- return stack->size ? StackTrace::GetPreviousInstructionPc(stack->trace[0])
- : 0;
-}
-
-namespace {
-class BaseReport {
- public:
- BaseReport(StackTrace *stack, bool fatal, uptr tagged_addr, uptr access_size)
- : scoped_report(fatal),
- stack(stack),
- tagged_addr(tagged_addr),
- access_size(access_size),
- untagged_addr(UntagAddr(tagged_addr)),
- ptr_tag(GetTagFromPointer(tagged_addr)),
- mismatch_offset(FindMismatchOffset()),
- heap(CopyHeapChunk()),
- allocations(CopyAllocations()),
- candidate(FindBufferOverflowCandidate()),
- shadow(CopyShadow()) {}
-
- protected:
- struct OverflowCandidate {
- uptr untagged_addr = 0;
- bool after = false;
- bool is_close = false;
-
- struct {
- uptr begin = 0;
- uptr end = 0;
- u32 thread_id = 0;
- u32 stack_id = 0;
- bool is_allocated = false;
- } heap;
- };
-
- struct HeapAllocation {
- HeapAllocationRecord har = {};
- uptr ring_index = 0;
- uptr num_matching_addrs = 0;
- uptr num_matching_addrs_4b = 0;
- u32 free_thread_id = 0;
- };
-
- struct Allocations {
- ArrayRef<SavedStackAllocations> stack;
- ArrayRef<HeapAllocation> heap;
- };
-
- struct HeapChunk {
- uptr begin = 0;
- uptr size = 0;
- u32 stack_id = 0;
- bool from_small_heap = false;
- bool is_allocated = false;
- };
-
- struct Shadow {
- uptr addr = 0;
- tag_t tags[kShadowDumpSize] = {};
- tag_t short_tags[kShortDumpSize] = {};
- };
-
- sptr FindMismatchOffset() const;
- Shadow CopyShadow() const;
- tag_t GetTagCopy(uptr addr) const;
- tag_t GetShortTagCopy(uptr addr) const;
- HeapChunk CopyHeapChunk() const;
- Allocations CopyAllocations();
- OverflowCandidate FindBufferOverflowCandidate() const;
- void PrintAddressDescription() const;
- void PrintHeapOrGlobalCandidate() const;
- void PrintTags(uptr addr) const;
-
- SavedStackAllocations stack_allocations_storage[16];
- HeapAllocation heap_allocations_storage[256];
-
- const ScopedReport scoped_report;
- const StackTrace *stack = nullptr;
- const uptr tagged_addr = 0;
- const uptr access_size = 0;
- const uptr untagged_addr = 0;
- const tag_t ptr_tag = 0;
- const sptr mismatch_offset = 0;
-
- const HeapChunk heap;
- const Allocations allocations;
- const OverflowCandidate candidate;
-
- const Shadow shadow;
-};
-
-sptr BaseReport::FindMismatchOffset() const {
- if (!access_size)
- return 0;
- sptr offset =
- __hwasan_test_shadow(reinterpret_cast<void *>(tagged_addr), access_size);
- CHECK_GE(offset, 0);
- CHECK_LT(offset, static_cast<sptr>(access_size));
- tag_t *tag_ptr =
- reinterpret_cast<tag_t *>(MemToShadow(untagged_addr + offset));
- tag_t mem_tag = *tag_ptr;
-
- if (mem_tag && mem_tag < kShadowAlignment) {
- tag_t *granule_ptr = reinterpret_cast<tag_t *>((untagged_addr + offset) &
- ~(kShadowAlignment - 1));
- // If offset is 0, (untagged_addr + offset) is not aligned to granules.
- // This is the offset of the leftmost accessed byte within the bad granule.
- u8 in_granule_offset = (untagged_addr + offset) & (kShadowAlignment - 1);
- tag_t short_tag = granule_ptr[kShadowAlignment - 1];
- // The first mismatch was a short granule that matched the ptr_tag.
- if (short_tag == ptr_tag) {
- // If the access starts after the end of the short granule, then the first
- // bad byte is the first byte of the access; otherwise it is the first
- // byte past the end of the short granule
- if (mem_tag > in_granule_offset) {
- offset += mem_tag - in_granule_offset;
- }
- }
- }
- return offset;
-}
-
-BaseReport::Shadow BaseReport::CopyShadow() const {
- Shadow result;
- if (!MemIsApp(untagged_addr))
- return result;
-
- result.addr = GetPrintTagStart(untagged_addr + mismatch_offset);
- uptr tag_addr = result.addr;
- uptr short_end = kShortDumpOffset + ARRAY_SIZE(shadow.short_tags);
- for (uptr i = 0; i < ARRAY_SIZE(result.tags); ++i, ++tag_addr) {
- if (!MemIsShadow(tag_addr))
- continue;
- result.tags[i] = *reinterpret_cast<tag_t *>(tag_addr);
- if (i < kShortDumpOffset || i >= short_end)
- continue;
- uptr granule_addr = ShadowToMem(tag_addr);
- if (1 <= result.tags[i] && result.tags[i] <= kShadowAlignment &&
- IsAccessibleMemoryRange(granule_addr, kShadowAlignment)) {
- result.short_tags[i - kShortDumpOffset] =
- *reinterpret_cast<tag_t *>(granule_addr + kShadowAlignment - 1);
- }
- }
- return result;
-}
-
-tag_t BaseReport::GetTagCopy(uptr addr) const {
- CHECK_GE(addr, shadow.addr);
- uptr idx = addr - shadow.addr;
- CHECK_LT(idx, ARRAY_SIZE(shadow.tags));
- return shadow.tags[idx];
-}
-
-tag_t BaseReport::GetShortTagCopy(uptr addr) const {
- CHECK_GE(addr, shadow.addr + kShortDumpOffset);
- uptr idx = addr - shadow.addr - kShortDumpOffset;
- CHECK_LT(idx, ARRAY_SIZE(shadow.short_tags));
- return shadow.short_tags[idx];
-}
-
-BaseReport::HeapChunk BaseReport::CopyHeapChunk() const {
- HeapChunk result = {};
- if (MemIsShadow(untagged_addr))
- return result;
- HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
- result.begin = chunk.Beg();
- if (result.begin) {
- result.size = chunk.ActualSize();
- result.from_small_heap = chunk.FromSmallHeap();
- result.is_allocated = chunk.IsAllocated();
- result.stack_id = chunk.GetAllocStackId();
- }
- return result;
-}
-
-BaseReport::Allocations BaseReport::CopyAllocations() {
- if (MemIsShadow(untagged_addr))
- return {};
- uptr stack_allocations_count = 0;
- uptr heap_allocations_count = 0;
- hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
- if (stack_allocations_count < ARRAY_SIZE(stack_allocations_storage) &&
- t->AddrIsInStack(untagged_addr)) {
- stack_allocations_storage[stack_allocations_count++].CopyFrom(t);
- }
-
- if (heap_allocations_count < ARRAY_SIZE(heap_allocations_storage)) {
- // Scan all threads' ring buffers to find if it's a heap-use-after-free.
- HeapAllocationRecord har;
- uptr ring_index, num_matching_addrs, num_matching_addrs_4b;
- if (FindHeapAllocation(t->heap_allocations(), tagged_addr, &har,
- &ring_index, &num_matching_addrs,
- &num_matching_addrs_4b)) {
- auto &ha = heap_allocations_storage[heap_allocations_count++];
- ha.har = har;
- ha.ring_index = ring_index;
- ha.num_matching_addrs = num_matching_addrs;
- ha.num_matching_addrs_4b = num_matching_addrs_4b;
- ha.free_thread_id = t->unique_id();
- }
- }
- });
-
- return {{stack_allocations_storage, stack_allocations_count},
- {heap_allocations_storage, heap_allocations_count}};
-}
-
-BaseReport::OverflowCandidate BaseReport::FindBufferOverflowCandidate() const {
- OverflowCandidate result = {};
- if (MemIsShadow(untagged_addr))
- return result;
- // Check if this looks like a heap buffer overflow by scanning
- // the shadow left and right and looking for the first adjacent
- // object with a different memory tag. If that tag matches ptr_tag,
- // check the allocator if it has a live chunk there.
- tag_t *tag_ptr = reinterpret_cast<tag_t *>(MemToShadow(untagged_addr));
- tag_t *candidate_tag_ptr = nullptr, *left = tag_ptr, *right = tag_ptr;
- uptr candidate_distance = 0;
- for (; candidate_distance < 1000; candidate_distance++) {
- if (MemIsShadow(reinterpret_cast<uptr>(left)) && TagsEqual(ptr_tag, left)) {
- candidate_tag_ptr = left;
- break;
- }
- --left;
- if (MemIsShadow(reinterpret_cast<uptr>(right)) &&
- TagsEqual(ptr_tag, right)) {
- candidate_tag_ptr = right;
- break;
- }
- ++right;
- }
-
- constexpr auto kCloseCandidateDistance = 1;
- result.is_close = candidate_distance <= kCloseCandidateDistance;
-
- result.after = candidate_tag_ptr == left;
- result.untagged_addr = ShadowToMem(reinterpret_cast<uptr>(candidate_tag_ptr));
- HwasanChunkView chunk = FindHeapChunkByAddress(result.untagged_addr);
- if (chunk.IsAllocated()) {
- result.heap.is_allocated = true;
- result.heap.begin = chunk.Beg();
- result.heap.end = chunk.End();
- result.heap.thread_id = chunk.GetAllocThreadId();
- result.heap.stack_id = chunk.GetAllocStackId();
- }
- return result;
-}
-
-void BaseReport::PrintHeapOrGlobalCandidate() const {
- Decorator d;
- if (candidate.heap.is_allocated) {
- uptr offset;
- const char *whence;
- if (candidate.heap.begin <= untagged_addr &&
- untagged_addr < candidate.heap.end) {
- offset = untagged_addr - candidate.heap.begin;
- whence = "inside";
- } else if (candidate.after) {
- offset = untagged_addr - candidate.heap.end;
- whence = "after";
- } else {
- offset = candidate.heap.begin - untagged_addr;
- whence = "before";
- }
- Printf("%s", d.Error());
- Printf("\nCause: heap-buffer-overflow\n");
- Printf("%s", d.Default());
- Printf("%s", d.Location());
- Printf("%p is located %zd bytes %s a %zd-byte region [%p,%p)\n",
- untagged_addr, offset, whence,
- candidate.heap.end - candidate.heap.begin, candidate.heap.begin,
- candidate.heap.end);
- Printf("%s", d.Allocation());
- Printf("allocated by thread T%u here:\n", candidate.heap.thread_id);
- Printf("%s", d.Default());
- GetStackTraceFromId(candidate.heap.stack_id).Print();
- return;
- }
- // Check whether the address points into a loaded library. If so, this is
- // most likely a global variable.
- const char *module_name;
- uptr module_address;
- Symbolizer *sym = Symbolizer::GetOrInit();
- if (sym->GetModuleNameAndOffsetForPC(candidate.untagged_addr, &module_name,
- &module_address)) {
- Printf("%s", d.Error());
- Printf("\nCause: global-overflow\n");
- Printf("%s", d.Default());
- DataInfo info;
- Printf("%s", d.Location());
- if (sym->SymbolizeData(candidate.untagged_addr, &info) && info.start) {
- Printf(
- "%p is located %zd bytes %s a %zd-byte global variable "
- "%s [%p,%p) in %s\n",
- untagged_addr,
- candidate.after ? untagged_addr - (info.start + info.size)
- : info.start - untagged_addr,
- candidate.after ? "after" : "before", info.size, info.name,
- info.start, info.start + info.size, module_name);
- } else {
- uptr size = GetGlobalSizeFromDescriptor(candidate.untagged_addr);
- if (size == 0)
- // We couldn't find the size of the global from the descriptors.
- Printf(
- "%p is located %s a global variable in "
- "\n #0 0x%x (%s+0x%x)\n",
- untagged_addr, candidate.after ? "after" : "before",
- candidate.untagged_addr, module_name, module_address);
- else
- Printf(
- "%p is located %s a %zd-byte global variable in "
- "\n #0 0x%x (%s+0x%x)\n",
- untagged_addr, candidate.after ? "after" : "before", size,
- candidate.untagged_addr, module_name, module_address);
- }
- Printf("%s", d.Default());
- }
-}
-
-void BaseReport::PrintAddressDescription() const {
- Decorator d;
- int num_descriptions_printed = 0;
-
- if (MemIsShadow(untagged_addr)) {
- Printf("%s%p is HWAsan shadow memory.\n%s", d.Location(), untagged_addr,
- d.Default());
- return;
- }
-
- // Print some very basic information about the address, if it's a heap.
- if (heap.begin) {
- Printf(
- "%s[%p,%p) is a %s %s heap chunk; "
- "size: %zd offset: %zd\n%s",
- d.Location(), heap.begin, heap.begin + heap.size,
- heap.from_small_heap ? "small" : "large",
- heap.is_allocated ? "allocated" : "unallocated", heap.size,
- untagged_addr - heap.begin, d.Default());
- }
-
- auto announce_by_id = [](u32 thread_id) {
- hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
- if (thread_id == t->unique_id())
- t->Announce();
- });
- };
-
- // Check stack first. If the address is on the stack of a live thread, we
- // know it cannot be a heap / global overflow.
- for (const auto &sa : allocations.stack) {
- Printf("%s", d.Error());
- Printf("\nCause: stack tag-mismatch\n");
- Printf("%s", d.Location());
- Printf("Address %p is located in stack of thread T%zd\n", untagged_addr,
- sa.thread_id());
- Printf("%s", d.Default());
- announce_by_id(sa.thread_id());
- PrintStackAllocations(sa.get(), ptr_tag, untagged_addr);
- num_descriptions_printed++;
- }
-
- if (allocations.stack.empty() && candidate.untagged_addr &&
- candidate.is_close) {
- PrintHeapOrGlobalCandidate();
- num_descriptions_printed++;
- }
-
- for (const auto &ha : allocations.heap) {
- const HeapAllocationRecord har = ha.har;
-
- Printf("%s", d.Error());
- Printf("\nCause: use-after-free\n");
- Printf("%s", d.Location());
- Printf("%p is located %zd bytes inside a %zd-byte region [%p,%p)\n",
- untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),
- har.requested_size, UntagAddr(har.tagged_addr),
- UntagAddr(har.tagged_addr) + har.requested_size);
- Printf("%s", d.Allocation());
- Printf("freed by thread T%u here:\n", ha.free_thread_id);
- Printf("%s", d.Default());
- GetStackTraceFromId(har.free_context_id).Print();
-
- Printf("%s", d.Allocation());
- Printf("previously allocated by thread T%u here:\n", har.alloc_thread_id);
- Printf("%s", d.Default());
- GetStackTraceFromId(har.alloc_context_id).Print();
-
- // Print a developer note: the index of this heap object
- // in the thread's deallocation ring buffer.
- Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", ha.ring_index + 1,
- flags()->heap_history_size);
- Printf("hwasan_dev_note_num_matching_addrs: %zd\n", ha.num_matching_addrs);
- Printf("hwasan_dev_note_num_matching_addrs_4b: %zd\n",
- ha.num_matching_addrs_4b);
-
- announce_by_id(ha.free_thread_id);
- // TODO: announce_by_id(har.alloc_thread_id);
- num_descriptions_printed++;
- }
-
- if (candidate.untagged_addr && num_descriptions_printed == 0) {
- PrintHeapOrGlobalCandidate();
- num_descriptions_printed++;
- }
-
- // Print the remaining threads, as an extra information, 1 line per thread.
- if (flags()->print_live_threads_info) {
- Printf("\n");
- hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); });
- }
-
- if (!num_descriptions_printed)
- // We exhausted our possibilities. Bail out.
- Printf("HWAddressSanitizer can not describe address in more detail.\n");
- if (num_descriptions_printed > 1) {
- Printf(
- "There are %d potential causes, printed above in order "
- "of likeliness.\n",
- num_descriptions_printed);
- }
-}
-
-void BaseReport::PrintTags(uptr addr) const {
- if (shadow.addr) {
- PrintTagsAroundAddr(
- addr, [&](uptr addr) { return GetTagCopy(addr); },
- [&](uptr addr) { return GetShortTagCopy(addr); });
- }
-}
-
-class InvalidFreeReport : public BaseReport {
- public:
- InvalidFreeReport(StackTrace *stack, uptr tagged_addr)
- : BaseReport(stack, flags()->halt_on_error, tagged_addr, 0) {}
- ~InvalidFreeReport();
-
- private:
-};
-
-InvalidFreeReport::~InvalidFreeReport() {
- Decorator d;
- Printf("%s", d.Error());
- uptr pc = GetTopPc(stack);
- const char *bug_type = "invalid-free";
- const Thread *thread = GetCurrentThread();
- if (thread) {
- Report("ERROR: %s: %s on address %p at pc %p on thread T%zd\n",
- SanitizerToolName, bug_type, untagged_addr, pc, thread->unique_id());
- } else {
- Report("ERROR: %s: %s on address %p at pc %p on unknown thread\n",
- SanitizerToolName, bug_type, untagged_addr, pc);
- }
- Printf("%s", d.Access());
- if (shadow.addr) {
- Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag,
- GetTagCopy(MemToShadow(untagged_addr)));
- }
- Printf("%s", d.Default());
-
- stack->Print();
-
- PrintAddressDescription();
- PrintTags(untagged_addr);
- MaybePrintAndroidHelpUrl();
- ReportErrorSummary(bug_type, stack);
-}
-
-class TailOverwrittenReport : public BaseReport {
- public:
- explicit TailOverwrittenReport(StackTrace *stack, uptr tagged_addr,
- uptr orig_size, const u8 *expected)
- : BaseReport(stack, flags()->halt_on_error, tagged_addr, 0),
- orig_size(orig_size),
- tail_size(kShadowAlignment - (orig_size % kShadowAlignment)) {
- CHECK_GT(tail_size, 0U);
- CHECK_LT(tail_size, kShadowAlignment);
- internal_memcpy(tail_copy,
- reinterpret_cast<u8 *>(untagged_addr + orig_size),
- tail_size);
- internal_memcpy(actual_expected, expected, tail_size);
- // Short granule is stashed in the last byte of the magic string. To avoid
- // confusion, make the expected magic string contain the short granule tag.
- if (orig_size % kShadowAlignment != 0)
- actual_expected[tail_size - 1] = ptr_tag;
- }
- ~TailOverwrittenReport();
-
- private:
- const uptr orig_size = 0;
- const uptr tail_size = 0;
- u8 actual_expected[kShadowAlignment] = {};
- u8 tail_copy[kShadowAlignment] = {};
-};
-
-TailOverwrittenReport::~TailOverwrittenReport() {
- Decorator d;
- Printf("%s", d.Error());
- const char *bug_type = "allocation-tail-overwritten";
- Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName,
- bug_type, untagged_addr, untagged_addr + orig_size, orig_size);
- Printf("\n%s", d.Default());
- Printf(
- "Stack of invalid access unknown. Issue detected at deallocation "
- "time.\n");
- Printf("%s", d.Allocation());
- Printf("deallocated here:\n");
- Printf("%s", d.Default());
- stack->Print();
- if (heap.begin) {
- Printf("%s", d.Allocation());
- Printf("allocated here:\n");
- Printf("%s", d.Default());
- GetStackTraceFromId(heap.stack_id).Print();
- }
-
- InternalScopedString s;
- u8 *tail = tail_copy;
- s.AppendF("Tail contains: ");
- for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(".. ");
- for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", tail[i]);
- s.AppendF("\n");
- s.AppendF("Expected: ");
- for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(".. ");
- for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", actual_expected[i]);
- s.AppendF("\n");
- s.AppendF(" ");
- for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(" ");
- for (uptr i = 0; i < tail_size; i++)
- s.AppendF("%s ", actual_expected[i] != tail[i] ? "^^" : " ");
-
- s.AppendF(
- "\nThis error occurs when a buffer overflow overwrites memory\n"
- "after a heap object, but within the %zd-byte granule, e.g.\n"
- " char *x = new char[20];\n"
- " x[25] = 42;\n"
- "%s does not detect such bugs in uninstrumented code at the time of "
- "write,"
- "\nbut can detect them at the time of free/delete.\n"
- "To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0\n",
- kShadowAlignment, SanitizerToolName);
- Printf("%s", s.data());
- GetCurrentThread()->Announce();
- PrintTags(untagged_addr);
- MaybePrintAndroidHelpUrl();
- ReportErrorSummary(bug_type, stack);
-}
-
-class TagMismatchReport : public BaseReport {
- public:
- explicit TagMismatchReport(StackTrace *stack, uptr tagged_addr,
- uptr access_size, bool is_store, bool fatal,
- uptr *registers_frame)
- : BaseReport(stack, fatal, tagged_addr, access_size),
- is_store(is_store),
- registers_frame(registers_frame) {}
- ~TagMismatchReport();
-
- private:
- const bool is_store;
- const uptr *registers_frame;
-};
-
-TagMismatchReport::~TagMismatchReport() {
- Decorator d;
- // TODO: when possible, try to print heap-use-after-free, etc.
- const char *bug_type = "tag-mismatch";
- uptr pc = GetTopPc(stack);
- Printf("%s", d.Error());
- Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
- untagged_addr, pc);
-
- Thread *t = GetCurrentThread();
-
- tag_t mem_tag = GetTagCopy(MemToShadow(untagged_addr + mismatch_offset));
-
- Printf("%s", d.Access());
- if (mem_tag && mem_tag < kShadowAlignment) {
- tag_t short_tag =
- GetShortTagCopy(MemToShadow(untagged_addr + mismatch_offset));
- Printf(
- "%s of size %zu at %p tags: %02x/%02x(%02x) (ptr/mem) in thread T%zd\n",
- is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
- mem_tag, short_tag, t->unique_id());
- } else {
- Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n",
- is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
- mem_tag, t->unique_id());
- }
- if (mismatch_offset)
- Printf("Invalid access starting at offset %zu\n", mismatch_offset);
- Printf("%s", d.Default());
-
- stack->Print();
-
- PrintAddressDescription();
- t->Announce();
-
- PrintTags(untagged_addr + mismatch_offset);
-
- if (registers_frame)
- ReportRegisters(registers_frame, pc);
-
- MaybePrintAndroidHelpUrl();
- ReportErrorSummary(bug_type, stack);
-}
-} // namespace
-
-void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
- InvalidFreeReport R(stack, tagged_addr);
-}
-
-void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size,
- const u8 *expected) {
- TailOverwrittenReport R(stack, tagged_addr, orig_size, expected);
-}
-
-void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
- bool is_store, bool fatal, uptr *registers_frame) {
- TagMismatchReport R(stack, tagged_addr, access_size, is_store, fatal,
- registers_frame);
-}
-
-// See the frame breakdown defined in __hwasan_tag_mismatch (from
-// hwasan_tag_mismatch_{aarch64,riscv64}.S).
-void ReportRegisters(const uptr *frame, uptr pc) {
- Printf("\nRegisters where the failure occurred (pc %p):\n", pc);
-
- // We explicitly print a single line (4 registers/line) each iteration to
- // reduce the amount of logcat error messages printed. Each Printf() will
- // result in a new logcat line, irrespective of whether a newline is present,
- // and so we wish to reduce the number of Printf() calls we have to make.
-#if defined(__aarch64__)
- Printf(" x0 %016llx x1 %016llx x2 %016llx x3 %016llx\n",
- frame[0], frame[1], frame[2], frame[3]);
-#elif SANITIZER_RISCV64
- Printf(" sp %016llx x1 %016llx x2 %016llx x3 %016llx\n",
- reinterpret_cast<const u8 *>(frame) + 256, frame[1], frame[2],
- frame[3]);
-#endif
- Printf(" x4 %016llx x5 %016llx x6 %016llx x7 %016llx\n",
- frame[4], frame[5], frame[6], frame[7]);
- Printf(" x8 %016llx x9 %016llx x10 %016llx x11 %016llx\n",
- frame[8], frame[9], frame[10], frame[11]);
- Printf(" x12 %016llx x13 %016llx x14 %016llx x15 %016llx\n",
- frame[12], frame[13], frame[14], frame[15]);
- Printf(" x16 %016llx x17 %016llx x18 %016llx x19 %016llx\n",
- frame[16], frame[17], frame[18], frame[19]);
- Printf(" x20 %016llx x21 %016llx x22 %016llx x23 %016llx\n",
- frame[20], frame[21], frame[22], frame[23]);
- Printf(" x24 %016llx x25 %016llx x26 %016llx x27 %016llx\n",
- frame[24], frame[25], frame[26], frame[27]);
- // hwasan_check* reduces the stack pointer by 256, then __hwasan_tag_mismatch
- // passes it to this function.
-#if defined(__aarch64__)
- Printf(" x28 %016llx x29 %016llx x30 %016llx sp %016llx\n", frame[28],
- frame[29], frame[30], reinterpret_cast<const u8 *>(frame) + 256);
-#elif SANITIZER_RISCV64
- Printf(" x28 %016llx x29 %016llx x30 %016llx x31 %016llx\n", frame[28],
- frame[29], frame[30], frame[31]);
-#else
-#endif
-}
-
-} // namespace __hwasan
-
-void __hwasan_set_error_report_callback(void (*callback)(const char *)) {
- __hwasan::ScopedReport::SetErrorReportCallback(callback);
-}
+//===-- hwasan_report.cpp -------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of HWAddressSanitizer.
+//
+// Error reporting.
+//===----------------------------------------------------------------------===//
+
+#include "hwasan_report.h"
+#include <dlfcn.h>
+#include "hwasan.h"
+#include "hwasan_allocator.h"
+#include "hwasan_globals.h"
+#include "hwasan_mapping.h"
+#include "hwasan_thread.h"
+#include "hwasan_thread_list.h"
+#include "sanitizer_common/sanitizer_allocator_internal.h"
+#include "sanitizer_common/sanitizer_array_ref.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_mutex.h"
+#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_stackdepot.h"
+#include "sanitizer_common/sanitizer_stacktrace_printer.h"
+#include "sanitizer_common/sanitizer_symbolizer.h"
+
+using namespace __sanitizer;
+
+namespace __hwasan {
+
+class ScopedReport {
+ public:
+ explicit ScopedReport(bool fatal) : fatal(fatal) {
+ Lock lock(&error_message_lock_);
+ error_message_ptr_ = fatal ? &error_message_ : nullptr;
+ ++hwasan_report_count;
+ }
+
+ ~ScopedReport() {
+ void (*report_cb)(const char *);
+ {
+ Lock lock(&error_message_lock_);
+ report_cb = error_report_callback_;
+ error_message_ptr_ = nullptr;
+ }
+ if (report_cb)
+ report_cb(error_message_.data());
+ if (fatal)
+ SetAbortMessage(error_message_.data());
+ if (common_flags()->print_module_map >= 2 ||
+ (fatal && common_flags()->print_module_map))
+ DumpProcessMap();
+ if (fatal)
+ Die();
+ }
+
+ static void MaybeAppendToErrorMessage(const char *msg) {
+ Lock lock(&error_message_lock_);
+ if (!error_message_ptr_)
+ return;
+ error_message_ptr_->Append(msg);
+ }
+
+ static void SetErrorReportCallback(void (*callback)(const char *)) {
+ Lock lock(&error_message_lock_);
+ error_report_callback_ = callback;
+ }
+
+ private:
+ InternalScopedString error_message_;
+ bool fatal;
+
+ static Mutex error_message_lock_;
+ static InternalScopedString *error_message_ptr_
+ SANITIZER_GUARDED_BY(error_message_lock_);
+ static void (*error_report_callback_)(const char *);
+};
+
+Mutex ScopedReport::error_message_lock_;
+InternalScopedString *ScopedReport::error_message_ptr_;
+void (*ScopedReport::error_report_callback_)(const char *);
+
+// If there is an active ScopedReport, append to its error message.
+void AppendToErrorMessageBuffer(const char *buffer) {
+ ScopedReport::MaybeAppendToErrorMessage(buffer);
+}
+
+static StackTrace GetStackTraceFromId(u32 id) {
+ CHECK(id);
+ StackTrace res = StackDepotGet(id);
+ CHECK(res.trace);
+ return res;
+}
+
+static void MaybePrintAndroidHelpUrl() {
+#if SANITIZER_ANDROID
+ Printf(
+ "Learn more about HWASan reports: "
+ "https://source.android.com/docs/security/test/memory-safety/"
+ "hwasan-reports\n");
+#endif
+}
+
+namespace {
+// A RAII object that holds a copy of the current thread stack ring buffer.
+// The actual stack buffer may change while we are iterating over it (for
+// example, Printf may call syslog() which can itself be built with hwasan).
+class SavedStackAllocations {
+ public:
+ SavedStackAllocations() = default;
+
+ explicit SavedStackAllocations(Thread *t) { CopyFrom(t); }
+
+ void CopyFrom(Thread *t) {
+ StackAllocationsRingBuffer *rb = t->stack_allocations();
+ uptr size = rb->size() * sizeof(uptr);
+ void *storage =
+ MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");
+ new (&rb_) StackAllocationsRingBuffer(*rb, storage);
+ thread_id_ = t->unique_id();
+ }
+
+ ~SavedStackAllocations() {
+ if (rb_) {
+ StackAllocationsRingBuffer *rb = get();
+ UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));
+ }
+ }
+
+ const StackAllocationsRingBuffer *get() const {
+ return (const StackAllocationsRingBuffer *)&rb_;
+ }
+
+ StackAllocationsRingBuffer *get() {
+ return (StackAllocationsRingBuffer *)&rb_;
+ }
+
+ u32 thread_id() const { return thread_id_; }
+
+ private:
+ uptr rb_ = 0;
+ u32 thread_id_;
+};
+
+class Decorator: public __sanitizer::SanitizerCommonDecorator {
+ public:
+ Decorator() : SanitizerCommonDecorator() { }
+ const char *Access() { return Blue(); }
+ const char *Allocation() const { return Magenta(); }
+ const char *Origin() const { return Magenta(); }
+ const char *Name() const { return Green(); }
+ const char *Location() { return Green(); }
+ const char *Thread() { return Green(); }
+};
+} // namespace
+
+static bool FindHeapAllocation(HeapAllocationsRingBuffer *rb, uptr tagged_addr,
+ HeapAllocationRecord *har, uptr *ring_index,
+ uptr *num_matching_addrs,
+ uptr *num_matching_addrs_4b) {
+ if (!rb) return false;
+
+ *num_matching_addrs = 0;
+ *num_matching_addrs_4b = 0;
+ for (uptr i = 0, size = rb->size(); i < size; i++) {
+ auto h = (*rb)[i];
+ if (h.tagged_addr <= tagged_addr &&
+ h.tagged_addr + h.requested_size > tagged_addr) {
+ *har = h;
+ *ring_index = i;
+ return true;
+ }
+
+ // Measure the number of heap ring buffer entries that would have matched
+ // if we had only one entry per address (e.g. if the ring buffer data was
+ // stored at the address itself). This will help us tune the allocator
+ // implementation for MTE.
+ if (UntagAddr(h.tagged_addr) <= UntagAddr(tagged_addr) &&
+ UntagAddr(h.tagged_addr) + h.requested_size > UntagAddr(tagged_addr)) {
+ ++*num_matching_addrs;
+ }
+
+ // Measure the number of heap ring buffer entries that would have matched
+ // if we only had 4 tag bits, which is the case for MTE.
+ auto untag_4b = [](uptr p) {
+ return p & ((1ULL << 60) - 1);
+ };
+ if (untag_4b(h.tagged_addr) <= untag_4b(tagged_addr) &&
+ untag_4b(h.tagged_addr) + h.requested_size > untag_4b(tagged_addr)) {
+ ++*num_matching_addrs_4b;
+ }
+ }
+ return false;
+}
+
+static void PrintStackAllocations(const StackAllocationsRingBuffer *sa,
+ tag_t addr_tag, uptr untagged_addr) {
+ uptr frames = Min((uptr)flags()->stack_history_size, sa->size());
+ bool found_local = false;
+ InternalScopedString location;
+ for (uptr i = 0; i < frames; i++) {
+ const uptr *record_addr = &(*sa)[i];
+ uptr record = *record_addr;
+ if (!record)
+ break;
+ tag_t base_tag =
+ reinterpret_cast<uptr>(record_addr) >> kRecordAddrBaseTagShift;
+ uptr fp = (record >> kRecordFPShift) << kRecordFPLShift;
+ uptr pc_mask = (1ULL << kRecordFPShift) - 1;
+ uptr pc = record & pc_mask;
+ FrameInfo frame;
+ if (Symbolizer::GetOrInit()->SymbolizeFrame(pc, &frame)) {
+ for (LocalInfo &local : frame.locals) {
+ if (!local.has_frame_offset || !local.has_size || !local.has_tag_offset)
+ continue;
+ if (!(local.name && internal_strlen(local.name)))
+ continue;
+ tag_t obj_tag = base_tag ^ local.tag_offset;
+ if (obj_tag != addr_tag)
+ continue;
+ // Guess top bits of local variable from the faulting address, because
+ // we only store bits 4-19 of FP (bits 0-3 are guaranteed to be zero).
+ uptr local_beg = (fp + local.frame_offset) |
+ (untagged_addr & ~(uptr(kRecordFPModulus) - 1));
+ uptr local_end = local_beg + local.size;
+
+ if (!found_local) {
+ Printf("\nPotentially referenced stack objects:\n");
+ found_local = true;
+ }
+
+ uptr offset;
+ const char *whence;
+ const char *cause;
+ if (local_beg <= untagged_addr && untagged_addr < local_end) {
+ offset = untagged_addr - local_beg;
+ whence = "inside";
+ cause = "use-after-scope";
+ } else if (untagged_addr >= local_end) {
+ offset = untagged_addr - local_end;
+ whence = "after";
+ cause = "stack-buffer-overflow";
+ } else {
+ offset = local_beg - untagged_addr;
+ whence = "before";
+ cause = "stack-buffer-overflow";
+ }
+ Decorator d;
+ Printf("%s", d.Error());
+ Printf("Cause: %s\n", cause);
+ Printf("%s", d.Default());
+ Printf("%s", d.Location());
+ StackTracePrinter::GetOrInit()->RenderSourceLocation(
+ &location, local.decl_file, local.decl_line, /* column= */ 0,
+ common_flags()->symbolize_vs_style,
+ common_flags()->strip_path_prefix);
+ Printf(
+ "%p is located %zd bytes %s a %zd-byte local variable %s [%p,%p) "
+ "in %s %s\n",
+ untagged_addr, offset, whence, local_end - local_beg, local.name,
+ local_beg, local_end, local.function_name, location.data());
+ location.clear();
+ Printf("%s\n", d.Default());
+ }
+ frame.Clear();
+ }
+ }
+
+ if (found_local)
+ return;
+
+ // We didn't find any locals. Most likely we don't have symbols, so dump
+ // the information that we have for offline analysis.
+ InternalScopedString frame_desc;
+ Printf("Previously allocated frames:\n");
+ for (uptr i = 0; i < frames; i++) {
+ const uptr *record_addr = &(*sa)[i];
+ uptr record = *record_addr;
+ if (!record)
+ break;
+ uptr pc_mask = (1ULL << 48) - 1;
+ uptr pc = record & pc_mask;
+ frame_desc.AppendF(" record_addr:0x%zx record:0x%zx",
+ reinterpret_cast<uptr>(record_addr), record);
+ SymbolizedStackHolder symbolized_stack(
+ Symbolizer::GetOrInit()->SymbolizePC(pc));
+ const SymbolizedStack *frame = symbolized_stack.get();
+ if (frame) {
+ StackTracePrinter::GetOrInit()->RenderFrame(
+ &frame_desc, " %F %L", 0, frame->info.address, &frame->info,
+ common_flags()->symbolize_vs_style,
+ common_flags()->strip_path_prefix);
+ }
+ Printf("%s\n", frame_desc.data());
+ frame_desc.clear();
+ }
+}
+
+// Returns true if tag == *tag_ptr, reading tags from short granules if
+// necessary. This may return a false positive if tags 1-15 are used as a
+// regular tag rather than a short granule marker.
+static bool TagsEqual(tag_t tag, tag_t *tag_ptr) {
+ if (tag == *tag_ptr)
+ return true;
+ if (*tag_ptr == 0 || *tag_ptr > kShadowAlignment - 1)
+ return false;
+ uptr mem = ShadowToMem(reinterpret_cast<uptr>(tag_ptr));
+ tag_t inline_tag = *reinterpret_cast<tag_t *>(mem + kShadowAlignment - 1);
+ return tag == inline_tag;
+}
+
+// HWASan globals store the size of the global in the descriptor. In cases where
+// we don't have a binary with symbols, we can't grab the size of the global
+// from the debug info - but we might be able to retrieve it from the
+// descriptor. Returns zero if the lookup failed.
+static uptr GetGlobalSizeFromDescriptor(uptr ptr) {
+ // Find the ELF object that this global resides in.
+ Dl_info info;
+ if (dladdr(reinterpret_cast<void *>(ptr), &info) == 0)
+ return 0;
+ auto *ehdr = reinterpret_cast<const ElfW(Ehdr) *>(info.dli_fbase);
+ auto *phdr_begin = reinterpret_cast<const ElfW(Phdr) *>(
+ reinterpret_cast<const u8 *>(ehdr) + ehdr->e_phoff);
+
+ // Get the load bias. This is normally the same as the dli_fbase address on
+ // position-independent code, but can be different on non-PIE executables,
+ // binaries using LLD's partitioning feature, or binaries compiled with a
+ // linker script.
+ ElfW(Addr) load_bias = 0;
+ for (const auto &phdr :
+ ArrayRef<const ElfW(Phdr)>(phdr_begin, phdr_begin + ehdr->e_phnum)) {
+ if (phdr.p_type != PT_LOAD || phdr.p_offset != 0)
+ continue;
+ load_bias = reinterpret_cast<ElfW(Addr)>(ehdr) - phdr.p_vaddr;
+ break;
+ }
+
+ // Walk all globals in this ELF object, looking for the one we're interested
+ // in. Once we find it, we can stop iterating and return the size of the
+ // global we're interested in.
+ for (const hwasan_global &global :
+ HwasanGlobalsFor(load_bias, phdr_begin, ehdr->e_phnum))
+ if (global.addr() <= ptr && ptr < global.addr() + global.size())
+ return global.size();
+
+ return 0;
+}
+
+void ReportStats() {}
+
+constexpr uptr kDumpWidth = 16;
+constexpr uptr kShadowLines = 17;
+constexpr uptr kShadowDumpSize = kShadowLines * kDumpWidth;
+
+constexpr uptr kShortLines = 3;
+constexpr uptr kShortDumpSize = kShortLines * kDumpWidth;
+constexpr uptr kShortDumpOffset = (kShadowLines - kShortLines) / 2 * kDumpWidth;
+
+static uptr GetPrintTagStart(uptr addr) {
+ addr = MemToShadow(addr);
+ addr = RoundDownTo(addr, kDumpWidth);
+ addr -= kDumpWidth * (kShadowLines / 2);
+ return addr;
+}
+
+template <typename PrintTag>
+static void PrintTagInfoAroundAddr(uptr addr, uptr num_rows,
+ InternalScopedString &s,
+ PrintTag print_tag) {
+ uptr center_row_beg = RoundDownTo(addr, kDumpWidth);
+ uptr beg_row = center_row_beg - kDumpWidth * (num_rows / 2);
+ uptr end_row = center_row_beg + kDumpWidth * ((num_rows + 1) / 2);
+ for (uptr row = beg_row; row < end_row; row += kDumpWidth) {
+ s.Append(row == center_row_beg ? "=>" : " ");
+ s.AppendF("%p:", (void *)ShadowToMem(row));
+ for (uptr i = 0; i < kDumpWidth; i++) {
+ s.Append(row + i == addr ? "[" : " ");
+ print_tag(s, row + i);
+ s.Append(row + i == addr ? "]" : " ");
+ }
+ s.AppendF("\n");
+ }
+}
+
+template <typename GetTag, typename GetShortTag>
+static void PrintTagsAroundAddr(uptr addr, GetTag get_tag,
+ GetShortTag get_short_tag) {
+ InternalScopedString s;
+ addr = MemToShadow(addr);
+ s.AppendF(
+ "\nMemory tags around the buggy address (one tag corresponds to %zd "
+ "bytes):\n",
+ kShadowAlignment);
+ PrintTagInfoAroundAddr(addr, kShadowLines, s,
+ [&](InternalScopedString &s, uptr tag_addr) {
+ tag_t tag = get_tag(tag_addr);
+ s.AppendF("%02x", tag);
+ });
+
+ s.AppendF(
+ "Tags for short granules around the buggy address (one tag corresponds "
+ "to %zd bytes):\n",
+ kShadowAlignment);
+ PrintTagInfoAroundAddr(addr, kShortLines, s,
+ [&](InternalScopedString &s, uptr tag_addr) {
+ tag_t tag = get_tag(tag_addr);
+ if (tag >= 1 && tag <= kShadowAlignment) {
+ tag_t short_tag = get_short_tag(tag_addr);
+ s.AppendF("%02x", short_tag);
+ } else {
+ s.AppendF("..");
+ }
+ });
+ s.AppendF(
+ "See "
+ "https://clang.llvm.org/docs/"
+ "HardwareAssistedAddressSanitizerDesign.html#short-granules for a "
+ "description of short granule tags\n");
+ Printf("%s", s.data());
+}
+
+static uptr GetTopPc(const StackTrace *stack) {
+ return stack->size ? StackTrace::GetPreviousInstructionPc(stack->trace[0])
+ : 0;
+}
+
+namespace {
+class BaseReport {
+ public:
+ BaseReport(StackTrace *stack, bool fatal, uptr tagged_addr, uptr access_size)
+ : scoped_report(fatal),
+ stack(stack),
+ tagged_addr(tagged_addr),
+ access_size(access_size),
+ untagged_addr(UntagAddr(tagged_addr)),
+ ptr_tag(GetTagFromPointer(tagged_addr)),
+ mismatch_offset(FindMismatchOffset()),
+ heap(CopyHeapChunk()),
+ allocations(CopyAllocations()),
+ candidate(FindBufferOverflowCandidate()),
+ shadow(CopyShadow()) {}
+
+ protected:
+ struct OverflowCandidate {
+ uptr untagged_addr = 0;
+ bool after = false;
+ bool is_close = false;
+
+ struct {
+ uptr begin = 0;
+ uptr end = 0;
+ u32 thread_id = 0;
+ u32 stack_id = 0;
+ bool is_allocated = false;
+ } heap;
+ };
+
+ struct HeapAllocation {
+ HeapAllocationRecord har = {};
+ uptr ring_index = 0;
+ uptr num_matching_addrs = 0;
+ uptr num_matching_addrs_4b = 0;
+ u32 free_thread_id = 0;
+ };
+
+ struct Allocations {
+ ArrayRef<SavedStackAllocations> stack;
+ ArrayRef<HeapAllocation> heap;
+ };
+
+ struct HeapChunk {
+ uptr begin = 0;
+ uptr size = 0;
+ u32 stack_id = 0;
+ bool from_small_heap = false;
+ bool is_allocated = false;
+ };
+
+ struct Shadow {
+ uptr addr = 0;
+ tag_t tags[kShadowDumpSize] = {};
+ tag_t short_tags[kShortDumpSize] = {};
+ };
+
+ sptr FindMismatchOffset() const;
+ Shadow CopyShadow() const;
+ tag_t GetTagCopy(uptr addr) const;
+ tag_t GetShortTagCopy(uptr addr) const;
+ HeapChunk CopyHeapChunk() const;
+ Allocations CopyAllocations();
+ OverflowCandidate FindBufferOverflowCandidate() const;
+ void PrintAddressDescription() const;
+ void PrintHeapOrGlobalCandidate() const;
+ void PrintTags(uptr addr) const;
+
+ SavedStackAllocations stack_allocations_storage[16];
+ HeapAllocation heap_allocations_storage[256];
+
+ const ScopedReport scoped_report;
+ const StackTrace *stack = nullptr;
+ const uptr tagged_addr = 0;
+ const uptr access_size = 0;
+ const uptr untagged_addr = 0;
+ const tag_t ptr_tag = 0;
+ const sptr mismatch_offset = 0;
+
+ const HeapChunk heap;
+ const Allocations allocations;
+ const OverflowCandidate candidate;
+
+ const Shadow shadow;
+};
+
+sptr BaseReport::FindMismatchOffset() const {
+ if (!access_size)
+ return 0;
+ sptr offset =
+ __hwasan_test_shadow(reinterpret_cast<void *>(tagged_addr), access_size);
+ CHECK_GE(offset, 0);
+ CHECK_LT(offset, static_cast<sptr>(access_size));
+ tag_t *tag_ptr =
+ reinterpret_cast<tag_t *>(MemToShadow(untagged_addr + offset));
+ tag_t mem_tag = *tag_ptr;
+
+ if (mem_tag && mem_tag < kShadowAlignment) {
+ tag_t *granule_ptr = reinterpret_cast<tag_t *>((untagged_addr + offset) &
+ ~(kShadowAlignment - 1));
+ // If offset is 0, (untagged_addr + offset) is not aligned to granules.
+ // This is the offset of the leftmost accessed byte within the bad granule.
+ u8 in_granule_offset = (untagged_addr + offset) & (kShadowAlignment - 1);
+ tag_t short_tag = granule_ptr[kShadowAlignment - 1];
+ // The first mismatch was a short granule that matched the ptr_tag.
+ if (short_tag == ptr_tag) {
+ // If the access starts after the end of the short granule, then the first
+ // bad byte is the first byte of the access; otherwise it is the first
+ // byte past the end of the short granule
+ if (mem_tag > in_granule_offset) {
+ offset += mem_tag - in_granule_offset;
+ }
+ }
+ }
+ return offset;
+}
+
+BaseReport::Shadow BaseReport::CopyShadow() const {
+ Shadow result;
+ if (!MemIsApp(untagged_addr))
+ return result;
+
+ result.addr = GetPrintTagStart(untagged_addr + mismatch_offset);
+ uptr tag_addr = result.addr;
+ uptr short_end = kShortDumpOffset + ARRAY_SIZE(shadow.short_tags);
+ for (uptr i = 0; i < ARRAY_SIZE(result.tags); ++i, ++tag_addr) {
+ if (!MemIsShadow(tag_addr))
+ continue;
+ result.tags[i] = *reinterpret_cast<tag_t *>(tag_addr);
+ if (i < kShortDumpOffset || i >= short_end)
+ continue;
+ uptr granule_addr = ShadowToMem(tag_addr);
+ if (1 <= result.tags[i] && result.tags[i] <= kShadowAlignment &&
+ IsAccessibleMemoryRange(granule_addr, kShadowAlignment)) {
+ result.short_tags[i - kShortDumpOffset] =
+ *reinterpret_cast<tag_t *>(granule_addr + kShadowAlignment - 1);
+ }
+ }
+ return result;
+}
+
+tag_t BaseReport::GetTagCopy(uptr addr) const {
+ CHECK_GE(addr, shadow.addr);
+ uptr idx = addr - shadow.addr;
+ CHECK_LT(idx, ARRAY_SIZE(shadow.tags));
+ return shadow.tags[idx];
+}
+
+tag_t BaseReport::GetShortTagCopy(uptr addr) const {
+ CHECK_GE(addr, shadow.addr + kShortDumpOffset);
+ uptr idx = addr - shadow.addr - kShortDumpOffset;
+ CHECK_LT(idx, ARRAY_SIZE(shadow.short_tags));
+ return shadow.short_tags[idx];
+}
+
+BaseReport::HeapChunk BaseReport::CopyHeapChunk() const {
+ HeapChunk result = {};
+ if (MemIsShadow(untagged_addr))
+ return result;
+ HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
+ result.begin = chunk.Beg();
+ if (result.begin) {
+ result.size = chunk.ActualSize();
+ result.from_small_heap = chunk.FromSmallHeap();
+ result.is_allocated = chunk.IsAllocated();
+ result.stack_id = chunk.GetAllocStackId();
+ }
+ return result;
+}
+
+BaseReport::Allocations BaseReport::CopyAllocations() {
+ if (MemIsShadow(untagged_addr))
+ return {};
+ uptr stack_allocations_count = 0;
+ uptr heap_allocations_count = 0;
+ hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
+ if (stack_allocations_count < ARRAY_SIZE(stack_allocations_storage) &&
+ t->AddrIsInStack(untagged_addr)) {
+ stack_allocations_storage[stack_allocations_count++].CopyFrom(t);
+ }
+
+ if (heap_allocations_count < ARRAY_SIZE(heap_allocations_storage)) {
+ // Scan all threads' ring buffers to find if it's a heap-use-after-free.
+ HeapAllocationRecord har;
+ uptr ring_index, num_matching_addrs, num_matching_addrs_4b;
+ if (FindHeapAllocation(t->heap_allocations(), tagged_addr, &har,
+ &ring_index, &num_matching_addrs,
+ &num_matching_addrs_4b)) {
+ auto &ha = heap_allocations_storage[heap_allocations_count++];
+ ha.har = har;
+ ha.ring_index = ring_index;
+ ha.num_matching_addrs = num_matching_addrs;
+ ha.num_matching_addrs_4b = num_matching_addrs_4b;
+ ha.free_thread_id = t->unique_id();
+ }
+ }
+ });
+
+ return {{stack_allocations_storage, stack_allocations_count},
+ {heap_allocations_storage, heap_allocations_count}};
+}
+
+BaseReport::OverflowCandidate BaseReport::FindBufferOverflowCandidate() const {
+ OverflowCandidate result = {};
+ if (MemIsShadow(untagged_addr))
+ return result;
+ // Check if this looks like a heap buffer overflow by scanning
+ // the shadow left and right and looking for the first adjacent
+ // object with a different memory tag. If that tag matches ptr_tag,
+ // check the allocator if it has a live chunk there.
+ tag_t *tag_ptr = reinterpret_cast<tag_t *>(MemToShadow(untagged_addr));
+ tag_t *candidate_tag_ptr = nullptr, *left = tag_ptr, *right = tag_ptr;
+ uptr candidate_distance = 0;
+ for (; candidate_distance < 1000; candidate_distance++) {
+ if (MemIsShadow(reinterpret_cast<uptr>(left)) && TagsEqual(ptr_tag, left)) {
+ candidate_tag_ptr = left;
+ break;
+ }
+ --left;
+ if (MemIsShadow(reinterpret_cast<uptr>(right)) &&
+ TagsEqual(ptr_tag, right)) {
+ candidate_tag_ptr = right;
+ break;
+ }
+ ++right;
+ }
+
+ constexpr auto kCloseCandidateDistance = 1;
+ result.is_close = candidate_distance <= kCloseCandidateDistance;
+
+ result.after = candidate_tag_ptr == left;
+ result.untagged_addr = ShadowToMem(reinterpret_cast<uptr>(candidate_tag_ptr));
+ HwasanChunkView chunk = FindHeapChunkByAddress(result.untagged_addr);
+ if (chunk.IsAllocated()) {
+ result.heap.is_allocated = true;
+ result.heap.begin = chunk.Beg();
+ result.heap.end = chunk.End();
+ result.heap.thread_id = chunk.GetAllocThreadId();
+ result.heap.stack_id = chunk.GetAllocStackId();
+ }
+ return result;
+}
+
+void BaseReport::PrintHeapOrGlobalCandidate() const {
+ Decorator d;
+ if (candidate.heap.is_allocated) {
+ uptr offset;
+ const char *whence;
+ if (candidate.heap.begin <= untagged_addr &&
+ untagged_addr < candidate.heap.end) {
+ offset = untagged_addr - candidate.heap.begin;
+ whence = "inside";
+ } else if (candidate.after) {
+ offset = untagged_addr - candidate.heap.end;
+ whence = "after";
+ } else {
+ offset = candidate.heap.begin - untagged_addr;
+ whence = "before";
+ }
+ Printf("%s", d.Error());
+ Printf("\nCause: heap-buffer-overflow\n");
+ Printf("%s", d.Default());
+ Printf("%s", d.Location());
+ Printf("%p is located %zd bytes %s a %zd-byte region [%p,%p)\n",
+ untagged_addr, offset, whence,
+ candidate.heap.end - candidate.heap.begin, candidate.heap.begin,
+ candidate.heap.end);
+ Printf("%s", d.Allocation());
+ Printf("allocated by thread T%u here:\n", candidate.heap.thread_id);
+ Printf("%s", d.Default());
+ GetStackTraceFromId(candidate.heap.stack_id).Print();
+ return;
+ }
+ // Check whether the address points into a loaded library. If so, this is
+ // most likely a global variable.
+ const char *module_name;
+ uptr module_address;
+ Symbolizer *sym = Symbolizer::GetOrInit();
+ if (sym->GetModuleNameAndOffsetForPC(candidate.untagged_addr, &module_name,
+ &module_address)) {
+ Printf("%s", d.Error());
+ Printf("\nCause: global-overflow\n");
+ Printf("%s", d.Default());
+ DataInfo info;
+ Printf("%s", d.Location());
+ if (sym->SymbolizeData(candidate.untagged_addr, &info) && info.start) {
+ Printf(
+ "%p is located %zd bytes %s a %zd-byte global variable "
+ "%s [%p,%p) in %s\n",
+ untagged_addr,
+ candidate.after ? untagged_addr - (info.start + info.size)
+ : info.start - untagged_addr,
+ candidate.after ? "after" : "before", info.size, info.name,
+ info.start, info.start + info.size, module_name);
+ } else {
+ uptr size = GetGlobalSizeFromDescriptor(candidate.untagged_addr);
+ if (size == 0)
+ // We couldn't find the size of the global from the descriptors.
+ Printf(
+ "%p is located %s a global variable in "
+ "\n #0 0x%x (%s+0x%x)\n",
+ untagged_addr, candidate.after ? "after" : "before",
+ candidate.untagged_addr, module_name, module_address);
+ else
+ Printf(
+ "%p is located %s a %zd-byte global variable in "
+ "\n #0 0x%x (%s+0x%x)\n",
+ untagged_addr, candidate.after ? "after" : "before", size,
+ candidate.untagged_addr, module_name, module_address);
+ }
+ Printf("%s", d.Default());
+ }
+}
+
+void BaseReport::PrintAddressDescription() const {
+ Decorator d;
+ int num_descriptions_printed = 0;
+
+ if (MemIsShadow(untagged_addr)) {
+ Printf("%s%p is HWAsan shadow memory.\n%s", d.Location(), untagged_addr,
+ d.Default());
+ return;
+ }
+
+ // Print some very basic information about the address, if it's a heap.
+ if (heap.begin) {
+ Printf(
+ "%s[%p,%p) is a %s %s heap chunk; "
+ "size: %zd offset: %zd\n%s",
+ d.Location(), heap.begin, heap.begin + heap.size,
+ heap.from_small_heap ? "small" : "large",
+ heap.is_allocated ? "allocated" : "unallocated", heap.size,
+ untagged_addr - heap.begin, d.Default());
+ }
+
+ auto announce_by_id = [](u32 thread_id) {
+ hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
+ if (thread_id == t->unique_id())
+ t->Announce();
+ });
+ };
+
+ // Check stack first. If the address is on the stack of a live thread, we
+ // know it cannot be a heap / global overflow.
+ for (const auto &sa : allocations.stack) {
+ Printf("%s", d.Error());
+ Printf("\nCause: stack tag-mismatch\n");
+ Printf("%s", d.Location());
+ Printf("Address %p is located in stack of thread T%zd\n", untagged_addr,
+ sa.thread_id());
+ Printf("%s", d.Default());
+ announce_by_id(sa.thread_id());
+ PrintStackAllocations(sa.get(), ptr_tag, untagged_addr);
+ num_descriptions_printed++;
+ }
+
+ if (allocations.stack.empty() && candidate.untagged_addr &&
+ candidate.is_close) {
+ PrintHeapOrGlobalCandidate();
+ num_descriptions_printed++;
+ }
+
+ for (const auto &ha : allocations.heap) {
+ const HeapAllocationRecord har = ha.har;
+
+ Printf("%s", d.Error());
+ Printf("\nCause: use-after-free\n");
+ Printf("%s", d.Location());
+ Printf("%p is located %zd bytes inside a %zd-byte region [%p,%p)\n",
+ untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),
+ har.requested_size, UntagAddr(har.tagged_addr),
+ UntagAddr(har.tagged_addr) + har.requested_size);
+ Printf("%s", d.Allocation());
+ Printf("freed by thread T%u here:\n", ha.free_thread_id);
+ Printf("%s", d.Default());
+ GetStackTraceFromId(har.free_context_id).Print();
+
+ Printf("%s", d.Allocation());
+ Printf("previously allocated by thread T%u here:\n", har.alloc_thread_id);
+ Printf("%s", d.Default());
+ GetStackTraceFromId(har.alloc_context_id).Print();
+
+ // Print a developer note: the index of this heap object
+ // in the thread's deallocation ring buffer.
+ Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", ha.ring_index + 1,
+ flags()->heap_history_size);
+ Printf("hwasan_dev_note_num_matching_addrs: %zd\n", ha.num_matching_addrs);
+ Printf("hwasan_dev_note_num_matching_addrs_4b: %zd\n",
+ ha.num_matching_addrs_4b);
+
+ announce_by_id(ha.free_thread_id);
+ // TODO: announce_by_id(har.alloc_thread_id);
+ num_descriptions_printed++;
+ }
+
+ if (candidate.untagged_addr && num_descriptions_printed == 0) {
+ PrintHeapOrGlobalCandidate();
+ num_descriptions_printed++;
+ }
+
+ // Print the remaining threads, as an extra information, 1 line per thread.
+ if (flags()->print_live_threads_info) {
+ Printf("\n");
+ hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); });
+ }
+
+ if (!num_descriptions_printed)
+ // We exhausted our possibilities. Bail out.
+ Printf("HWAddressSanitizer can not describe address in more detail.\n");
+ if (num_descriptions_printed > 1) {
+ Printf(
+ "There are %d potential causes, printed above in order "
+ "of likeliness.\n",
+ num_descriptions_printed);
+ }
+}
+
+void BaseReport::PrintTags(uptr addr) const {
+ if (shadow.addr) {
+ PrintTagsAroundAddr(
+ addr, [&](uptr addr) { return GetTagCopy(addr); },
+ [&](uptr addr) { return GetShortTagCopy(addr); });
+ }
+}
+
+class InvalidFreeReport : public BaseReport {
+ public:
+ InvalidFreeReport(StackTrace *stack, uptr tagged_addr)
+ : BaseReport(stack, flags()->halt_on_error, tagged_addr, 0) {}
+ ~InvalidFreeReport();
+
+ private:
+};
+
+InvalidFreeReport::~InvalidFreeReport() {
+ Decorator d;
+ Printf("%s", d.Error());
+ uptr pc = GetTopPc(stack);
+ const char *bug_type = "invalid-free";
+ const Thread *thread = GetCurrentThread();
+ if (thread) {
+ Report("ERROR: %s: %s on address %p at pc %p on thread T%zd\n",
+ SanitizerToolName, bug_type, untagged_addr, pc, thread->unique_id());
+ } else {
+ Report("ERROR: %s: %s on address %p at pc %p on unknown thread\n",
+ SanitizerToolName, bug_type, untagged_addr, pc);
+ }
+ Printf("%s", d.Access());
+ if (shadow.addr) {
+ Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag,
+ GetTagCopy(MemToShadow(untagged_addr)));
+ }
+ Printf("%s", d.Default());
+
+ stack->Print();
+
+ PrintAddressDescription();
+ PrintTags(untagged_addr);
+ MaybePrintAndroidHelpUrl();
+ ReportErrorSummary(bug_type, stack);
+}
+
+class TailOverwrittenReport : public BaseReport {
+ public:
+ explicit TailOverwrittenReport(StackTrace *stack, uptr tagged_addr,
+ uptr orig_size, const u8 *expected)
+ : BaseReport(stack, flags()->halt_on_error, tagged_addr, 0),
+ orig_size(orig_size),
+ tail_size(kShadowAlignment - (orig_size % kShadowAlignment)) {
+ CHECK_GT(tail_size, 0U);
+ CHECK_LT(tail_size, kShadowAlignment);
+ internal_memcpy(tail_copy,
+ reinterpret_cast<u8 *>(untagged_addr + orig_size),
+ tail_size);
+ internal_memcpy(actual_expected, expected, tail_size);
+ // Short granule is stashed in the last byte of the magic string. To avoid
+ // confusion, make the expected magic string contain the short granule tag.
+ if (orig_size % kShadowAlignment != 0)
+ actual_expected[tail_size - 1] = ptr_tag;
+ }
+ ~TailOverwrittenReport();
+
+ private:
+ const uptr orig_size = 0;
+ const uptr tail_size = 0;
+ u8 actual_expected[kShadowAlignment] = {};
+ u8 tail_copy[kShadowAlignment] = {};
+};
+
+TailOverwrittenReport::~TailOverwrittenReport() {
+ Decorator d;
+ Printf("%s", d.Error());
+ const char *bug_type = "allocation-tail-overwritten";
+ Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName,
+ bug_type, untagged_addr, untagged_addr + orig_size, orig_size);
+ Printf("\n%s", d.Default());
+ Printf(
+ "Stack of invalid access unknown. Issue detected at deallocation "
+ "time.\n");
+ Printf("%s", d.Allocation());
+ Printf("deallocated here:\n");
+ Printf("%s", d.Default());
+ stack->Print();
+ if (heap.begin) {
+ Printf("%s", d.Allocation());
+ Printf("allocated here:\n");
+ Printf("%s", d.Default());
+ GetStackTraceFromId(heap.stack_id).Print();
+ }
+
+ InternalScopedString s;
+ u8 *tail = tail_copy;
+ s.AppendF("Tail contains: ");
+ for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(".. ");
+ for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", tail[i]);
+ s.AppendF("\n");
+ s.AppendF("Expected: ");
+ for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(".. ");
+ for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", actual_expected[i]);
+ s.AppendF("\n");
+ s.AppendF(" ");
+ for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.AppendF(" ");
+ for (uptr i = 0; i < tail_size; i++)
+ s.AppendF("%s ", actual_expected[i] != tail[i] ? "^^" : " ");
+
+ s.AppendF(
+ "\nThis error occurs when a buffer overflow overwrites memory\n"
+ "after a heap object, but within the %zd-byte granule, e.g.\n"
+ " char *x = new char[20];\n"
+ " x[25] = 42;\n"
+ "%s does not detect such bugs in uninstrumented code at the time of "
+ "write,"
+ "\nbut can detect them at the time of free/delete.\n"
+ "To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0\n",
+ kShadowAlignment, SanitizerToolName);
+ Printf("%s", s.data());
+ GetCurrentThread()->Announce();
+ PrintTags(untagged_addr);
+ MaybePrintAndroidHelpUrl();
+ ReportErrorSummary(bug_type, stack);
+}
+
+class TagMismatchReport : public BaseReport {
+ public:
+ explicit TagMismatchReport(StackTrace *stack, uptr tagged_addr,
+ uptr access_size, bool is_store, bool fatal,
+ uptr *registers_frame)
+ : BaseReport(stack, fatal, tagged_addr, access_size),
+ is_store(is_store),
+ registers_frame(registers_frame) {}
+ ~TagMismatchReport();
+
+ private:
+ const bool is_store;
+ const uptr *registers_frame;
+};
+
+TagMismatchReport::~TagMismatchReport() {
+ Decorator d;
+ // TODO: when possible, try to print heap-use-after-free, etc.
+ const char *bug_type = "tag-mismatch";
+ uptr pc = GetTopPc(stack);
+ Printf("%s", d.Error());
+ Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
+ untagged_addr, pc);
+
+ Thread *t = GetCurrentThread();
+
+ tag_t mem_tag = GetTagCopy(MemToShadow(untagged_addr + mismatch_offset));
+
+ Printf("%s", d.Access());
+ if (mem_tag && mem_tag < kShadowAlignment) {
+ tag_t short_tag =
+ GetShortTagCopy(MemToShadow(untagged_addr + mismatch_offset));
+ Printf(
+ "%s of size %zu at %p tags: %02x/%02x(%02x) (ptr/mem) in thread T%zd\n",
+ is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
+ mem_tag, short_tag, t->unique_id());
+ } else {
+ Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n",
+ is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
+ mem_tag, t->unique_id());
+ }
+ if (mismatch_offset)
+ Printf("Invalid access starting at offset %zu\n", mismatch_offset);
+ Printf("%s", d.Default());
+
+ stack->Print();
+
+ PrintAddressDescription();
+ t->Announce();
+
+ PrintTags(untagged_addr + mismatch_offset);
+
+ if (registers_frame)
+ ReportRegisters(registers_frame, pc);
+
+ MaybePrintAndroidHelpUrl();
+ ReportErrorSummary(bug_type, stack);
+}
+} // namespace
+
+void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
+ InvalidFreeReport R(stack, tagged_addr);
+}
+
+void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size,
+ const u8 *expected) {
+ TailOverwrittenReport R(stack, tagged_addr, orig_size, expected);
+}
+
+void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
+ bool is_store, bool fatal, uptr *registers_frame) {
+ TagMismatchReport R(stack, tagged_addr, access_size, is_store, fatal,
+ registers_frame);
+}
+
+// See the frame breakdown defined in __hwasan_tag_mismatch (from
+// hwasan_tag_mismatch_{aarch64,riscv64}.S).
+void ReportRegisters(const uptr *frame, uptr pc) {
+ Printf("\nRegisters where the failure occurred (pc %p):\n", pc);
+
+ // We explicitly print a single line (4 registers/line) each iteration to
+ // reduce the amount of logcat error messages printed. Each Printf() will
+ // result in a new logcat line, irrespective of whether a newline is present,
+ // and so we wish to reduce the number of Printf() calls we have to make.
+#if defined(__aarch64__)
+ Printf(" x0 %016llx x1 %016llx x2 %016llx x3 %016llx\n",
+ frame[0], frame[1], frame[2], frame[3]);
+#elif SANITIZER_RISCV64
+ Printf(" sp %016llx x1 %016llx x2 %016llx x3 %016llx\n",
+ reinterpret_cast<const u8 *>(frame) + 256, frame[1], frame[2],
+ frame[3]);
+#endif
+ Printf(" x4 %016llx x5 %016llx x6 %016llx x7 %016llx\n",
+ frame[4], frame[5], frame[6], frame[7]);
+ Printf(" x8 %016llx x9 %016llx x10 %016llx x11 %016llx\n",
+ frame[8], frame[9], frame[10], frame[11]);
+ Printf(" x12 %016llx x13 %016llx x14 %016llx x15 %016llx\n",
+ frame[12], frame[13], frame[14], frame[15]);
+ Printf(" x16 %016llx x17 %016llx x18 %016llx x19 %016llx\n",
+ frame[16], frame[17], frame[18], frame[19]);
+ Printf(" x20 %016llx x21 %016llx x22 %016llx x23 %016llx\n",
+ frame[20], frame[21], frame[22], frame[23]);
+ Printf(" x24 %016llx x25 %016llx x26 %016llx x27 %016llx\n",
+ frame[24], frame[25], frame[26], frame[27]);
+ // hwasan_check* reduces the stack pointer by 256, then __hwasan_tag_mismatch
+ // passes it to this function.
+#if defined(__aarch64__)
+ Printf(" x28 %016llx x29 %016llx x30 %016llx sp %016llx\n", frame[28],
+ frame[29], frame[30], reinterpret_cast<const u8 *>(frame) + 256);
+#elif SANITIZER_RISCV64
+ Printf(" x28 %016llx x29 %016llx x30 %016llx x31 %016llx\n", frame[28],
+ frame[29], frame[30], frame[31]);
+#else
+#endif
+}
+
+} // namespace __hwasan
+
+void __hwasan_set_error_report_callback(void (*callback)(const char *)) {
+ __hwasan::ScopedReport::SetErrorReportCallback(callback);
+}
>From ddef3b6d2cfad8b103b73695aebdf26bb49478be Mon Sep 17 00:00:00 2001
From: Harbandana Kaur <harbandanakaur13 at chromium.org>
Date: Wed, 24 Jan 2024 16:52:51 +0530
Subject: [PATCH 2/5] Add support for riscv64 #68735
---
.../cmake/Modules/AllSupportedArchDefs.cmake | 197 +++++++++---------
1 file changed, 100 insertions(+), 97 deletions(-)
diff --git a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
index 416777171d2ca..6eff048174246 100644
--- a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
+++ b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
@@ -1,97 +1,100 @@
-set(ARM64 aarch64)
-set(ARM32 arm armhf)
-set(HEXAGON hexagon)
-set(X86 i386)
-set(X86_64 x86_64)
-set(LOONGARCH64 loongarch64)
-set(MIPS32 mips mipsel)
-set(MIPS64 mips64 mips64el)
-set(PPC32 powerpc powerpcspe)
-set(PPC64 powerpc64 powerpc64le)
-set(RISCV32 riscv32)
-set(RISCV64 riscv64)
-set(S390X s390x)
-set(SPARC sparc)
-set(SPARCV9 sparcv9)
-set(WASM32 wasm32)
-set(WASM64 wasm64)
-set(VE ve)
-
-if(APPLE)
- set(ARM64 arm64)
- set(ARM32 armv7 armv7s armv7k)
- set(X86_64 x86_64 x86_64h)
-endif()
-
-set(ALL_SANITIZER_COMMON_SUPPORTED_ARCH ${X86} ${X86_64} ${PPC64} ${RISCV64}
- ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9}
- ${HEXAGON} ${LOONGARCH64})
-set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
- ${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
- ${LOONGARCH64})
-set(ALL_ASAN_ABI_SUPPORTED_ARCH ${X86_64} ${ARM64})
-set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${LOONGARCH64})
-
-if(ANDROID)
- set(OS_NAME "Android")
-else()
- set(OS_NAME "${CMAKE_SYSTEM_NAME}")
-endif()
-
-if(OS_NAME MATCHES "Linux")
- set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${S390X}
- ${RISCV64} ${LOONGARCH64})
-elseif (OS_NAME MATCHES "Windows")
- set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64})
-elseif(OS_NAME MATCHES "Android")
- set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64})
-elseif(OS_NAME MATCHES "Fuchsia")
- set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
-else()
- set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64})
-endif()
-
-set(ALL_GWP_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64})
-if(APPLE)
- set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64})
-else()
- set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64} ${ARM32}
- ${PPC64} ${S390X} ${RISCV64} ${HEXAGON} ${LOONGARCH64})
-endif()
-set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X}
- ${LOONGARCH64})
-set(ALL_HWASAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
-set(ALL_MEMPROF_SUPPORTED_ARCH ${X86_64})
-set(ALL_PROFILE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32} ${PPC64}
- ${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
- ${RISCV32} ${RISCV64} ${LOONGARCH64})
-set(ALL_TSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X}
- ${LOONGARCH64} ${RISCV64})
-set(ALL_UBSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
- ${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
- ${LOONGARCH64})
-set(ALL_SAFESTACK_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM64} ${MIPS32} ${MIPS64}
- ${HEXAGON} ${LOONGARCH64})
-set(ALL_CFI_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS64}
- ${HEXAGON} ${LOONGARCH64})
-set(ALL_SCUDO_STANDALONE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64}
- ${MIPS32} ${MIPS64} ${PPC64} ${HEXAGON} ${LOONGARCH64} ${RISCV64})
-if(APPLE)
-set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM64})
-else()
-set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64}
- powerpc64le ${HEXAGON} ${LOONGARCH64})
-endif()
-set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64})
-
-if (UNIX)
- if (OS_NAME MATCHES "Linux")
- set(ALL_ORC_SUPPORTED_ARCH ${X86_64} ${ARM64} ${ARM32} ${PPC64})
- else()
- set(ALL_ORC_SUPPORTED_ARCH ${X86_64} ${ARM64} ${ARM32})
- endif()
-endif()
-
-if (WIN32)
- set(ALL_ORC_SUPPORTED_ARCH ${X86_64})
-endif()
+set(ARM64 aarch64)
+set(ARM32 arm armhf)
+set(HEXAGON hexagon)
+set(X86 i386)
+set(X86_64 x86_64)
+set(LOONGARCH64 loongarch64)
+set(MIPS32 mips mipsel)
+set(MIPS64 mips64 mips64el)
+set(PPC32 powerpc powerpcspe)
+set(PPC64 powerpc64 powerpc64le)
+set(RISCV32 riscv32)
+set(RISCV64 riscv64)
+set(S390X s390x)
+set(SPARC sparc)
+set(SPARCV9 sparcv9)
+set(WASM32 wasm32)
+set(WASM64 wasm64)
+set(VE ve)
+
+if(APPLE)
+ set(ARM64 arm64)
+ set(ARM32 armv7 armv7s armv7k)
+ set(X86_64 x86_64 x86_64h)
+endif()
+
+set(ALL_SANITIZER_COMMON_SUPPORTED_ARCH ${X86} ${X86_64} ${PPC64} ${RISCV64}
+ ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9}
+ ${HEXAGON} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to common supported architectures
+
+set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
+ ${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
+ ${LOONGARCH64} ${RISCV64}) # Added riscv64 to ASAN supported architectures
+
+set(ALL_ASAN_ABI_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64}) # Added riscv64 to ASAN ABI supported architectures
+
+set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to DFSAN supported architectures
+
+if(ANDROID)
+ set(OS_NAME "Android")
+else()
+ set(OS_NAME "${CMAKE_SYSTEM_NAME}")
+endif()
+
+if(OS_NAME MATCHES "Linux")
+ set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${S390X}
+ ${RISCV64} ${LOONGARCH64}) # Added riscv64 to fuzzer supported architectures
+elseif (OS_NAME MATCHES "Windows")
+ set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64})
+elseif(OS_NAME MATCHES "Android")
+ set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64})
+elseif(OS_NAME MATCHES "Fuchsia")
+ set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
+else()
+ set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64})
+endif()
+
+set(ALL_GWP_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}) # Added riscv64 to GWP ASAN supported architectures
+if(APPLE)
+ set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64} ${RISCV64})
+else()
+ set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64} ${ARM32}
+ ${PPC64} ${S390X} ${RISCV64} ${HEXAGON} ${LOONGARCH64})
+endif()
+set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X}
+ ${LOONGARCH64} ${RISCV64}) # Added riscv64 to MSAN supported architectures
+set(ALL_HWASAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64}) # Added riscv64 to HWASAN supported architectures
+set(ALL_MEMPROF_SUPPORTED_ARCH ${X86_64})
+set(ALL_PROFILE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32} ${PPC64}
+ ${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
+ ${RISCV32} ${RISCV64} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to profile supported architectures
+set(ALL_TSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X}
+ ${LOONGARCH64} ${RISCV64}) # Added riscv64 to TSAN supported architectures
+set(ALL_UBSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
+ ${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
+ ${LOONGARCH64}) # Added riscv64 to UBSAN supported architectures
+set(ALL_SAFESTACK_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM64} ${MIPS32} ${MIPS64}
+ ${HEXAGON} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to Safestack supported architectures
+set(ALL_CFI_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS64}
+ ${HEXAGON} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to CFI supported architectures
+set(ALL_SCUDO_STANDALONE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64}
+ ${MIPS32} ${MIPS64} ${PPC64} ${HEXAGON} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to Scudo Standalone supported architectures
+if(APPLE)
+set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64}) # Added riscv64 to XRAY supported architectures
+else()
+set(ALL_XRAY_SUPPORTED_ARCH ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64}
+ powerpc64le ${HEXAGON} ${LOONGARCH64} ${RISCV64}) # Added riscv64 to XRAY supported architectures
+endif()
+set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64} ${RISCV64}) # Added riscv64 to Shadowcallstack supported architectures
+
+if (UNIX)
+ if (OS_NAME MATCHES "Linux")
+ set(ALL_ORC_SUPPORTED_ARCH ${X86_64} ${ARM64} ${ARM32} ${PPC64} ${RISCV64}) # Added riscv64 to ORC supported architectures on Linux
+ else()
+ set(ALL_ORC_SUPPORTED_ARCH ${X86_64} ${ARM64} ${ARM32})
+ endif()
+endif()
+
+if (WIN32)
+ set(ALL_ORC_SUPPORTED_ARCH ${X86_64})
+endif()
>From e237a66932c8680ee6c548f7ac089c9550b7cd12 Mon Sep 17 00:00:00 2001
From: Harbandana Kaur <harbandanakaur13 at chromium.org>
Date: Tue, 30 Jan 2024 02:34:33 +0530
Subject: [PATCH 3/5] Add support for the .yml file extension
---
.../lib/Tooling/ApplyReplacements.cpp | 545 +++++++++---------
1 file changed, 274 insertions(+), 271 deletions(-)
diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
index 87ed1b8797cb0..9ab0551b478eb 100644
--- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
+++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
@@ -1,271 +1,274 @@
-//===-- ApplyReplacements.cpp - Apply and deduplicate replacements --------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-///
-/// \file
-/// This file provides the implementation for deduplicating, detecting
-/// conflicts in, and applying collections of Replacements.
-///
-/// FIXME: Use Diagnostics for output instead of llvm::errs().
-///
-//===----------------------------------------------------------------------===//
-#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
-#include "clang/Basic/LangOptions.h"
-#include "clang/Basic/SourceManager.h"
-#include "clang/Format/Format.h"
-#include "clang/Lex/Lexer.h"
-#include "clang/Rewrite/Core/Rewriter.h"
-#include "clang/Tooling/Core/Diagnostic.h"
-#include "clang/Tooling/DiagnosticsYaml.h"
-#include "clang/Tooling/ReplacementsYaml.h"
-#include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/StringSet.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/Path.h"
-#include "llvm/Support/raw_ostream.h"
-#include <optional>
-
-using namespace llvm;
-using namespace clang;
-
-static void eatDiagnostics(const SMDiagnostic &, void *) {}
-
-namespace clang {
-namespace replace {
-
-namespace detail {
-template <typename TranslationUnits>
-static std::error_code collectReplacementsFromDirectory(
- const llvm::StringRef Directory, TranslationUnits &TUs,
- TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
- using namespace llvm::sys::fs;
- using namespace llvm::sys::path;
-
- std::error_code ErrorCode;
-
- for (recursive_directory_iterator I(Directory, ErrorCode), E;
- I != E && !ErrorCode; I.increment(ErrorCode)) {
- if (filename(I->path())[0] == '.') {
- // Indicate not to descend into directories beginning with '.'
- I.no_push();
- continue;
- }
-
- if (extension(I->path()) != ".yaml")
- continue;
-
- TUFiles.push_back(I->path());
-
- ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
- MemoryBuffer::getFile(I->path());
- if (std::error_code BufferError = Out.getError()) {
- errs() << "Error reading " << I->path() << ": " << BufferError.message()
- << "\n";
- continue;
- }
-
- yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
- typename TranslationUnits::value_type TU;
- YIn >> TU;
- if (YIn.error()) {
- // File doesn't appear to be a header change description. Ignore it.
- continue;
- }
-
- // Only keep files that properly parse.
- TUs.push_back(TU);
- }
-
- return ErrorCode;
-}
-} // namespace detail
-
-template <>
-std::error_code collectReplacementsFromDirectory(
- const llvm::StringRef Directory, TUReplacements &TUs,
- TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
- return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
- Diagnostics);
-}
-
-template <>
-std::error_code collectReplacementsFromDirectory(
- const llvm::StringRef Directory, TUDiagnostics &TUs,
- TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
- return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
- Diagnostics);
-}
-
-/// Extract replacements from collected TranslationUnitReplacements and
-/// TranslationUnitDiagnostics and group them per file. Identical replacements
-/// from diagnostics are deduplicated.
-///
-/// \param[in] TUs Collection of all found and deserialized
-/// TranslationUnitReplacements.
-/// \param[in] TUDs Collection of all found and deserialized
-/// TranslationUnitDiagnostics.
-/// \param[in] SM Used to deduplicate paths.
-///
-/// \returns A map mapping FileEntry to a set of Replacement targeting that
-/// file.
-static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
-groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
- const clang::SourceManager &SM) {
- llvm::StringSet<> Warned;
- llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
- GroupedReplacements;
-
- // Deduplicate identical replacements in diagnostics unless they are from the
- // same TU.
- // FIXME: Find an efficient way to deduplicate on diagnostics level.
- llvm::DenseMap<const FileEntry *,
- std::map<tooling::Replacement,
- const tooling::TranslationUnitDiagnostics *>>
- DiagReplacements;
-
- auto AddToGroup = [&](const tooling::Replacement &R,
- const tooling::TranslationUnitDiagnostics *SourceTU,
- const std::optional<std::string> BuildDir) {
- // Use the file manager to deduplicate paths. FileEntries are
- // automatically canonicalized. Since relative paths can come from different
- // build directories, make them absolute immediately.
- SmallString<128> Path = R.getFilePath();
- if (BuildDir)
- llvm::sys::fs::make_absolute(*BuildDir, Path);
- else
- SM.getFileManager().makeAbsolutePath(Path);
-
- if (auto Entry = SM.getFileManager().getOptionalFileRef(Path)) {
- if (SourceTU) {
- auto &Replaces = DiagReplacements[*Entry];
- auto It = Replaces.find(R);
- if (It == Replaces.end())
- Replaces.emplace(R, SourceTU);
- else if (It->second != SourceTU)
- // This replacement is a duplicate of one suggested by another TU.
- return;
- }
- GroupedReplacements[*Entry].push_back(R);
- } else if (Warned.insert(Path).second) {
- errs() << "Described file '" << R.getFilePath()
- << "' doesn't exist. Ignoring...\n";
- }
- };
-
- for (const auto &TU : TUs)
- for (const tooling::Replacement &R : TU.Replacements)
- AddToGroup(R, nullptr, {});
-
- for (const auto &TU : TUDs)
- for (const auto &D : TU.Diagnostics)
- if (const auto *ChoosenFix = tooling::selectFirstFix(D)) {
- for (const auto &Fix : *ChoosenFix)
- for (const tooling::Replacement &R : Fix.second)
- AddToGroup(R, &TU, D.BuildDirectory);
- }
-
- // Sort replacements per file to keep consistent behavior when
- // clang-apply-replacements run on differents machine.
- for (auto &FileAndReplacements : GroupedReplacements) {
- llvm::sort(FileAndReplacements.second);
- }
-
- return GroupedReplacements;
-}
-
-bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
- FileToChangesMap &FileChanges,
- clang::SourceManager &SM, bool IgnoreInsertConflict) {
- auto GroupedReplacements = groupReplacements(TUs, TUDs, SM);
- bool ConflictDetected = false;
-
- // To report conflicting replacements on corresponding file, all replacements
- // are stored into 1 big AtomicChange.
- for (const auto &FileAndReplacements : GroupedReplacements) {
- FileEntryRef Entry = FileAndReplacements.first;
- const SourceLocation BeginLoc =
- SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User));
- tooling::AtomicChange FileChange(Entry.getName(), Entry.getName());
- for (const auto &R : FileAndReplacements.second) {
- llvm::Error Err =
- FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),
- R.getLength(), R.getReplacementText());
- if (Err) {
- // FIXME: This will report conflicts by pair using a file+offset format
- // which is not so much human readable.
- // A first improvement could be to translate offset to line+col. For
- // this and without loosing error message some modifications around
- // `tooling::ReplacementError` are need (access to
- // `getReplacementErrString`).
- // A better strategy could be to add a pretty printer methods for
- // conflict reporting. Methods that could be parameterized to report a
- // conflict in different format, file+offset, file+line+col, or even
- // more human readable using VCS conflict markers.
- // For now, printing directly the error reported by `AtomicChange` is
- // the easiest solution.
- errs() << llvm::toString(std::move(Err)) << "\n";
- if (IgnoreInsertConflict) {
- tooling::Replacements &Replacements = FileChange.getReplacements();
- unsigned NewOffset =
- Replacements.getShiftedCodePosition(R.getOffset());
- unsigned NewLength = Replacements.getShiftedCodePosition(
- R.getOffset() + R.getLength()) -
- NewOffset;
- if (NewLength == R.getLength()) {
- tooling::Replacement RR = tooling::Replacement(
- R.getFilePath(), NewOffset, NewLength, R.getReplacementText());
- Replacements = Replacements.merge(tooling::Replacements(RR));
- } else {
- llvm::errs()
- << "Can't resolve conflict, skipping the replacement.\n";
- ConflictDetected = true;
- }
- } else
- ConflictDetected = true;
- }
- }
- FileChanges.try_emplace(Entry,
- std::vector<tooling::AtomicChange>{FileChange});
- }
-
- return !ConflictDetected;
-}
-
-llvm::Expected<std::string>
-applyChanges(StringRef File, const std::vector<tooling::AtomicChange> &Changes,
- const tooling::ApplyChangesSpec &Spec,
- DiagnosticsEngine &Diagnostics) {
- FileManager Files((FileSystemOptions()));
- SourceManager SM(Diagnostics, Files);
-
- llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
- SM.getFileManager().getBufferForFile(File);
- if (!Buffer)
- return errorCodeToError(Buffer.getError());
- return tooling::applyAtomicChanges(File, Buffer.get()->getBuffer(), Changes,
- Spec);
-}
-
-bool deleteReplacementFiles(const TUReplacementFiles &Files,
- clang::DiagnosticsEngine &Diagnostics) {
- bool Success = true;
- for (const auto &Filename : Files) {
- std::error_code Error = llvm::sys::fs::remove(Filename);
- if (Error) {
- Success = false;
- // FIXME: Use Diagnostics for outputting errors.
- errs() << "Error deleting file: " << Filename << "\n";
- errs() << Error.message() << "\n";
- errs() << "Please delete the file manually\n";
- }
- }
- return Success;
-}
-
-} // end namespace replace
-} // end namespace clang
+//===-- ApplyReplacements.cpp - Apply and deduplicate replacements --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file provides the implementation for deduplicating, detecting
+/// conflicts in, and applying collections of Replacements.
+///
+/// FIXME: Use Diagnostics for output instead of llvm::errs().
+///
+//===----------------------------------------------------------------------===//
+#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Format/Format.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/Core/Diagnostic.h"
+#include "clang/Tooling/DiagnosticsYaml.h"
+#include "clang/Tooling/ReplacementsYaml.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringSet.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/raw_ostream.h"
+#include <optional>
+
+using namespace llvm;
+using namespace clang;
+
+static void eatDiagnostics(const SMDiagnostic &, void *) {}
+
+namespace clang {
+namespace replace {
+
+namespace detail {
+
+static constexpr std::array<StringRef, 3> AllowedExtensions = {".yaml", ".yml", ""};
+
+template <typename TranslationUnits>
+static std::error_code collectReplacementsFromDirectory(
+ const llvm::StringRef Directory, TranslationUnits &TUs,
+ TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
+ using namespace llvm::sys::fs;
+ using namespace llvm::sys::path;
+
+ std::error_code ErrorCode;
+
+ for (recursive_directory_iterator I(Directory, ErrorCode), E;
+ I != E && !ErrorCode; I.increment(ErrorCode)) {
+ if (filename(I->path())[0] == '.') {
+ // Indicate not to descend into directories beginning with '.'
+ I.no_push();
+ continue;
+ }
+
+ if (!is_contained(AllowedExtensions, extension(I->path())))
+ continue;
+
+ TUFiles.push_back(I->path());
+
+ ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
+ MemoryBuffer::getFile(I->path());
+ if (std::error_code BufferError = Out.getError()) {
+ errs() << "Error reading " << I->path() << ": " << BufferError.message()
+ << "\n";
+ continue;
+ }
+
+ yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
+ typename TranslationUnits::value_type TU;
+ YIn >> TU;
+ if (YIn.error()) {
+ // File doesn't appear to be a header change description. Ignore it.
+ continue;
+ }
+
+ // Only keep files that properly parse.
+ TUs.push_back(TU);
+ }
+
+ return ErrorCode;
+}
+} // namespace detail
+
+template <>
+std::error_code collectReplacementsFromDirectory(
+ const llvm::StringRef Directory, TUReplacements &TUs,
+ TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
+ return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
+ Diagnostics);
+}
+
+template <>
+std::error_code collectReplacementsFromDirectory(
+ const llvm::StringRef Directory, TUDiagnostics &TUs,
+ TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
+ return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
+ Diagnostics);
+}
+
+/// Extract replacements from collected TranslationUnitReplacements and
+/// TranslationUnitDiagnostics and group them per file. Identical replacements
+/// from diagnostics are deduplicated.
+///
+/// \param[in] TUs Collection of all found and deserialized
+/// TranslationUnitReplacements.
+/// \param[in] TUDs Collection of all found and deserialized
+/// TranslationUnitDiagnostics.
+/// \param[in] SM Used to deduplicate paths.
+///
+/// \returns A map mapping FileEntry to a set of Replacement targeting that
+/// file.
+static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
+groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
+ const clang::SourceManager &SM) {
+ llvm::StringSet<> Warned;
+ llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
+ GroupedReplacements;
+
+ // Deduplicate identical replacements in diagnostics unless they are from the
+ // same TU.
+ // FIXME: Find an efficient way to deduplicate on diagnostics level.
+ llvm::DenseMap<const FileEntry *,
+ std::map<tooling::Replacement,
+ const tooling::TranslationUnitDiagnostics *>>
+ DiagReplacements;
+
+ auto AddToGroup = [&](const tooling::Replacement &R,
+ const tooling::TranslationUnitDiagnostics *SourceTU,
+ const std::optional<std::string> BuildDir) {
+ // Use the file manager to deduplicate paths. FileEntries are
+ // automatically canonicalized. Since relative paths can come from different
+ // build directories, make them absolute immediately.
+ SmallString<128> Path = R.getFilePath();
+ if (BuildDir)
+ llvm::sys::fs::make_absolute(*BuildDir, Path);
+ else
+ SM.getFileManager().makeAbsolutePath(Path);
+
+ if (auto Entry = SM.getFileManager().getOptionalFileRef(Path)) {
+ if (SourceTU) {
+ auto &Replaces = DiagReplacements[*Entry];
+ auto It = Replaces.find(R);
+ if (It == Replaces.end())
+ Replaces.emplace(R, SourceTU);
+ else if (It->second != SourceTU)
+ // This replacement is a duplicate of one suggested by another TU.
+ return;
+ }
+ GroupedReplacements[*Entry].push_back(R);
+ } else if (Warned.insert(Path).second) {
+ errs() << "Described file '" << R.getFilePath()
+ << "' doesn't exist. Ignoring...\n";
+ }
+ };
+
+ for (const auto &TU : TUs)
+ for (const tooling::Replacement &R : TU.Replacements)
+ AddToGroup(R, nullptr, {});
+
+ for (const auto &TU : TUDs)
+ for (const auto &D : TU.Diagnostics)
+ if (const auto *ChoosenFix = tooling::selectFirstFix(D)) {
+ for (const auto &Fix : *ChoosenFix)
+ for (const tooling::Replacement &R : Fix.second)
+ AddToGroup(R, &TU, D.BuildDirectory);
+ }
+
+ // Sort replacements per file to keep consistent behavior when
+ // clang-apply-replacements run on differents machine.
+ for (auto &FileAndReplacements : GroupedReplacements) {
+ llvm::sort(FileAndReplacements.second);
+ }
+
+ return GroupedReplacements;
+}
+
+bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
+ FileToChangesMap &FileChanges,
+ clang::SourceManager &SM, bool IgnoreInsertConflict) {
+ auto GroupedReplacements = groupReplacements(TUs, TUDs, SM);
+ bool ConflictDetected = false;
+
+ // To report conflicting replacements on corresponding file, all replacements
+ // are stored into 1 big AtomicChange.
+ for (const auto &FileAndReplacements : GroupedReplacements) {
+ FileEntryRef Entry = FileAndReplacements.first;
+ const SourceLocation BeginLoc =
+ SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User));
+ tooling::AtomicChange FileChange(Entry.getName(), Entry.getName());
+ for (const auto &R : FileAndReplacements.second) {
+ llvm::Error Err =
+ FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),
+ R.getLength(), R.getReplacementText());
+ if (Err) {
+ // FIXME: This will report conflicts by pair using a file+offset format
+ // which is not so much human readable.
+ // A first improvement could be to translate offset to line+col. For
+ // this and without loosing error message some modifications around
+ // `tooling::ReplacementError` are need (access to
+ // `getReplacementErrString`).
+ // A better strategy could be to add a pretty printer methods for
+ // conflict reporting. Methods that could be parameterized to report a
+ // conflict in different format, file+offset, file+line+col, or even
+ // more human readable using VCS conflict markers.
+ // For now, printing directly the error reported by `AtomicChange` is
+ // the easiest solution.
+ errs() << llvm::toString(std::move(Err)) << "\n";
+ if (IgnoreInsertConflict) {
+ tooling::Replacements &Replacements = FileChange.getReplacements();
+ unsigned NewOffset =
+ Replacements.getShiftedCodePosition(R.getOffset());
+ unsigned NewLength = Replacements.getShiftedCodePosition(
+ R.getOffset() + R.getLength()) -
+ NewOffset;
+ if (NewLength == R.getLength()) {
+ tooling::Replacement RR = tooling::Replacement(
+ R.getFilePath(), NewOffset, NewLength, R.getReplacementText());
+ Replacements = Replacements.merge(tooling::Replacements(RR));
+ } else {
+ llvm::errs()
+ << "Can't resolve conflict, skipping the replacement.\n";
+ ConflictDetected = true;
+ }
+ } else
+ ConflictDetected = true;
+ }
+ }
+ FileChanges.try_emplace(Entry,
+ std::vector<tooling::AtomicChange>{FileChange});
+ }
+
+ return !ConflictDetected;
+}
+
+llvm::Expected<std::string>
+applyChanges(StringRef File, const std::vector<tooling::AtomicChange> &Changes,
+ const tooling::ApplyChangesSpec &Spec,
+ DiagnosticsEngine &Diagnostics) {
+ FileManager Files((FileSystemOptions()));
+ SourceManager SM(Diagnostics, Files);
+
+ llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
+ SM.getFileManager().getBufferForFile(File);
+ if (!Buffer)
+ return errorCodeToError(Buffer.getError());
+ return tooling::applyAtomicChanges(File, Buffer.get()->getBuffer(), Changes,
+ Spec);
+}
+
+bool deleteReplacementFiles(const TUReplacementFiles &Files,
+ clang::DiagnosticsEngine &Diagnostics) {
+ bool Success = true;
+ for (const auto &Filename : Files) {
+ std::error_code Error = llvm::sys::fs::remove(Filename);
+ if (Error) {
+ Success = false;
+ // FIXME: Use Diagnostics for outputting errors.
+ errs() << "Error deleting file: " << Filename << "\n";
+ errs() << Error.message() << "\n";
+ errs() << "Please delete the file manually\n";
+ }
+ }
+ return Success;
+}
+
+} // end namespace replace
+} // end namespace clang
>From 86232bd5dd6fe28ea830f0b5fe3ffa4f48d545ce Mon Sep 17 00:00:00 2001
From: Harbandana Kaur <harbandanakaur13 at chromium.org>
Date: Tue, 30 Jan 2024 02:39:22 +0530
Subject: [PATCH 4/5] =?UTF-8?q?Add=20support=20for=20the=20.yml=20file=20e?=
=?UTF-8?q?xtension=C2=A0#78842?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../lib/Tooling/ApplyReplacements.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
index 9ab0551b478eb..20bafef724043 100644
--- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
+++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
@@ -13,6 +13,7 @@
/// FIXME: Use Diagnostics for output instead of llvm::errs().
///
//===----------------------------------------------------------------------===//
+
#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
@@ -270,5 +271,5 @@ bool deleteReplacementFiles(const TUReplacementFiles &Files,
return Success;
}
-} // end namespace replace
-} // end namespace clang
+}
+}
>From c0a67d82b97594fea40eaa8b137bc8e976efeec3 Mon Sep 17 00:00:00 2001
From: Harbandana Kaur <harbandanakaur13 at chromium.org>
Date: Wed, 31 Jan 2024 03:05:18 +0530
Subject: [PATCH 5/5] clang::Linkage #71049
---
clang/lib/AST/APValue.cpp | 2418 +++++++++++++++++++------------------
1 file changed, 1212 insertions(+), 1206 deletions(-)
diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 4eae308ef5b34..5ae894d457baf 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -1,1206 +1,1212 @@
-//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the APValue class.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/AST/APValue.h"
-#include "Linkage.h"
-#include "clang/AST/ASTContext.h"
-#include "clang/AST/CharUnits.h"
-#include "clang/AST/DeclCXX.h"
-#include "clang/AST/Expr.h"
-#include "clang/AST/ExprCXX.h"
-#include "clang/AST/Type.h"
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/raw_ostream.h"
-using namespace clang;
-
-/// The identity of a type_info object depends on the canonical unqualified
-/// type only.
-TypeInfoLValue::TypeInfoLValue(const Type *T)
- : T(T->getCanonicalTypeUnqualified().getTypePtr()) {}
-
-void TypeInfoLValue::print(llvm::raw_ostream &Out,
- const PrintingPolicy &Policy) const {
- Out << "typeid(";
- QualType(getType(), 0).print(Out, Policy);
- Out << ")";
-}
-
-static_assert(
- 1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <=
- alignof(Type),
- "Type is insufficiently aligned");
-
-APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
- : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()) : nullptr), Local{I, V} {}
-APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
- : Ptr(P), Local{I, V} {}
-
-APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV,
- QualType Type) {
- LValueBase Base;
- Base.Ptr = LV;
- Base.DynamicAllocType = Type.getAsOpaquePtr();
- return Base;
-}
-
-APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV,
- QualType TypeInfo) {
- LValueBase Base;
- Base.Ptr = LV;
- Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
- return Base;
-}
-
-QualType APValue::LValueBase::getType() const {
- if (!*this) return QualType();
- if (const ValueDecl *D = dyn_cast<const ValueDecl*>()) {
- // FIXME: It's unclear where we're supposed to take the type from, and
- // this actually matters for arrays of unknown bound. Eg:
- //
- // extern int arr[]; void f() { extern int arr[3]; };
- // constexpr int *p = &arr[1]; // valid?
- //
- // For now, we take the most complete type we can find.
- for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
- Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) {
- QualType T = Redecl->getType();
- if (!T->isIncompleteArrayType())
- return T;
- }
- return D->getType();
- }
-
- if (is<TypeInfoLValue>())
- return getTypeInfoType();
-
- if (is<DynamicAllocLValue>())
- return getDynamicAllocType();
-
- const Expr *Base = get<const Expr*>();
-
- // For a materialized temporary, the type of the temporary we materialized
- // may not be the type of the expression.
- if (const MaterializeTemporaryExpr *MTE =
- clang::dyn_cast<MaterializeTemporaryExpr>(Base)) {
- SmallVector<const Expr *, 2> CommaLHSs;
- SmallVector<SubobjectAdjustment, 2> Adjustments;
- const Expr *Temp = MTE->getSubExpr();
- const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs,
- Adjustments);
- // Keep any cv-qualifiers from the reference if we generated a temporary
- // for it directly. Otherwise use the type after adjustment.
- if (!Adjustments.empty())
- return Inner->getType();
- }
-
- return Base->getType();
-}
-
-unsigned APValue::LValueBase::getCallIndex() const {
- return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0
- : Local.CallIndex;
-}
-
-unsigned APValue::LValueBase::getVersion() const {
- return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0 : Local.Version;
-}
-
-QualType APValue::LValueBase::getTypeInfoType() const {
- assert(is<TypeInfoLValue>() && "not a type_info lvalue");
- return QualType::getFromOpaquePtr(TypeInfoType);
-}
-
-QualType APValue::LValueBase::getDynamicAllocType() const {
- assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue");
- return QualType::getFromOpaquePtr(DynamicAllocType);
-}
-
-void APValue::LValueBase::Profile(llvm::FoldingSetNodeID &ID) const {
- ID.AddPointer(Ptr.getOpaqueValue());
- if (is<TypeInfoLValue>() || is<DynamicAllocLValue>())
- return;
- ID.AddInteger(Local.CallIndex);
- ID.AddInteger(Local.Version);
-}
-
-namespace clang {
-bool operator==(const APValue::LValueBase &LHS,
- const APValue::LValueBase &RHS) {
- if (LHS.Ptr != RHS.Ptr)
- return false;
- if (LHS.is<TypeInfoLValue>() || LHS.is<DynamicAllocLValue>())
- return true;
- return LHS.Local.CallIndex == RHS.Local.CallIndex &&
- LHS.Local.Version == RHS.Local.Version;
-}
-}
-
-APValue::LValuePathEntry::LValuePathEntry(BaseOrMemberType BaseOrMember) {
- if (const Decl *D = BaseOrMember.getPointer())
- BaseOrMember.setPointer(D->getCanonicalDecl());
- Value = reinterpret_cast<uintptr_t>(BaseOrMember.getOpaqueValue());
-}
-
-void APValue::LValuePathEntry::Profile(llvm::FoldingSetNodeID &ID) const {
- ID.AddInteger(Value);
-}
-
-APValue::LValuePathSerializationHelper::LValuePathSerializationHelper(
- ArrayRef<LValuePathEntry> Path, QualType ElemTy)
- : Ty((const void *)ElemTy.getTypePtrOrNull()), Path(Path) {}
-
-QualType APValue::LValuePathSerializationHelper::getType() {
- return QualType::getFromOpaquePtr(Ty);
-}
-
-namespace {
- struct LVBase {
- APValue::LValueBase Base;
- CharUnits Offset;
- unsigned PathLength;
- bool IsNullPtr : 1;
- bool IsOnePastTheEnd : 1;
- };
-}
-
-void *APValue::LValueBase::getOpaqueValue() const {
- return Ptr.getOpaqueValue();
-}
-
-bool APValue::LValueBase::isNull() const {
- return Ptr.isNull();
-}
-
-APValue::LValueBase::operator bool () const {
- return static_cast<bool>(Ptr);
-}
-
-clang::APValue::LValueBase
-llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
- clang::APValue::LValueBase B;
- B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
- return B;
-}
-
-clang::APValue::LValueBase
-llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
- clang::APValue::LValueBase B;
- B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
- return B;
-}
-
-namespace clang {
-llvm::hash_code hash_value(const APValue::LValueBase &Base) {
- if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>())
- return llvm::hash_value(Base.getOpaqueValue());
- return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
- Base.getVersion());
-}
-}
-
-unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
- const clang::APValue::LValueBase &Base) {
- return hash_value(Base);
-}
-
-bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
- const clang::APValue::LValueBase &LHS,
- const clang::APValue::LValueBase &RHS) {
- return LHS == RHS;
-}
-
-struct APValue::LV : LVBase {
- static const unsigned InlinePathSpace =
- (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
-
- /// Path - The sequence of base classes, fields and array indices to follow to
- /// walk from Base to the subobject. When performing GCC-style folding, there
- /// may not be such a path.
- union {
- LValuePathEntry Path[InlinePathSpace];
- LValuePathEntry *PathPtr;
- };
-
- LV() { PathLength = (unsigned)-1; }
- ~LV() { resizePath(0); }
-
- void resizePath(unsigned Length) {
- if (Length == PathLength)
- return;
- if (hasPathPtr())
- delete [] PathPtr;
- PathLength = Length;
- if (hasPathPtr())
- PathPtr = new LValuePathEntry[Length];
- }
-
- bool hasPath() const { return PathLength != (unsigned)-1; }
- bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
-
- LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
- const LValuePathEntry *getPath() const {
- return hasPathPtr() ? PathPtr : Path;
- }
-};
-
-namespace {
- struct MemberPointerBase {
- llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
- unsigned PathLength;
- };
-}
-
-struct APValue::MemberPointerData : MemberPointerBase {
- static const unsigned InlinePathSpace =
- (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
- typedef const CXXRecordDecl *PathElem;
- union {
- PathElem Path[InlinePathSpace];
- PathElem *PathPtr;
- };
-
- MemberPointerData() { PathLength = 0; }
- ~MemberPointerData() { resizePath(0); }
-
- void resizePath(unsigned Length) {
- if (Length == PathLength)
- return;
- if (hasPathPtr())
- delete [] PathPtr;
- PathLength = Length;
- if (hasPathPtr())
- PathPtr = new PathElem[Length];
- }
-
- bool hasPathPtr() const { return PathLength > InlinePathSpace; }
-
- PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
- const PathElem *getPath() const {
- return hasPathPtr() ? PathPtr : Path;
- }
-};
-
-// FIXME: Reduce the malloc traffic here.
-
-APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
- Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
- NumElts(NumElts), ArrSize(Size) {}
-APValue::Arr::~Arr() { delete [] Elts; }
-
-APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
- Elts(new APValue[NumBases+NumFields]),
- NumBases(NumBases), NumFields(NumFields) {}
-APValue::StructData::~StructData() {
- delete [] Elts;
-}
-
-APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
-APValue::UnionData::~UnionData () {
- delete Value;
-}
-
-APValue::APValue(const APValue &RHS) : Kind(None) {
- switch (RHS.getKind()) {
- case None:
- case Indeterminate:
- Kind = RHS.getKind();
- break;
- case Int:
- MakeInt();
- setInt(RHS.getInt());
- break;
- case Float:
- MakeFloat();
- setFloat(RHS.getFloat());
- break;
- case FixedPoint: {
- APFixedPoint FXCopy = RHS.getFixedPoint();
- MakeFixedPoint(std::move(FXCopy));
- break;
- }
- case Vector:
- MakeVector();
- setVector(((const Vec *)(const char *)&RHS.Data)->Elts,
- RHS.getVectorLength());
- break;
- case ComplexInt:
- MakeComplexInt();
- setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
- break;
- case ComplexFloat:
- MakeComplexFloat();
- setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
- break;
- case LValue:
- MakeLValue();
- if (RHS.hasLValuePath())
- setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
- RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
- else
- setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
- RHS.isNullPointer());
- break;
- case Array:
- MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
- for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
- getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
- if (RHS.hasArrayFiller())
- getArrayFiller() = RHS.getArrayFiller();
- break;
- case Struct:
- MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
- for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
- getStructBase(I) = RHS.getStructBase(I);
- for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
- getStructField(I) = RHS.getStructField(I);
- break;
- case Union:
- MakeUnion();
- setUnion(RHS.getUnionField(), RHS.getUnionValue());
- break;
- case MemberPointer:
- MakeMemberPointer(RHS.getMemberPointerDecl(),
- RHS.isMemberPointerToDerivedMember(),
- RHS.getMemberPointerPath());
- break;
- case AddrLabelDiff:
- MakeAddrLabelDiff();
- setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
- break;
- }
-}
-
-APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
- RHS.Kind = None;
-}
-
-APValue &APValue::operator=(const APValue &RHS) {
- if (this != &RHS)
- *this = APValue(RHS);
- return *this;
-}
-
-APValue &APValue::operator=(APValue &&RHS) {
- if (this != &RHS) {
- if (Kind != None && Kind != Indeterminate)
- DestroyDataAndMakeUninit();
- Kind = RHS.Kind;
- Data = RHS.Data;
- RHS.Kind = None;
- }
- return *this;
-}
-
-void APValue::DestroyDataAndMakeUninit() {
- if (Kind == Int)
- ((APSInt *)(char *)&Data)->~APSInt();
- else if (Kind == Float)
- ((APFloat *)(char *)&Data)->~APFloat();
- else if (Kind == FixedPoint)
- ((APFixedPoint *)(char *)&Data)->~APFixedPoint();
- else if (Kind == Vector)
- ((Vec *)(char *)&Data)->~Vec();
- else if (Kind == ComplexInt)
- ((ComplexAPSInt *)(char *)&Data)->~ComplexAPSInt();
- else if (Kind == ComplexFloat)
- ((ComplexAPFloat *)(char *)&Data)->~ComplexAPFloat();
- else if (Kind == LValue)
- ((LV *)(char *)&Data)->~LV();
- else if (Kind == Array)
- ((Arr *)(char *)&Data)->~Arr();
- else if (Kind == Struct)
- ((StructData *)(char *)&Data)->~StructData();
- else if (Kind == Union)
- ((UnionData *)(char *)&Data)->~UnionData();
- else if (Kind == MemberPointer)
- ((MemberPointerData *)(char *)&Data)->~MemberPointerData();
- else if (Kind == AddrLabelDiff)
- ((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData();
- Kind = None;
-}
-
-bool APValue::needsCleanup() const {
- switch (getKind()) {
- case None:
- case Indeterminate:
- case AddrLabelDiff:
- return false;
- case Struct:
- case Union:
- case Array:
- case Vector:
- return true;
- case Int:
- return getInt().needsCleanup();
- case Float:
- return getFloat().needsCleanup();
- case FixedPoint:
- return getFixedPoint().getValue().needsCleanup();
- case ComplexFloat:
- assert(getComplexFloatImag().needsCleanup() ==
- getComplexFloatReal().needsCleanup() &&
- "In _Complex float types, real and imaginary values always have the "
- "same size.");
- return getComplexFloatReal().needsCleanup();
- case ComplexInt:
- assert(getComplexIntImag().needsCleanup() ==
- getComplexIntReal().needsCleanup() &&
- "In _Complex int types, real and imaginary values must have the "
- "same size.");
- return getComplexIntReal().needsCleanup();
- case LValue:
- return reinterpret_cast<const LV *>(&Data)->hasPathPtr();
- case MemberPointer:
- return reinterpret_cast<const MemberPointerData *>(&Data)->hasPathPtr();
- }
- llvm_unreachable("Unknown APValue kind!");
-}
-
-void APValue::swap(APValue &RHS) {
- std::swap(Kind, RHS.Kind);
- std::swap(Data, RHS.Data);
-}
-
-/// Profile the value of an APInt, excluding its bit-width.
-static void profileIntValue(llvm::FoldingSetNodeID &ID, const llvm::APInt &V) {
- for (unsigned I = 0, N = V.getBitWidth(); I < N; I += 32)
- ID.AddInteger((uint32_t)V.extractBitsAsZExtValue(std::min(32u, N - I), I));
-}
-
-void APValue::Profile(llvm::FoldingSetNodeID &ID) const {
- // Note that our profiling assumes that only APValues of the same type are
- // ever compared. As a result, we don't consider collisions that could only
- // happen if the types are different. (For example, structs with different
- // numbers of members could profile the same.)
-
- ID.AddInteger(Kind);
-
- switch (Kind) {
- case None:
- case Indeterminate:
- return;
-
- case AddrLabelDiff:
- ID.AddPointer(getAddrLabelDiffLHS()->getLabel()->getCanonicalDecl());
- ID.AddPointer(getAddrLabelDiffRHS()->getLabel()->getCanonicalDecl());
- return;
-
- case Struct:
- for (unsigned I = 0, N = getStructNumBases(); I != N; ++I)
- getStructBase(I).Profile(ID);
- for (unsigned I = 0, N = getStructNumFields(); I != N; ++I)
- getStructField(I).Profile(ID);
- return;
-
- case Union:
- if (!getUnionField()) {
- ID.AddInteger(0);
- return;
- }
- ID.AddInteger(getUnionField()->getFieldIndex() + 1);
- getUnionValue().Profile(ID);
- return;
-
- case Array: {
- if (getArraySize() == 0)
- return;
-
- // The profile should not depend on whether the array is expanded or
- // not, but we don't want to profile the array filler many times for
- // a large array. So treat all equal trailing elements as the filler.
- // Elements are profiled in reverse order to support this, and the
- // first profiled element is followed by a count. For example:
- //
- // ['a', 'c', 'x', 'x', 'x'] is profiled as
- // [5, 'x', 3, 'c', 'a']
- llvm::FoldingSetNodeID FillerID;
- (hasArrayFiller() ? getArrayFiller()
- : getArrayInitializedElt(getArrayInitializedElts() - 1))
- .Profile(FillerID);
- ID.AddNodeID(FillerID);
- unsigned NumFillers = getArraySize() - getArrayInitializedElts();
- unsigned N = getArrayInitializedElts();
-
- // Count the number of elements equal to the last one. This loop ends
- // by adding an integer indicating the number of such elements, with
- // N set to the number of elements left to profile.
- while (true) {
- if (N == 0) {
- // All elements are fillers.
- assert(NumFillers == getArraySize());
- ID.AddInteger(NumFillers);
- break;
- }
-
- // No need to check if the last element is equal to the last
- // element.
- if (N != getArraySize()) {
- llvm::FoldingSetNodeID ElemID;
- getArrayInitializedElt(N - 1).Profile(ElemID);
- if (ElemID != FillerID) {
- ID.AddInteger(NumFillers);
- ID.AddNodeID(ElemID);
- --N;
- break;
- }
- }
-
- // This is a filler.
- ++NumFillers;
- --N;
- }
-
- // Emit the remaining elements.
- for (; N != 0; --N)
- getArrayInitializedElt(N - 1).Profile(ID);
- return;
- }
-
- case Vector:
- for (unsigned I = 0, N = getVectorLength(); I != N; ++I)
- getVectorElt(I).Profile(ID);
- return;
-
- case Int:
- profileIntValue(ID, getInt());
- return;
-
- case Float:
- profileIntValue(ID, getFloat().bitcastToAPInt());
- return;
-
- case FixedPoint:
- profileIntValue(ID, getFixedPoint().getValue());
- return;
-
- case ComplexFloat:
- profileIntValue(ID, getComplexFloatReal().bitcastToAPInt());
- profileIntValue(ID, getComplexFloatImag().bitcastToAPInt());
- return;
-
- case ComplexInt:
- profileIntValue(ID, getComplexIntReal());
- profileIntValue(ID, getComplexIntImag());
- return;
-
- case LValue:
- getLValueBase().Profile(ID);
- ID.AddInteger(getLValueOffset().getQuantity());
- ID.AddInteger((isNullPointer() ? 1 : 0) |
- (isLValueOnePastTheEnd() ? 2 : 0) |
- (hasLValuePath() ? 4 : 0));
- if (hasLValuePath()) {
- ID.AddInteger(getLValuePath().size());
- // For uniqueness, we only need to profile the entries corresponding
- // to union members, but we don't have the type here so we don't know
- // how to interpret the entries.
- for (LValuePathEntry E : getLValuePath())
- E.Profile(ID);
- }
- return;
-
- case MemberPointer:
- ID.AddPointer(getMemberPointerDecl());
- ID.AddInteger(isMemberPointerToDerivedMember());
- for (const CXXRecordDecl *D : getMemberPointerPath())
- ID.AddPointer(D);
- return;
- }
-
- llvm_unreachable("Unknown APValue kind!");
-}
-
-static double GetApproxValue(const llvm::APFloat &F) {
- llvm::APFloat V = F;
- bool ignored;
- V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
- &ignored);
- return V.convertToDouble();
-}
-
-static bool TryPrintAsStringLiteral(raw_ostream &Out,
- const PrintingPolicy &Policy,
- const ArrayType *ATy,
- ArrayRef<APValue> Inits) {
- if (Inits.empty())
- return false;
-
- QualType Ty = ATy->getElementType();
- if (!Ty->isAnyCharacterType())
- return false;
-
- // Nothing we can do about a sequence that is not null-terminated
- if (!Inits.back().isInt() || !Inits.back().getInt().isZero())
- return false;
-
- Inits = Inits.drop_back();
-
- llvm::SmallString<40> Buf;
- Buf.push_back('"');
-
- // Better than printing a two-digit sequence of 10 integers.
- constexpr size_t MaxN = 36;
- StringRef Ellipsis;
- if (Inits.size() > MaxN && !Policy.EntireContentsOfLargeArray) {
- Ellipsis = "[...]";
- Inits =
- Inits.take_front(std::min(MaxN - Ellipsis.size() / 2, Inits.size()));
- }
-
- for (auto &Val : Inits) {
- if (!Val.isInt())
- return false;
- int64_t Char64 = Val.getInt().getExtValue();
- if (!isASCII(Char64))
- return false; // Bye bye, see you in integers.
- auto Ch = static_cast<unsigned char>(Char64);
- // The diagnostic message is 'quoted'
- StringRef Escaped = escapeCStyle<EscapeChar::SingleAndDouble>(Ch);
- if (Escaped.empty()) {
- if (!isPrintable(Ch))
- return false;
- Buf.emplace_back(Ch);
- } else {
- Buf.append(Escaped);
- }
- }
-
- Buf.append(Ellipsis);
- Buf.push_back('"');
-
- if (Ty->isWideCharType())
- Out << 'L';
- else if (Ty->isChar8Type())
- Out << "u8";
- else if (Ty->isChar16Type())
- Out << 'u';
- else if (Ty->isChar32Type())
- Out << 'U';
-
- Out << Buf;
- return true;
-}
-
-void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx,
- QualType Ty) const {
- printPretty(Out, Ctx.getPrintingPolicy(), Ty, &Ctx);
-}
-
-void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
- QualType Ty, const ASTContext *Ctx) const {
- // There are no objects of type 'void', but values of this type can be
- // returned from functions.
- if (Ty->isVoidType()) {
- Out << "void()";
- return;
- }
-
- switch (getKind()) {
- case APValue::None:
- Out << "<out of lifetime>";
- return;
- case APValue::Indeterminate:
- Out << "<uninitialized>";
- return;
- case APValue::Int:
- if (Ty->isBooleanType())
- Out << (getInt().getBoolValue() ? "true" : "false");
- else
- Out << getInt();
- return;
- case APValue::Float:
- Out << GetApproxValue(getFloat());
- return;
- case APValue::FixedPoint:
- Out << getFixedPoint();
- return;
- case APValue::Vector: {
- Out << '{';
- QualType ElemTy = Ty->castAs<VectorType>()->getElementType();
- getVectorElt(0).printPretty(Out, Policy, ElemTy, Ctx);
- for (unsigned i = 1; i != getVectorLength(); ++i) {
- Out << ", ";
- getVectorElt(i).printPretty(Out, Policy, ElemTy, Ctx);
- }
- Out << '}';
- return;
- }
- case APValue::ComplexInt:
- Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
- return;
- case APValue::ComplexFloat:
- Out << GetApproxValue(getComplexFloatReal()) << "+"
- << GetApproxValue(getComplexFloatImag()) << "i";
- return;
- case APValue::LValue: {
- bool IsReference = Ty->isReferenceType();
- QualType InnerTy
- = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
- if (InnerTy.isNull())
- InnerTy = Ty;
-
- LValueBase Base = getLValueBase();
- if (!Base) {
- if (isNullPointer()) {
- Out << (Policy.Nullptr ? "nullptr" : "0");
- } else if (IsReference) {
- Out << "*(" << InnerTy.stream(Policy) << "*)"
- << getLValueOffset().getQuantity();
- } else {
- Out << "(" << Ty.stream(Policy) << ")"
- << getLValueOffset().getQuantity();
- }
- return;
- }
-
- if (!hasLValuePath()) {
- // No lvalue path: just print the offset.
- CharUnits O = getLValueOffset();
- CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).value_or(
- CharUnits::Zero())
- : CharUnits::Zero();
- if (!O.isZero()) {
- if (IsReference)
- Out << "*(";
- if (S.isZero() || O % S) {
- Out << "(char*)";
- S = CharUnits::One();
- }
- Out << '&';
- } else if (!IsReference) {
- Out << '&';
- }
-
- if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
- Out << *VD;
- else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
- TI.print(Out, Policy);
- } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
- Out << "{*new "
- << Base.getDynamicAllocType().stream(Policy) << "#"
- << DA.getIndex() << "}";
- } else {
- assert(Base.get<const Expr *>() != nullptr &&
- "Expecting non-null Expr");
- Base.get<const Expr*>()->printPretty(Out, nullptr, Policy);
- }
-
- if (!O.isZero()) {
- Out << " + " << (O / S);
- if (IsReference)
- Out << ')';
- }
- return;
- }
-
- // We have an lvalue path. Print it out nicely.
- if (!IsReference)
- Out << '&';
- else if (isLValueOnePastTheEnd())
- Out << "*(&";
-
- QualType ElemTy = Base.getType();
- if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
- Out << *VD;
- } else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
- TI.print(Out, Policy);
- } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
- Out << "{*new " << Base.getDynamicAllocType().stream(Policy) << "#"
- << DA.getIndex() << "}";
- } else {
- const Expr *E = Base.get<const Expr*>();
- assert(E != nullptr && "Expecting non-null Expr");
- E->printPretty(Out, nullptr, Policy);
- }
-
- ArrayRef<LValuePathEntry> Path = getLValuePath();
- const CXXRecordDecl *CastToBase = nullptr;
- for (unsigned I = 0, N = Path.size(); I != N; ++I) {
- if (ElemTy->isRecordType()) {
- // The lvalue refers to a class type, so the next path entry is a base
- // or member.
- const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
- if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
- CastToBase = RD;
- // Leave ElemTy referring to the most-derived class. The actual type
- // doesn't matter except for array types.
- } else {
- const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
- Out << ".";
- if (CastToBase)
- Out << *CastToBase << "::";
- Out << *VD;
- ElemTy = VD->getType();
- }
- } else if (ElemTy->isAnyComplexType()) {
- // The lvalue refers to a complex type
- Out << (Path[I].getAsArrayIndex() == 0 ? ".real" : ".imag");
- ElemTy = ElemTy->castAs<ComplexType>()->getElementType();
- } else {
- // The lvalue must refer to an array.
- Out << '[' << Path[I].getAsArrayIndex() << ']';
- ElemTy = ElemTy->castAsArrayTypeUnsafe()->getElementType();
- }
- }
-
- // Handle formatting of one-past-the-end lvalues.
- if (isLValueOnePastTheEnd()) {
- // FIXME: If CastToBase is non-0, we should prefix the output with
- // "(CastToBase*)".
- Out << " + 1";
- if (IsReference)
- Out << ')';
- }
- return;
- }
- case APValue::Array: {
- const ArrayType *AT = Ty->castAsArrayTypeUnsafe();
- unsigned N = getArrayInitializedElts();
- if (N != 0 && TryPrintAsStringLiteral(Out, Policy, AT,
- {&getArrayInitializedElt(0), N}))
- return;
- QualType ElemTy = AT->getElementType();
- Out << '{';
- unsigned I = 0;
- switch (N) {
- case 0:
- for (; I != N; ++I) {
- Out << ", ";
- if (I == 10 && !Policy.EntireContentsOfLargeArray) {
- Out << "...}";
- return;
- }
- [[fallthrough]];
- default:
- getArrayInitializedElt(I).printPretty(Out, Policy, ElemTy, Ctx);
- }
- }
- Out << '}';
- return;
- }
- case APValue::Struct: {
- Out << '{';
- const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl();
- bool First = true;
- if (unsigned N = getStructNumBases()) {
- const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
- CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
- for (unsigned I = 0; I != N; ++I, ++BI) {
- assert(BI != CD->bases_end());
- if (!First)
- Out << ", ";
- getStructBase(I).printPretty(Out, Policy, BI->getType(), Ctx);
- First = false;
- }
- }
- for (const auto *FI : RD->fields()) {
- if (!First)
- Out << ", ";
- if (FI->isUnnamedBitfield()) continue;
- getStructField(FI->getFieldIndex()).
- printPretty(Out, Policy, FI->getType(), Ctx);
- First = false;
- }
- Out << '}';
- return;
- }
- case APValue::Union:
- Out << '{';
- if (const FieldDecl *FD = getUnionField()) {
- Out << "." << *FD << " = ";
- getUnionValue().printPretty(Out, Policy, FD->getType(), Ctx);
- }
- Out << '}';
- return;
- case APValue::MemberPointer:
- // FIXME: This is not enough to unambiguously identify the member in a
- // multiple-inheritance scenario.
- if (const ValueDecl *VD = getMemberPointerDecl()) {
- Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
- return;
- }
- Out << "0";
- return;
- case APValue::AddrLabelDiff:
- Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
- Out << " - ";
- Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
- return;
- }
- llvm_unreachable("Unknown APValue kind!");
-}
-
-std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
- std::string Result;
- llvm::raw_string_ostream Out(Result);
- printPretty(Out, Ctx, Ty);
- Out.flush();
- return Result;
-}
-
-bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
- const ASTContext &Ctx) const {
- if (isInt()) {
- Result = getInt();
- return true;
- }
-
- if (isLValue() && isNullPointer()) {
- Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy);
- return true;
- }
-
- if (isLValue() && !getLValueBase()) {
- Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy);
- return true;
- }
-
- return false;
-}
-
-const APValue::LValueBase APValue::getLValueBase() const {
- assert(isLValue() && "Invalid accessor");
- return ((const LV *)(const void *)&Data)->Base;
-}
-
-bool APValue::isLValueOnePastTheEnd() const {
- assert(isLValue() && "Invalid accessor");
- return ((const LV *)(const void *)&Data)->IsOnePastTheEnd;
-}
-
-CharUnits &APValue::getLValueOffset() {
- assert(isLValue() && "Invalid accessor");
- return ((LV *)(void *)&Data)->Offset;
-}
-
-bool APValue::hasLValuePath() const {
- assert(isLValue() && "Invalid accessor");
- return ((const LV *)(const char *)&Data)->hasPath();
-}
-
-ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
- assert(isLValue() && hasLValuePath() && "Invalid accessor");
- const LV &LVal = *((const LV *)(const char *)&Data);
- return llvm::ArrayRef(LVal.getPath(), LVal.PathLength);
-}
-
-unsigned APValue::getLValueCallIndex() const {
- assert(isLValue() && "Invalid accessor");
- return ((const LV *)(const char *)&Data)->Base.getCallIndex();
-}
-
-unsigned APValue::getLValueVersion() const {
- assert(isLValue() && "Invalid accessor");
- return ((const LV *)(const char *)&Data)->Base.getVersion();
-}
-
-bool APValue::isNullPointer() const {
- assert(isLValue() && "Invalid usage");
- return ((const LV *)(const char *)&Data)->IsNullPtr;
-}
-
-void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
- bool IsNullPtr) {
- assert(isLValue() && "Invalid accessor");
- LV &LVal = *((LV *)(char *)&Data);
- LVal.Base = B;
- LVal.IsOnePastTheEnd = false;
- LVal.Offset = O;
- LVal.resizePath((unsigned)-1);
- LVal.IsNullPtr = IsNullPtr;
-}
-
-MutableArrayRef<APValue::LValuePathEntry>
-APValue::setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size,
- bool IsOnePastTheEnd, bool IsNullPtr) {
- assert(isLValue() && "Invalid accessor");
- LV &LVal = *((LV *)(char *)&Data);
- LVal.Base = B;
- LVal.IsOnePastTheEnd = IsOnePastTheEnd;
- LVal.Offset = O;
- LVal.IsNullPtr = IsNullPtr;
- LVal.resizePath(Size);
- return {LVal.getPath(), Size};
-}
-
-void APValue::setLValue(LValueBase B, const CharUnits &O,
- ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
- bool IsNullPtr) {
- MutableArrayRef<APValue::LValuePathEntry> InternalPath =
- setLValueUninit(B, O, Path.size(), IsOnePastTheEnd, IsNullPtr);
- if (Path.size()) {
- memcpy(InternalPath.data(), Path.data(),
- Path.size() * sizeof(LValuePathEntry));
- }
-}
-
-void APValue::setUnion(const FieldDecl *Field, const APValue &Value) {
- assert(isUnion() && "Invalid accessor");
- ((UnionData *)(char *)&Data)->Field =
- Field ? Field->getCanonicalDecl() : nullptr;
- *((UnionData *)(char *)&Data)->Value = Value;
-}
-
-const ValueDecl *APValue::getMemberPointerDecl() const {
- assert(isMemberPointer() && "Invalid accessor");
- const MemberPointerData &MPD =
- *((const MemberPointerData *)(const char *)&Data);
- return MPD.MemberAndIsDerivedMember.getPointer();
-}
-
-bool APValue::isMemberPointerToDerivedMember() const {
- assert(isMemberPointer() && "Invalid accessor");
- const MemberPointerData &MPD =
- *((const MemberPointerData *)(const char *)&Data);
- return MPD.MemberAndIsDerivedMember.getInt();
-}
-
-ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
- assert(isMemberPointer() && "Invalid accessor");
- const MemberPointerData &MPD =
- *((const MemberPointerData *)(const char *)&Data);
- return llvm::ArrayRef(MPD.getPath(), MPD.PathLength);
-}
-
-void APValue::MakeLValue() {
- assert(isAbsent() && "Bad state change");
- static_assert(sizeof(LV) <= DataSize, "LV too big");
- new ((void *)(char *)&Data) LV();
- Kind = LValue;
-}
-
-void APValue::MakeArray(unsigned InitElts, unsigned Size) {
- assert(isAbsent() && "Bad state change");
- new ((void *)(char *)&Data) Arr(InitElts, Size);
- Kind = Array;
-}
-
-MutableArrayRef<APValue::LValuePathEntry>
-setLValueUninit(APValue::LValueBase B, const CharUnits &O, unsigned Size,
- bool OnePastTheEnd, bool IsNullPtr);
-
-MutableArrayRef<const CXXRecordDecl *>
-APValue::setMemberPointerUninit(const ValueDecl *Member, bool IsDerivedMember,
- unsigned Size) {
- assert(isAbsent() && "Bad state change");
- MemberPointerData *MPD = new ((void *)(char *)&Data) MemberPointerData;
- Kind = MemberPointer;
- MPD->MemberAndIsDerivedMember.setPointer(
- Member ? cast<ValueDecl>(Member->getCanonicalDecl()) : nullptr);
- MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
- MPD->resizePath(Size);
- return {MPD->getPath(), MPD->PathLength};
-}
-
-void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
- ArrayRef<const CXXRecordDecl *> Path) {
- MutableArrayRef<const CXXRecordDecl *> InternalPath =
- setMemberPointerUninit(Member, IsDerivedMember, Path.size());
- for (unsigned I = 0; I != Path.size(); ++I)
- InternalPath[I] = Path[I]->getCanonicalDecl();
-}
-
-LinkageInfo LinkageComputer::getLVForValue(const APValue &V,
- LVComputationKind computation) {
- LinkageInfo LV = LinkageInfo::external();
-
- auto MergeLV = [&](LinkageInfo MergeLV) {
- LV.merge(MergeLV);
- return LV.getLinkage() == Linkage::Internal;
- };
- auto Merge = [&](const APValue &V) {
- return MergeLV(getLVForValue(V, computation));
- };
-
- switch (V.getKind()) {
- case APValue::None:
- case APValue::Indeterminate:
- case APValue::Int:
- case APValue::Float:
- case APValue::FixedPoint:
- case APValue::ComplexInt:
- case APValue::ComplexFloat:
- case APValue::Vector:
- break;
-
- case APValue::AddrLabelDiff:
- // Even for an inline function, it's not reasonable to treat a difference
- // between the addresses of labels as an external value.
- return LinkageInfo::internal();
-
- case APValue::Struct: {
- for (unsigned I = 0, N = V.getStructNumBases(); I != N; ++I)
- if (Merge(V.getStructBase(I)))
- break;
- for (unsigned I = 0, N = V.getStructNumFields(); I != N; ++I)
- if (Merge(V.getStructField(I)))
- break;
- break;
- }
-
- case APValue::Union:
- if (V.getUnionField())
- Merge(V.getUnionValue());
- break;
-
- case APValue::Array: {
- for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; ++I)
- if (Merge(V.getArrayInitializedElt(I)))
- break;
- if (V.hasArrayFiller())
- Merge(V.getArrayFiller());
- break;
- }
-
- case APValue::LValue: {
- if (!V.getLValueBase()) {
- // Null or absolute address: this is external.
- } else if (const auto *VD =
- V.getLValueBase().dyn_cast<const ValueDecl *>()) {
- if (VD && MergeLV(getLVForDecl(VD, computation)))
- break;
- } else if (const auto TI = V.getLValueBase().dyn_cast<TypeInfoLValue>()) {
- if (MergeLV(getLVForType(*TI.getType(), computation)))
- break;
- } else if (const Expr *E = V.getLValueBase().dyn_cast<const Expr *>()) {
- // Almost all expression bases are internal. The exception is
- // lifetime-extended temporaries.
- // FIXME: These should be modeled as having the
- // LifetimeExtendedTemporaryDecl itself as the base.
- // FIXME: If we permit Objective-C object literals in template arguments,
- // they should not imply internal linkage.
- auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
- if (!MTE || MTE->getStorageDuration() == SD_FullExpression)
- return LinkageInfo::internal();
- if (MergeLV(getLVForDecl(MTE->getExtendingDecl(), computation)))
- break;
- } else {
- assert(V.getLValueBase().is<DynamicAllocLValue>() &&
- "unexpected LValueBase kind");
- return LinkageInfo::internal();
- }
- // The lvalue path doesn't matter: pointers to all subobjects always have
- // the same visibility as pointers to the complete object.
- break;
- }
-
- case APValue::MemberPointer:
- if (const NamedDecl *D = V.getMemberPointerDecl())
- MergeLV(getLVForDecl(D, computation));
- // Note that we could have a base-to-derived conversion here to a member of
- // a derived class with less linkage/visibility. That's covered by the
- // linkage and visibility of the value's type.
- break;
- }
-
- return LV;
-}
+//===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the APValue class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/APValue.h"
+#include "Linkage.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/CharUnits.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/Type.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace clang;
+
+
+enum Linkage {
+ Invalid = 0,
+ External = 1,
+ Internal = 2,
+};
+
+/// The identity of a type_info object depends on the canonical unqualified
+/// type only.
+TypeInfoLValue::TypeInfoLValue(const Type *T)
+ : T(T->getCanonicalTypeUnqualified().getTypePtr()) {}
+
+void TypeInfoLValue::print(llvm::raw_ostream &Out,
+ const PrintingPolicy &Policy) const {
+ Out << "typeid(";
+ QualType(getType(), 0).print(Out, Policy);
+ Out << ")";
+}
+
+static_assert(
+ 1 << llvm::PointerLikeTypeTraits<TypeInfoLValue>::NumLowBitsAvailable <=
+ alignof(Type),
+ "Type is insufficiently aligned");
+
+APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
+ : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()) : nullptr), Local{I, V} {}
+APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
+ : Ptr(P), Local{I, V} {}
+
+APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV,
+ QualType Type) {
+ LValueBase Base;
+ Base.Ptr = LV;
+ Base.DynamicAllocType = Type.getAsOpaquePtr();
+ return Base;
+}
+
+APValue::LValueBase APValue::LValueBase::getTypeInfo(TypeInfoLValue LV,
+ QualType TypeInfo) {
+ LValueBase Base;
+ Base.Ptr = LV;
+ Base.TypeInfoType = TypeInfo.getAsOpaquePtr();
+ return Base;
+}
+
+QualType APValue::LValueBase::getType() const {
+ if (!*this) return QualType();
+ if (const ValueDecl *D = dyn_cast<const ValueDecl*>()) {
+ // FIXME: It's unclear where we're supposed to take the type from, and
+ // this actually matters for arrays of unknown bound. Eg:
+ //
+ // extern int arr[]; void f() { extern int arr[3]; };
+ // constexpr int *p = &arr[1]; // valid?
+ //
+ // For now, we take the most complete type we can find.
+ for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
+ Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) {
+ QualType T = Redecl->getType();
+ if (!T->isIncompleteArrayType())
+ return T;
+ }
+ return D->getType();
+ }
+
+ if (is<TypeInfoLValue>())
+ return getTypeInfoType();
+
+ if (is<DynamicAllocLValue>())
+ return getDynamicAllocType();
+
+ const Expr *Base = get<const Expr*>();
+
+ // For a materialized temporary, the type of the temporary we materialized
+ // may not be the type of the expression.
+ if (const MaterializeTemporaryExpr *MTE =
+ clang::dyn_cast<MaterializeTemporaryExpr>(Base)) {
+ SmallVector<const Expr *, 2> CommaLHSs;
+ SmallVector<SubobjectAdjustment, 2> Adjustments;
+ const Expr *Temp = MTE->getSubExpr();
+ const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs,
+ Adjustments);
+ // Keep any cv-qualifiers from the reference if we generated a temporary
+ // for it directly. Otherwise use the type after adjustment.
+ if (!Adjustments.empty())
+ return Inner->getType();
+ }
+
+ return Base->getType();
+}
+
+unsigned APValue::LValueBase::getCallIndex() const {
+ return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0
+ : Local.CallIndex;
+}
+
+unsigned APValue::LValueBase::getVersion() const {
+ return (is<TypeInfoLValue>() || is<DynamicAllocLValue>()) ? 0 : Local.Version;
+}
+
+QualType APValue::LValueBase::getTypeInfoType() const {
+ assert(is<TypeInfoLValue>() && "not a type_info lvalue");
+ return QualType::getFromOpaquePtr(TypeInfoType);
+}
+
+QualType APValue::LValueBase::getDynamicAllocType() const {
+ assert(is<DynamicAllocLValue>() && "not a dynamic allocation lvalue");
+ return QualType::getFromOpaquePtr(DynamicAllocType);
+}
+
+void APValue::LValueBase::Profile(llvm::FoldingSetNodeID &ID) const {
+ ID.AddPointer(Ptr.getOpaqueValue());
+ if (is<TypeInfoLValue>() || is<DynamicAllocLValue>())
+ return;
+ ID.AddInteger(Local.CallIndex);
+ ID.AddInteger(Local.Version);
+}
+
+namespace clang {
+bool operator==(const APValue::LValueBase &LHS,
+ const APValue::LValueBase &RHS) {
+ if (LHS.Ptr != RHS.Ptr)
+ return false;
+ if (LHS.is<TypeInfoLValue>() || LHS.is<DynamicAllocLValue>())
+ return true;
+ return LHS.Local.CallIndex == RHS.Local.CallIndex &&
+ LHS.Local.Version == RHS.Local.Version;
+}
+}
+APValue::LValuePathEntry::LValuePathEntry(BaseOrMemberType BaseOrMember) {
+ if (const Decl *D = BaseOrMember.getPointer())
+ BaseOrMember.setPointer(D->getCanonicalDecl());
+ Value = reinterpret_cast<uintptr_t>(BaseOrMember.getOpaqueValue());
+}
+
+void APValue::LValuePathEntry::Profile(llvm::FoldingSetNodeID &ID) const {
+ ID.AddInteger(Value);
+}
+
+APValue::LValuePathSerializationHelper::LValuePathSerializationHelper(
+ ArrayRef<LValuePathEntry> Path, QualType ElemTy)
+ : Ty((const void *)ElemTy.getTypePtrOrNull()), Path(Path) {}
+
+QualType APValue::LValuePathSerializationHelper::getType() {
+ return QualType::getFromOpaquePtr(Ty);
+}
+
+namespace {
+ struct LVBase {
+ APValue::LValueBase Base;
+ CharUnits Offset;
+ unsigned PathLength;
+ bool IsNullPtr : 1;
+ bool IsOnePastTheEnd : 1;
+ };
+}
+
+void *APValue::LValueBase::getOpaqueValue() const {
+ return Ptr.getOpaqueValue();
+}
+
+bool APValue::LValueBase::isNull() const {
+ return Ptr.isNull();
+}
+
+APValue::LValueBase::operator bool () const {
+ return static_cast<bool>(Ptr);
+}
+
+clang::APValue::LValueBase
+llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
+ clang::APValue::LValueBase B;
+ B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
+ return B;
+}
+
+clang::APValue::LValueBase
+llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
+ clang::APValue::LValueBase B;
+ B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
+ return B;
+}
+
+namespace clang {
+llvm::hash_code hash_value(const APValue::LValueBase &Base) {
+ if (Base.is<TypeInfoLValue>() || Base.is<DynamicAllocLValue>())
+ return llvm::hash_value(Base.getOpaqueValue());
+ return llvm::hash_combine(Base.getOpaqueValue(), Base.getCallIndex(),
+ Base.getVersion());
+}
+}
+
+unsigned llvm::DenseMapInfo<clang::APValue::LValueBase>::getHashValue(
+ const clang::APValue::LValueBase &Base) {
+ return hash_value(Base);
+}
+
+bool llvm::DenseMapInfo<clang::APValue::LValueBase>::isEqual(
+ const clang::APValue::LValueBase &LHS,
+ const clang::APValue::LValueBase &RHS) {
+ return LHS == RHS;
+}
+
+struct APValue::LV : LVBase {
+ static const unsigned InlinePathSpace =
+ (DataSize - sizeof(LVBase)) / sizeof(LValuePathEntry);
+
+ /// Path - The sequence of base classes, fields and array indices to follow to
+ /// walk from Base to the subobject. When performing GCC-style folding, there
+ /// may not be such a path.
+ union {
+ LValuePathEntry Path[InlinePathSpace];
+ LValuePathEntry *PathPtr;
+ };
+
+ LV() { PathLength = (unsigned)-1; }
+ ~LV() { resizePath(0); }
+
+ void resizePath(unsigned Length) {
+ if (Length == PathLength)
+ return;
+ if (hasPathPtr())
+ delete [] PathPtr;
+ PathLength = Length;
+ if (hasPathPtr())
+ PathPtr = new LValuePathEntry[Length];
+ }
+
+ bool hasPath() const { return PathLength != (unsigned)-1; }
+ bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; }
+
+ LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; }
+ const LValuePathEntry *getPath() const {
+ return hasPathPtr() ? PathPtr : Path;
+ }
+};
+
+namespace {
+ struct MemberPointerBase {
+ llvm::PointerIntPair<const ValueDecl*, 1, bool> MemberAndIsDerivedMember;
+ unsigned PathLength;
+ };
+}
+
+struct APValue::MemberPointerData : MemberPointerBase {
+ static const unsigned InlinePathSpace =
+ (DataSize - sizeof(MemberPointerBase)) / sizeof(const CXXRecordDecl*);
+ typedef const CXXRecordDecl *PathElem;
+ union {
+ PathElem Path[InlinePathSpace];
+ PathElem *PathPtr;
+ };
+
+ MemberPointerData() { PathLength = 0; }
+ ~MemberPointerData() { resizePath(0); }
+
+ void resizePath(unsigned Length) {
+ if (Length == PathLength)
+ return;
+ if (hasPathPtr())
+ delete [] PathPtr;
+ PathLength = Length;
+ if (hasPathPtr())
+ PathPtr = new PathElem[Length];
+ }
+
+ bool hasPathPtr() const { return PathLength > InlinePathSpace; }
+
+ PathElem *getPath() { return hasPathPtr() ? PathPtr : Path; }
+ const PathElem *getPath() const {
+ return hasPathPtr() ? PathPtr : Path;
+ }
+};
+
+// FIXME: Reduce the malloc traffic here.
+
+APValue::Arr::Arr(unsigned NumElts, unsigned Size) :
+ Elts(new APValue[NumElts + (NumElts != Size ? 1 : 0)]),
+ NumElts(NumElts), ArrSize(Size) {}
+APValue::Arr::~Arr() { delete [] Elts; }
+
+APValue::StructData::StructData(unsigned NumBases, unsigned NumFields) :
+ Elts(new APValue[NumBases+NumFields]),
+ NumBases(NumBases), NumFields(NumFields) {}
+APValue::StructData::~StructData() {
+ delete [] Elts;
+}
+
+APValue::UnionData::UnionData() : Field(nullptr), Value(new APValue) {}
+APValue::UnionData::~UnionData () {
+ delete Value;
+}
+
+APValue::APValue(const APValue &RHS) : Kind(None) {
+ switch (RHS.getKind()) {
+ case None:
+ case Indeterminate:
+ Kind = RHS.getKind();
+ break;
+ case Int:
+ MakeInt();
+ setInt(RHS.getInt());
+ break;
+ case Float:
+ MakeFloat();
+ setFloat(RHS.getFloat());
+ break;
+ case FixedPoint: {
+ APFixedPoint FXCopy = RHS.getFixedPoint();
+ MakeFixedPoint(std::move(FXCopy));
+ break;
+ }
+ case Vector:
+ MakeVector();
+ setVector(((const Vec *)(const char *)&RHS.Data)->Elts,
+ RHS.getVectorLength());
+ break;
+ case ComplexInt:
+ MakeComplexInt();
+ setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag());
+ break;
+ case ComplexFloat:
+ MakeComplexFloat();
+ setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag());
+ break;
+ case LValue:
+ MakeLValue();
+ if (RHS.hasLValuePath())
+ setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), RHS.getLValuePath(),
+ RHS.isLValueOnePastTheEnd(), RHS.isNullPointer());
+ else
+ setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath(),
+ RHS.isNullPointer());
+ break;
+ case Array:
+ MakeArray(RHS.getArrayInitializedElts(), RHS.getArraySize());
+ for (unsigned I = 0, N = RHS.getArrayInitializedElts(); I != N; ++I)
+ getArrayInitializedElt(I) = RHS.getArrayInitializedElt(I);
+ if (RHS.hasArrayFiller())
+ getArrayFiller() = RHS.getArrayFiller();
+ break;
+ case Struct:
+ MakeStruct(RHS.getStructNumBases(), RHS.getStructNumFields());
+ for (unsigned I = 0, N = RHS.getStructNumBases(); I != N; ++I)
+ getStructBase(I) = RHS.getStructBase(I);
+ for (unsigned I = 0, N = RHS.getStructNumFields(); I != N; ++I)
+ getStructField(I) = RHS.getStructField(I);
+ break;
+ case Union:
+ MakeUnion();
+ setUnion(RHS.getUnionField(), RHS.getUnionValue());
+ break;
+ case MemberPointer:
+ MakeMemberPointer(RHS.getMemberPointerDecl(),
+ RHS.isMemberPointerToDerivedMember(),
+ RHS.getMemberPointerPath());
+ break;
+ case AddrLabelDiff:
+ MakeAddrLabelDiff();
+ setAddrLabelDiff(RHS.getAddrLabelDiffLHS(), RHS.getAddrLabelDiffRHS());
+ break;
+ }
+}
+
+APValue::APValue(APValue &&RHS) : Kind(RHS.Kind), Data(RHS.Data) {
+ RHS.Kind = None;
+}
+
+APValue &APValue::operator=(const APValue &RHS) {
+ if (this != &RHS)
+ *this = APValue(RHS);
+ return *this;
+}
+
+APValue &APValue::operator=(APValue &&RHS) {
+ if (this != &RHS) {
+ if (Kind != None && Kind != Indeterminate)
+ DestroyDataAndMakeUninit();
+ Kind = RHS.Kind;
+ Data = RHS.Data;
+ RHS.Kind = None;
+ }
+ return *this;
+}
+
+void APValue::DestroyDataAndMakeUninit() {
+ if (Kind == Int)
+ ((APSInt *)(char *)&Data)->~APSInt();
+ else if (Kind == Float)
+ ((APFloat *)(char *)&Data)->~APFloat();
+ else if (Kind == FixedPoint)
+ ((APFixedPoint *)(char *)&Data)->~APFixedPoint();
+ else if (Kind == Vector)
+ ((Vec *)(char *)&Data)->~Vec();
+ else if (Kind == ComplexInt)
+ ((ComplexAPSInt *)(char *)&Data)->~ComplexAPSInt();
+ else if (Kind == ComplexFloat)
+ ((ComplexAPFloat *)(char *)&Data)->~ComplexAPFloat();
+ else if (Kind == LValue)
+ ((LV *)(char *)&Data)->~LV();
+ else if (Kind == Array)
+ ((Arr *)(char *)&Data)->~Arr();
+ else if (Kind == Struct)
+ ((StructData *)(char *)&Data)->~StructData();
+ else if (Kind == Union)
+ ((UnionData *)(char *)&Data)->~UnionData();
+ else if (Kind == MemberPointer)
+ ((MemberPointerData *)(char *)&Data)->~MemberPointerData();
+ else if (Kind == AddrLabelDiff)
+ ((AddrLabelDiffData *)(char *)&Data)->~AddrLabelDiffData();
+ Kind = None;
+}
+
+bool APValue::needsCleanup() const {
+ switch (getKind()) {
+ case None:
+ case Indeterminate:
+ case AddrLabelDiff:
+ return false;
+ case Struct:
+ case Union:
+ case Array:
+ case Vector:
+ return true;
+ case Int:
+ return getInt().needsCleanup();
+ case Float:
+ return getFloat().needsCleanup();
+ case FixedPoint:
+ return getFixedPoint().getValue().needsCleanup();
+ case ComplexFloat:
+ assert(getComplexFloatImag().needsCleanup() ==
+ getComplexFloatReal().needsCleanup() &&
+ "In _Complex float types, real and imaginary values always have the "
+ "same size.");
+ return getComplexFloatReal().needsCleanup();
+ case ComplexInt:
+ assert(getComplexIntImag().needsCleanup() ==
+ getComplexIntReal().needsCleanup() &&
+ "In _Complex int types, real and imaginary values must have the "
+ "same size.");
+ return getComplexIntReal().needsCleanup();
+ case LValue:
+ return reinterpret_cast<const LV *>(&Data)->hasPathPtr();
+ case MemberPointer:
+ return reinterpret_cast<const MemberPointerData *>(&Data)->hasPathPtr();
+ }
+ llvm_unreachable("Unknown APValue kind!");
+}
+
+void APValue::swap(APValue &RHS) {
+ std::swap(Kind, RHS.Kind);
+ std::swap(Data, RHS.Data);
+}
+
+/// Profile the value of an APInt, excluding its bit-width.
+static void profileIntValue(llvm::FoldingSetNodeID &ID, const llvm::APInt &V) {
+ for (unsigned I = 0, N = V.getBitWidth(); I < N; I += 32)
+ ID.AddInteger((uint32_t)V.extractBitsAsZExtValue(std::min(32u, N - I), I));
+}
+
+void APValue::Profile(llvm::FoldingSetNodeID &ID) const {
+ // Note that our profiling assumes that only APValues of the same type are
+ // ever compared. As a result, we don't consider collisions that could only
+ // happen if the types are different. (For example, structs with different
+ // numbers of members could profile the same.)
+
+ ID.AddInteger(Kind);
+
+ switch (Kind) {
+ case None:
+ case Indeterminate:
+ return;
+
+ case AddrLabelDiff:
+ ID.AddPointer(getAddrLabelDiffLHS()->getLabel()->getCanonicalDecl());
+ ID.AddPointer(getAddrLabelDiffRHS()->getLabel()->getCanonicalDecl());
+ return;
+
+ case Struct:
+ for (unsigned I = 0, N = getStructNumBases(); I != N; ++I)
+ getStructBase(I).Profile(ID);
+ for (unsigned I = 0, N = getStructNumFields(); I != N; ++I)
+ getStructField(I).Profile(ID);
+ return;
+
+ case Union:
+ if (!getUnionField()) {
+ ID.AddInteger(0);
+ return;
+ }
+ ID.AddInteger(getUnionField()->getFieldIndex() + 1);
+ getUnionValue().Profile(ID);
+ return;
+
+ case Array: {
+ if (getArraySize() == 0)
+ return;
+
+ // The profile should not depend on whether the array is expanded or
+ // not, but we don't want to profile the array filler many times for
+ // a large array. So treat all equal trailing elements as the filler.
+ // Elements are profiled in reverse order to support this, and the
+ // first profiled element is followed by a count. For example:
+ //
+ // ['a', 'c', 'x', 'x', 'x'] is profiled as
+ // [5, 'x', 3, 'c', 'a']
+ llvm::FoldingSetNodeID FillerID;
+ (hasArrayFiller() ? getArrayFiller()
+ : getArrayInitializedElt(getArrayInitializedElts() - 1))
+ .Profile(FillerID);
+ ID.AddNodeID(FillerID);
+ unsigned NumFillers = getArraySize() - getArrayInitializedElts();
+ unsigned N = getArrayInitializedElts();
+
+ // Count the number of elements equal to the last one. This loop ends
+ // by adding an integer indicating the number of such elements, with
+ // N set to the number of elements left to profile.
+ while (true) {
+ if (N == 0) {
+ // All elements are fillers.
+ assert(NumFillers == getArraySize());
+ ID.AddInteger(NumFillers);
+ break;
+ }
+
+ // No need to check if the last element is equal to the last
+ // element.
+ if (N != getArraySize()) {
+ llvm::FoldingSetNodeID ElemID;
+ getArrayInitializedElt(N - 1).Profile(ElemID);
+ if (ElemID != FillerID) {
+ ID.AddInteger(NumFillers);
+ ID.AddNodeID(ElemID);
+ --N;
+ break;
+ }
+ }
+
+ // This is a filler.
+ ++NumFillers;
+ --N;
+ }
+
+ // Emit the remaining elements.
+ for (; N != 0; --N)
+ getArrayInitializedElt(N - 1).Profile(ID);
+ return;
+ }
+
+ case Vector:
+ for (unsigned I = 0, N = getVectorLength(); I != N; ++I)
+ getVectorElt(I).Profile(ID);
+ return;
+
+ case Int:
+ profileIntValue(ID, getInt());
+ return;
+
+ case Float:
+ profileIntValue(ID, getFloat().bitcastToAPInt());
+ return;
+
+ case FixedPoint:
+ profileIntValue(ID, getFixedPoint().getValue());
+ return;
+
+ case ComplexFloat:
+ profileIntValue(ID, getComplexFloatReal().bitcastToAPInt());
+ profileIntValue(ID, getComplexFloatImag().bitcastToAPInt());
+ return;
+
+ case ComplexInt:
+ profileIntValue(ID, getComplexIntReal());
+ profileIntValue(ID, getComplexIntImag());
+ return;
+
+ case LValue:
+ getLValueBase().Profile(ID);
+ ID.AddInteger(getLValueOffset().getQuantity());
+ ID.AddInteger((isNullPointer() ? 1 : 0) |
+ (isLValueOnePastTheEnd() ? 2 : 0) |
+ (hasLValuePath() ? 4 : 0));
+ if (hasLValuePath()) {
+ ID.AddInteger(getLValuePath().size());
+ // For uniqueness, we only need to profile the entries corresponding
+ // to union members, but we don't have the type here so we don't know
+ // how to interpret the entries.
+ for (LValuePathEntry E : getLValuePath())
+ E.Profile(ID);
+ }
+ return;
+
+ case MemberPointer:
+ ID.AddPointer(getMemberPointerDecl());
+ ID.AddInteger(isMemberPointerToDerivedMember());
+ for (const CXXRecordDecl *D : getMemberPointerPath())
+ ID.AddPointer(D);
+ return;
+ }
+
+ llvm_unreachable("Unknown APValue kind!");
+}
+
+static double GetApproxValue(const llvm::APFloat &F) {
+ llvm::APFloat V = F;
+ bool ignored;
+ V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
+ &ignored);
+ return V.convertToDouble();
+}
+
+static bool TryPrintAsStringLiteral(raw_ostream &Out,
+ const PrintingPolicy &Policy,
+ const ArrayType *ATy,
+ ArrayRef<APValue> Inits) {
+ if (Inits.empty())
+ return false;
+
+ QualType Ty = ATy->getElementType();
+ if (!Ty->isAnyCharacterType())
+ return false;
+
+ // Nothing we can do about a sequence that is not null-terminated
+ if (!Inits.back().isInt() || !Inits.back().getInt().isZero())
+ return false;
+
+ Inits = Inits.drop_back();
+
+ llvm::SmallString<40> Buf;
+ Buf.push_back('"');
+
+ // Better than printing a two-digit sequence of 10 integers.
+ constexpr size_t MaxN = 36;
+ StringRef Ellipsis;
+ if (Inits.size() > MaxN && !Policy.EntireContentsOfLargeArray) {
+ Ellipsis = "[...]";
+ Inits =
+ Inits.take_front(std::min(MaxN - Ellipsis.size() / 2, Inits.size()));
+ }
+
+ for (auto &Val : Inits) {
+ if (!Val.isInt())
+ return false;
+ int64_t Char64 = Val.getInt().getExtValue();
+ if (!isASCII(Char64))
+ return false; // Bye bye, see you in integers.
+ auto Ch = static_cast<unsigned char>(Char64);
+ // The diagnostic message is 'quoted'
+ StringRef Escaped = escapeCStyle<EscapeChar::SingleAndDouble>(Ch);
+ if (Escaped.empty()) {
+ if (!isPrintable(Ch))
+ return false;
+ Buf.emplace_back(Ch);
+ } else {
+ Buf.append(Escaped);
+ }
+ }
+
+ Buf.append(Ellipsis);
+ Buf.push_back('"');
+
+ if (Ty->isWideCharType())
+ Out << 'L';
+ else if (Ty->isChar8Type())
+ Out << "u8";
+ else if (Ty->isChar16Type())
+ Out << 'u';
+ else if (Ty->isChar32Type())
+ Out << 'U';
+
+ Out << Buf;
+ return true;
+}
+
+void APValue::printPretty(raw_ostream &Out, const ASTContext &Ctx,
+ QualType Ty) const {
+ printPretty(Out, Ctx.getPrintingPolicy(), Ty, &Ctx);
+}
+
+void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
+ QualType Ty, const ASTContext *Ctx) const {
+ // There are no objects of type 'void', but values of this type can be
+ // returned from functions.
+ if (Ty->isVoidType()) {
+ Out << "void()";
+ return;
+ }
+
+ switch (getKind()) {
+ case APValue::None:
+ Out << "<out of lifetime>";
+ return;
+ case APValue::Indeterminate:
+ Out << "<uninitialized>";
+ return;
+ case APValue::Int:
+ if (Ty->isBooleanType())
+ Out << (getInt().getBoolValue() ? "true" : "false");
+ else
+ Out << getInt();
+ return;
+ case APValue::Float:
+ Out << GetApproxValue(getFloat());
+ return;
+ case APValue::FixedPoint:
+ Out << getFixedPoint();
+ return;
+ case APValue::Vector: {
+ Out << '{';
+ QualType ElemTy = Ty->castAs<VectorType>()->getElementType();
+ getVectorElt(0).printPretty(Out, Policy, ElemTy, Ctx);
+ for (unsigned i = 1; i != getVectorLength(); ++i) {
+ Out << ", ";
+ getVectorElt(i).printPretty(Out, Policy, ElemTy, Ctx);
+ }
+ Out << '}';
+ return;
+ }
+ case APValue::ComplexInt:
+ Out << getComplexIntReal() << "+" << getComplexIntImag() << "i";
+ return;
+ case APValue::ComplexFloat:
+ Out << GetApproxValue(getComplexFloatReal()) << "+"
+ << GetApproxValue(getComplexFloatImag()) << "i";
+ return;
+ case APValue::LValue: {
+ bool IsReference = Ty->isReferenceType();
+ QualType InnerTy
+ = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType();
+ if (InnerTy.isNull())
+ InnerTy = Ty;
+
+ LValueBase Base = getLValueBase();
+ if (!Base) {
+ if (isNullPointer()) {
+ Out << (Policy.Nullptr ? "nullptr" : "0");
+ } else if (IsReference) {
+ Out << "*(" << InnerTy.stream(Policy) << "*)"
+ << getLValueOffset().getQuantity();
+ } else {
+ Out << "(" << Ty.stream(Policy) << ")"
+ << getLValueOffset().getQuantity();
+ }
+ return;
+ }
+
+ if (!hasLValuePath()) {
+ // No lvalue path: just print the offset.
+ CharUnits O = getLValueOffset();
+ CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).value_or(
+ CharUnits::Zero())
+ : CharUnits::Zero();
+ if (!O.isZero()) {
+ if (IsReference)
+ Out << "*(";
+ if (S.isZero() || O % S) {
+ Out << "(char*)";
+ S = CharUnits::One();
+ }
+ Out << '&';
+ } else if (!IsReference) {
+ Out << '&';
+ }
+
+ if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
+ Out << *VD;
+ else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
+ TI.print(Out, Policy);
+ } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
+ Out << "{*new "
+ << Base.getDynamicAllocType().stream(Policy) << "#"
+ << DA.getIndex() << "}";
+ } else {
+ assert(Base.get<const Expr *>() != nullptr &&
+ "Expecting non-null Expr");
+ Base.get<const Expr*>()->printPretty(Out, nullptr, Policy);
+ }
+
+ if (!O.isZero()) {
+ Out << " + " << (O / S);
+ if (IsReference)
+ Out << ')';
+ }
+ return;
+ }
+
+ // We have an lvalue path. Print it out nicely.
+ if (!IsReference)
+ Out << '&';
+ else if (isLValueOnePastTheEnd())
+ Out << "*(&";
+
+ QualType ElemTy = Base.getType();
+ if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) {
+ Out << *VD;
+ } else if (TypeInfoLValue TI = Base.dyn_cast<TypeInfoLValue>()) {
+ TI.print(Out, Policy);
+ } else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
+ Out << "{*new " << Base.getDynamicAllocType().stream(Policy) << "#"
+ << DA.getIndex() << "}";
+ } else {
+ const Expr *E = Base.get<const Expr*>();
+ assert(E != nullptr && "Expecting non-null Expr");
+ E->printPretty(Out, nullptr, Policy);
+ }
+
+ ArrayRef<LValuePathEntry> Path = getLValuePath();
+ const CXXRecordDecl *CastToBase = nullptr;
+ for (unsigned I = 0, N = Path.size(); I != N; ++I) {
+ if (ElemTy->isRecordType()) {
+ // The lvalue refers to a class type, so the next path entry is a base
+ // or member.
+ const Decl *BaseOrMember = Path[I].getAsBaseOrMember().getPointer();
+ if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(BaseOrMember)) {
+ CastToBase = RD;
+ // Leave ElemTy referring to the most-derived class. The actual type
+ // doesn't matter except for array types.
+ } else {
+ const ValueDecl *VD = cast<ValueDecl>(BaseOrMember);
+ Out << ".";
+ if (CastToBase)
+ Out << *CastToBase << "::";
+ Out << *VD;
+ ElemTy = VD->getType();
+ }
+ } else if (ElemTy->isAnyComplexType()) {
+ // The lvalue refers to a complex type
+ Out << (Path[I].getAsArrayIndex() == 0 ? ".real" : ".imag");
+ ElemTy = ElemTy->castAs<ComplexType>()->getElementType();
+ } else {
+ // The lvalue must refer to an array.
+ Out << '[' << Path[I].getAsArrayIndex() << ']';
+ ElemTy = ElemTy->castAsArrayTypeUnsafe()->getElementType();
+ }
+ }
+
+ // Handle formatting of one-past-the-end lvalues.
+ if (isLValueOnePastTheEnd()) {
+ // FIXME: If CastToBase is non-0, we should prefix the output with
+ // "(CastToBase*)".
+ Out << " + 1";
+ if (IsReference)
+ Out << ')';
+ }
+ return;
+ }
+ case APValue::Array: {
+ const ArrayType *AT = Ty->castAsArrayTypeUnsafe();
+ unsigned N = getArrayInitializedElts();
+ if (N != 0 && TryPrintAsStringLiteral(Out, Policy, AT,
+ {&getArrayInitializedElt(0), N}))
+ return;
+ QualType ElemTy = AT->getElementType();
+ Out << '{';
+ unsigned I = 0;
+ switch (N) {
+ case 0:
+ for (; I != N; ++I) {
+ Out << ", ";
+ if (I == 10 && !Policy.EntireContentsOfLargeArray) {
+ Out << "...}";
+ return;
+ }
+ [[fallthrough]];
+ default:
+ getArrayInitializedElt(I).printPretty(Out, Policy, ElemTy, Ctx);
+ }
+ }
+ Out << '}';
+ return;
+ }
+ case APValue::Struct: {
+ Out << '{';
+ const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl();
+ bool First = true;
+ if (unsigned N = getStructNumBases()) {
+ const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
+ CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
+ for (unsigned I = 0; I != N; ++I, ++BI) {
+ assert(BI != CD->bases_end());
+ if (!First)
+ Out << ", ";
+ getStructBase(I).printPretty(Out, Policy, BI->getType(), Ctx);
+ First = false;
+ }
+ }
+ for (const auto *FI : RD->fields()) {
+ if (!First)
+ Out << ", ";
+ if (FI->isUnnamedBitfield()) continue;
+ getStructField(FI->getFieldIndex()).
+ printPretty(Out, Policy, FI->getType(), Ctx);
+ First = false;
+ }
+ Out << '}';
+ return;
+ }
+ case APValue::Union:
+ Out << '{';
+ if (const FieldDecl *FD = getUnionField()) {
+ Out << "." << *FD << " = ";
+ getUnionValue().printPretty(Out, Policy, FD->getType(), Ctx);
+ }
+ Out << '}';
+ return;
+ case APValue::MemberPointer:
+ // FIXME: This is not enough to unambiguously identify the member in a
+ // multiple-inheritance scenario.
+ if (const ValueDecl *VD = getMemberPointerDecl()) {
+ Out << '&' << *cast<CXXRecordDecl>(VD->getDeclContext()) << "::" << *VD;
+ return;
+ }
+ Out << "0";
+ return;
+ case APValue::AddrLabelDiff:
+ Out << "&&" << getAddrLabelDiffLHS()->getLabel()->getName();
+ Out << " - ";
+ Out << "&&" << getAddrLabelDiffRHS()->getLabel()->getName();
+ return;
+ }
+ llvm_unreachable("Unknown APValue kind!");
+}
+
+std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
+ std::string Result;
+ llvm::raw_string_ostream Out(Result);
+ printPretty(Out, Ctx, Ty);
+ Out.flush();
+ return Result;
+}
+
+bool APValue::toIntegralConstant(APSInt &Result, QualType SrcTy,
+ const ASTContext &Ctx) const {
+ if (isInt()) {
+ Result = getInt();
+ return true;
+ }
+
+ if (isLValue() && isNullPointer()) {
+ Result = Ctx.MakeIntValue(Ctx.getTargetNullPointerValue(SrcTy), SrcTy);
+ return true;
+ }
+
+ if (isLValue() && !getLValueBase()) {
+ Result = Ctx.MakeIntValue(getLValueOffset().getQuantity(), SrcTy);
+ return true;
+ }
+
+ return false;
+}
+
+const APValue::LValueBase APValue::getLValueBase() const {
+ assert(isLValue() && "Invalid accessor");
+ return ((const LV *)(const void *)&Data)->Base;
+}
+
+bool APValue::isLValueOnePastTheEnd() const {
+ assert(isLValue() && "Invalid accessor");
+ return ((const LV *)(const void *)&Data)->IsOnePastTheEnd;
+}
+
+CharUnits &APValue::getLValueOffset() {
+ assert(isLValue() && "Invalid accessor");
+ return ((LV *)(void *)&Data)->Offset;
+}
+
+bool APValue::hasLValuePath() const {
+ assert(isLValue() && "Invalid accessor");
+ return ((const LV *)(const char *)&Data)->hasPath();
+}
+
+ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const {
+ assert(isLValue() && hasLValuePath() && "Invalid accessor");
+ const LV &LVal = *((const LV *)(const char *)&Data);
+ return llvm::ArrayRef(LVal.getPath(), LVal.PathLength);
+}
+
+unsigned APValue::getLValueCallIndex() const {
+ assert(isLValue() && "Invalid accessor");
+ return ((const LV *)(const char *)&Data)->Base.getCallIndex();
+}
+
+unsigned APValue::getLValueVersion() const {
+ assert(isLValue() && "Invalid accessor");
+ return ((const LV *)(const char *)&Data)->Base.getVersion();
+}
+
+bool APValue::isNullPointer() const {
+ assert(isLValue() && "Invalid usage");
+ return ((const LV *)(const char *)&Data)->IsNullPtr;
+}
+
+void APValue::setLValue(LValueBase B, const CharUnits &O, NoLValuePath,
+ bool IsNullPtr) {
+ assert(isLValue() && "Invalid accessor");
+ LV &LVal = *((LV *)(char *)&Data);
+ LVal.Base = B;
+ LVal.IsOnePastTheEnd = false;
+ LVal.Offset = O;
+ LVal.resizePath((unsigned)-1);
+ LVal.IsNullPtr = IsNullPtr;
+}
+
+MutableArrayRef<APValue::LValuePathEntry>
+APValue::setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size,
+ bool IsOnePastTheEnd, bool IsNullPtr) {
+ assert(isLValue() && "Invalid accessor");
+ LV &LVal = *((LV *)(char *)&Data);
+ LVal.Base = B;
+ LVal.IsOnePastTheEnd = IsOnePastTheEnd;
+ LVal.Offset = O;
+ LVal.IsNullPtr = IsNullPtr;
+ LVal.resizePath(Size);
+ return {LVal.getPath(), Size};
+}
+
+void APValue::setLValue(LValueBase B, const CharUnits &O,
+ ArrayRef<LValuePathEntry> Path, bool IsOnePastTheEnd,
+ bool IsNullPtr) {
+ MutableArrayRef<APValue::LValuePathEntry> InternalPath =
+ setLValueUninit(B, O, Path.size(), IsOnePastTheEnd, IsNullPtr);
+ if (Path.size()) {
+ memcpy(InternalPath.data(), Path.data(),
+ Path.size() * sizeof(LValuePathEntry));
+ }
+}
+
+void APValue::setUnion(const FieldDecl *Field, const APValue &Value) {
+ assert(isUnion() && "Invalid accessor");
+ ((UnionData *)(char *)&Data)->Field =
+ Field ? Field->getCanonicalDecl() : nullptr;
+ *((UnionData *)(char *)&Data)->Value = Value;
+}
+
+const ValueDecl *APValue::getMemberPointerDecl() const {
+ assert(isMemberPointer() && "Invalid accessor");
+ const MemberPointerData &MPD =
+ *((const MemberPointerData *)(const char *)&Data);
+ return MPD.MemberAndIsDerivedMember.getPointer();
+}
+
+bool APValue::isMemberPointerToDerivedMember() const {
+ assert(isMemberPointer() && "Invalid accessor");
+ const MemberPointerData &MPD =
+ *((const MemberPointerData *)(const char *)&Data);
+ return MPD.MemberAndIsDerivedMember.getInt();
+}
+
+ArrayRef<const CXXRecordDecl*> APValue::getMemberPointerPath() const {
+ assert(isMemberPointer() && "Invalid accessor");
+ const MemberPointerData &MPD =
+ *((const MemberPointerData *)(const char *)&Data);
+ return llvm::ArrayRef(MPD.getPath(), MPD.PathLength);
+}
+
+void APValue::MakeLValue() {
+ assert(isAbsent() && "Bad state change");
+ static_assert(sizeof(LV) <= DataSize, "LV too big");
+ new ((void *)(char *)&Data) LV();
+ Kind = LValue;
+}
+
+void APValue::MakeArray(unsigned InitElts, unsigned Size) {
+ assert(isAbsent() && "Bad state change");
+ new ((void *)(char *)&Data) Arr(InitElts, Size);
+ Kind = Array;
+}
+
+MutableArrayRef<APValue::LValuePathEntry>
+setLValueUninit(APValue::LValueBase B, const CharUnits &O, unsigned Size,
+ bool OnePastTheEnd, bool IsNullPtr);
+
+MutableArrayRef<const CXXRecordDecl *>
+APValue::setMemberPointerUninit(const ValueDecl *Member, bool IsDerivedMember,
+ unsigned Size) {
+ assert(isAbsent() && "Bad state change");
+ MemberPointerData *MPD = new ((void *)(char *)&Data) MemberPointerData;
+ Kind = MemberPointer;
+ MPD->MemberAndIsDerivedMember.setPointer(
+ Member ? cast<ValueDecl>(Member->getCanonicalDecl()) : nullptr);
+ MPD->MemberAndIsDerivedMember.setInt(IsDerivedMember);
+ MPD->resizePath(Size);
+ return {MPD->getPath(), MPD->PathLength};
+}
+
+void APValue::MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember,
+ ArrayRef<const CXXRecordDecl *> Path) {
+ MutableArrayRef<const CXXRecordDecl *> InternalPath =
+ setMemberPointerUninit(Member, IsDerivedMember, Path.size());
+ for (unsigned I = 0; I != Path.size(); ++I)
+ InternalPath[I] = Path[I]->getCanonicalDecl();
+}
+
+LinkageInfo LinkageComputer::getLVForValue(const APValue &V,
+ LVComputationKind computation) {
+ LinkageInfo LV = LinkageInfo::external();
+
+ auto MergeLV = [&](LinkageInfo MergeLV) {
+ LV.merge(MergeLV);
+ return LV.getLinkage() == Linkage::Internal;
+ };
+ auto Merge = [&](const APValue &V) {
+ return MergeLV(getLVForValue(V, computation));
+ };
+
+ switch (V.getKind()) {
+ case APValue::None:
+ case APValue::Indeterminate:
+ case APValue::Int:
+ case APValue::Float:
+ case APValue::FixedPoint:
+ case APValue::ComplexInt:
+ case APValue::ComplexFloat:
+ case APValue::Vector:
+ break;
+
+ case APValue::AddrLabelDiff:
+ // Even for an inline function, it's not reasonable to treat a difference
+ // between the addresses of labels as an external value.
+ return LinkageInfo::internal();
+
+ case APValue::Struct: {
+ for (unsigned I = 0, N = V.getStructNumBases(); I != N; ++I)
+ if (Merge(V.getStructBase(I)))
+ break;
+ for (unsigned I = 0, N = V.getStructNumFields(); I != N; ++I)
+ if (Merge(V.getStructField(I)))
+ break;
+ break;
+ }
+
+ case APValue::Union:
+ if (V.getUnionField())
+ Merge(V.getUnionValue());
+ break;
+
+ case APValue::Array: {
+ for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; ++I)
+ if (Merge(V.getArrayInitializedElt(I)))
+ break;
+ if (V.hasArrayFiller())
+ Merge(V.getArrayFiller());
+ break;
+ }
+
+ case APValue::LValue: {
+ if (!V.getLValueBase()) {
+ // Null or absolute address: this is external.
+ } else if (const auto *VD =
+ V.getLValueBase().dyn_cast<const ValueDecl *>()) {
+ if (VD && MergeLV(getLVForDecl(VD, computation)))
+ break;
+ } else if (const auto TI = V.getLValueBase().dyn_cast<TypeInfoLValue>()) {
+ if (MergeLV(getLVForType(*TI.getType(), computation)))
+ break;
+ } else if (const Expr *E = V.getLValueBase().dyn_cast<const Expr *>()) {
+ // Almost all expression bases are internal. The exception is
+ // lifetime-extended temporaries.
+ // FIXME: These should be modeled as having the
+ // LifetimeExtendedTemporaryDecl itself as the base.
+ // FIXME: If we permit Objective-C object literals in template arguments,
+ // they should not imply internal linkage.
+ auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
+ if (!MTE || MTE->getStorageDuration() == SD_FullExpression)
+ return LinkageInfo::internal();
+ if (MergeLV(getLVForDecl(MTE->getExtendingDecl(), computation)))
+ break;
+ } else {
+ assert(V.getLValueBase().is<DynamicAllocLValue>() &&
+ "unexpected LValueBase kind");
+ return LinkageInfo::internal();
+ }
+ // The lvalue path doesn't matter: pointers to all subobjects always have
+ // the same visibility as pointers to the complete object.
+ break;
+ }
+
+ case APValue::MemberPointer:
+ if (const NamedDecl *D = V.getMemberPointerDecl())
+ MergeLV(getLVForDecl(D, computation));
+ // Note that we could have a base-to-derived conversion here to a member of
+ // a derived class with less linkage/visibility. That's covered by the
+ // linkage and visibility of the value's type.
+ break;
+ }
+
+ return LV;
+}
More information about the cfe-commits
mailing list