thread-test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * source URL:
  3. * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=67838
  4. */
  5. /*
  6. * Author: Petter Reinholdtsen <pere@hungry.com>
  7. * Date: 2002-02-13
  8. * Lisence: GPL
  9. *
  10. * Small test program to demonstrate efence crashing on multithreaded
  11. * programs.
  12. *
  13. * Link it like this, and it crashes with segfault.
  14. * cc -o efence-thread-test efence-thread-test.c -lpthread -lefence
  15. * Remove the -lefence, and it work as it should.
  16. * The output from gdb when it fails:
  17. *
  18. * Program received signal SIGSEGV, Segmentation fault.
  19. * [Switching to Thread 1026 (LWP 1189)]
  20. * 0x400382c7 in memalign () from /usr/lib/libefence.so.0
  21. * (gdb) bt
  22. * #0 0x400382c7 in memalign () from /usr/lib/libefence.so.0
  23. * #1 0x4003873c in malloc () from /usr/lib/libefence.so.0
  24. * #2 0x400a0811 in fopen () from /lib/libc.so.6
  25. * #3 0x080488e8 in thread_func ()
  26. * #4 0x40027f0a in pthread_start_thread () from /lib/libpthread.so.0
  27. * #5 0x40027f51 in pthread_start_thread_event () from /lib/libpthread.so.0
  28. */
  29. /* check for pthread library */
  30. #if ( !defined( WIN32 ) || defined( __CYGWIN__ ))
  31. #define HAVE_PTHREADS 1
  32. #else /* if ( !defined( WIN32 ) || defined( __CYGWIN__ )) */
  33. #define HAVE_PTHREADS 0
  34. #endif /* if ( !defined( WIN32 ) || defined( __CYGWIN__ )) */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #if HAVE_PTHREADS
  38. #include "../duma.h"
  39. #include <pthread.h>
  40. static pthread_mutex_t mutex;
  41. static volatile int threads_left = 2;
  42. static void *
  43. thread_func(void *arg)
  44. {
  45. int i = 2000;
  46. char *name = (char *)arg;
  47. while (--i)
  48. {
  49. if (pthread_mutex_lock(&mutex))
  50. {
  51. fprintf(stderr, "error: %s failed to lock mutex.\n", name);
  52. exit(1);
  53. }
  54. printf("%s : %d\n", name, i);
  55. if (pthread_mutex_unlock(&mutex))
  56. {
  57. fprintf(stderr, "error: %s failed to unlock mutex.\n", name);
  58. exit(1);
  59. }
  60. {
  61. /* Try to trigger efence error */
  62. FILE *fp = fopen("/etc/resolv.conf", "r");
  63. if (NULL != fp)
  64. {
  65. char buf[1024];
  66. fread(buf, sizeof ( buf ), 1, fp);
  67. fclose(fp);
  68. }
  69. }
  70. }
  71. --threads_left;
  72. return ( NULL );
  73. }
  74. static void *
  75. idle_func(void *arg)
  76. {
  77. (void)arg;
  78. while (threads_left)
  79. {
  80. ;
  81. }
  82. /* NOTREACHED */
  83. return ( NULL );
  84. }
  85. int
  86. main(int argc, char **argv)
  87. {
  88. pthread_t hello_thread, goodbye_thread;
  89. (void)argc;
  90. (void)argv;
  91. pthread_mutex_init(&mutex, NULL);
  92. if (pthread_create(&hello_thread, NULL, thread_func, (void *)"hello"))
  93. {
  94. fprintf(stderr, "Failed to create hello thread\n");
  95. exit(1);
  96. }
  97. if (pthread_create(&goodbye_thread, NULL, thread_func, (void *)"goodbye"))
  98. {
  99. fprintf(stderr, "Failed to create goodbye thread\n");
  100. exit(1);
  101. }
  102. idle_func(NULL);
  103. pthread_mutex_destroy(&mutex);
  104. /* NOTREACHED */
  105. return ( 0 );
  106. }
  107. #else /* if HAVE_PTHREADS */
  108. int
  109. main(int argc, char **argv)
  110. {
  111. (void)argc;
  112. (void)argv;
  113. printf("Test not implemented for Win32 and Mingw\n");
  114. return ( 0 );
  115. }
  116. #endif /* if HAVE_PTHREADS */