summaryrefslogtreecommitdiffstats
path: root/src/audit-error.c
blob: 7636f35399c249809da110a73f3c698f67b01754 (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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

#include <stdio.h>
#include <search.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <strings.h>
#include <string.h>
#include <signal.h>

#include "config.h"
#include "backtrace.h"

int lt_audit_parse_init(struct lt_config_shared *cfg);
int lt_audit_parse();
extern FILE *lt_audit_in;

#define SYMBOL_TAB_SIZE 100
static struct hsearch_data symbol_tab;
static int symbol_tab_init = 0;

struct lt_error_sym* lt_error_sym_get(struct lt_config_audit *cfg,
				      const char *name)
{
	struct lt_error_sym *sym;
	ENTRY e, *ep;

	if (!symbol_tab_init)
		return NULL;

	PRINT_VERBOSE(cfg, 1, "request for <%s>\n", name);

	e.key = (char*) name;
	hsearch_r(e, FIND, &ep, &symbol_tab);

	if (!ep) {
		PRINT_VERBOSE(cfg, 1, "failed to find <%s>\n", name);
		return NULL;
	}

	sym = (struct lt_error_sym*) ep->data;

	PRINT_VERBOSE(cfg, 1, "found %p <%s>\n", sym, sym->name);
	return sym;
}

extern __thread int pipe_fd;

static void share_info(struct lt_config_audit *cfg,
		       long ret, int keep,
		       struct timeval *tv,
		       struct link_map *lr,
		       const La_regs *inregs)
{
	void *ret_addr;
#define LT_MAXMSG 100
	char text[LT_MAXMSG];

	lt_error_get_retaddr(cfg, &ret_addr, inregs);
	ret_addr -= lr->l_addr;

	if (keep)
		snprintf(text, LT_MAXMSG, "ERROR SIMULATION no change, ret %p",
			 ret_addr);
	else
		snprintf(text, LT_MAXMSG, "ERROR SIMULATION inserted %ld, ret %p",
			 ret, ret_addr);

	if (lt_sh(cfg, pipe)) {
		char buf[LT_FIFO_MSG_MAXLEN];
		int len;

		len = lt_fifo_mtext_get(cfg->sh, buf, tv, text);
		/* TODO erro handling */
		lt_fifo_send(cfg->sh, pipe_fd, buf, len);
	} else
		lt_out_text(cfg->sh, tv, syscall(SYS_gettid), text);

	dump_backtrace(cfg->sh, 0);
}

static struct lt_config_shared *ss_cfg;

static void sig_segv_handler(int sig)
{
	char text[LT_MAXMSG];
	struct timeval tv;

	gettimeofday(&tv, NULL);

	snprintf(text, LT_MAXMSG, "ERROR SIMULATION got SIGSEGV");

	if (lt_sh(ss_cfg, pipe)) {
		char buf[LT_FIFO_MSG_MAXLEN];
		int len;

		len = lt_fifo_mtext_get(ss_cfg, buf, &tv, text);
		/* TODO erro handling */
		lt_fifo_send(ss_cfg, pipe_fd, buf, len);
	} else
		lt_out_text(ss_cfg->sh, &tv, syscall(SYS_gettid), text);

	dump_backtrace(ss_cfg, 0);
	exit(-1);
}

static int install_sigsegv(struct lt_config_shared *cfg)
{
	struct sigaction act;

	bzero(&act, sizeof(act));
	act.sa_handler = sig_segv_handler;

	ss_cfg = cfg;

	return sigaction(SIGSEGV, &act, NULL);
}

static __thread unsigned long automated_sym_cnt = 0;


/* TODO make type agnostics... now only AFTER type */
static void update_start_info(struct lt_config_audit *cfg,
			     const char *symname, int is_entry)
{
	if (cfg->error_allowed)
		return;

	if (is_entry && cfg->error_start_after_entries) {
		if (strcmp(symname, cfg->error_start_after_sym))
			return;

		cfg->error_start_after_current++;

		if (cfg->error_start_after_current != cfg->error_start_after_entries)
			return;

		cfg->error_allowed = 1;
	}
}

int lt_error_sym_entry(struct lt_config_audit *cfg,
		       const char *symname)
{
	update_start_info(cfg, symname, 1);
	return 0;
}

int lt_error_sym_exit(struct lt_config_audit *cfg,
		      const char *symname,
		      struct lt_symbol *sym,
		      struct timeval *tv,
		      struct link_map *lr,
		      const La_regs *inregs,
		      La_retval *outregs,
		      long *info)
{
	struct lt_error_sym *esym;
	struct lt_error_config *cfg_err = cfg->error_config;
	int keep = 0;
	int handle_sigsegv;
	long ret;

	/* we're not interested in this symbol */
	if (!sym || !sym->error)
		return -1;

	update_start_info(cfg, symname, 0);

	if (!cfg->error_allowed)
		return -1;

	if (cfg_err->type == LT_ERROR_RUN_TYPE_AUTO) {
		struct lt_error_sym *esym;

		PRINT_VERBOSE(cfg, 1, "automated_sym_cnt %d,  cfg_err->sym_cnt %d\n",
			      automated_sym_cnt, cfg_err->sym_cnt);

		if (automated_sym_cnt >= cfg_err->sym_cnt)
			return -1;

		esym = &cfg_err->sym[automated_sym_cnt];
		ret = esym->ret;
		keep = esym->keep;
		handle_sigsegv = esym->handle_sigsegv;

		*info = automated_sym_cnt++;
	} else {

		esym = sym->error;

		PRINT_VERBOSE(cfg, 1, "symbol %s, call %lu, n %ld\n",
			      sym->name, esym->call_configured, cfg_err->n);

		/* we are not interested in this call number  */
		if (esym->call_current++ != esym->call_configured)
			return -1;

		ret = esym->ret;
		handle_sigsegv = esym->handle_sigsegv;
	}

	if (keep)
		PRINT_VERBOSE(cfg, 1, "keeping the return value\n",
			      ret);
	else
		PRINT_VERBOSE(cfg, 1, "changing retval to %lu\n",
			      ret);

	/* The fun begins right now.. let's change the return
	 * value for the symbol ... */
	if (!keep)
		lt_error_set_retval(cfg, ret, outregs);

	/* and share some info about the sinner */
	share_info(cfg, ret, keep, tv, lr, inregs);

	if (handle_sigsegv && install_sigsegv(cfg->sh))
		PRINT_VERBOSE(cfg, 1, "failed to install SIGSEGV handler\n");

	return 0;
}

static int symbol_add(struct lt_config_audit *cfg,
		      struct lt_error_config *cfg_err,
		      struct lt_error_sym *sym)
{
	ENTRY e, *ep;

	PRINT_VERBOSE(cfg, 1, "symbol %s, ret %ld\n",
			sym->name.symbol, sym->ret);

	/* should be safe, since only one thread
	 * is doing the initialization */
	if (!symbol_tab_init) {
		if (!hcreate_r(SYMBOL_TAB_SIZE, &symbol_tab)) {
			perror("failed to create hash table");
			return -1;
		}
		symbol_tab_init = 1;
		PRINT_VERBOSE(cfg, 1, "symbol table initialized\n");
	}

	e.key = sym->name.symbol;
	if (hsearch_r(e, FIND, &ep, &symbol_tab)) {
		PRINT_VERBOSE(cfg, 1, "symbol %s already in the table\n",
			      sym->name.symbol);
		/* This is a bug in normal processing,
		 * but completelly ok in automated mode. */
		return cfg_err->type == LT_ERROR_RUN_TYPE_AUTO? 0 : -1;
	}

	e.key = sym->name.symbol;
	e.data = sym;

	if (!hsearch_r(e, ENTER, &ep, &symbol_tab)) {
		perror("hsearch_r failed");
		free(sym);

		PRINT_VERBOSE(cfg, 3,
			"reached the error symbol limit %d\n",
			SYMBOL_TAB_SIZE);
		return -1;
	}

	PRINT_VERBOSE(cfg, 1, "ok\n");
	return 0;
}

static void display_error_symbols(struct lt_config_audit *cfg,
				  char *names, int size)
{
	char *p;

	PRINT_VERBOSE(cfg, 1, "START size %d\n", size);

	while(size) {
		PRINT_VERBOSE(cfg, 1, "name %s\n", names);
		p = memchr(names, 0, size);
		size -= ++p - names;
		names = p;
	}

	PRINT_VERBOSE(cfg, 1, "END\n");
}


static int init_start_info(struct lt_config_audit *cfg,
			   struct lt_error_config *cfg_err,
			   char *names)
{
	struct lt_error_start *start = &cfg_err->start;

	if (start->type == LT_ERROR_START_TYPE_AFTER) {
		if (start->after.name.index != -1)
			cfg->error_start_after_sym = names + start->after.name.index;

		cfg->error_start_after_entries = start->after.entries;
		cfg->error_start_after_exits   = start->after.exits;

		PRINT_VERBOSE(cfg, 1, "START AFTER entries %ld, exits %ld, sym %s\n",
			cfg->error_start_after_entries,
			cfg->error_start_after_exits,
			cfg->error_start_after_sym);
	}

	return 0;
}

int lt_error_init(struct lt_config_audit *cfg)
{
	struct lt_error_config *cfg_err = cfg->error_config;
	int sym_cnt = cfg_err->sym_cnt, i, ok = 1;
	char *names;

	lt_sh(cfg, error_sim) = 0;

	names = (char*) (((void*) &cfg_err->sym) +
		(sym_cnt * sizeof(struct lt_error_sym)));

	display_error_symbols(cfg, names, cfg_err->names_size);

	for(i = 0; i < sym_cnt; i++) {
		struct lt_error_sym *sym;

		sym = &cfg_err->sym[i];

		PRINT_VERBOSE(cfg, 1, "symbol index %d, name %s\n",
			      sym->name.index, names + sym->name.index);

		sym->name.symbol = names + sym->name.index;

		if (symbol_add(cfg, cfg_err, sym)) {
			ok = 0;
			break;
		}
	}

	if (!ok) {
		if (symbol_tab_init)
			hdestroy_r(&symbol_tab);
		return -1;
	}

	if (init_start_info(cfg, cfg_err, names))
		return -1;

	lt_sh(cfg, error_sim) = 1;
	return 0;
}

int lt_error_config_read(struct lt_config_audit *cfg, int fd)
{
	int size;
	struct lt_error_config *cfg_err, cfg_err_st;
	ssize_t n;

	n = read(fd, &cfg_err_st, sizeof(cfg_err_st));
	if (n != sizeof(cfg_err_st)) {
		perror("read failed");
		return -1;
	}

	size = cfg_err_st.sym_cnt * sizeof(struct lt_error_sym);
	size += cfg_err_st.names_size;

	PRINT_VERBOSE(cfg, 1, "symbols count %d, names_size %d, total size %d\n",
		      cfg_err_st.sym_cnt, cfg_err_st.names_size, size);

	cfg_err = malloc(sizeof(*cfg_err) + size);
	if (!cfg_err) {
		perror("malloc failed");
		return -1;
	}

	*cfg_err = cfg_err_st;

	if (size != read(fd, &cfg_err->sym, size)) {
		PRINT_VERBOSE(cfg, 1, "failed: no error symbols defined\n");
		return -1;
	}

	cfg->error_config = cfg_err;
	return 0;
}