pastebin - collaborative debugging tool
rovema.kpaste.net RSS


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

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