A shell that runs x86_64 assembly
c
x86-64
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

tests_asm_env.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "config.h"
  2. #include <check.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include "asmsh_check.h"
  8. #include "asm_env.h"
  9. #include "compile.h"
  10. #define REAL_CHILD "../child"
  11. #define STAMP_CHILD "samples/asm_env_stamps"
  12. static const long int stamp1 = 0x4242; // %r15
  13. static const long int stamp2 = -1; // %r14
  14. START_TEST(test_env)
  15. {
  16. const char childpath[] = REAL_CHILD;
  17. asmsh_env_t *env = asmsh_env(childpath);
  18. ck_assert_ptr_nonnull(env);
  19. ck_assert_str_eq(childpath, env->childpath);
  20. asmsh_env_free(env);
  21. }
  22. END_TEST
  23. START_TEST(test_asm_stamps)
  24. {
  25. const char childpath[] = STAMP_CHILD;
  26. asmsh_env_t *env = asmsh_env(childpath);
  27. ck_assert_ptr_nonnull(env);
  28. ck_assert_str_eq(env->childpath, childpath);
  29. ck_assert_mem_eq(&stamp1, &(env->regs.r15), 8);
  30. ck_assert_mem_eq(&stamp2, &(env->regs.r14), 8);
  31. asmsh_env_free(env);
  32. }
  33. END_TEST
  34. START_TEST(test_env_update)
  35. {
  36. const char childpath[] = STAMP_CHILD;
  37. asmsh_env_t *env = asmsh_env(childpath);
  38. ck_assert_ptr_nonnull(env);
  39. ck_assert_str_eq(env->childpath, childpath);
  40. asmsh_env_update_maps(env);
  41. asmsh_env_free(env);
  42. }
  43. END_TEST
  44. START_TEST(test_env_exit42)
  45. {
  46. const char childpath[] = REAL_CHILD;
  47. asmsh_asmc_ctx_t *cctx;
  48. asmsh_bytecode_t bcode;
  49. int ret, status;
  50. asmsh_env_t *env = asmsh_env(childpath);
  51. ck_assert_ptr_nonnull(env);
  52. cctx = asmsh_asmc_ctx_default();
  53. ck_assert_ptr_nonnull(cctx);
  54. ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $60, %rax", &bcode), 0);
  55. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  56. ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $42, %rdi", &bcode), 0);
  57. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  58. ck_assert_int_ge(asmsh_asmc_compile(cctx, "syscall", &bcode), 0);
  59. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  60. asmsh_asmc_ctx_free(cctx);
  61. for(int i=0; i<2; i++)
  62. {
  63. ret = asmsh_env_step(env, &status);
  64. ck_assert_int_eq(ret, 0);
  65. ck_assert_msg(WCOREDUMP(status) == 0, "SEGFAULT");
  66. ck_assert_int_ne(WIFSTOPPED(status), 0);
  67. ck_assert_int_eq(status>>8, 5);
  68. }
  69. ret = asmsh_env_step(env, &status);
  70. ck_assert_int_eq(ret, 1);
  71. ck_assert(WIFEXITED(status));
  72. ck_assert_int_eq(WEXITSTATUS(status), 42);
  73. asmsh_env_free(env);
  74. }
  75. END_TEST
  76. START_TEST(test_env_steps)
  77. {
  78. const char childpath[] = REAL_CHILD;
  79. const char *instr[] = {
  80. "mov $60, %rax",
  81. "mov $0x42,%rdi",
  82. "syscall",
  83. };
  84. asmsh_asmc_ctx_t *cctx;
  85. asmsh_bytecode_t bcode;
  86. int ret, status, i;
  87. asmsh_env_t *env = asmsh_env(childpath);
  88. ck_assert_ptr_nonnull(env);
  89. cctx = asmsh_asmc_ctx_default();
  90. ck_assert_ptr_nonnull(cctx);
  91. for(i=0; i<(sizeof(instr)/sizeof(*instr))-1; i++)
  92. {
  93. ck_assert_int_ge(asmsh_asmc_compile(cctx, instr[i], &bcode), 0);
  94. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  95. ret = asmsh_env_step(env, &status);
  96. ck_assert_int_eq(ret, 0);
  97. ck_assert_msg(WCOREDUMP(status) == 0, "SEGFAULT");
  98. ck_assert_int_ne(WIFSTOPPED(status), 0);
  99. ck_assert_int_eq(status>>8, 5);
  100. }
  101. ck_assert_int_ge(asmsh_asmc_compile(cctx, instr[i], &bcode), 0);
  102. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  103. ret = asmsh_env_step(env, &status);
  104. ck_assert_int_eq(ret, 1);
  105. ck_assert(WIFEXITED(status));
  106. ck_assert_int_eq(WEXITSTATUS(status), 0x42);
  107. asmsh_asmc_ctx_free(cctx);
  108. asmsh_env_free(env);
  109. }
  110. END_TEST
  111. START_TEST(test_env_regs)
  112. {
  113. const char childpath[] = REAL_CHILD;
  114. asmsh_env_t *env = asmsh_env(childpath);
  115. ck_assert_ptr_nonnull(env);
  116. asmsh_asmc_ctx_t *cctx;
  117. asmsh_bytecode_t bcode;
  118. int ret, status, i;
  119. struct _instr_tests {
  120. char *instr;
  121. long long unsigned int *reg;
  122. u_int64_t val;
  123. } tinstr[] = {
  124. {
  125. .instr = "mov $42, %r15",
  126. .reg = &env->regs.r15,
  127. .val = 42,
  128. },
  129. {
  130. .instr = "mov %r15, %rcx",
  131. .reg = &env->regs.rcx,
  132. .val = 42,
  133. },
  134. {
  135. .instr = "mov $24, %rbx",
  136. .reg = &env->regs.rbx,
  137. .val = 24,
  138. },
  139. {
  140. .instr = "add %rbx, %rcx",
  141. .reg = &env->regs.rcx,
  142. .val = 0x42,
  143. },
  144. {
  145. .instr = "mov $60, %rax",
  146. .reg = &env->regs.rax,
  147. .val = 60,
  148. },
  149. {
  150. .instr = "xor %rdi, %rdi",
  151. .reg = &env->regs.rdi,
  152. .val = 0,
  153. },
  154. {
  155. .instr = "syscall",
  156. .reg = NULL,
  157. .val = 0,
  158. },
  159. };
  160. cctx = asmsh_asmc_ctx_default();
  161. ck_assert_ptr_nonnull(cctx);
  162. for(i=0; i<(sizeof(tinstr)/sizeof(*tinstr))-1;i++)
  163. {
  164. ck_assert_int_ge(asmsh_asmc_compile(cctx, tinstr[i].instr, &bcode), 0);
  165. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  166. ret = asmsh_env_step(env, &status);
  167. ck_assert_int_eq(ret, 0);
  168. ck_assert_msg(WCOREDUMP(status) == 0, "SEGFAULT");
  169. ck_assert_int_ne(WIFSTOPPED(status), 0);
  170. ck_assert_int_eq(status>>8, 5);
  171. if(!tinstr[i].reg) { continue; }
  172. asmsh_env_update_regs(env);
  173. ck_assert_int_eq(*(tinstr[i].reg), tinstr[i].val);
  174. }
  175. ck_assert_int_ge(asmsh_asmc_compile(cctx, tinstr[i].instr, &bcode), 0);
  176. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  177. ret = asmsh_env_step(env, &status);
  178. ck_assert_int_eq(ret, 1);
  179. ck_assert(WIFEXITED(status));
  180. ck_assert_int_eq(WEXITSTATUS(status), 0);
  181. asmsh_asmc_ctx_free(cctx);
  182. asmsh_env_free(env);
  183. }
  184. END_TEST
  185. START_TEST(test_embed_env_exit42)
  186. {
  187. const char childpath[] = REAL_CHILD;
  188. asmsh_asmc_ctx_t *cctx;
  189. asmsh_bytecode_t bcode;
  190. int ret, status;
  191. asmsh_env_t *env = asmsh_env(NULL);
  192. ck_assert_ptr_nonnull(env);
  193. cctx = asmsh_asmc_ctx_default();
  194. ck_assert_ptr_nonnull(cctx);
  195. ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $60, %rax", &bcode), 0);
  196. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  197. ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $42, %rdi", &bcode), 0);
  198. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  199. ck_assert_int_ge(asmsh_asmc_compile(cctx, "syscall", &bcode), 0);
  200. ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
  201. asmsh_asmc_ctx_free(cctx);
  202. for(int i=0; i<2; i++)
  203. {
  204. ret = asmsh_env_step(env, &status);
  205. ck_assert_int_eq(ret, 0);
  206. ck_assert_msg(WCOREDUMP(status) == 0, "SEGFAULT");
  207. ck_assert_int_ne(WIFSTOPPED(status), 0);
  208. ck_assert_int_eq(status>>8, 5);
  209. }
  210. ret = asmsh_env_step(env, &status);
  211. ck_assert_int_eq(ret, 1);
  212. ck_assert(WIFEXITED(status));
  213. ck_assert_int_eq(WEXITSTATUS(status), 42);
  214. asmsh_env_free(env);
  215. }
  216. END_TEST
  217. ASMSH_CHECK_START("Testing asm env", "testing asm environment")
  218. ASMSH_ADD_TEST(test_env);
  219. ASMSH_ADD_TEST(test_asm_stamps);
  220. ASMSH_ADD_TEST(test_env_update);
  221. ASMSH_ADD_TEST(test_env_exit42);
  222. ASMSH_ADD_TEST(test_env_steps);
  223. ASMSH_ADD_TEST(test_env_regs);
  224. ASMSH_ADD_TEST(test_embed_env_exit42);
  225. ASMSH_CHECK_END