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.

shell.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include "shell.h"
  14. int asmsh_init(asmsh_t *sh, const char *child_path)
  15. {
  16. bzero(sh, sizeof(asmsh_t));
  17. if(!(sh->env = asmsh_env(child_path)))
  18. {
  19. perror("Unable to start child env");
  20. asmsh_log_fatal("Unable to start child env");
  21. goto err;
  22. }
  23. if(!(sh->cctx = asmsh_asmc_ctx_default()))
  24. {
  25. perror("Unable to start compilation ctx");
  26. asmsh_log_fatal("Unable to start compilation context");
  27. asmsh_env_free(sh->env);
  28. goto err;
  29. }
  30. if(child_path)
  31. {
  32. if(!(sh->child_path = strdup(child_path)))
  33. {
  34. perror("Unable to copy child path");
  35. goto err_all;
  36. }
  37. }
  38. else
  39. {
  40. sh->child_path = NULL;
  41. }
  42. return 0;
  43. err_all:
  44. asmsh_asmc_ctx_free(sh->cctx);
  45. asmsh_env_free(sh->env);
  46. err:
  47. bzero(sh, sizeof(asmsh_t));
  48. return -1;
  49. }
  50. void asmsh_cleanup(asmsh_t *sh)
  51. {
  52. if(!sh){ return; }
  53. if(sh->cctx)
  54. {
  55. asmsh_asmc_ctx_free(sh->cctx);
  56. }
  57. if(sh->env)
  58. {
  59. asmsh_env_free(sh->env);
  60. }
  61. if(sh->child_path)
  62. {
  63. free(sh->child_path);
  64. }
  65. if(sh->last_instr)
  66. {
  67. free(sh->last_instr);
  68. }
  69. bzero(sh, sizeof(asmsh_t));
  70. }
  71. /** Attempt to compile and step if sucess */
  72. static int _compile_step(asmsh_t *sh, const char *cmd);
  73. /** Attempt to handle given shell internal command (starting with '.') */
  74. static int _handle_command(asmsh_t *sh, const char *cmd);
  75. int asmsh_exec(asmsh_t *sh, const char *cmd)
  76. {
  77. if(!cmd)
  78. {
  79. errno=EINVAL;
  80. return -1;
  81. }
  82. int ret;
  83. //lstrip whitespace
  84. for(; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
  85. if(!*cmd) { return 0; }
  86. switch(*cmd)
  87. {
  88. case '.':
  89. ret = _handle_command(sh, cmd);
  90. break;
  91. default:
  92. ret = _compile_step(sh, cmd);
  93. break;
  94. }
  95. if(!ret)
  96. {
  97. asmsh_env_update_regs(sh->env);
  98. }
  99. return ret;
  100. }
  101. static int _compile_step(asmsh_t *sh, const char *cmd)
  102. {
  103. int ret, status;
  104. asmsh_bytecode_t bcode;
  105. if(asmsh_asmc_compile(sh->cctx, cmd, &bcode) < 0)
  106. {
  107. return -1;
  108. }
  109. if(!bcode.size)
  110. {
  111. asmsh_log_error("No bytecode returned...");
  112. return -1;
  113. }
  114. sh->last_bcode = bcode;
  115. if(sh->last_instr) { free(sh->last_instr); }
  116. sh->last_instr = strdup(cmd);
  117. //char buf[256];
  118. //int _ret;
  119. //_ret = snprintf(buf, 255, "'%s' = 0x", cmd);
  120. //for(int _i=0; _i<bcode.size; _i++)
  121. //{
  122. // _ret += snprintf(buf+_ret, 255-_ret, "%02x", bcode.bytes[_i]);
  123. //}
  124. //_ret += snprintf(buf+_ret, 255-_ret, "(%d)", bcode.size);
  125. //buf[_ret] = '\0';
  126. //asmsh_log_info(buf);
  127. if(asmsh_env_write_code(sh->env, &bcode))
  128. {
  129. return -1;
  130. }
  131. status=0;
  132. ret = asmsh_env_step(sh->env, &status);
  133. if(ret < 0)
  134. {
  135. perror("Unable to step");
  136. asmsh_log_fatal("Error will child step");
  137. return -1;
  138. }
  139. else if(ret > 0)
  140. {
  141. asmsh_log_fatal("Child exited with non-zero status");
  142. return 1+WEXITSTATUS(status);
  143. }
  144. return 0;
  145. }
  146. static int _handle_command(asmsh_t *sh, const char *cmd)
  147. {
  148. const asmsh_cmd_t *match;
  149. int ret, err;
  150. asmsh_cmd_args_t *args;
  151. args = asmsh_cmd_parse(cmd);
  152. if(!args)
  153. {
  154. err=errno;
  155. asmsh_log_error("Parse error"); // TODO better error
  156. errno=err;
  157. return -1;
  158. }
  159. match = asmsh_cmd_match(args->cmd, strlen(args->cmd));
  160. if(!match)
  161. {
  162. err=errno;
  163. asmsh_log_error("Unknow command");
  164. asmsh_cmd_args_free(args);
  165. errno=err;
  166. return -1;
  167. }
  168. ret = match->cmd(sh, args);
  169. asmsh_cmd_args_free(args);
  170. return ret;
  171. }