summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/sanitizer_allocator_report.cpp
blob: dbcf2b7bf260da261f6e994694f045c2583bc13d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//===-- sanitizer_allocator_report.cpp --------------------------*- C++ -*-===//
//
// 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
/// Shared allocator error reporting for ThreadSanitizer, MemorySanitizer, etc.
///
//===----------------------------------------------------------------------===//

#include "sanitizer_allocator.h"
#include "sanitizer_allocator_report.h"
#include "sanitizer_common.h"
#include "sanitizer_report_decorator.h"

namespace __sanitizer {

class ScopedAllocatorErrorReport {
 public:
  ScopedAllocatorErrorReport(const char *error_summary_,
                             const StackTrace *stack_)
      : error_summary(error_summary_),
        stack(stack_) {
    Printf("%s", d.Error());
  }
  ~ScopedAllocatorErrorReport() {
    Printf("%s", d.Default());
    stack->Print();
    PrintHintAllocatorCannotReturnNull();
    ReportErrorSummary(error_summary, stack);
  }

 private:
  ScopedErrorReportLock lock;
  const char *error_summary;
  const StackTrace* const stack;
  const SanitizerCommonDecorator d;
};

void NORETURN ReportCallocOverflow(uptr count, uptr size,
                                   const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("calloc-overflow", stack);
    Report("ERROR: %s: calloc parameters overflow: count * size (%zd * %zd) "
           "cannot be represented in type size_t\n", SanitizerToolName, count,
           size);
  }
  Die();
}

void NORETURN ReportReallocArrayOverflow(uptr count, uptr size,
                                         const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("reallocarray-overflow", stack);
    Report(
        "ERROR: %s: reallocarray parameters overflow: count * size (%zd * %zd) "
        "cannot be represented in type size_t\n",
        SanitizerToolName, count, size);
  }
  Die();
}

void NORETURN ReportPvallocOverflow(uptr size, const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("pvalloc-overflow", stack);
    Report("ERROR: %s: pvalloc parameters overflow: size 0x%zx rounded up to "
           "system page size 0x%zx cannot be represented in type size_t\n",
           SanitizerToolName, size, GetPageSizeCached());
  }
  Die();
}

void NORETURN ReportInvalidAllocationAlignment(uptr alignment,
                                               const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("invalid-allocation-alignment", stack);
    Report("ERROR: %s: invalid allocation alignment: %zd, alignment must be a "
           "power of two\n", SanitizerToolName, alignment);
  }
  Die();
}

void NORETURN ReportInvalidAlignedAllocAlignment(uptr size, uptr alignment,
                                                 const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("invalid-aligned-alloc-alignment", stack);
#if SANITIZER_POSIX
    Report("ERROR: %s: invalid alignment requested in "
           "aligned_alloc: %zd, alignment must be a power of two and the "
           "requested size 0x%zx must be a multiple of alignment\n",
           SanitizerToolName, alignment, size);
#else
    Report("ERROR: %s: invalid alignment requested in aligned_alloc: %zd, "
           "the requested size 0x%zx must be a multiple of alignment\n",
           SanitizerToolName, alignment, size);
#endif
  }
  Die();
}

void NORETURN ReportInvalidPosixMemalignAlignment(uptr alignment,
                                                  const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("invalid-posix-memalign-alignment",
                                      stack);
    Report("ERROR: %s: invalid alignment requested in "
           "posix_memalign: %zd, alignment must be a power of two and a "
           "multiple of sizeof(void*) == %zd\n", SanitizerToolName, alignment,
           sizeof(void*));  // NOLINT
  }
  Die();
}

void NORETURN ReportAllocationSizeTooBig(uptr user_size, uptr max_size,
                                         const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("allocation-size-too-big", stack);
    Report("ERROR: %s: requested allocation size 0x%zx exceeds maximum "
           "supported size of 0x%zx\n", SanitizerToolName, user_size, max_size);
  }
  Die();
}

void NORETURN ReportOutOfMemory(uptr requested_size, const StackTrace *stack) {
  {
    ScopedAllocatorErrorReport report("out-of-memory", stack);
    Report("ERROR: %s: allocator is out of memory trying to allocate 0x%zx "
           "bytes\n", SanitizerToolName, requested_size);
  }
  Die();
}

}  // namespace __sanitizer