pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Intercepting pthread_mutext_init() via LD_PRELOAD
Posted by Anonymous on Wed 6th Jul 2022 11:58
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 12:57:16.111555681 +0200
  3. @@ -0,0 +1,64 @@
  4. +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
  5. +
  6. +#include <pthread.h>
  7. +#include <unistd.h>
  8. +#include <errno.h>
  9. +#include <stdio.h>
  10. +#include <string.h>
  11. +#include <stdbool.h>
  12. +#include <dlfcn.h>
  13. +
  14. +static int once = 0;
  15. +
  16. +#define STDERR_MSG(msg) \
  17. +       (void)write(2, (msg), strlen(msg))
  18. +
  19. +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  20. +               const pthread_mutexattr_t *restrict attr) {
  21. +       pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
  22. +       int pmi_res;
  23. +       int pmi_errno;
  24. +
  25. +       const char *mymessage = "pthread_mutex_init_log.so: In our own pthread_mutex_init()\n";
  26. +       /* just print message ONCE */
  27. +       if (once++ == 0)
  28. +               STDERR_MSG(mymessage);
  29. +
  30. +       int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
  31. +               const pthread_mutexattr_t *restrict attr);
  32. +       original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
  33. +
  34. +       pthread_mutexattr_t myattr;
  35. +       bool myattr_used = false;
  36. +
  37. +       if (!xattr) {
  38. +               pthread_mutexattr_init(&myattr);
  39. +               myattr_used = true;
  40. +               xattr = &myattr;
  41. +       }
  42. +
  43. +       int mutex_protocol = -666;
  44. +       (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
  45. +       if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
  46. +               char msgbuff[1024];
  47. +      
  48. +               pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
  49. +               snprintf(msgbuff,
  50. +                       sizeof(msgbuff),
  51. +                       "pthread_mutex_init_log.so: "
  52. +                       "PTHREAD_PRIO_INHERIT enforced for %lx, was %d\n",
  53. +                       (void *)mutex,
  54. +                       mutex_protocol);
  55. +               STDERR_MSG(msgbuff);
  56. +       }
  57. +
  58. +       pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
  59. +       pmi_errno = errno;
  60. +
  61. +       if (myattr_used) {
  62. +               pthread_mutexattr_destroy(&myattr);
  63. +       }
  64. +
  65. +       errno = pmi_errno;
  66. +       return pmi_res;
  67. +}
  68. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  69. +++ test1.c     2022-07-06 11:16:22.874096153 +0200
  70. @@ -0,0 +1,16 @@
  71. +#include <pthread.h>
  72. +#include <errno.h>
  73. +#include <stdio.h>
  74. +
  75. +int main(void) {
  76. +       (void)puts("# start.");
  77. +
  78. +       pthread_mutex_t mutex;
  79. +      
  80. +       pthread_mutex_init(&mutex, NULL);
  81. +       perror("pthread_mutex_init() result");
  82. +
  83. +       (void)puts("# end.");
  84. +
  85. +       return 0;
  86. +}
  87. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  88. +++ Makefile    2022-07-06 12:55:59.122798683 +0200
  89. @@ -0,0 +1,14 @@
  90. +
  91. +pthread_mutex_init_log.o: pthread_mutex_init_log.c
  92. +       gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
  93. +pthread_mutex_init_log.so: pthread_mutex_init_log.o
  94. +       gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -lpthread -o pthread_mutex_init_log.so
  95. +
  96. +test1: test1.c
  97. +       gcc -std=gnu11 -g test1.c -o test1
  98. +      
  99. +run_test: test1 pthread_mutex_init_log.so
  100. +       ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
  101. +
  102. +clean:
  103. +       rm -f *.o *.so test1

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