pastebin - collaborative debugging tool
rovema.kpaste.net RSS


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