memtest.c 3.6 KB

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