Home | History | Annotate | Line # | Download | only in sanitizer_common
      1 //===-- sanitizer_allocator_report.cc ---------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// Shared allocator error reporting for ThreadSanitizer, MemorySanitizer, etc.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "sanitizer_allocator.h"
     16 #include "sanitizer_allocator_report.h"
     17 #include "sanitizer_common.h"
     18 #include "sanitizer_report_decorator.h"
     19 
     20 namespace __sanitizer {
     21 
     22 class ScopedAllocatorErrorReport {
     23  public:
     24   ScopedAllocatorErrorReport(const char *error_summary_,
     25                              const StackTrace *stack_)
     26       : error_summary(error_summary_),
     27         stack(stack_) {
     28     Printf("%s", d.Error());
     29   }
     30   ~ScopedAllocatorErrorReport() {
     31     Printf("%s", d.Default());
     32     stack->Print();
     33     PrintHintAllocatorCannotReturnNull();
     34     ReportErrorSummary(error_summary, stack);
     35   }
     36 
     37  private:
     38   ScopedErrorReportLock lock;
     39   const char *error_summary;
     40   const StackTrace* const stack;
     41   const SanitizerCommonDecorator d;
     42 };
     43 
     44 void NORETURN ReportCallocOverflow(uptr count, uptr size,
     45                                    const StackTrace *stack) {
     46   {
     47     ScopedAllocatorErrorReport report("calloc-overflow", stack);
     48     Report("ERROR: %s: calloc parameters overflow: count * size (%zd * %zd) "
     49            "cannot be represented in type size_t\n", SanitizerToolName, count,
     50            size);
     51   }
     52   Die();
     53 }
     54 
     55 void NORETURN ReportPvallocOverflow(uptr size, const StackTrace *stack) {
     56   {
     57     ScopedAllocatorErrorReport report("pvalloc-overflow", stack);
     58     Report("ERROR: %s: pvalloc parameters overflow: size 0x%zx rounded up to "
     59            "system page size 0x%zx cannot be represented in type size_t\n",
     60            SanitizerToolName, size, GetPageSizeCached());
     61   }
     62   Die();
     63 }
     64 
     65 void NORETURN ReportInvalidAllocationAlignment(uptr alignment,
     66                                                const StackTrace *stack) {
     67   {
     68     ScopedAllocatorErrorReport report("invalid-allocation-alignment", stack);
     69     Report("ERROR: %s: invalid allocation alignment: %zd, alignment must be a "
     70            "power of two\n", SanitizerToolName, alignment);
     71   }
     72   Die();
     73 }
     74 
     75 void NORETURN ReportInvalidAlignedAllocAlignment(uptr size, uptr alignment,
     76                                                  const StackTrace *stack) {
     77   {
     78     ScopedAllocatorErrorReport report("invalid-aligned-alloc-alignment", stack);
     79 #if SANITIZER_POSIX
     80     Report("ERROR: %s: invalid alignment requested in "
     81            "aligned_alloc: %zd, alignment must be a power of two and the "
     82            "requested size 0x%zx must be a multiple of alignment\n",
     83            SanitizerToolName, alignment, size);
     84 #else
     85     Report("ERROR: %s: invalid alignment requested in aligned_alloc: %zd, "
     86            "the requested size 0x%zx must be a multiple of alignment\n",
     87            SanitizerToolName, alignment, size);
     88 #endif
     89   }
     90   Die();
     91 }
     92 
     93 void NORETURN ReportInvalidPosixMemalignAlignment(uptr alignment,
     94                                                   const StackTrace *stack) {
     95   {
     96     ScopedAllocatorErrorReport report("invalid-posix-memalign-alignment",
     97                                       stack);
     98     Report("ERROR: %s: invalid alignment requested in "
     99            "posix_memalign: %zd, alignment must be a power of two and a "
    100            "multiple of sizeof(void*) == %zd\n", SanitizerToolName, alignment,
    101            sizeof(void*));  // NOLINT
    102   }
    103   Die();
    104 }
    105 
    106 void NORETURN ReportAllocationSizeTooBig(uptr user_size, uptr max_size,
    107                                          const StackTrace *stack) {
    108   {
    109     ScopedAllocatorErrorReport report("allocation-size-too-big", stack);
    110     Report("ERROR: %s: requested allocation size 0x%zx exceeds maximum "
    111            "supported size of 0x%zx\n", SanitizerToolName, user_size, max_size);
    112   }
    113   Die();
    114 }
    115 
    116 void NORETURN ReportOutOfMemory(uptr requested_size, const StackTrace *stack) {
    117   {
    118     ScopedAllocatorErrorReport report("out-of-memory", stack);
    119     Report("ERROR: %s: allocator is out of memory trying to allocate 0x%zx "
    120            "bytes\n", SanitizerToolName, requested_size);
    121   }
    122   Die();
    123 }
    124 
    125 }  // namespace __sanitizer
    126