File: sieve-binary-debug.c

package info (click to toggle)
dovecot 1%3A2.2.27-3%2Bdeb9u2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 48,520 kB
  • sloc: ansic: 430,475; sh: 17,438; makefile: 6,587; cpp: 1,557; perl: 295; python: 67; xml: 44; pascal: 27
file content (256 lines) | stat: -rw-r--r-- 6,272 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2016 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"

#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-code.h"

#include "sieve-binary-private.h"

/* Quick 'n dirty debug */
#if 0
#define debug_printf(...) printf ("lineinfo: " __VA_ARGS__)
#else
#define debug_printf(...)
#endif

/*
 * Opcodes
 */

enum {
	LINPROG_OP_COPY,
	LINPROG_OP_ADVANCE_PC,
	LINPROG_OP_ADVANCE_LINE,
	LINPROG_OP_SET_COLUMN,
	LINPROG_OP_SPECIAL_BASE
};

#define LINPROG_LINE_BASE   0
#define LINPROG_LINE_RANGE  4

/*
 * Lineinfo writer
 */

struct sieve_binary_debug_writer {
	struct sieve_binary_block *sblock;

	sieve_size_t address;
	unsigned long int line;
	unsigned long int column;
};

struct sieve_binary_debug_writer *sieve_binary_debug_writer_init
(struct sieve_binary_block *sblock)
{
	struct sieve_binary_debug_writer *dwriter;

	dwriter = i_new(struct sieve_binary_debug_writer, 1);
	dwriter->sblock = sblock;

	return dwriter;
}

void sieve_binary_debug_writer_deinit
(struct sieve_binary_debug_writer **dwriter)
{
	i_free(*dwriter);
	*dwriter = NULL;
}

void sieve_binary_debug_emit
(struct sieve_binary_debug_writer *dwriter, sieve_size_t code_address,
	unsigned int code_line, unsigned int code_column)
{
	struct sieve_binary_block *sblock = dwriter->sblock;
	sieve_size_t address_inc = code_address - dwriter->address;
	unsigned int line_inc = code_line - dwriter->line;
	unsigned int sp_opcode = 0;

	/* Check for applicability of special opcode */
	if ( (LINPROG_LINE_BASE + LINPROG_LINE_RANGE - 1) >= line_inc ) {
		sp_opcode = LINPROG_OP_SPECIAL_BASE + (line_inc - LINPROG_LINE_BASE) +
			(LINPROG_LINE_RANGE * address_inc);

		if ( sp_opcode > 255 )
			sp_opcode = 0;
	}

	/* Update line and address */
	if ( sp_opcode == 0 ) {
		if ( line_inc > 0 ) {
			(void)sieve_binary_emit_byte(sblock, LINPROG_OP_ADVANCE_LINE);
			(void)sieve_binary_emit_unsigned(sblock, line_inc);
		}

		if ( address_inc > 0 ) {
			(void)sieve_binary_emit_byte(sblock, LINPROG_OP_ADVANCE_PC);
			(void)sieve_binary_emit_unsigned(sblock, address_inc);
		}
	} else {
		(void)sieve_binary_emit_byte(sblock, sp_opcode);
	}

	/* Set column */
	if ( dwriter->column != code_column ) {
		(void)sieve_binary_emit_byte(sblock, LINPROG_OP_SET_COLUMN);
		(void)sieve_binary_emit_unsigned(sblock, code_column);
	}

	/* Generate matrix row */
	(void)sieve_binary_emit_byte(sblock, LINPROG_OP_COPY);

	dwriter->address = code_address;
	dwriter->line = code_line;
	dwriter->column = code_column;
}

/*
 * Debug reader
 */

struct sieve_binary_debug_reader {
	struct sieve_binary_block *sblock;

	sieve_size_t address, last_address;
	unsigned long int line, last_line;

	unsigned long int column;

	sieve_size_t state;
};

struct sieve_binary_debug_reader *sieve_binary_debug_reader_init
(struct sieve_binary_block *sblock)
{
	struct sieve_binary_debug_reader *dreader;

	dreader = i_new(struct sieve_binary_debug_reader, 1);
	dreader->sblock = sblock;

	return dreader;
}

void sieve_binary_debug_reader_deinit
(struct sieve_binary_debug_reader **dreader)
{
	i_free(*dreader);
	*dreader = NULL;
}

void sieve_binary_debug_reader_reset
(struct sieve_binary_debug_reader *dreader)
{
	dreader->address = 0;
	dreader->line = 0;
	dreader->column = 0;
	dreader->state = 0;
}

unsigned int sieve_binary_debug_read_line
(struct sieve_binary_debug_reader *dreader, sieve_size_t code_address)
{
	size_t linprog_size;
	sieve_size_t address;
	unsigned long int line;

	if ( code_address < dreader->last_address )
		sieve_binary_debug_reader_reset(dreader);

	if ( code_address >= dreader->last_address &&
		code_address < dreader->address ) {
		debug_printf("%08llx: NOOP [%08llx]\n",
			(unsigned long long) dreader->state, (unsigned long long) code_address);
		return dreader->last_line;
	}

	address = dreader->address;
	line = dreader->line;

	debug_printf("%08llx: READ [%08llx]\n",
		(unsigned long long) dreader->state, (unsigned long long) code_address);

	linprog_size = sieve_binary_block_get_size(dreader->sblock);
	while ( dreader->state < linprog_size ) {
		unsigned int opcode;
		unsigned int value;

		if ( sieve_binary_read_byte(dreader->sblock, &dreader->state, &opcode) ) {
			switch ( opcode ) {

			case LINPROG_OP_COPY:
				debug_printf("%08llx: COPY ==> %08llx: %ld\n",
					(unsigned long long) dreader->state, (unsigned long long) address,
					line);

				dreader->last_address = dreader->address;
				dreader->last_line = dreader->line;

				dreader->address = address;
				dreader->line = line;

				if ( code_address < address ) {
					return dreader->last_line;
				} else if ( code_address == address ) {
					return dreader->line;
				}
				break;

			case LINPROG_OP_ADVANCE_PC:
				debug_printf("%08llx: ADV_PC\n", (unsigned long long) dreader->state);
				if ( !sieve_binary_read_unsigned
					(dreader->sblock, &dreader->state, &value) ) {
					sieve_binary_debug_reader_reset(dreader);
					return 0;
				}
				debug_printf("        : + %d\n", value);
				address += value;
				break;

			case LINPROG_OP_ADVANCE_LINE:
				debug_printf("%08llx: ADV_LINE\n", (unsigned long long) dreader->state);
				if ( !sieve_binary_read_unsigned
					(dreader->sblock, &dreader->state, &value) ) {
					sieve_binary_debug_reader_reset(dreader);
					return 0;
				}
				debug_printf("        : + %d\n", value);
				line += value;
				break;

			case LINPROG_OP_SET_COLUMN:
				debug_printf("%08llx: SET_COL\n", (unsigned long long) dreader->state);
				if ( !sieve_binary_read_unsigned
					(dreader->sblock, &dreader->state, &value) ) {
					sieve_binary_debug_reader_reset(dreader);
					return 0;
				}
				debug_printf("        : = %d\n", value);
				dreader->column = value;
				break;

			default:
				opcode -= LINPROG_OP_SPECIAL_BASE;

				address += (opcode / LINPROG_LINE_RANGE);
				line += LINPROG_LINE_BASE + (opcode % LINPROG_LINE_RANGE);

				debug_printf("%08llx: SPECIAL\n", (unsigned long long) dreader->state);
				debug_printf("        :  +A %d +L %d\n", (opcode / LINPROG_LINE_RANGE),
					LINPROG_LINE_BASE + (opcode % LINPROG_LINE_RANGE));
				break;
			}
		} else {
			debug_printf("OPCODE READ FAILED\n");
			sieve_binary_debug_reader_reset(dreader);
			return 0;
		}
	}

	return dreader->line;
}