-
Notifications
You must be signed in to change notification settings - Fork 6
/
diagnoser.h
150 lines (128 loc) · 4.11 KB
/
diagnoser.h
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
/*
* diagnoser.h
* Copyright (C) 2021 Zhuo Zhang, Xiangyu Zhang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DIAGNOSER_H
#define __DIAGNOSER_H
#include "binary.h"
#include "config.h"
#include "crs_config.h"
#include "disassembler.h"
#include "patcher.h"
#include "rewriter.h"
#include "sys_optarg.h"
#include <gmodule.h>
/*
* CrashPoint Type
*
* CP_INTERNAL: need to disassemble address
* CP_EXTERNAL: need to disassembly address and build jump bridge
* CP_RETADDR: need to build jump bridge
*/
// XXX: CP_RETADDR are only used when pdisasm is not fully supported. Note that
// in this situation, even we misidentify a CP_RETADDR, it would not impact the
// rewriting procedure (i.e., any wrong bridge will got fixed later / not
// uncertain_patches in Patcher).
typedef enum cp_type_t {
CP_NONE = 0UL,
CP_INTERNAL, // internal indirect call/jump
CP_EXTERNAL, // external callback from library
CP_RETADDR, // return address when calling library
} CPType;
#define z_cptype_string(t) \
((type == CP_INTERNAL) ? "INTERNAL" \
: ((type == CP_EXTERNAL) ? "EXTERNAL" : "RETADDR"))
/*
* Logged CrashPoint
*/
typedef struct crash_point_t {
addr_t addr;
CPType type;
bool is_real;
} CrashPoint;
/*
* The range of Dup-Binary-Search
*/
#define DD_RANGE 4
/*
* Stage for delta debugging mode
*/
typedef enum delta_debugging_stage {
DD_STAGE0, // validate whether it is a rewriting error
DD_STAGE1, // binary search to locate the e_iter in Patcher
DD_STAGE2, // validate whether all rewriting errors are in a DD_RANGE
DD_STAGE3, // binary search to locate the s_iter in Pacther
DD_NONE = -1, // not in the delta debugging mode
} DDStage;
/*
* Diagnoser distinguishes the intentional crashes and the unintentional ones,
* while it also manages the schedule of self-recovering.
*/
STRUCT(Diagnoser, {
Binary *binary;
Patcher *patcher;
Rewriter *rewriter;
Disassembler *disassembler;
DDStage dd_stage;
int dd_status;
addr_t dd_addr;
uint32_t dd_cov;
// used for distinguishing crash and checking runs
CRSStatus dd_crs_status;
const char *dd_banner;
// used for dup-binary-search (int64_t to avoid overflow)
int64_t dd_low;
int64_t dd_high;
int64_t dd_s_cur;
int64_t dd_e_cur;
// XXX: for effeciency, a CrashPoint struct is broken into three elements in
// the queue.
GQueue *crashpoints;
const char *cp_filename;
// rewriting optargs
RewritingOptArgs *opts;
});
DECLARE_GETTER(Diagnoser, diagnoser, GQueue *, crashpoints);
/*
* Create diagnoser
*/
Z_API Diagnoser *z_diagnoser_create(Patcher *patcher, Rewriter *rewriter,
Disassembler *disassembler,
RewritingOptArgs *opts);
/*
* Destroy diagnoser
*/
Z_API void z_diagnoser_destroy(Diagnoser *g);
/*
* Read recorded crashpoints from log file
*/
Z_API void z_diagnoser_read_crashpoint_log(Diagnoser *g);
/*
* Log down recorded crashpoints
*/
Z_API void z_diagnoser_write_crashpoint_log(Diagnoser *g);
/*
* Apply all logged crashpoints
*/
Z_API void z_diagnoser_apply_logged_crashpoints(Diagnoser *g);
/*
* Find a new crashpoint, and diagnoser will validate this crashpoint and does
* patch accordingly.
*/
Z_API CRSStatus z_diagnoser_new_crashpoint(Diagnoser *g, int status,
addr_t addr, uint32_t cov,
bool check_run_enabled);
#endif