tst-bz20544.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Verify atexit, on_exit, etc. abort on NULL function pointer.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <signal.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <support/capture_subprocess.h>
  20. #include <support/check.h>
  21. #include <support/support.h>
  22. #include <support/test-driver.h>
  23. extern int __cxa_atexit (void (*func) (void *), void *arg, void *d);
  24. extern int __cxa_at_quick_exit (void (*func) (void *), void *arg, void *d);
  25. /* GCC "knows" that atexit and on_exit should not be called with NULL
  26. function pointer, and emits diagnostics if we try to do so.
  27. Presumably it could emit a trap and drop the call altogether.
  28. The aliases below are intended to bypass this. */
  29. extern int atexit_alias (void (*) (void)) __asm__ ("atexit");
  30. extern int at_quick_exit_alias (void (*) (void)) __asm__ ("at_quick_exit");
  31. extern int on_exit_alias (void (*) (void), void *) __asm__ ("on_exit");
  32. static void
  33. test_bz20544_atexit (void *closure)
  34. {
  35. atexit_alias (NULL); /* Should assert. */
  36. exit (EXIT_FAILURE);
  37. }
  38. static void
  39. test_bz20544_at_quick_exit (void *closure)
  40. {
  41. at_quick_exit_alias (NULL); /* Should assert. */
  42. exit (EXIT_FAILURE);
  43. }
  44. static void
  45. test_bz20544_on_exit (void *closure)
  46. {
  47. on_exit_alias (NULL, NULL); /* Should assert. */
  48. exit (EXIT_FAILURE);
  49. }
  50. static void
  51. test_bz20544_cxa_atexit (void *closure)
  52. {
  53. __cxa_atexit (NULL, NULL, NULL); /* Should assert. */
  54. exit (EXIT_FAILURE);
  55. }
  56. static void
  57. test_bz20544_cxa_at_quick_exit (void *closure)
  58. {
  59. __cxa_at_quick_exit (NULL, NULL, NULL); /* Should assert. */
  60. exit (EXIT_FAILURE);
  61. }
  62. static void
  63. test_one_fn (void (*test_fn) (void *))
  64. {
  65. const char expected_error[] = "Assertion `func != NULL' failed.\n";
  66. struct support_capture_subprocess result;
  67. result = support_capture_subprocess (test_fn, NULL);
  68. support_capture_subprocess_check (&result, "bz20544", -SIGABRT,
  69. sc_allow_stderr);
  70. if (strstr (result.err.buffer, expected_error) == NULL)
  71. {
  72. support_record_failure ();
  73. printf ("Did not find expected string in error output:\n"
  74. " expected: >>>%s<<<\n"
  75. " actual: >>>%s<<<\n",
  76. expected_error, result.err.buffer);
  77. }
  78. support_capture_subprocess_free (&result);
  79. }
  80. static int
  81. do_test (void)
  82. {
  83. #if defined (NDEBUG)
  84. FAIL_UNSUPPORTED ("Assertions disabled (NDEBUG). "
  85. "Can't verify that assertions fire.");
  86. #endif
  87. test_one_fn (test_bz20544_atexit);
  88. test_one_fn (test_bz20544_at_quick_exit);
  89. test_one_fn (test_bz20544_on_exit);
  90. test_one_fn (test_bz20544_cxa_atexit);
  91. test_one_fn (test_bz20544_cxa_at_quick_exit);
  92. return 0;
  93. }
  94. #include <support/test-driver.c>