/* * thread start callback (analysis function) * * allocate space for the syscall context and VCPUs * (i.e., thread context), and set the TLS-like pointer * (i.e., thread_ctx_ptr) accordingly * * @tid: thread id * @ctx: CPU context * @flags: OS specific flags for the new thread * @v: callback value */ static void thread_alloc(THREADID tid, CONTEXT *ctx, INT32 flags, VOID *v) { /* thread context pointer (ptr) */ thread_ctx_t *tctx = NULL; /* allocate space for the thread context; optimized branch */ if (unlikely((tctx = (thread_ctx_t *)calloc(1, sizeof(thread_ctx_t))) == NULL)) { /* error message */ LOG(string(__func__) + ": thread_ctx_t allocation failed (" + string(strerror(errno)) + ")\n"); /* die */ libdft_die(); } /* save the address of the per-thread context to the spilled register */ PIN_SetContextReg(ctx, thread_ctx_ptr, (ADDRINT)tctx); char fileName[256]; sprintf(fileName, "C:\\itrace_thread%u.txt", tid); thread_local *t_local = new thread_local; t_local->insaddr = 0; t_local->logfile = fopen(fileName, "w"); PIN_SetThreadData(trace_tls_key, t_local, tid); }
/* * thread start callback (analysis function) * * allocate space for the syscall context and VCPUs * (i.e., thread context), and set the TLS-like pointer * (i.e., thread_ctx_ptr) accordingly * * @tid: thread id * @ctx: CPU context * @flags: OS specific flags for the new thread * @v: callback value */ static void thread_alloc(THREADID tid, CONTEXT *ctx, INT32 flags, VOID *v) { /* thread context pointer (ptr) */ thread_ctx_t *tctx = NULL; /* allocate space for the thread context; optimized branch */ if (unlikely((tctx = (thread_ctx_t *)calloc(1, sizeof(thread_ctx_t))) == NULL)) { /* error message */ LOG(string(__FUNCTION__) + ": thread_ctx_t allocation failed (" + string(strerror(errno)) + ")\n"); /* die */ libdft_die(); } /* save the address of the per-thread context to the spilled register */ PIN_SetContextReg(ctx, thread_ctx_ptr, (ADDRINT)tctx); }
/* * Tool used for verifying that libdft propagates taint correctly. */ int main(int argc, char **argv) { /* initialize symbol processing */ PIN_InitSymbols(); if (unlikely(PIN_Init(argc, argv))) goto err; IMG_AddInstrumentFunction(ImageLoad, 0); PIN_AddFiniFunction(OnExit, 0); #ifdef DTRACKER_DEBUG INS_AddInstrumentFunction(CheckMagicValue, 0); #endif LOG("Initializing libdft.\n"); if (unlikely(libdft_init() != 0)) goto err; // reset counters bzero(stdcount, sizeof(stdcount)); // Open raw prov file. // This file is to be post-processed to get the data in a proper format. rawProvStream.open(ProvRawKnob.Value().c_str()); /* * Install taint sources and sinks. * syscall_set_{pre, post}() set the callbacks in the libdft * syscall description struct. * These callbacks are respectively invoked through * sysenter_save() and sysexit_save() function of libdft. * In turn, these libdft functions are hooked to run before/after * every syscall using PIN_AddSyscall{Entry, Exit}Function(). */ /* dtracker_openclose.cpp: open(2), creat(2), close(2) */ (void)syscall_set_pre(&syscall_desc[__NR_open], pre_open_hook); (void)syscall_set_pre(&syscall_desc[__NR_creat], pre_open_hook); (void)syscall_set_post(&syscall_desc[__NR_open], post_open_hook); (void)syscall_set_post(&syscall_desc[__NR_creat], post_open_hook); (void)syscall_set_post(&syscall_desc[__NR_close], post_close_hook); /* dtracker_read.cpp: read(2), readv(2) */ (void)syscall_set_post(&syscall_desc[__NR_read], post_read_hook); (void)syscall_set_post(&syscall_desc[__NR_readv], post_readv_hook); /* dtracker_write.cpp: write(2), writev(2) */ (void)syscall_set_post(&syscall_desc[__NR_write], post_write_hook); (void)syscall_set_post(&syscall_desc[__NR_writev], post_writev_hook); /* dtracker_mmap.cpp: mmap2(2), munmap(2) */ (void)syscall_set_post(&syscall_desc[__NR_mmap2], post_mmap2_hook); (void)syscall_set_post(&syscall_desc[__NR_munmap], post_munmap_hook); /* start the program and return something to make the compiler happy */ LOG("Starting program.\n"); PIN_StartProgram(); return EXIT_SUCCESS; err: /* error handling */ /* detach from the process */ libdft_die(); /* return */ return EXIT_FAILURE; }