nodelete.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <dlfcn.h>
  2. #include <setjmp.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. static sigjmp_buf jmpbuf;
  7. int fini_ran;
  8. static void
  9. __attribute__ ((noreturn))
  10. handler (int sig)
  11. {
  12. siglongjmp (jmpbuf, 1);
  13. }
  14. #define TEST_FUNCTION do_test ()
  15. static int
  16. do_test (void)
  17. {
  18. /* We are testing the two possibilities to mark an object as not deletable:
  19. - marked on the linker commandline with `-z nodelete'
  20. - with the RTLD_NODELETE flag at dlopen()-time.
  21. The test we are performing should be safe. We are loading the objects,
  22. get the address of variables in the respective object, unload the object
  23. and then try to read the variable. If the object is unloaded this
  24. should lead to an segmentation fault. */
  25. void *p;
  26. struct sigaction sa;
  27. sa.sa_handler = handler;
  28. sigfillset (&sa.sa_mask);
  29. sa.sa_flags = SA_RESTART;
  30. if (sigaction (SIGSEGV, &sa, NULL) == -1)
  31. puts ("cannot install signal handler: %m");
  32. p = dlopen ("nodelmod1.so", RTLD_LAZY);
  33. if (p == NULL)
  34. {
  35. printf ("failed to load \"nodelmod1.so\": %s\n", dlerror ());
  36. exit (1);
  37. }
  38. else
  39. {
  40. int *varp;
  41. varp = dlsym (p, "var1");
  42. if (varp == NULL)
  43. {
  44. puts ("failed to get address of \"var1\" in \"nodelmod1.so\"");
  45. exit (1);
  46. }
  47. else
  48. {
  49. *varp = 20000720;
  50. /* Now close the object. */
  51. fini_ran = 0;
  52. if (dlclose (p) != 0)
  53. {
  54. puts ("failed to close \"nodelmod1.so\"");
  55. exit (1);
  56. }
  57. else if (! sigsetjmp (jmpbuf, 1))
  58. {
  59. /* Access the variable again. */
  60. if (*varp != 20000720)
  61. {
  62. puts ("\"var1\" value not correct");
  63. exit (1);
  64. }
  65. else if (fini_ran != 0)
  66. {
  67. puts ("destructor of \"nodelmod1.so\" ran");
  68. exit (1);
  69. }
  70. else
  71. puts ("-z nodelete test succeeded");
  72. }
  73. else
  74. {
  75. /* We caught an segmentation fault. */
  76. puts ("\"nodelmod1.so\" got deleted!");
  77. exit (1);
  78. }
  79. }
  80. }
  81. p = dlopen ("nodelmod2.so", RTLD_LAZY | RTLD_NODELETE);
  82. if (p == NULL)
  83. {
  84. printf ("failed to load \"nodelmod2.so\": %s\n", dlerror ());
  85. exit (1);
  86. }
  87. else
  88. {
  89. int *varp;
  90. varp = dlsym (p, "var2");
  91. if (varp == NULL)
  92. {
  93. puts ("failed to get address of \"var2\" in \"nodelmod2.so\"");
  94. exit (1);
  95. }
  96. else
  97. {
  98. *varp = 42;
  99. /* Now close the object. */
  100. fini_ran = 0;
  101. if (dlclose (p) != 0)
  102. {
  103. puts ("failed to close \"nodelmod2.so\"");
  104. exit (1);
  105. }
  106. else if (! sigsetjmp (jmpbuf, 1))
  107. {
  108. /* Access the variable again. */
  109. if (*varp != 42)
  110. {
  111. puts ("\"var2\" value not correct");
  112. exit (1);
  113. }
  114. else if (fini_ran != 0)
  115. {
  116. puts ("destructor of \"nodelmod2.so\" ran");
  117. exit (1);
  118. }
  119. else
  120. puts ("RTLD_NODELETE test succeeded");
  121. }
  122. else
  123. {
  124. /* We caught an segmentation fault. */
  125. puts ("\"nodelmod2.so\" got deleted!");
  126. exit (1);
  127. }
  128. }
  129. }
  130. p = dlopen ("nodelmod3.so", RTLD_LAZY);
  131. if (p == NULL)
  132. {
  133. printf ("failed to load \"nodelmod3.so\": %s\n", dlerror ());
  134. exit (1);
  135. }
  136. else
  137. {
  138. int *(*fctp) (void);
  139. fctp = dlsym (p, "addr");
  140. if (fctp == NULL)
  141. {
  142. puts ("failed to get address of \"addr\" in \"nodelmod3.so\"");
  143. exit (1);
  144. }
  145. else
  146. {
  147. int *varp = fctp ();
  148. *varp = -1;
  149. /* Now close the object. */
  150. fini_ran = 0;
  151. if (dlclose (p) != 0)
  152. {
  153. puts ("failed to close \"nodelmod3.so\"");
  154. exit (1);
  155. }
  156. else if (! sigsetjmp (jmpbuf, 1))
  157. {
  158. /* Access the variable again. */
  159. if (*varp != -1)
  160. {
  161. puts ("\"var_in_mod4\" value not correct");
  162. exit (1);
  163. }
  164. else if (fini_ran != 0)
  165. {
  166. puts ("destructor of \"nodelmod4.so\" ran");
  167. exit (1);
  168. }
  169. else
  170. puts ("-z nodelete in dependency succeeded");
  171. }
  172. else
  173. {
  174. /* We caught an segmentation fault. */
  175. puts ("\"nodelmod4.so\" got deleted!");
  176. exit (1);
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. #include "../test-skeleton.c"