memtest.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) Christos Zoulas 2021.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice immediately at the beginning of the file, without modification,
  10. * this list of conditions, and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifndef lint
  28. FILE_RCSID("@(#)$File: memtest.c,v 1.3 2022/09/13 18:46:07 christos Exp $")
  29. #endif
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/mman.h>
  33. #include <stdio.h>
  34. #include <stdbool.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <err.h>
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <dlfcn.h>
  41. #include <magic.h>
  42. void *
  43. malloc(size_t len)
  44. {
  45. char buf[512];
  46. void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc");
  47. void *p = (*orig)(len);
  48. int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p);
  49. write(2, buf, l);
  50. return p;
  51. }
  52. void
  53. free(void *p)
  54. {
  55. char buf[512];
  56. void (*orig)(void *) = dlsym(RTLD_NEXT, "free");
  57. (*orig)(p);
  58. int l = snprintf(buf, sizeof(buf), "free %p\n", p);
  59. write(2, buf, l);
  60. }
  61. void *
  62. calloc(size_t len, size_t nitems)
  63. {
  64. char buf[512];
  65. void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc");
  66. void *p = (*orig)(len, nitems);
  67. size_t tot = len * nitems;
  68. int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p);
  69. write(2, buf, l);
  70. return p;
  71. }
  72. void *
  73. realloc(void *q, size_t len)
  74. {
  75. char buf[512];
  76. void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
  77. void *p = (*orig)(q, len);
  78. int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p);
  79. write(2, buf, l);
  80. return p;
  81. }
  82. static void
  83. usage(void)
  84. {
  85. fprintf(stderr, "Usage: test [-b] <filename>\n");
  86. exit(EXIT_FAILURE);
  87. }
  88. int
  89. main(int argc, char *argv[])
  90. {
  91. bool buf = false;
  92. int c;
  93. while ((c = getopt(argc, argv, "b")) != -1)
  94. switch (c) {
  95. case 'b':
  96. buf = true;
  97. break;
  98. default:
  99. usage();
  100. }
  101. argc -= optind;
  102. argv += optind;
  103. if (argc == 0)
  104. usage();
  105. magic_t m = magic_open(0);
  106. if (m == NULL)
  107. err(EXIT_FAILURE, "magic_open");
  108. magic_load(m, NULL);
  109. const char *r;
  110. if (buf) {
  111. int fd = open(argv[0], O_RDONLY);
  112. if (fd == -1)
  113. err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
  114. struct stat st;
  115. if (fstat(fd, &st) == -1)
  116. err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]);
  117. size_t l = (size_t)st.st_size;
  118. void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd,
  119. (off_t)0);
  120. if (p == MAP_FAILED)
  121. err(EXIT_FAILURE, "Cannot map `%s'", argv[0]);
  122. close(fd);
  123. r = magic_buffer(m, p, l);
  124. munmap(p, l);
  125. } else {
  126. r = magic_file(m, argv[0]);
  127. }
  128. magic_close(m);
  129. printf("%s\n", r ? r : "(null)");
  130. return 0;
  131. }