VMState * vm_newstate_no_code(VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); // ramsize is in bytes err_malloc(newstate->ram = calloc(ramsize, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif // We make all registers ints err_malloc(newstate->registers = calloc(nregisters, sizeof(OPCODE_TYPE))); err_malloc(newstate->pins = calloc(npins, sizeof(OPCODE_TYPE))); newstate->interrupt_policy = interrupt_policy; newstate->break_async = false; return newstate; error: vm_closestate(newstate); return NULL; }
/* allocation functions */ VMState * vm_newstate(void *program, size_t program_size, VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); // ramsize is in bytes err_malloc(newstate->ram = calloc(ramsize, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif // We make all registers ints err_malloc(newstate->registers = calloc(nregisters, sizeof(int))); newstate->interrupt_policy = interrupt_policy; newstate->interrupts = NULL; newstate->break_async = false; newstate->breakpoints = NULL; if (!_read_elf(newstate, program, program_size)) goto error; return newstate; error: vm_closestate(newstate); return NULL; }
bool vm_write(VMState *state, VMStateDiff *diff, VMInfoType type, size_t destaddr, OPCODE_TYPE value) { OPCODE_TYPE *dest; VMSingleStateDiff *singlediff; if (!(dest =_get_location(state, type, destaddr))) goto error; /* update our diffs */ err_malloc((singlediff = malloc(sizeof(VMSingleStateDiff)))); singlediff->oldval = *dest; singlediff->newval = value; singlediff->type = type; singlediff->location = destaddr; singlediff->next = (VMIterable *) diff->singlediff; diff->singlediff = singlediff; /* finally, write the value */ *dest = value; return true; error: return false; }
static Opcode * disassemble(unsigned int *assembly, size_t assembly_length) { Opcode *result = NULL; size_t i, j; err_malloc(result = malloc(sizeof(Opcode) * assembly_length)); // Walk though assembly for (i = 0; i < assembly_length; ++i) { // Walk through opcode handlers for (j = 0; j < n_opcode_handlers; ++j) { // If the opcode matches an opcode handlers opcode, we found our handler if ((assembly[i] & opcode_handlers[j].mask) == opcode_handlers[j].opcode) { result->opcode_index = j; result->opcode = assembly[i]; break; } } } return result; error: free(result); return NULL; }
char *make_full_path(char *file, char *path) { char *full; int size; int i; int a; a = 0; i = 0; if (!file || !path) return (NULL); if (file[0] == '/') return (ft_strdup(file)); size = ft_strlen(file) + ft_strlen(path) + 1; if (!(full = (char*)malloc(sizeof(*full) * (size + 1)))) err_malloc(); while (path[i]) full[a++] = path[i++]; full[a++] = '/'; i = 0; while (file[i]) full[a++] = file[i++]; full[a] = '\0'; return (full); }
t_piece *read_chunk(int fd, char *execute) { char *line; char buff; t_piece *piece; static int count; if (!(piece = ft_memalloc(4 * sizeof(t_piece)))) err_malloc(); ft_bzero(piece, sizeof(t_piece)); if (read_line(fd, &line) == 0) return (0); if (read(fd, &buff, 1) != 1) *execute = 0; else if (buff != '\n') err_reading(); if (lee_fill(line, piece) == 0) err_reading(); free(line); line = NULL; count++; if (count > 26) err_reading(); return (piece); }
VMStateDiff * vm_newdiff(void) { VMStateDiff *newdiff = NULL; err_malloc(newdiff = calloc(1, sizeof(VMStateDiff))); return newdiff; error: vm_closediff(newdiff); return NULL; }
VMState * vm_newstate_no_code(VMInterruptPolicy interrupt_policy) { VMState *newstate = NULL; err_malloc(newstate = calloc(1, sizeof(VMState))); err_malloc(newstate->chunk = calloc(CHUNK_END, 1)); #ifdef VM_WITH_THREADS err(pthread_mutex_init(&newstate->lock, NULL) == 0, "pthread_mutex_init", VM_OSERROR); #endif newstate->interrupt_policy = interrupt_policy; newstate->break_async = false; return newstate; error: vm_closestate(newstate); return NULL; }
/*! Create a new interrupt item */ static VMInterruptItem * vm_new_interrupt_item(VMInterruptType interrupt_type, unsigned int cycles) { VMInterruptItem *result = NULL; err_malloc(result = calloc(1, sizeof(VMInterruptItem))); result->interrupt_type = interrupt_type; result->cycles = cycles; return result; error: free(result); return NULL; }
bool vm_break(VMState *state, size_t code_offset) { VMBreakpoint *bp = NULL; err_malloc(bp = calloc(1, sizeof(VMBreakpoint))); /* prepend breakpoint */ bp->next = (VMIterable *) state->breakpoints; bp->offset = code_offset; state->breakpoints = bp; return true; error: free(bp); return false; }
/* Except in response to the WM_INITDIALOG message, the dialog box procedure should return nonzero if it processes the message, and zero if it does not. - see DialogProc */ BOOL CALLBACK CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { if (lParam); /* remove warning */ if (wParam); /* remove warning */ switch (msg) { case WM_INITDIALOG: { char * tmp = err_malloc(8192); char tmp2[MAX_PATH * 2]; argumentspec *as; int i; LocalizeDialog (hwndDlg, "IDD_COMMANDHELP_DLG"); as = (argumentspec*) lParam; *tmp = 0; strcpy (tmp, "Command line options:\n\n"); for (i = 0; i < as->arg_cnt; i ++) { if (!as->args[i].Internal) { sprintf(tmp2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name); strcat(tmp,tmp2); } } strcat (tmp, "\nExamples:\n\nMount a volume as X:\ttruecrypt.exe /q /v volume.tc /l X\nDismount a volume X:\ttruecrypt.exe /q /d X"); SetWindowText (GetDlgItem (hwndDlg, IDC_COMMANDHELP_TEXT), (char*) tmp); return 1; } case WM_COMMAND: EndDialog (hwndDlg, IDOK); return 1; case WM_CLOSE: EndDialog (hwndDlg, 0); return 1; } return 0; }
/* Except in response to the WM_INITDIALOG message, the dialog box procedure should return nonzero if it processes the message, and zero if it does not. - see DialogProc */ BOOL CALLBACK CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { if (lParam); /* remove warning */ if (wParam); /* remove warning */ switch (msg) { case WM_INITDIALOG: { char * tmp = err_malloc(8192); char tmp2[MAX_PATH * 2]; argumentspec *as; int i; LocalizeDialog (hwndDlg, "IDD_COMMANDHELP_DLG"); as = (argumentspec*) lParam; *tmp = 0; StringCbCopyA (tmp, 8192, "Command line options:\n\n"); for (i = 0; i < as->arg_cnt; i ++) { if (!as->args[i].Internal) { StringCchPrintf(tmp2, MAX_PATH * 2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name); StringCchCat(tmp, 8192, tmp2); } } SetWindowText (GetDlgItem (hwndDlg, IDC_COMMANDHELP_TEXT), (char*) tmp); TCfree(tmp); return 1; } case WM_COMMAND: EndDialog (hwndDlg, IDOK); return 1; case WM_CLOSE: EndDialog (hwndDlg, 0); return 1; } return 0; }
bool vm_register_interrupt_callable(VMState *state, interrupt_callable *func, void *argument) { VMInterruptCallable *callable; err_malloc(callable = calloc(1, sizeof(VMInterruptCallable))); callable->func = func; callable->argument = argument; /* prepend our struct in the linked list */ callable->next = state->interrupt_callables; state->interrupt_callables = callable; return true; error: free(callable); return false; }
void put_code(VMState *state) { Opcode *opcodes; err_malloc(opcodes = malloc(sizeof(Opcode) * 2)); opcodes[0].opcode_index = 0; /* noop */ opcodes[0].instruction = 0; opcodes[1].opcode_index = get_index("rjmp"); opcodes[1].instruction = 0b1100111111111110; /* rjmp -2 */ state->instructions = opcodes; state->instructions_size = 2; return; error: check_error(false); }
int create_iwatch_thread(pthread_t *id, int fd, queue_t q) { int err; struct watch_data_t *wdata; wdata = malloc(sizeof(struct watch_data_t)); if (wdata == NULL) { err_malloc(errno); err_msg("error[create_iwatch_thread]: Unable to malloc watch data.\n"); return -1; } wdata->fd = fd; wdata->q = q; err = create_joinable_thread(id, watch, wdata); if (err < 0) err_create_joinable_thread(errno); return err; }
char *get_name(char *str) { char *name; int i; i = 0; if (!str) return (NULL); while (str[i] != '=' && str[i]) ++i; --i; if (!(name = (char*)malloc(sizeof(*name) * (i + 2)))) err_malloc(); i = 0; while (str[i] != '=' && str[i]) { name[i] = str[i]; ++i; } name[i] = '\0'; return (name); }
bool vm_write_nbytes(VMState *state, VMStateDiff *diff, VMInfoType type, size_t destaddr, BIGTYPE value, int nbytes) { char *location; VMSingleStateDiff *singlediff; if (!(location = _get_location(state, type, destaddr, NULL))) goto error; if (diff) { /* update our diffs */ err_malloc((singlediff = malloc(sizeof(VMSingleStateDiff)))); /* We already did error checking, don't do it again. Read and store in host endianness so rstep can write to uC endianness. */ singlediff->oldval = vm_info(state, type, destaddr, NULL); #ifdef VM_DEBUG singlediff->newval = value; #endif singlediff->type = type; singlediff->location = destaddr; singlediff->nbytes = nbytes; singlediff->next = diff->singlediff; diff->singlediff = singlediff; } /* finally, write the value */ vm_convert_endianness((char *) &value, nbytes); memcpy(location, &value, nbytes); return true; error: return false; }
static Opcode * disassemble(OPCODE_TYPE *assembly, size_t assembly_length) { Opcode *result = NULL; OpcodeHandler *op_handler = NULL; size_t i, j; bool some_error = false, is_arg = false; err_malloc(result = malloc(sizeof(Opcode) * assembly_length)); for (i = 0; i < assembly_length; ++i) { bool found = false; char *name; OPCODE_TYPE instruction = assembly[i]; vm_convert_to_host_endianness((char *) &instruction, sizeof(instruction)); for (j = 0; opcode_handlers[j].opcode_name; ++j) { op_handler = &opcode_handlers[j]; if ((assembly[i] & op_handler->mask) == op_handler->opcode) { result[i].opcode_index = j; result[i].instruction = assembly[i]; found = true; name = op_handler->opcode_name; break; } } if (!found) { /* This is another (16-bits) argument to an instruction. Apparently no instruction handler could be found for the argument. We try to find a handler in any case to allow simulation of code that (incorrectly or maliciously) jumps to an argument. */ result[i].opcode_index = 0; /* noop */ result[i].instruction = assembly[i]; name = "noop"; if (!is_arg) { some_error = true; #ifdef VM_DEBUG fprintf( stderr, LOCATION " Cannot handle instruction 0x%x at address " "offset 0x%x.\n", (unsigned int) assembly[i], (unsigned int) (i * sizeof(OPCODE_TYPE))); #endif /* vm_seterrno(VM_ILLEGAL_INSTRUCTION); */ } } is_arg = op_handler->next_is_arg; } if (some_error) { fprintf(stderr, "WARNING: Some opcodes are not implemented, " "simulation might not work properly.\n"); } return result; error: free(result); return NULL; }