-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_demo.c
88 lines (77 loc) · 2.48 KB
/
simple_demo.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
#include "sanitizer_symbolizer_tool.h"
#include <stdlib.h>
#include <stdio.h>
#define USE_PROG "./bug-san0-dbg0-64.bin"
#define USE_LLVM "./llvm-symbolizer"
#define USE_AD2L "./addr2line"
#define SEC_HEAD_TEXT 0x1320U
#define SEC_TAIL_TEXT 0x1C65U
#define SEC_HEAD_DATA 0x4000U
#define SEC_TAIL_DATA 0x41B8U
#define SEC_HEAD_BSS 0x41C0U
#define SEC_TAIL_BSS 0x4460U
void code_each(unsigned int head, unsigned int tail) {
for (unsigned int i=head; i<=tail; ++i) {
printf("CODE at damn offset 0x%x\n", i);
unsigned long N = 0;
SanSymTool_addr_send(USE_PROG, i, &N);
if (N > 0) {
for (unsigned long j=0; j<=(N-1); ++j) {
char *pfile;
char *pfunc;
unsigned long lin;
unsigned long col;
SanSymTool_addr_read(j, &pfile, &pfunc, &lin, &col);
if (!pfile) { pfile = "??"; }
if (!pfunc) { pfunc = "??"; }
printf("%s in %s:%lu:%lu\n", pfunc, pfile, lin, col);
}
}
SanSymTool_addr_free();
}
}
void data_each(unsigned int head, unsigned int tail) {
for (unsigned int i=head; i<=tail; ++i) {
printf("DATA at damn offset 0x%x\n", i);
SanSymTool_data_send(USE_PROG, (unsigned int)i);
char *pfile;
char *pname;
unsigned long lin;
unsigned long stt;
unsigned long siz;
SanSymTool_data_read(&pfile, &pname, &lin, &stt, &siz);
if (!pfile) {
pfile = "FAIL";
} else if (*pfile == '\n') {
pfile = "NULL";
} else {}
if (!pname) {
pname = "FAIL";
} else {}
printf("%s in %s:%lu, %lu at %lu\n", pname, pfile, lin, siz, stt);
SanSymTool_data_free();
}
}
int main() {
int init_st;
printf("===== Using llvm-symbolizer =====\n");
init_st = SanSymTool_init(USE_LLVM);
if (init_st != 0) {
printf("Init failed. RetCode=%d\n", init_st);
exit(0);
}
code_each(SEC_HEAD_TEXT, SEC_TAIL_TEXT);
data_each(SEC_HEAD_DATA, SEC_TAIL_DATA);
data_each(SEC_HEAD_BSS , SEC_TAIL_BSS );
SanSymTool_fini();
printf("===== Using addr2line =====\n");
init_st = SanSymTool_init(USE_AD2L);
if (init_st != 0) {
printf("Init failed. RetCode=%d\n", init_st);
exit(0);
}
code_each(SEC_HEAD_TEXT, SEC_TAIL_TEXT);
data_each(SEC_HEAD_DATA, SEC_TAIL_DATA);
data_each(SEC_HEAD_BSS , SEC_TAIL_BSS );
SanSymTool_fini();
}