1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <errno.h>
30 #include <malloc.h>
31 #include <stdlib.h>
32 #include <sys/mman.h>
33 #include <sys/param.h>
34 #include <sys/prctl.h>
35
36 #include "private/bionic_macros.h"
37
38 #include "scudo.h"
39
40 // Disable Scudo's mismatch allocation check, as it is being triggered
41 // by some third party code.
__scudo_default_options()42 extern "C" const char *__scudo_default_options() {
43 return "DeallocationTypeMismatch=false";
44 }
45
AllocTooBig(size_t bytes)46 static inline bool AllocTooBig(size_t bytes) {
47 #if defined(__LP64__)
48 if (__predict_false(bytes > 0x10000000000ULL)) {
49 #else
50 if (__predict_false(bytes > 0x80000000ULL)) {
51 #endif
52 return true;
53 }
54 return false;
55 }
56
57 void* scudo_aligned_alloc(size_t alignment, size_t size) {
58 if (alignment == 0 || !powerof2(alignment) || (size % alignment) != 0) {
59 errno = EINVAL;
60 return nullptr;
61 }
62 if (AllocTooBig(size)) {
63 errno = ENOMEM;
64 return nullptr;
65 }
66
67 return aligned_alloc(alignment, size);
68 }
69
70 void* scudo_calloc(size_t item_count, size_t item_size) {
71 size_t total;
72 if (__builtin_mul_overflow(item_count, item_size, &total) || AllocTooBig(total)) {
73 errno = ENOMEM;
74 return nullptr;
75 }
76 return calloc(item_count, item_size);
77 }
78
79 void scudo_free(void* ptr) {
80 free(ptr);
81 }
82
83 extern "C" size_t __sanitizer_get_current_allocated_bytes();
84 extern "C" size_t __sanitizer_get_heap_size();
85
86 struct mallinfo scudo_mallinfo() {
87 struct mallinfo info {};
88 info.uordblks = __sanitizer_get_current_allocated_bytes();
89 info.hblkhd = __sanitizer_get_heap_size();
90 info.usmblks = info.hblkhd;
91 return info;
92 }
93
94 void* scudo_malloc(size_t byte_count) {
95 if (AllocTooBig(byte_count)) {
96 errno = ENOMEM;
97 return nullptr;
98 }
99 return malloc(byte_count);
100 }
101
102 size_t scudo_malloc_usable_size(const void* ptr) {
103 return malloc_usable_size(ptr);
104 }
105
106 void* scudo_memalign(size_t alignment, size_t byte_count) {
107 if (AllocTooBig(byte_count)) {
108 errno = ENOMEM;
109 return nullptr;
110 }
111 if (alignment != 0) {
112 if (!powerof2(alignment)) {
113 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
114 }
115 } else {
116 alignment = 1;
117 }
118 return memalign(alignment, byte_count);
119 }
120
121 void* scudo_realloc(void* ptr, size_t byte_count) {
122 if (AllocTooBig(byte_count)) {
123 errno = ENOMEM;
124 return nullptr;
125 }
126 return realloc(ptr, byte_count);
127 }
128
129 int scudo_posix_memalign(void** memptr, size_t alignment, size_t size) {
130 if (alignment < sizeof(void*) || !powerof2(alignment)) {
131 return EINVAL;
132 }
133 if (AllocTooBig(size)) {
134 return ENOMEM;
135 }
136 return posix_memalign(memptr, alignment, size);
137 }
138
139 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
140 extern "C" void* pvalloc(size_t);
141
142 void* scudo_pvalloc(size_t size) {
143 if (AllocTooBig(size)) {
144 errno = ENOMEM;
145 return nullptr;
146 }
147 return pvalloc(size);
148 }
149
150 extern "C" void* valloc(size_t);
151
152 void* scudo_valloc(size_t size) {
153 if (AllocTooBig(size)) {
154 errno = ENOMEM;
155 return nullptr;
156 }
157 return valloc(size);
158 }
159 #endif
160
161 // Do not try and name the scudo maps by overriding __sanitizer::internal_mmap.
162 // There is already a function called MmapNamed that names the maps.
163 // Unfortunately, there is no easy way to override MmapNamed because
164 // too much of the code is not compiled into functions available in the
165 // library, and the code is complicated.
166