test-dlclose-exit-race.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Test for exit/dlclose race (Bug 22180).
  2. Copyright (C) 2017-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. /* This file must be run from within a directory called "stdlib". */
  16. /* This test verifies that when dlopen in one thread races against exit
  17. in another thread, we don't call registered destructor twice.
  18. Expected result:
  19. second
  20. first
  21. ... clean termination
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <semaphore.h>
  26. #include <support/check.h>
  27. #include <support/xdlfcn.h>
  28. #include <support/xthread.h>
  29. /* Semaphore to ensure we have a happens-before between the first function
  30. starting and exit being called. */
  31. sem_t order1;
  32. /* Semaphore to ensure we have a happens-before between the second function
  33. starting and the first function returning. */
  34. sem_t order2;
  35. void *
  36. exit_thread (void *arg)
  37. {
  38. /* Wait for the dlclose to start... */
  39. sem_wait (&order1);
  40. /* Then try to run the exit sequence which should call all
  41. __cxa_atexit registered functions and in parallel with
  42. the executing dlclose(). */
  43. exit (0);
  44. }
  45. void
  46. last (void)
  47. {
  48. /* Let dlclose thread proceed. */
  49. sem_post (&order2);
  50. }
  51. int
  52. main (void)
  53. {
  54. void *dso;
  55. pthread_t thread;
  56. atexit (last);
  57. dso = xdlopen ("$ORIGIN/test-dlclose-exit-race-helper.so",
  58. RTLD_NOW|RTLD_GLOBAL);
  59. thread = xpthread_create (NULL, exit_thread, NULL);
  60. xdlclose (dso);
  61. xpthread_join (thread);
  62. FAIL_EXIT1 ("Did not terminate via exit(0) in exit_thread() as expected.");
  63. }