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