memtest.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <sys/mman.h>
  30. #include <stdio.h>
  31. #include <stdbool.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <err.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <dlfcn.h>
  38. #include <magic.h>
  39. void *
  40. malloc(size_t len)
  41. {
  42. char buf[512];
  43. void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc");
  44. void *p = (*orig)(len);
  45. int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p);
  46. write(2, buf, l);
  47. return p;
  48. }
  49. void
  50. free(void *p)
  51. {
  52. char buf[512];
  53. void (*orig)(void *) = dlsym(RTLD_NEXT, "free");
  54. (*orig)(p);
  55. int l = snprintf(buf, sizeof(buf), "free %p\n", p);
  56. write(2, buf, l);
  57. }
  58. void *
  59. calloc(size_t len, size_t nitems)
  60. {
  61. char buf[512];
  62. void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc");
  63. void *p = (*orig)(len, nitems);
  64. size_t tot = len * nitems;
  65. int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p);
  66. write(2, buf, l);
  67. return p;
  68. }
  69. void *
  70. realloc(void *q, size_t len)
  71. {
  72. char buf[512];
  73. void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
  74. void *p = (*orig)(q, len);
  75. int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p);
  76. write(2, buf, l);
  77. return p;
  78. }
  79. static void
  80. usage(void)
  81. {
  82. fprintf(stderr, "Usage: test [-b] <filename>\n");
  83. exit(EXIT_FAILURE);
  84. }
  85. int
  86. main(int argc, char *argv[])
  87. {
  88. bool buf = false;
  89. int c;
  90. while ((c = getopt(argc, argv, "b")) != -1)
  91. switch (c) {
  92. case 'b':
  93. buf = true;
  94. break;
  95. default:
  96. usage();
  97. }
  98. argc -= optind;
  99. argv += optind;
  100. if (argc == 0)
  101. usage();
  102. magic_t m = magic_open(0);
  103. if (m == NULL)
  104. err(EXIT_FAILURE, "magic_open");
  105. magic_load(m, NULL);
  106. const char *r;
  107. if (buf) {
  108. int fd = open(argv[0], O_RDONLY);
  109. if (fd == -1)
  110. err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
  111. struct stat st;
  112. if (fstat(fd, &st) == -1)
  113. err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]);
  114. size_t l = (size_t)st.st_size;
  115. void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd,
  116. (off_t)0);
  117. if (p == MAP_FAILED)
  118. err(EXIT_FAILURE, "Cannot map `%s'", argv[0]);
  119. close(fd);
  120. r = magic_buffer(m, p, l);
  121. munmap(p, l);
  122. } else {
  123. r = magic_file(m, argv[0]);
  124. }
  125. magic_close(m);
  126. printf("%s\n", r ? r : "(null)");
  127. return 0;
  128. }