From f6747ba6d9e0d45b2006e34f49112ed441af9946 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Fri, 4 Jan 2019 16:45:45 +0300 Subject: refactor code --- sketchtrial.c | 86 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/sketchtrial.c b/sketchtrial.c index 85354c9..56175cb 100644 --- a/sketchtrial.c +++ b/sketchtrial.c @@ -9,7 +9,7 @@ #include static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -static int nstime_calls_left = 3; +static int calls_left = 3; // actually 2 seems to be enough. use 3 just for safety static bool done = false; static char buf[32]; static long days = -5000; @@ -49,51 +49,55 @@ int gettimeofday(struct timeval *restrict tp, void *restrict tzp) { } int result = orig_gettimeofday(tp, tzp); - if (result == 0 && !done) { - pthread_mutex_lock(&mutex); - if (nstime_calls_left > 0) { - void *callstack[128]; - int frames = backtrace(callstack, 128); - char **strs = backtrace_symbols(callstack, frames); - - bool is_sketch = false, is_nstime = false, is_cfnet = false, is_nt = false; - int i; + if (result != 0 || done) { + return result; + } - for (i = 0; i < frames; ++i) { - parse_bt_source(strs[i], buf, 32); - if (!is_nstime && strstr(strs[i], "NSDate") != NULL) { - is_nstime = true; - } - if (!is_nt && strstr(strs[i], "NSNotification") != NULL) { - is_nt = true; - } - if (!is_sketch && !strcmp(buf, "Sketch")) { - is_sketch = true; - } - if (!is_cfnet && !strcmp(buf, "CFNetwork")) { - is_cfnet = true; - } - } + pthread_mutex_lock(&mutex); + if (!calls_left) { + goto end; + } - if (is_sketch && is_nstime && !is_cfnet && !is_nt) { - tp->tv_sec += days * 86400; - tp->tv_usec = tp->tv_sec * 1000000; - nstime_calls_left--; + void *callstack[128]; + int frames = backtrace(callstack, 128); + char **strs = backtrace_symbols(callstack, frames); + int i; + bool call_sketch = false; + bool call_nsdate = false; - printf("spoofed time on call stack:\n"); - for (i = 0; i < frames; i++) { - printf(" %s\n", strs[i]); - } - printf("\n"); - } - - free(strs); - if (!nstime_calls_left) { - done = true; - } + for (i = 0; i < frames; ++i) { + parse_bt_source(strs[i], buf, 32); + if (strstr(strs[i], "NSNotification") != NULL || !strcmp(buf, "CFNetwork")) { + call_sketch = false; + break; + } + if (!call_nsdate && strstr(strs[i], "NSDate") != NULL) { + call_nsdate = true; + } + if (!call_sketch && !strcmp(buf, "Sketch")) { + call_sketch = true; } - pthread_mutex_unlock(&mutex); } + if (call_sketch && call_nsdate) { + tp->tv_sec += days * 86400; + tp->tv_usec = tp->tv_sec * 1000000; + calls_left--; + + printf("spoofed time on call stack:\n"); + for (i = 0; i < frames; i++) { + printf(" %s\n", strs[i]); + } + printf("\n"); + } + + free(strs); + if (!calls_left) { + done = true; + } + +end: + pthread_mutex_unlock(&mutex); + return result; } -- cgit v1.2.3