instrumented_alloc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* instrumented_alloc.c -- Memory allocation instrumented to fail when
  2. requested, for testing purposes.
  3. Copyright (C) 2018-2021 Free Software Foundation, Inc.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. /* Include all the header files of alloc here, to make sure they're not
  28. processed when including alloc.c below, such that the redefinitions of malloc
  29. and realloc are only effective in alloc.c itself. This does not work for
  30. config.h, because it's not wrapped in "#ifndef CONFIG_H\n#define CONFIG_H"
  31. and "#endif" but that does not seem to be harmful. */
  32. #include "config.h"
  33. #include <errno.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #include <inttypes.h>
  37. #include "backtrace.h"
  38. #include "internal.h"
  39. extern void *instrumented_malloc (size_t size);
  40. extern void *instrumented_realloc (void *ptr, size_t size);
  41. #define malloc instrumented_malloc
  42. #define realloc instrumented_realloc
  43. #include "alloc.c"
  44. #undef malloc
  45. #undef realloc
  46. static uint64_t nr_allocs = 0;
  47. static uint64_t fail_at_alloc = 0;
  48. extern int at_fail_alloc_p (void);
  49. extern uint64_t get_nr_allocs (void);
  50. extern void set_fail_at_alloc (uint64_t);
  51. void *
  52. instrumented_malloc (size_t size)
  53. {
  54. void *res;
  55. if (at_fail_alloc_p ())
  56. return NULL;
  57. res = malloc (size);
  58. if (res != NULL)
  59. nr_allocs++;
  60. return res;
  61. }
  62. void *
  63. instrumented_realloc (void *ptr, size_t size)
  64. {
  65. void *res;
  66. if (size != 0)
  67. {
  68. if (at_fail_alloc_p ())
  69. return NULL;
  70. }
  71. res = realloc (ptr, size);
  72. if (res != NULL)
  73. nr_allocs++;
  74. return res;
  75. }
  76. int
  77. at_fail_alloc_p (void)
  78. {
  79. return fail_at_alloc == nr_allocs + 1;
  80. }
  81. uint64_t
  82. get_nr_allocs (void)
  83. {
  84. return nr_allocs;
  85. }
  86. void
  87. set_fail_at_alloc (uint64_t nr)
  88. {
  89. fail_at_alloc = nr;
  90. }