pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Intercepting pthread_mutext_init() via LD_PRELOAD
Posted by Anonymous on Wed 6th Jul 2022 18:29
raw | new post
view followups (newest first): Intercepting pthread_mutext_init() via LD_PRELOAD by Anonymous
modification of post by Anonymous (view diff)

  1. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  2. +++ pthread_interceptor.c       2022-07-06 19:28:52.893103035 +0200
  3. @@ -0,0 +1,90 @@
  4. +/*
  5. + * pthread_interceptor.c
  6. + *
  7. + * intercept pthread calls
  8. + * (like |pthread_mutex_init()|) via LD_PRELOAD
  9. + *
  10. + * Example usage:
  11. + * $ ksh93 -c 'LD_PRELOAD="$PWD/pthread_interceptor.so" ./test_prog'
  12. + *
  13. + * Written by Roland Mainz <roland.mainz@nrubsig.org>
  14. + */
  15. +
  16. +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
  17. +
  18. +#include <pthread.h>
  19. +#include <unistd.h>
  20. +#include <errno.h>
  21. +#include <stdio.h>
  22. +#include <string.h>
  23. +#include <stdbool.h>
  24. +#include <dlfcn.h>
  25. +
  26. +/* config */
  27. +#define PICFG_PRINT_ONCE_MSG 1
  28. +#define PICFG_ENFORCE_PRIO_INHERIT 1
  29. +
  30. +/* misc */
  31. +#define STDERR_MSG(msg) \
  32. +       (void)write(STDERR_FILENO, (msg), strlen(msg))
  33. +
  34. +
  35. +/*
  36. + * interceptor for |pthread_mutex_init()|
  37. + */
  38. +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  39. +               const pthread_mutexattr_t *restrict attr) {
  40. +       pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
  41. +       int pmi_res;
  42. +       int pmi_errno;
  43. +
  44. +#if PICFG_PRINT_ONCE_MSG
  45. +       static int once = 0;
  46. +       const char *mymessage = "pthread_interceptor.so: "
  47. +               "Once: In our own pthread_mutex_init()\n";     
  48. +       /* just print message ONCE (BUG: every INT_MAX times) */
  49. +       if (once++ == 0)
  50. +               STDERR_MSG(mymessage);
  51. +#endif /* PICFG_PRINT_ONCE_MSG */
  52. +
  53. +       int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
  54. +               const pthread_mutexattr_t *restrict attr);
  55. +       original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
  56. +
  57. +       pthread_mutexattr_t myattr;
  58. +       bool myattr_used = false;
  59. +
  60. +       if (!xattr) {
  61. +               (void)pthread_mutexattr_init(&myattr);
  62. +               myattr_used = true;
  63. +               xattr = &myattr;
  64. +       }
  65. +
  66. +#if PICFG_ENFORCE_PRIO_INHERIT
  67. +       int mutex_protocol = -666;
  68. +       (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
  69. +       if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
  70. +               char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
  71. +      
  72. +               (void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
  73. +               (void)snprintf(msgbuff,
  74. +                       sizeof(msgbuff),
  75. +                       "pthread_interceptor.so: "
  76. +                       "PTHREAD_PRIO_INHERIT enforced "
  77. +                       "for mutex=0x%lx, was protocol=%d\n",
  78. +                       (unsigned long)(void *)mutex,
  79. +                       mutex_protocol);
  80. +               STDERR_MSG(msgbuff);
  81. +       }
  82. +#endif /* PICFG_ENFORCE_PRIO_INHERIT */
  83. +
  84. +       pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
  85. +       pmi_errno = errno;
  86. +
  87. +       if (myattr_used) {
  88. +               (void)pthread_mutexattr_destroy(&myattr);
  89. +       }
  90. +
  91. +       errno = pmi_errno;
  92. +       return pmi_res;
  93. +}
  94. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  95. +++ test1.c     2022-07-06 13:52:16.151772792 +0200
  96. @@ -0,0 +1,27 @@
  97. +/*
  98. + * test1 for pthread_interceptor.c
  99. + *
  100. + */
  101. +
  102. +#include <stdlib.h>
  103. +#include <stdio.h>
  104. +#include <pthread.h>
  105. +#include <errno.h>
  106. +
  107. +int main(int ac, char *av[])
  108. +{
  109. +       (void)puts("# start.");
  110. +
  111. +       pthread_mutex_t mutex1;
  112. +       pthread_mutex_t mutex2;
  113. +      
  114. +       (void)pthread_mutex_init(&mutex1, NULL);
  115. +       perror("pthread_mutex_init(1) result");
  116. +
  117. +       (void)pthread_mutex_init(&mutex2, NULL);
  118. +       perror("pthread_mutex_init(2) result");
  119. +
  120. +       (void)puts("# end.");
  121. +
  122. +       return EXIT_SUCCESS;
  123. +}
  124. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  125. +++ Makefile    2022-07-06 19:26:43.063939487 +0200
  126. @@ -0,0 +1,34 @@
  127. +#
  128. +# Makefile for LD_PRELOAD pthreadinterceptor
  129. +#
  130. +
  131. +CSTDFLAGS=-std=c11
  132. +CARCHFLAGS=-m64 # -m32 or -m64
  133. +CC=gcc # gcc or clang
  134. +
  135. +#
  136. +# build LD_PRELOAD function interceptor
  137. +#
  138. +pthread_interceptor.o: pthread_interceptor.c
  139. +       $(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_interceptor.c -fPIC -c -o pthread_interceptor.o
  140. +pthread_interceptor.so: pthread_interceptor.o
  141. +       $(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_interceptor.o -fPIC -shared -ldl -lpthread -o pthread_interceptor.so
  142. +
  143. +#
  144. +# tests
  145. +#
  146. +test1: test1.c
  147. +       $(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g test1.c -o test1
  148. +      
  149. +run_test: test1 pthread_interceptor.so
  150. +       ksh93 -c 'LD_PRELOAD="$$PWD/pthread_interceptor.so" ./test1'
  151. +
  152. +#
  153. +# misc
  154. +#
  155. +all:   run_test
  156. +
  157. +clean:
  158. +       rm -f *.o *.so test1
  159. +
  160. +# EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at