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_asm_env.c 6.3KB

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