Loading...
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | /* * Copyright (c) 2018 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include "internal.h" #if !MALLOC_TARGET_EXCLAVES /* global flag to suppress ASL logging e.g. for syslogd */ int _malloc_no_asl_log = 0; typedef enum { DEBUG_WRITE_NONE, DEBUG_WRITE_ON_CRASH, DEBUG_WRITE_ALWAYS, } write_debug_mode_t; static const char Malloc_Facility[] = "com.apple.Libsystem.malloc"; static int malloc_debug_file = STDERR_FILENO; static write_debug_mode_t debug_mode = DEBUG_WRITE_NONE; static boolean_t malloc_error_stop; // Stop when reporting error. static boolean_t malloc_error_sleep; // Sleep after reporting error. static const int default_sleep_time = 3600; #endif /* !MALLOC_TARGET_EXCLAVES */ // Gets the default time to sleep for when reporting an error. Returns 0 // (meaning do not sleep) if malloc_error_sleep is 0 (that is, if sleeping on // error is not configured). unsigned _malloc_default_debug_sleep_time(void) { #if !MALLOC_TARGET_EXCLAVES return malloc_error_sleep ? default_sleep_time : 0; #else return 0; #endif /* !MALLOC_TARGET_EXCLAVES */ } #if !MALLOC_TARGET_EXCLAVES #define WRITE_TO_DEBUG_FILE(flags) \ (((flags) & MALLOC_REPORT_NOWRITE) == 0 && \ ((debug_mode == DEBUG_WRITE_ALWAYS) || \ (debug_mode == DEBUG_WRITE_ON_CRASH && ((flags) & MALLOC_REPORT_CRASH)))) #define MALLOC_REPORT_LEVEL_MASK 0x0f #endif /* !MALLOC_TARGET_EXCLAVES */ #pragma mark - #pragma mark Configuration void malloc_print_configure(bool restricted) { #if !MALLOC_TARGET_EXCLAVES char *flag = getenv("MallocDebugReport"); if (flag) { if (!strcmp(flag, "stderr")) { debug_mode = DEBUG_WRITE_ALWAYS; } else if (!strcmp(flag, "crash")) { debug_mode = DEBUG_WRITE_ON_CRASH; } else if (!strcmp(flag, "none")) { debug_mode = DEBUG_WRITE_NONE; } else { debug_mode = DEBUG_WRITE_ALWAYS; malloc_printf("Unrecognized value for MallocDebugReport (%s) - using 'stderr'\n", flag); } } else { // Default is to write to stderr only if it's a tty. if (isatty(STDERR_FILENO)) { debug_mode = DEBUG_WRITE_ALWAYS; } } if (getenv("MallocErrorStop")) { malloc_error_stop = TRUE; } if (getenv("MallocErrorSleep")) { malloc_error_sleep = TRUE; } #else (void)restricted; #endif /* !MALLOC_TARGET_EXCLAVES */ } #pragma mark - #pragma mark Low level debug output /* * The functions that follow use _simple_*printf. They deal with a * subset of printf format specifiers and do not call malloc internally. */ static void _malloc_put(uint32_t flags, const char * __null_terminated msg) { #if !MALLOC_TARGET_EXCLAVES _SIMPLE_STRING b; if ((b = _simple_salloc()) == NULL) { if (WRITE_TO_DEBUG_FILE(flags)) { if (!(flags & MALLOC_REPORT_NOPREFIX)) { void *self = _pthread_self_direct(); _simple_dprintf(malloc_debug_file, "%s(%d,%p) malloc: ", getprogname(), getpid(), self); } write(malloc_debug_file, msg, strlen(msg)); } return; } if (!(flags & MALLOC_REPORT_NOPREFIX)) { void *self = _pthread_self_direct(); _simple_sprintf(b, "%s(%d,%p) malloc: ", getprogname(), getpid(), self); } _simple_sprintf(b, "%s", msg); if (WRITE_TO_DEBUG_FILE(flags)) { _simple_put(b, malloc_debug_file); } if (_malloc_no_asl_log & !(flags & MALLOC_REPORT_NOLOG)) { _simple_asl_log(flags & MALLOC_REPORT_LEVEL_MASK, Malloc_Facility, _simple_string(b)); } _simple_sfree(b); #else if (!(flags & MALLOC_REPORT_NOPREFIX)) { printf("(0x%lx) malloc: ", pthread_self()); } printf("%s", msg); #endif /* !MALLOC_TARGET_EXCLAVES */ } #pragma mark - #pragma mark High-Level Reporting Functions #define MALLOC_BT_DEPTH 50 static void _malloc_append_backtrace(_SIMPLE_STRING b) { void * __unsafe_indexable btarray[MALLOC_BT_DEPTH]; ssize_t count = backtrace(btarray, MALLOC_BT_DEPTH); if (!count) { return; } #if !MALLOC_TARGET_EXCLAVES struct image_offset bt_image_offsets[MALLOC_BT_DEPTH]; backtrace_image_offsets(btarray, bt_image_offsets, (int)count); #else (void)b; #endif /* !MALLOC_TARGET_EXCLAVES */ for (ssize_t i = 0; i < count; i++) { #if !MALLOC_TARGET_EXCLAVES uuid_t last_uuid; // simple de-duping for runs of UUIDs common in backtraces if (i == 0 || uuid_compare(last_uuid, bt_image_offsets[i].uuid) != 0) { uuid_copy(last_uuid, bt_image_offsets[i].uuid); uuid_string_t uus; uuid_unparse(bt_image_offsets[i].uuid, uus); _simple_sappend(b, uus); } else { _simple_sappend(b, "last"); } _simple_sprintf(b, "+%u,", bt_image_offsets[i].offset); #else printf("+%p", btarray[i]); #endif /* !MALLOC_TARGET_EXCLAVES */ } } MALLOC_NOINLINE void malloc_vreport(uint32_t flags, unsigned sleep_time, const char * __null_terminated prefix_msg, const void *prefix_arg, const char * __null_terminated fmt, va_list ap) { #if !MALLOC_TARGET_EXCLAVES const char *crash_msg = NULL; _SIMPLE_STRING b = NULL; if ((b = _simple_salloc()) == NULL) { if (WRITE_TO_DEBUG_FILE(flags)) { if (!(flags & MALLOC_REPORT_NOPREFIX)) { void *self = _pthread_self_direct(); _simple_dprintf(malloc_debug_file, "%s(%d,%p) malloc: ", getprogname(), getpid(), self); } if (prefix_msg) { _simple_dprintf(malloc_debug_file, prefix_msg, prefix_arg); } _simple_vdprintf(malloc_debug_file, fmt, ap); } if (flags & MALLOC_REPORT_CRASH) { crash_msg = fmt; } } else { if (!(flags & MALLOC_REPORT_NOPREFIX)) { void *self = _pthread_self_direct(); _simple_sprintf(b, "%s(%d,%p) malloc: ", getprogname(), getpid(), self); } if (prefix_msg) { _simple_sprintf(b, prefix_msg, prefix_arg); } _simple_vsprintf(b, fmt, ap); if (flags & MALLOC_REPORT_BACKTRACE) { _malloc_append_backtrace(b); } if (WRITE_TO_DEBUG_FILE(flags)) { _simple_put(b, malloc_debug_file); } if (!_malloc_no_asl_log && !(flags & MALLOC_REPORT_NOLOG)) { _simple_asl_log(flags & MALLOC_REPORT_LEVEL_MASK, Malloc_Facility, _simple_string(b)); } if (flags & MALLOC_REPORT_CRASH) { crash_msg = _simple_string(b); } else { _simple_sfree(b); } } if (flags & (MALLOC_REPORT_DEBUG | MALLOC_REPORT_CRASH)) { _malloc_put(flags, "*** set a breakpoint in malloc_error_break to debug\n"); malloc_error_break(); if (malloc_error_stop) { _malloc_put(ASL_LEVEL_NOTICE, "*** sending SIGSTOP to help debug\n"); kill(getpid(), SIGSTOP); } else if (sleep_time) { _malloc_put(ASL_LEVEL_NOTICE, "*** sleeping to help debug\n"); sleep(sleep_time); } } if (flags & MALLOC_REPORT_CRASH) { _os_set_crash_log_message_dynamic(crash_msg); abort(); } #else if (!(flags & MALLOC_REPORT_NOPREFIX)) { printf("(0x%lx) malloc: ", pthread_self()); } if (prefix_msg) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" printf(prefix_msg, prefix_arg); #pragma GCC diagnostic pop } vprintf(fmt, ap); if (flags & MALLOC_REPORT_BACKTRACE) { _malloc_append_backtrace(NULL); } if (flags & (MALLOC_REPORT_DEBUG | MALLOC_REPORT_CRASH)) { _malloc_put(flags, "*** set a breakpoint in malloc_error_break to debug\n"); malloc_error_break(); if (sleep_time) { _malloc_put(ASL_LEVEL_NOTICE, "*** sleeping to help debug\n"); sleep(sleep_time); } } if (flags & MALLOC_REPORT_CRASH) { abort(); } #endif /* !MALLOC_TARGET_EXCLAVES */ } MALLOC_NOEXPORT void malloc_report(uint32_t flags, const char *fmt, ...) { va_list ap; va_start(ap, fmt); malloc_vreport(flags, _malloc_default_debug_sleep_time(), NULL, NULL, fmt, ap); va_end(ap); } MALLOC_NOEXPORT void malloc_report_simple(const char *fmt, ...) { va_list ap; va_start(ap, fmt); malloc_vreport(MALLOC_REPORT_NOLOG | MALLOC_REPORT_NOPREFIX, _malloc_default_debug_sleep_time(), NULL, NULL, fmt, ap); va_end(ap); } #pragma mark - #pragma mark Zone Error Reporing #ifndef MALLOC_BUILDING_XCTESTS void malloc_zone_error(uint32_t flags, bool is_corruption, const char *fmt, ...) { va_list ap; va_start(ap, fmt); uint32_t report_flags = MALLOC_REPORT_DEBUG | MALLOC_REPORT_NOLOG; if ((is_corruption && (flags & MALLOC_ABORT_ON_CORRUPTION)) || (flags & MALLOC_ABORT_ON_ERROR)) { report_flags = MALLOC_REPORT_CRASH; } malloc_vreport(report_flags | ASL_LEVEL_ERR, _malloc_default_debug_sleep_time(), NULL, NULL, fmt, ap); va_end(ap); } #endif // MALLOC_BUILDING_XCTESTS #if MALLOC_TARGET_EXCLAVES || MALLOC_TARGET_EXCLAVES_INTROSPECTOR MALLOC_NOEXPORT void malloc_error_break(void) { // Provides a non-inlined place for various malloc error procedures to call // that will be called after an error message appears. It does not make // sense for developers to call this function, so it is marked // hidden to prevent it from becoming API. } #endif // MALLOC_TARGET_EXCLAVES || MALLOC_TARGET_EXCLAVES_INTROSPECTOR #pragma mark - #pragma mark Malloc Output API. // malloc_printf() needs to be retained and exported because it's API (defined // in malloc/malloc.h). It's equivalent to calling malloc_report() with // a flags value of ASL_LEVEL_ERR, so does not result in a crash or any prompts // for diagnostics or breakpoints. // Do not use in malloc code. #if MALLOC_TARGET_EXCLAVES MALLOC_NOEXPORT #endif // MALLOC_TARGET_EXCLAVES void malloc_printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); malloc_vreport(ASL_LEVEL_ERR, 0, NULL, NULL, fmt, ap); va_end(ap); } |