From 56bc5571beb7429c47175856823a1be29d3f2d6d Mon Sep 17 00:00:00 2001 From: Steffen Schulz Date: Mon, 9 Aug 2021 16:24:27 -0700 Subject: [PATCH] dump_pt: create-open & truncate output file on each execution Previous implementation only opened the file once. --- nyx/interface.c | 6 +++--- nyx/pt.c | 40 ++++++++++++++++++++++++++-------------- nyx/pt.h | 4 ++-- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/nyx/interface.c b/nyx/interface.c index eabbae9972..af4b431212 100644 --- a/nyx/interface.c +++ b/nyx/interface.c @@ -277,9 +277,9 @@ static bool verify_workdir_state(nyx_interface_state *s, Error **errp){ init_redqueen_state(); if(s->dump_pt_trace){ - assert(asprintf(&tmp, "%s/pt_trace_dump_%d", workdir, id) != -1); - pt_open_pt_trace_file(tmp); - free(tmp); + assert(asprintf(&tmp, "%s/pt_trace_dump_%d", workdir, id) != -1); + pt_trace_dump_enable(tmp); + free(tmp); } diff --git a/nyx/pt.c b/nyx/pt.c index 193ef8ea09..89b4093c97 100644 --- a/nyx/pt.c +++ b/nyx/pt.c @@ -54,20 +54,34 @@ uint32_t alt_bitmap_size = 0; uint8_t* alt_bitmap = NULL; int pt_trace_dump_fd = 0; +char *pt_trace_dump_filename; bool should_dump_pt_trace= false; /* dump PT trace as returned from HW */ -void pt_open_pt_trace_file(char* filename){ - printf("using pt trace at %s",filename); - pt_trace_dump_fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); - should_dump_pt_trace = true; - assert(pt_trace_dump_fd >= 0); +void pt_trace_dump_enable(char* filename) +{ + int test_fd; + + printf("Enable pt trace dump at %s", filename); + pt_trace_dump_filename = filename; + should_dump_pt_trace = true; + + test_fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); + if (test_fd < 0) + fprintf(stderr, "Error accessing pt_dump output path: %s", strerror(errno)); + assert(test_fd >= 0); } -void pt_trucate_pt_trace_file(void){ - if(should_dump_pt_trace){ - assert(lseek(pt_trace_dump_fd, 0, SEEK_SET) == 0); - assert(ftruncate(pt_trace_dump_fd, 0)==0); - } +void pt_write_pt_dump_file(uint8_t *data, size_t bytes) +{ + int fd; + + if (!should_dump_pt_trace) + return; + + fd = open(pt_trace_dump_filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); + assert(fd >= 0); + + assert(bytes == write(fd, data, bytes)); } static void pt_set(CPUState *cpu, run_on_cpu_data arg){ @@ -166,9 +180,8 @@ void dump_pt_trace(void* buffer, int bytes){ #endif void pt_dump(CPUState *cpu, int bytes){ - if(should_dump_pt_trace){ - assert(bytes == write(pt_trace_dump_fd, cpu->pt_mmap, bytes)); - } + pt_write_pt_dump_file(cpu->pt_mmap, bytes); + if(!(GET_GLOBAL_STATE()->redqueen_state && GET_GLOBAL_STATE()->redqueen_state->intercept_mode)){ if (GET_GLOBAL_STATE()->in_fuzzing_mode && GET_GLOBAL_STATE()->decoder_page_fault == false && GET_GLOBAL_STATE()->decoder && !GET_GLOBAL_STATE()->dump_page){ GET_GLOBAL_STATE()->pt_trace_size += bytes; @@ -204,7 +217,6 @@ int pt_enable(CPUState *cpu, bool hmp_mode){ delete_trace_files(); alt_bitmap_reset(); } - pt_trucate_pt_trace_file(); return pt_cmd(cpu, KVM_VMX_PT_ENABLE, hmp_mode); } diff --git a/nyx/pt.h b/nyx/pt.h index 7b23719ec1..22790eb85b 100644 --- a/nyx/pt.h +++ b/nyx/pt.h @@ -44,7 +44,7 @@ void pt_post_kvm_run(CPUState *cpu); void pt_handle_overflow(CPUState *cpu); void pt_dump(CPUState *cpu, int bytes); -void pt_open_pt_trace_file(char* filename); -void pt_trucate_pt_trace_file(void); +void pt_trace_dump_enable(char* filename); +void pt_write_pt_dump_file(uint8_t *data, size_t bytes); #endif