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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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, (13<<8)+12, 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, 0, (8<<8)+2, 173521,
  53. "/usr/bin/dbus-daemon"},
  54. {(void*)0x652000, (void*)0x655000,
  55. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, (8<<8)+2, 173521,
  56. "/usr/bin/dbus-daemon"},
  57. {(void*)0xe3000, (void*)0xe24000,
  58. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, 0, 0,
  59. "[heap]"},
  60. {(void*)0x24000, (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, (13<<8)+12, 135522,
  65. "/usr/lib64/ld-2.15.so"},
  66. {(void*)0x35b1a21000, (void*)0x35b1a22000,
  67. PROT_READ | PROT_WRITE | MAP_PRIVATE, 0x20000, 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. close(fd);
  80. }
  81. END_TEST
  82. START_TEST(test_mmap_get_invalid)
  83. {
  84. child_mmap_l maps;
  85. int ret = child_mmap_get(-1, &maps);
  86. ck_assert_int_eq(ret, -1);
  87. }
  88. END_TEST
  89. START_TEST(test_mmap_get_self)
  90. {
  91. child_mmap_l maps;
  92. pid_t spid = getpid();
  93. child_mmap_init(&maps);
  94. int ret = child_mmap_get(spid, &maps);
  95. ck_assert_int_eq(ret, 0);
  96. }
  97. END_TEST
  98. START_TEST(test_shrink_map)
  99. {
  100. child_mmap_l maps;
  101. int ret, fd;
  102. fd = open(PARSE_FD_SAMPLE, O_RDONLY);
  103. if(fd < 0)
  104. {
  105. perror("Unable to open sample");
  106. ck_abort_msg("Unable to open sample");
  107. }
  108. child_mmap_init(&maps);
  109. ret = child_mmap_get_fd(fd, &maps);
  110. ck_assert_int_eq(ret, 0);
  111. close(fd);
  112. fd = open(PARSE_FD_SAMPLE_SM, O_RDONLY);
  113. if(fd < 0)
  114. {
  115. perror("Unable to open sample");
  116. ck_abort_msg("Unable to open sample");
  117. }
  118. ret = child_mmap_get_fd(fd, &maps);
  119. ck_assert_int_eq(ret, 0);
  120. close(fd);
  121. }
  122. ASMSH_CHECK_START("/proc/[pid]/map file parser", "parsing tests")
  123. ASMSH_ADD_TEST(test_parse_line);
  124. ASMSH_ADD_TEST(test_parse_fd);
  125. ASMSH_ADD_TEST(test_mmap_get_invalid);
  126. ASMSH_ADD_TEST(test_mmap_get_self);
  127. ASMSH_ADD_TEST(test_shrink_map);
  128. ASMSH_CHECK_END