| 1 | /* |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <string.h> |
| 17 | |
| 18 | /* Define _GNU_SOURCE to get RTLD_NEXT definition from dlfcn.h */ |
| 19 | #define _GNU_SOURCE |
| 20 | #include <dlfcn.h> |
| 21 | |
| 22 | /* Overrides will work only if RTLD_NEXT is defined */ |
| 23 | #ifdef RTLD_NEXT |
| 24 | |
| 25 | /* if we use glibc, include malloc.h for malloc_usable_size */ |
| 26 | #ifdef __GLIBC__ |
| 27 | #include <malloc.h> |
| 28 | #define HAVE_MALLOC_USABLE_SIZE |
| 29 | #endif |
| 30 | |
| 31 | typedef int (*posix_memalign_fn)(void **memptr, size_t alignment, size_t size); |
| 32 | typedef void *(*realloc_fn)(void *ptr, size_t size); |
| 33 | |
| 34 | posix_memalign_fn orig_posix_memalign = NULL; |
| 35 | realloc_fn orig_realloc = NULL; |
| 36 | |
| 37 | int posix_memalign(void **memptr, size_t alignment, size_t size) |
| 38 | { |
| 39 | /* Override original posix_memalign to fill allocated memory with some data |
| 40 | * to catch errors due to missing initialization */ |
| 41 | int rc; |
| 42 | |
| 43 | if (orig_posix_memalign == NULL) { |
| 44 | /* C99 forbids converting void * to function pointers, so direct |
| 45 | * assignemnt fails with -pedantic. Yet dlsym is still used to return |
| 46 | * function pointers in standard library, despite that it returns |
| 47 | * void *. Casting function pointer to void ** and dereferencing it |
| 48 | * allows to bypass compiler warnings. */ |
| 49 | *(void **) &orig_posix_memalign = dlsym(RTLD_NEXT, "posix_memalign"); |
| 50 | } |
| 51 | |
| 52 | rc = orig_posix_memalign(memptr, alignment, size); |
| 53 | |
| 54 | memset(*memptr, 0xff, size); |
| 55 | |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | void *realloc(void *ptr, size_t size) |
| 60 | { |
| 61 | /* Override original realloc to fill allocated memory with some data to |
| 62 | * catch errors due to missing initialization */ |
| 63 | void *p; |
| 64 | |
| 65 | if (orig_realloc == NULL) { |
| 66 | /* C99 forbids converting void * to function pointers, so direct |
| 67 | * assignemnt fails with -pedantic. Yet dlsym is still used to return |
| 68 | * function pointers in standard library, despite that it returns |
| 69 | * void *. Casting function pointer to void ** and dereferencing it |
| 70 | * allows to bypass compiler warnings. */ |
| 71 | *(void **) &orig_realloc = dlsym(RTLD_NEXT, "realloc"); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | #ifdef HAVE_MALLOC_USABLE_SIZE |
| 76 | /* If malloc_usable_size is available, we can get the size of previously |
| 77 | * allocated buffer, to find out how many new bytes we've allocated. |
| 78 | * Get the usable size for ptr before we call realloc, because realloc may call |
| 79 | * free() on the original pointer. */ |
| 80 | size_t ptr_alloc_size = malloc_usable_size(ptr); |
| 81 | #endif |
| 82 | |
| 83 | p = orig_realloc(ptr, size); |
| 84 | |
| 85 | #ifdef HAVE_MALLOC_USABLE_SIZE |
| 86 | size_t p_alloc_size = malloc_usable_size(p); |
| 87 | |
| 88 | /* If call succeeded and we're enlarging memory, fill the extension with |
| 89 | * non-zero data */ |
| 90 | if (p && p_alloc_size > ptr_alloc_size) { |
| 91 | memset((char *) p + ptr_alloc_size, 0xff, p_alloc_size - ptr_alloc_size); |
| 92 | } |
| 93 | #else |
| 94 | /* If we're allocating new buffer and the call succeeded, fill the buffer |
| 95 | * with non-zero data*/ |
| 96 | if (p && !ptr) { |
| 97 | memset(p, 0xff, size); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | return p; |
| 102 | } |
| 103 | |
| 104 | #endif |