scudo_malloc.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===-- scudo_malloc.cpp ----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// Interceptors for malloc related functions.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "scudo_allocator.h"
  13. #include "interception/interception.h"
  14. #include "sanitizer_common/sanitizer_platform_interceptors.h"
  15. #include <stddef.h>
  16. using namespace __scudo;
  17. extern "C" {
  18. INTERCEPTOR_ATTRIBUTE void free(void *ptr) {
  19. scudoDeallocate(ptr, 0, 0, FromMalloc);
  20. }
  21. INTERCEPTOR_ATTRIBUTE void *malloc(size_t size) {
  22. return scudoAllocate(size, 0, FromMalloc);
  23. }
  24. INTERCEPTOR_ATTRIBUTE void *realloc(void *ptr, size_t size) {
  25. return scudoRealloc(ptr, size);
  26. }
  27. INTERCEPTOR_ATTRIBUTE void *calloc(size_t nmemb, size_t size) {
  28. return scudoCalloc(nmemb, size);
  29. }
  30. INTERCEPTOR_ATTRIBUTE void *valloc(size_t size) {
  31. return scudoValloc(size);
  32. }
  33. INTERCEPTOR_ATTRIBUTE
  34. int posix_memalign(void **memptr, size_t alignment, size_t size) {
  35. return scudoPosixMemalign(memptr, alignment, size);
  36. }
  37. #if SANITIZER_INTERCEPT_CFREE
  38. INTERCEPTOR_ATTRIBUTE void cfree(void *ptr) ALIAS("free");
  39. #endif
  40. #if SANITIZER_INTERCEPT_MEMALIGN
  41. INTERCEPTOR_ATTRIBUTE void *memalign(size_t alignment, size_t size) {
  42. return scudoAllocate(size, alignment, FromMemalign);
  43. }
  44. INTERCEPTOR_ATTRIBUTE
  45. void *__libc_memalign(size_t alignment, size_t size) ALIAS("memalign");
  46. #endif
  47. #if SANITIZER_INTERCEPT_PVALLOC
  48. INTERCEPTOR_ATTRIBUTE void *pvalloc(size_t size) {
  49. return scudoPvalloc(size);
  50. }
  51. #endif
  52. #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
  53. INTERCEPTOR_ATTRIBUTE void *aligned_alloc(size_t alignment, size_t size) {
  54. return scudoAlignedAlloc(alignment, size);
  55. }
  56. #endif
  57. #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
  58. INTERCEPTOR_ATTRIBUTE size_t malloc_usable_size(void *ptr) {
  59. return scudoMallocUsableSize(ptr);
  60. }
  61. #endif
  62. #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
  63. INTERCEPTOR_ATTRIBUTE int mallopt(int cmd, int value) {
  64. return 0;
  65. }
  66. #endif
  67. } // extern "C"