edtest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* edtest.c -- Test for libbacktrace storage allocation stress handling
  2. Copyright (C) 2017-2021 Free Software Foundation, Inc.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. (1) Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. (2) Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. (3) The name of the author may not be used to
  13. endorse or promote products derived from this software without
  14. specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE. */
  26. #include "config.h"
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include "backtrace.h"
  33. #include "backtrace-supported.h"
  34. #include "internal.h"
  35. #include "testlib.h"
  36. static int test1 (void) __attribute__ ((noinline, noclone, unused));
  37. extern int f2 (int);
  38. extern int f3 (int, int);
  39. static int
  40. test1 (void)
  41. {
  42. /* Returning a value here and elsewhere avoids a tailcall which
  43. would mess up the backtrace. */
  44. return f2 (__LINE__) + 1;
  45. }
  46. int
  47. f3 (int f1line, int f2line)
  48. {
  49. struct info all[20];
  50. struct bdata data;
  51. int f3line;
  52. int i;
  53. data.all = &all[0];
  54. data.index = 0;
  55. data.max = 20;
  56. data.failed = 0;
  57. f3line = __LINE__ + 1;
  58. i = backtrace_full (state, 0, callback_one, error_callback_one, &data);
  59. if (i != 0)
  60. {
  61. fprintf (stderr, "test1: unexpected return value %d\n", i);
  62. data.failed = 1;
  63. }
  64. if (data.index < 3)
  65. {
  66. fprintf (stderr,
  67. "test1: not enough frames; got %zu, expected at least 3\n",
  68. data.index);
  69. data.failed = 1;
  70. }
  71. check ("test1", 0, all, f3line, "f3", "edtest.c", &data.failed);
  72. check ("test1", 1, all, f2line, "f2", "edtest2_build.c", &data.failed);
  73. check ("test1", 2, all, f1line, "test1", "edtest.c", &data.failed);
  74. printf ("%s: backtrace_full alloc stress\n", data.failed ? "FAIL" : "PASS");
  75. if (data.failed)
  76. ++failures;
  77. return failures;
  78. }
  79. int
  80. main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
  81. {
  82. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  83. error_callback_create, NULL);
  84. // Grab the storage allocation lock prior to doing anything interesting.
  85. // The intent here is to insure that the backtrace_alloc code is forced
  86. // to always call mmap() for new memory as opposed to reusing previously
  87. // allocated memory from the free list. Doing things this way helps
  88. // simulate what you might see in a multithreaded program in which there
  89. // are racing calls to the allocator.
  90. struct backtrace_state *state_internal =
  91. (struct backtrace_state *) state;
  92. state_internal->lock_alloc = 1;
  93. // Kick off the test
  94. test1();
  95. exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
  96. }