-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
executable file
·126 lines (103 loc) · 2.89 KB
/
test.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/******************************************/
#include "splinter.h"
/******************************************/
#define run() runit(argc, argv)
int splinter_debug_level = 99;
int splinter_test_mode = 0;
uint_t stats_memory_used = 0;
void * splinter_memory_alloc(uint_t size) {
void * ptr;
if (!size) return NULL;
if ((ptr = calloc(1, size)) != NULL) {
stats_memory_used += 1;
} else {
debug(DEBUG_ALL, "could not alloc %lu bytes", size);
}
return ptr;
}
void * splinter_memory_free(void *p) {
if (p) {
free(p);
stats_memory_used -= 1;
}
return NULL;
}
char * splinter_find_variable(char * name) {
return NULL;
}
uint_t splinter_find_symbol(char * name) {
return 0;
}
int funnn(char *s, int argc, char **argv, int a) {
int i = 0;
int j = 3;
int ret = a ? j + a : i + a + a + a;
fprintf(stderr, "funnn: s = [%s], argc = %d, argv = %u, a = %d, ret = %d\n", s, argc, (unsigned) argv, a, ret);
return ret;
}
void runit(int argc, char **argv) {
static int i = 0;
fprintf(stderr, "runit: ret = %d\n", funnn("string", argc, argv, i++));
}
static void dump_ringbuffer() {
byte_t buf_data[1025];
uint_t buf_length;
uint_t buf_dropped;
do {
buf_length = sizeof(buf_data) - 1;
ringbuf_read(buf_data, &buf_length, &buf_dropped, ringbuf_data, &ringbuf_head, &ringbuf_length, &ringbuf_dropped, ringbuf_size);
if(buf_length) {
buf_data[buf_length] = 0;
fprintf(stderr, "dropped = %lu\n[%s]\n", buf_dropped, buf_data);
}
}
while(buf_length > 0);
}
int main(int argc, char **argv)
{
int i;
context_t ctx;
char *entry_hook_line =
"{exec {print-str ' entry:'} [var 0 0]"
"\n {while <is-le [var 0] 10>"
"\n {exec"
"\n {print-char 32}"
"\n {print-hex0 [reg [var 0]]}"
"\n {if <is-null [reg [var 0]]>"
"\n (break)"
"\n }"
"\n [var 0 (add [var 0] 1)]"
"\n }"
"\n }"
"\n (print-char 10)"
"\n}";
char *exit_hook_line =
"{exec {print-str ' exit:'} [var 0 0]"
"\n {while <is-le [var 0] 10>"
"\n {exec {print-str ' '} (print-hex0 [reg [var 0]]) [var 0 (add [var 0] 1)]}"
"\n }"
"\n (print-char 10)"
"\n}";
hook_p h;
if (strings_init(STRING_BUFF)
|| atoms_init(MAX_ATOMS)
|| symbols_init(MAX_SYMBOLS)
|| ringbuf_init(4096)
|| hooks_init(4, NULL, NULL)) return -1;
if (argc > 1) entry_hook_line = argv[1];
if (argc > 2) exit_hook_line = argv[2];
h = hook_install((uint_t)funnn, entry_hook_line, exit_hook_line, NULL, ringbuf_dump, splinter_test_mode);
if (!h) {
fprintf(stderr, "%s", splinter_error_get());
return -1;
}
h->enabled = 1;
memset(&ctx, 0, sizeof(ctx));
for(i = 0; i < 1000000; i++) {
context_call(&h->entry_chain, h, &ctx);
context_call(&h->exit_chain, h, &ctx);
context_close(h, &ctx);
}
dump_ringbuffer();
return 0;
}