aboutsummaryrefslogtreecommitdiff
path: root/sketchtrial.c
blob: 56175cbab2060bd9288f10e9eedd2eb7adab7345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <execinfo.h>
#include <dlfcn.h>
#include <pthread.h>

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
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;

// pointer to the original function
int (*orig_gettimeofday) (struct timeval *restrict tp, void *restrict tzp) = NULL;

void parse_bt_source(char* src, char* dst, int len) {
    int pos = 0;
    int state = 0;
    for (char *c = src; *c != '\0' && pos < len-1; c++) {
        if (!state) {
            if (*c == ' ') {
                state = 1;
            }
            continue;
        }
        if (state == 1) {
            if (*c != ' ') {
                state = 2;
            }
        }
        if (state == 2) {
            if (*c != ' ') {
                dst[pos++] = *c;
            } else {
                dst[pos] = '\0';
                break;
            }
        }
    }
}

int gettimeofday(struct timeval *restrict tp, void *restrict tzp) {
    if (!orig_gettimeofday) {
        orig_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
    }

    int result = orig_gettimeofday(tp, tzp);
    if (result != 0 || done) {
        return result;
    }

    pthread_mutex_lock(&mutex);
    if (!calls_left) {
        goto end;
    }

    void *callstack[128];
    int frames = backtrace(callstack, 128);
    char **strs = backtrace_symbols(callstack, frames);
    int i;
    bool call_sketch = false;
    bool call_nsdate = false;

    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;
        }
    }

    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;
}