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.

asm_env.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef ASMSH_ASM_ENV_H
  2. #define ASMSH_ASM_ENV_H
  3. #include "config.h"
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <sys/ptrace.h>
  8. #include <sys/types.h>
  9. #include <sys/user.h>
  10. #include <sys/wait.h>
  11. #include "mmap_parse.h"
  12. #include "compile.h"
  13. ///! Initial size of the child's memory map with PROT_EXEC permission
  14. #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
  15. #define ASMSH_CHILD_PATH_DEFAULT "./child"
  16. typedef struct asmsh_env_s asmsh_env_t;
  17. struct asmsh_env_s
  18. {
  19. ///! Path on wich the child will execve
  20. char *childpath;
  21. ///! Child process pid
  22. pid_t pid;
  23. ///! Last status fetched from child using waitpid
  24. int status;
  25. ///! Child process registers value
  26. struct user_regs_struct regs;
  27. ///! Child's memory map
  28. child_mmap_l mmap;
  29. /**Pointer, in child memory, of the mmap with PROT_EXEC
  30. * perm allowing use to write code to be executed by the
  31. * child
  32. */
  33. void *txt_map_addr;
  34. /** child's map size */
  35. size_t txt_map_sz;
  36. /** Pointer on current write addr in child's map */
  37. void *txt_map_ptr;
  38. /** Pointer on the next addr where we should write some
  39. * compiled bytecode */
  40. void *code_write_ptr;
  41. /** Pointer, in child memory, of a mmap with PROT_WRITE
  42. * kind of .data segment
  43. */
  44. void *data_map_addr;
  45. size_t data_map_sz;
  46. /** Pointer, in child memory, of the stack */
  47. void *stack_addr;
  48. size_t stack_sz;
  49. };
  50. /** Run a new asm child and return the environment
  51. * @param const char* the path of the executable to execve in child
  52. * @return NULL if error or a pointer on a newly allocated env
  53. */
  54. asmsh_env_t* asmsh_env(const char *childpath);
  55. /** Free an env returned by @ref asmsh_env() */
  56. void asmsh_env_free(asmsh_env_t *asmenv);
  57. /** Write a buffer in tracee memory
  58. *
  59. * Handle un-alligned addr by reading tracee memory before writing if
  60. * needed.
  61. * @param asmsh_env_t*
  62. * @param void* The address in tracee memory
  63. * @param const unsigned char* The buffer to write
  64. * @param size_t number of bytes to write
  65. * @return 0 if no errors else -1 with errno set
  66. */
  67. int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
  68. /** Write bytecode at the next location available in the text map
  69. * @param asmsh_env_t* The run environment
  70. * @param asmsh_bytecode_t* The bytecode to copy
  71. * @return 0 if no error occured 1 if child finished in unexpected state or -1
  72. * on other errror
  73. */
  74. int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
  75. /** Run the next instruction
  76. * @param asmsh_env_t* The run environment
  77. * @param int* If not null will be set to the status of the child PID (error cases)
  78. * @return 0 if no error from the child pid else -1 and wstatus is set to the child status
  79. */
  80. int asmsh_env_step(asmsh_env_t *env, int *status);
  81. /** Update both registers and memory maps */
  82. int asmsh_env_update(asmsh_env_t *asmenv);
  83. /** Update env registers from child */
  84. int asmsh_env_update_regs(asmsh_env_t *asmenv);
  85. /** Update child's memory map in env */
  86. int asmsh_env_update_maps(asmsh_env_t *asmenv);
  87. /** Convert a child's memory address into a "txt" addr
  88. *
  89. * The local address is calculated as an offset from the start of the child's
  90. * "txt" map (the one with PROT_READ | PROT_EXEC perm)
  91. *
  92. * @param asmsh_env_t*
  93. * @param void* the address to convert
  94. * @param void** the converted txt offset
  95. * @return 0 if no error else -1
  96. */
  97. int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
  98. #endif