123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #ifndef ASMSH_ASM_ENV_H
- #define ASMSH_ASM_ENV_H
- #include "config.h"
-
- #include <errno.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <sys/user.h>
- #include <sys/wait.h>
-
- #include "mmap_parse.h"
- #include "compile.h"
-
- ///! Initial size of the child's memory map with PROT_EXEC permission
- #define ASMSH_CHILD_TEXT_MAP_SZ 0x1000 // defined in child.s
-
- #define ASMSH_CHILD_PATH_DEFAULT "./child"
-
- typedef struct asmsh_env_s asmsh_env_t;
-
- struct asmsh_env_s
- {
- ///! Path on wich the child will execve
- char *childpath;
- ///! Child process pid
- pid_t pid;
- ///! Last status fetched from child using waitpid
- int status;
- ///! Child process registers value
- struct user_regs_struct regs;
- ///! Child's memory map
- child_mmap_l mmap;
-
- /**Pointer, in child memory, of the mmap with PROT_EXEC
- * perm allowing use to write code to be executed by the
- * child
- */
- void *txt_map_addr;
- /** child's map size */
- size_t txt_map_sz;
- /** Pointer on current write addr in child's map */
- void *txt_map_ptr;
-
- /** Pointer on the next addr where we should write some
- * compiled bytecode */
- void *code_write_ptr;
-
- /** Pointer, in child memory, of a mmap with PROT_WRITE
- * kind of .data segment
- */
- void *data_map_addr;
- size_t data_map_sz;
-
- /** Pointer, in child memory, of the stack */
- void *stack_addr;
- size_t stack_sz;
-
- };
-
- /** Run a new asm child and return the environment
- * @param const char* the path of the executable to execve in child
- * @return NULL if error or a pointer on a newly allocated env
- */
- asmsh_env_t* asmsh_env(const char *childpath);
- /** Free an env returned by @ref asmsh_env() */
- void asmsh_env_free(asmsh_env_t *asmenv);
-
- /** Write a buffer in tracee memory
- *
- * Handle un-alligned addr by reading tracee memory before writing if
- * needed.
- * @param asmsh_env_t*
- * @param void* The address in tracee memory
- * @param const unsigned char* The buffer to write
- * @param size_t number of bytes to write
- * @return 0 if no errors else -1 with errno set
- */
- int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf, size_t buf_sz);
-
- /** Write bytecode at the next location available in the text map
- * @param asmsh_env_t* The run environment
- * @param asmsh_bytecode_t* The bytecode to copy
- * @return 0 if no error occured 1 if child finished in unexpected state or -1
- * on other errror
- */
- int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode);
-
- /** Run the next instruction
- * @param asmsh_env_t* The run environment
- * @param int* If not null will be set to the status of the child PID (error cases)
- * @return 0 if no error from the child pid else -1 and wstatus is set to the child status
- */
- int asmsh_env_step(asmsh_env_t *env, int *status);
-
- /** Update both registers and memory maps */
- int asmsh_env_update(asmsh_env_t *asmenv);
- /** Update env registers from child */
- int asmsh_env_update_regs(asmsh_env_t *asmenv);
- /** Update child's memory map in env */
- int asmsh_env_update_maps(asmsh_env_t *asmenv);
-
- /** Convert a child's memory address into a "txt" addr
- *
- * The local address is calculated as an offset from the start of the child's
- * "txt" map (the one with PROT_READ | PROT_EXEC perm)
- *
- * @param asmsh_env_t*
- * @param void* the address to convert
- * @param void** the converted txt offset
- * @return 0 if no error else -1
- */
- int asmsh_env_txt_addr(asmsh_env_t *asmenv, void *addr, void **loc);
-
- #endif
|