File: tracing.c

package info (click to toggle)
eztrace 2.0%2Brepack-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,132 kB
  • sloc: ansic: 23,501; perl: 910; sh: 857; cpp: 771; makefile: 696; fortran: 327; f90: 320; python: 57
file content (297 lines) | stat: -rw-r--r-- 7,754 bytes parent folder | download | duplicates (10)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- c-file-style: "GNU" -*- */
/*
 * Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
 * See COPYING in top-level directory.
 *
 *
 * tracing.c - test for tracing functions
 *
 *  Created on: 26 Aug. 2012
 *      Author: Damien Martin-Guillerez <damien.martin-guillerez@inria.fr>
 */

#include <string.h>
#include <binary.h>
#include <tracing.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include "common.h"

int testfork;

// Common parts
#if __x86_64__
// x86_64
#define instr_call_parent() asm("int3")
#elif __arm__
// ARMv7
#define instr_call_parent() asm("bkpt #0")
#endif
#define call_parent() \
	do { \
		DEBUG("traced process is before call_parent"); \
		instr_call_parent(); \
		DEBUG("traced process is after call_parent"); \
	} while(0)
#define call_parent_lbl(lbl) \
	do { \
		DEBUG("traced process is before call_parent"); \
		instr_call_parent(); \
		do_lbl(lbl); \
	} while(0)
#define lbl_name(lbl) test_##lbl
#define lbl_addr(lbl) &&lbl_name(lbl)
#define do_lbl(lbl) do { \
		lbl_name(lbl): \
		DEBUG("traced process is after label " #lbl); \
	} while(0)

pid_t child;
int fds[2];

static inline int run_ipc() {
  if (testfork)
    return 0;
  pipe(fds);
  child = trace_run(NULL, NULL, NULL, 1);
  return child > 0 ? 1 : 0;
}
static inline void close_ipc() {
  close(fds[0]);
  close(fds[1]);
}
#define FINISH_IPC() do { \
		trace_detach(child); \
		trace_wait(child); \
		DEBUG("process finished"); \
	} while(0)
#define IPC_END() do { \
		DEBUG("traced process is exiting"); \
		exit(0); \
	} while(0)
#define READ_WORD(w) read(fds[0], &w, sizeof(w))
#define WRITE_WORD(w) write(fds[1], &w, sizeof(w))

// End of common parts
BEGIN_TEST(getset_ip)
  void* r;
  word_uint rr;
  // print the ip of the int 3 instruction and propose a function ip
  if (run_ipc()) {
    // The tracing process
    trace_continue(child);
    r = (void*) get_ip(child);
    TEST2(lbl_addr(getset_ip_lbl1) == r, get_ip,
          "returned %p while awaiting %p!", r, lbl_addr(getset_ip_lbl1));
    set_ip(child, (word_uint)lbl_addr(getset_ip_lbl2));
    FINISH_IPC()
    ;
    READ_WORD(rr);
    TEST1(rr == 1, set_ip, "returned "WORD_DEC_FORMAT" while awaiting 1!", rr);
    close_ipc();
  } else {
    // The traced process
    rr = 1;
    call_parent_lbl(getset_ip_lbl1);
    rr = 0;
    do_lbl(getset_ip_lbl2);
    WRITE_WORD(rr);
    IPC_END();
  }
  END_TEST(getset_ip)

BEGIN_TEST(trace_readwrite)
  static word_uint result; 	// try write then read result and compares
  if (run_ipc()) {
    word_uint rr;
    trace_continue(child);
    READ_WORD(result);
    trace_read(child, (word_uint) &result, (uint8_t*) &rr, sizeof(rr));
    TEST2(rr == result, trace_read,
          "returned "WORD_HEX_FORMAT" while awaiting "WORD_HEX_FORMAT"!", rr,
          result);
    rr = 42;
    trace_write(child, (word_uint) &result, (uint8_t*) &rr, sizeof(rr));
    FINISH_IPC()
    ;
    READ_WORD(result);
    TEST2(
        rr == result,
        trace_write,
        "trace_write(): value of result is "WORD_HEX_FORMAT" while awaiting "WORD_HEX_FORMAT"!",
        rr, result);
    close_ipc();
  } else {
    result = 4242;
    WRITE_WORD(result);
    call_parent();
    WRITE_WORD(result);
    IPC_END();
  }
  END_TEST(trace_readwrite)

BEGIN_TEST(trace_replace)
// give one address and the original value and a value to replace
  static uint8_t value;
  if (run_ipc()) {
    trace_continue(child);
    READ_WORD(value);
    uint8_t rr = trace_replace(child, (word_uint) &value, 42);
    TEST2(value == rr, trace_replace, "returned %x while awaiting %x", rr,
          value);
    FINISH_IPC()
    ;
    READ_WORD(value);
    TEST1(value == 42, trace_replace, "written %x while awaiting 42", value);
    close_ipc();
  } else {
    value = 24;
    WRITE_WORD(value);
    call_parent();
    WRITE_WORD(value);
    IPC_END();
  }
  END_TEST(trace_replace)

BEGIN_TEST(trace_copy)
// give two addresses and a string then print the resulting string
  static char buffer[10];
  static const char *tocopy = "BBBBBBBBB\0ABC";
  word_uint val = -1;
  if (run_ipc()) {
    trace_copy(child, (word_uint) tocopy, (word_uint) buffer,
               strlen(tocopy) + 1);
    FINISH_IPC()
    ;
    READ_WORD(val);
    TEST0(val == 1, trace_copy, "the buffer comparison failed");
    close_ipc();
  } else {
    val = strcmp(buffer, tocopy) == 0 ? 1 : 0;
    if (!val) {
      DEBUG2("%s != %s!", buffer, tocopy);
    }
    WRITE_WORD(val);
    IPC_END();
  }
  END_TEST(trace_copy)

BEGIN_TEST(trace_singlestep)
  void *r;
  if (run_ipc()) {
    trace_continue(child);
    r = (void*) get_ip(child);
    TEST2(lbl_addr(singlestep_lbl1) == r, set-up,
          "eip is %p while awaiting %p!", r, lbl_addr(singlestep_lbl1));
    trace_singlestep(child);
    r = (void*) get_ip(child);
    TEST2(lbl_addr(singlestep_lbl2) == r, singlestep,
          "eip is %p while awaiting %p!", r, lbl_addr(singlestep_lbl2));
    FINISH_IPC()
    ;
    close_ipc();
  } else {
    instr_call_parent();
    lbl_name(singlestep_lbl1):
#if __x86_64__
// x86_64
#if __WORDSIZE == 64
    asm("inc %rax");
#else
    asm("inc %eax");
#endif

#elif __arm__
// ARMv7
    asm("add r0, r0, #1");
#endif
    lbl_name(singlestep_lbl2):
    IPC_END();
  }
  END_TEST(trace_singlestep)

BEGIN_TEST(trace_mmap)
  static char *buf1 = "ABCDEF";
  static char *buf2;
  if (run_ipc()) {
    word_uint mmap = trace_mmap(child, 0, 1024, PROT_READ | PROT_WRITE);
    TEST0(mmap != 0, mmap_nonnull, "mmap returned null!");
    int errcode = (int) (-(word_uint) mmap);
    TEST2(errcode < 0 || errcode > 127, mmap_failed,
          "mmap returned the error code %d (strerror = %s)!", errcode,
          strerror(errcode));
    DEBUG1("trace_mmap returned %p", (void* )mmap);
    trace_write(child, (word_uint) &buf2, (uint8_t*) &mmap, sizeof(mmap));
    DEBUG("written the address of mmap to buf2");
    trace_copy(child, (word_uint) buf1, mmap, strlen(buf1) + 1);
    DEBUG("copied content of buf1 to mmap'd content");
    FINISH_IPC()
    ;
    READ_WORD(mmap);
    TEST0(mmap == 1, mmap_copy, "data comparison failed!");
    close_ipc();
  } else {
    DEBUG1("buf2 = %p", buf2);
    word_uint val = strcmp(buf1, buf2) == 0 ? 1 : 0;
    if (!val) {
      DEBUG2("%s != %s!", buf1, buf2);
    }
    WRITE_WORD(val);
    IPC_END();
  }
  END_TEST(trace_mmap)

void usage(char **av, char option) {
  if (option != 0) {
    fprintf(stderr, "Unknown option '%c'", option);
  }
  fprintf(
      stderr,
      "Usage: %s [-v[v]] [test1 test2 test3 ...]\n"
      "\t-v output verbose debugging information (the more d option, the more verbose the output)\n"
      "\t-d don't actually run the tests, simply execute the child (used in conjunction with test specifiers to debug)\n"
      "\ttest specifies the tests to runs (default is all tests). Available tests: getset_ip, readwrite, replace, copy, singlestep, mmap.\n",
      av[0]);
  exit(-1);
}
int main(int ac, char **av) {
  int r, i, j;
  debug = 0;
  testfork = 0;
  int runall = 1;
  for (i = 1; i < ac; i++) {
    if (av[i][0] == '-') {
      for (j = 1; av[i][j] != 0; j++) {
        switch (av[i][j]) {
        case 'v':
          debug++;
          break;
        case 'd':
          testfork++;
          break;
        default:
          usage(av, av[i][j]);
        }
      }
    } else {
      PARSE_TEST(getset_ip);
      PARSE_TEST(trace_readwrite);
      PARSE_TEST(trace_replace);
      PARSE_TEST(trace_copy);
      PARSE_TEST(trace_singlestep);
      PARSE_TEST(trace_mmap);
    }
  }

  RUN_TEST(getset_ip);
  RUN_TEST(trace_readwrite);
  RUN_TEST(trace_replace);
  RUN_TEST(trace_copy);
  RUN_TEST(trace_singlestep);
  RUN_TEST(trace_mmap);

  return 0;
}