A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests_mmap.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <check.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include "asmsh_check.h"
  7. #include "mmap_parse.h"
  8. #define PARSE_FD_SAMPLE "samples/procfs_pid_maps"
  9. #define PARSE_FD_SAMPLE_SM "samples/procfs_pid_maps_sm"
  10. typedef struct {
  11. child_mmap_t map;
  12. char *line;
  13. } mmap_linecheck_t;
  14. START_TEST (test_parse_line)
  15. {
  16. child_mmap_t res;
  17. size_t i;
  18. mmap_linecheck_t lcheck[] = {
  19. {{(void*)0x123456, (void*)0x654321,
  20. MAP_PRIVATE, 0, 0, 0, "[stack]"},
  21. "123456-654321 ---p 00000000 0:0 0 [stack]"},
  22. {{(void*)0, (void*)0x1000,
  23. PROT_READ | PROT_WRITE | PROT_EXEC | MAP_SHARED,
  24. 1, (0x13<<8)+0x12, 123, "[stack]"},
  25. "0-01000 rwxs 00000001 13:12 123 [stack]"},
  26. {{(void*)0x35b1800000, (void*)0x35b1820000,
  27. PROT_READ | PROT_EXEC | MAP_PRIVATE, 0x1f000, (8<<8)+2, 135522, "/usr/lib64/ld-2.15.so"},
  28. "35b1800000-35b1820000 r-xp 0001f000 08:02 135522 /usr/lib64/ld-2.15.so"},
  29. };
  30. for(i=0; i<sizeof(lcheck) / sizeof(mmap_linecheck_t); i++)
  31. {
  32. ck_assert_int_eq(child_mmap_parseline(lcheck[i].line, &res), 0);
  33. ck_assert_ptr_eq(lcheck[i].map.start, res.start);
  34. ck_assert_ptr_eq(lcheck[i].map.stop, res.stop);
  35. ck_assert_int_eq(lcheck[i].map.perm, res.perm);
  36. ck_assert(lcheck[i].map.offset == res.offset);
  37. ck_assert(lcheck[i].map.device == res.device);
  38. ck_assert(lcheck[i].map.inode == res.inode);
  39. ck_assert_str_eq(lcheck[i].map.pathname, res.pathname);
  40. }
  41. }
  42. END_TEST
  43. START_TEST (test_parse_fd)
  44. {
  45. child_mmap_l maps;
  46. int fd, ret;
  47. child_mmap_t checks[] = {
  48. {(void*)0x400000, (void*)0x452000,
  49. PROT_READ | PROT_EXEC | MAP_PRIVATE, 0, (8<<8)+2, 173521,
  50. "/usr/bin/dbus-daemon"},
  51. {(void*)0x651000, (void*)0x652000,
  52. PROT_READ | MAP_PRIVATE, 0x51000, (8<<8)+2, 173521,
  53. "/usr/bin/dbus-daemon"},
  54. {(void*)0x652000, (void*)0x655000,
  55. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0x52000, (8<<8)+2, 173521,
  56. "/usr/bin/dbus-daemon"},
  57. {(void*)0xe03000, (void*)0xe24000,
  58. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, 0, 0,
  59. "[heap]"},
  60. {(void*)0xe24000, (void*)0x11f7000,
  61. PROT_READ | PROT_WRITE | MAP_SHARED, 0, 0, 0,
  62. "[heap]"},
  63. {(void*)0x35b1a20000, (void*)0x35b1a21000,
  64. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0x20000, (0x13<<8)+0x12, 135522,
  65. "/usr/lib64/ld-2.15.so"},
  66. {(void*)0x35b1a21000, (void*)0x35b1a22000,
  67. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, 0, 0,
  68. ""},
  69. };
  70. child_mmap_init(&maps);
  71. fd = open(PARSE_FD_SAMPLE, O_RDONLY);
  72. if(fd < 0)
  73. {
  74. perror("Unable to open sample");
  75. ck_abort_msg("Unable to open sample");
  76. }
  77. ret = child_mmap_get_fd(fd, &maps);
  78. ck_assert_int_eq(ret, 0);
  79. int i;
  80. for(i=0; i<sizeof(checks)/sizeof(*checks);i++)
  81. {
  82. dprintf(2, "line %d\n", i);
  83. child_mmap_t *check, *res;
  84. check = &checks[i];
  85. res = &maps.maps[i];
  86. ck_assert_ptr_eq(check->start, res->start);
  87. ck_assert_ptr_eq(check->stop, res->stop);
  88. ck_assert_int_eq(check->perm, res->perm);
  89. ck_assert_int_eq(check->offset, res->offset);
  90. ck_assert_mem_eq(&check->device, &res->device, sizeof(check->device));
  91. ck_assert_mem_eq(&check->inode, &res->inode, sizeof(check->inode));
  92. ck_assert_str_eq(check->pathname, res->pathname);
  93. }
  94. ck_assert_int_eq(i, maps.size);
  95. close(fd);
  96. }
  97. END_TEST
  98. START_TEST(test_mmap_get_invalid)
  99. {
  100. child_mmap_l maps;
  101. int ret = child_mmap_get(-1, &maps);
  102. ck_assert_int_eq(ret, -1);
  103. }
  104. END_TEST
  105. START_TEST(test_mmap_get_self)
  106. {
  107. child_mmap_l maps;
  108. pid_t spid = getpid();
  109. child_mmap_init(&maps);
  110. int ret = child_mmap_get(spid, &maps);
  111. ck_assert_int_eq(ret, 0);
  112. }
  113. END_TEST
  114. START_TEST(test_shrink_map)
  115. {
  116. child_mmap_l maps;
  117. int ret, fd;
  118. fd = open(PARSE_FD_SAMPLE, O_RDONLY);
  119. if(fd < 0)
  120. {
  121. perror("Unable to open sample");
  122. ck_abort_msg("Unable to open sample");
  123. }
  124. child_mmap_init(&maps);
  125. ret = child_mmap_get_fd(fd, &maps);
  126. ck_assert_int_eq(ret, 0);
  127. close(fd);
  128. fd = open(PARSE_FD_SAMPLE_SM, O_RDONLY);
  129. if(fd < 0)
  130. {
  131. perror("Unable to open sample");
  132. ck_abort_msg("Unable to open sample");
  133. }
  134. ret = child_mmap_get_fd(fd, &maps);
  135. ck_assert_int_eq(ret, 0);
  136. close(fd);
  137. }
  138. ASMSH_CHECK_START("/proc/[pid]/map file parser", "parsing tests")
  139. ASMSH_ADD_TEST(test_parse_line);
  140. ASMSH_ADD_TEST(test_parse_fd);
  141. ASMSH_ADD_TEST(test_mmap_get_invalid);
  142. ASMSH_ADD_TEST(test_mmap_get_self);
  143. ASMSH_ADD_TEST(test_shrink_map);
  144. ASMSH_CHECK_END