File: vmod_debug_filters.c

package info (click to toggle)
varnish 7.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,256 kB
  • sloc: ansic: 104,222; python: 2,679; makefile: 1,303; sh: 1,077; awk: 114; perl: 105; ruby: 41
file content (692 lines) | stat: -rw-r--r-- 17,348 bytes parent folder | download | duplicates (2)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*-
 * Copyright (c) 2012-2019 Varnish Software AS
 * All rights reserved.
 *
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include "cache/cache_varnishd.h"
#include "cache/cache_filter.h"

#include "vgz.h"
#include "vsha256.h"
#include "vtim.h"
#include "vcc_debug_if.h"

#include "vmod_debug.h"

/**********************************************************************/

static enum vfp_status v_matchproto_(vfp_pull_f)
xyzzy_vfp_rot13_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
    ssize_t *lp)
{
	enum vfp_status vp;
	char *q;
	ssize_t l;

	(void)vfe;
	vp = VFP_Suck(vc, p, lp);
	if (vp == VFP_ERROR)
		return (vp);
	q = p;
	for (l = 0; l < *lp; l++, q++) {
		if (*q >= 'A' && *q <= 'Z')
			*q = (((*q - 'A') + 13) % 26) + 'A';
		if (*q >= 'a' && *q <= 'z')
			*q = (((*q - 'a') + 13) % 26) + 'a';
	}
	return (vp);
}

static const struct vfp xyzzy_vfp_rot13 = {
	.name = "rot13",
	.pull = xyzzy_vfp_rot13_pull,
};

/**********************************************************************/

// deliberately fragmenting the stream to make testing more interesting
#define ROT13_BUFSZ 8

static int v_matchproto_(vdp_init_f)
xyzzy_vdp_rot13_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);

	AN(priv);

	*priv = malloc(ROT13_BUFSZ);
	if (*priv == NULL)
		return (-1);

	return (0);
}

static int v_matchproto_(vdp_bytes_f)
xyzzy_vdp_rot13_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
    const void *ptr, ssize_t len)
{
	char *q;
	const char *pp;
	int i, j, retval = 0;

	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	AN(priv);
	AN(*priv);
	if (len <= 0)
		return (VDP_bytes(vdc, act, ptr, len));
	AN(ptr);
	if (act != VDP_END)
		act = VDP_FLUSH;
	q = *priv;
	pp = ptr;

	for (i = 0, j = 0; j < len; i++, j++) {
		if (pp[j] >= 'A' && pp[j] <= 'Z')
			q[i] = (((pp[j] - 'A') + 13) % 26) + 'A';
		else if (pp[j] >= 'a' && pp[j] <= 'z')
			q[i] = (((pp[j] - 'a') + 13) % 26) + 'a';
		else
			q[i] = pp[j];
		if (i == ROT13_BUFSZ - 1 && j < len - 1) {
			retval = VDP_bytes(vdc, VDP_FLUSH, q, ROT13_BUFSZ);
			if (retval != 0)
				return (retval);
			i = -1;
		}
	}
	if (i >= 0)
		retval = VDP_bytes(vdc, act, q, i);
	return (retval);
}

static int v_matchproto_(vdp_fini_f)
xyzzy_vdp_rot13_fini(struct vdp_ctx *vdc, void **priv)
{
	(void)vdc;
	AN(priv);
	free(*priv);
	*priv = NULL;
	return (0);
}

static const struct vdp xyzzy_vdp_rot13 = {
	.name  = "rot13",
	.init  = xyzzy_vdp_rot13_init,
	.bytes = xyzzy_vdp_rot13_bytes,
	.fini  = xyzzy_vdp_rot13_fini,
};

VCL_VOID v_matchproto_(td_debug_rot104)
xyzzy_rot104(VRT_CTX)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	// This should fail
	AN(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
}

/**********************************************************************
 * vdp debug_chunked: force http1 chunked encoding by removing the
 * Content-Length header
 *
 * this happens in a VDP because cnt_transmit() runs after VCL and
 * restores it
 */

static int v_matchproto_(vdp_init_f)
xyzzy_vdp_chunked_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);
	AN(priv);

	http_Unset(vdc->hp, H_Content_Length);
	*vdc->clen = -1;

	return (1);
}

static const struct vdp xyzzy_vdp_chunked = {
	.name  = "debug.chunked",
	.init  = xyzzy_vdp_chunked_init,
};

/**********************************************************************
 * pedantic tests of the VDP API:
 * - assert that we see a VDP_END
 * - assert that _fini gets called before the task ends
 *
 * note:
 * we could lookup our own vdpe in _fini and check for vdpe->end == VDP_END
 * yet that would cross the API
 */

enum vdp_state_e {
	VDPS_NULL = 0,
	VDPS_INIT,	// _init called
	VDPS_BYTES,	// _bytes called act != VDP_END
	VDPS_END,	// _bytes called act == VDP_END
	VDPS_FINI	// _fini called
};

struct vdp_state_s {
	unsigned		magic;
#define VDP_STATE_MAGIC	0x57c8d309
	enum vdp_state_e	state;
};

static void v_matchproto_(vmod_priv_fini_f)
priv_pedantic_fini(VRT_CTX, void *priv)
{
	struct vdp_state_s *vdps;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CAST_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);

	assert(vdps->state == VDPS_FINI);
}

static const struct vmod_priv_methods priv_pedantic_methods[1] = {{
	.magic = VMOD_PRIV_METHODS_MAGIC,
	.type = "debug_vdp_pedantic",
	.fini = priv_pedantic_fini
}};

static int v_matchproto_(vdp_init_f)
xyzzy_pedantic_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{
	struct vdp_state_s *vdps;
	struct vmod_priv *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);
	AN(priv);

	WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_STATE_MAGIC);
	if (vdps == NULL)
		return (-1);
	assert(vdps->state == VDPS_NULL);

	p = VRT_priv_task(ctx, (void *)vdc);
	if (p == NULL)
		return (-1);
	p->priv = vdps;
	p->methods = priv_pedantic_methods;

	*priv = vdps;

	vdps->state = VDPS_INIT;

	return (0);
}

static int v_matchproto_(vdp_bytes_f)
xyzzy_pedantic_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
    const void *ptr, ssize_t len)
{
	struct vdp_state_s *vdps;

	CAST_OBJ_NOTNULL(vdps, *priv, VDP_STATE_MAGIC);
	assert(vdps->state >= VDPS_INIT);
	assert(vdps->state < VDPS_END);

	if (act == VDP_END)
		vdps->state = VDPS_END;
	else
		vdps->state = VDPS_BYTES;

	return (VDP_bytes(vdc, act, ptr, len));
}

static int v_matchproto_(vdp_fini_f)
xyzzy_pedantic_fini(struct vdp_ctx *vdc, void **priv)
{
	struct vdp_state_s *vdps;

	(void) vdc;
	AN(priv);
	if (*priv == NULL)
		return (0);
	TAKE_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
	assert(vdps->state == VDPS_INIT || vdps->state == VDPS_END);
	vdps->state = VDPS_FINI;

	return (0);
}

static const struct vdp xyzzy_vdp_pedantic = {
	.name  = "debug.pedantic",
	.init  = xyzzy_pedantic_init,
	.bytes = xyzzy_pedantic_bytes,
	.fini  = xyzzy_pedantic_fini,
};

/**********************************************************************
 *
 * this trivial copy/paste/edit filter (of rot13) was specifically made for
 * someone who added a DBG_SLOW_BEREQ debug flag. It should actually be turned
 * in a proper "bandwidth control" filter, but that exceeds an evening's work,
 * so it's kept for later
 */

static enum vfp_status v_matchproto_(vfp_pull_f)
xyzzy_vfp_slow_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
    ssize_t *lp)
{

	(void)vfe;
	VTIM_sleep(1.0);
	return (VFP_Suck(vc, p, lp));
}

static const struct vfp xyzzy_vfp_slow = {
	.name = "debug.slow",
	.pull = xyzzy_vfp_slow_pull,
};

/**********************************************************************/

static int v_matchproto_(vdp_bytes_f)
xyzzy_vdp_slow_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
    const void *ptr, ssize_t len)
{

	(void)priv;
	VTIM_sleep(1.0);
	return (VDP_bytes(vdc, act, ptr, len));
}

static const struct vdp xyzzy_vdp_slow = {
	.name  = "debug.slow",
	.bytes = xyzzy_vdp_slow_bytes
};

/*
 * checksum VDP:
 * test that the stream of bytes has a certain checksum and either log
 * or panic
 *
 * The sha256 and crc32 variants are basically identical, but the amount of
 * code does not justify generalizing. (slink)
 */

enum vdp_chk_mode_e {
	//lint -esym(749, vdp_chk_mode_e::VDP_CHK_INVAL) deliberately not referenced
	VDP_CHK_INVAL = 0,
	VDP_CHK_LOG,
	VDP_CHK_PANIC,
	VDP_CHK_PANIC_UNLESS_ERROR
};

struct vdp_chksha256_cfg_s {
	unsigned			magic;
#define VDP_CHKSHA256_CFG_MAGIC		0x624f5b32
	enum vdp_chk_mode_e		mode;
	unsigned char			expected[VSHA256_DIGEST_LENGTH];
};

struct vdp_chkcrc32_cfg_s {
	unsigned			magic;
#define VDP_CHKCRC32_CFG_MAGIC		0x5a7a835c
	enum vdp_chk_mode_e		mode;
	uint32_t			expected;
};

struct vdp_chksha256_s {
	unsigned			magic;
#define VDP_CHKSHA256_MAGIC		0x6856e913
	unsigned			called;
	size_t				bytes;
	struct VSHA256Context		cx[1];
	struct vdp_chksha256_cfg_s	*cfg;
};

struct vdp_chkcrc32_s {
	unsigned			magic;
#define VDP_CHKCRC32_MAGIC		0x15c03d3c
	unsigned			called;
	size_t				bytes;
	uint32_t			crc;
	struct vdp_chkcrc32_cfg_s	*cfg;
};

static const void * const chksha256_priv_id = &chksha256_priv_id;
static const void * const chkcrc32_priv_id = &chkcrc32_priv_id;

static int v_matchproto_(vdp_init_f)
xyzzy_chksha256_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{
	struct vdp_chksha256_s *vdps;
	struct vmod_priv *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);
	AN(priv);

	WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKSHA256_MAGIC);
	if (vdps == NULL)
		return (-1);
	VSHA256_Init(vdps->cx);

	p = VRT_priv_task_get(ctx, chksha256_priv_id);
	if (p == NULL)
		return (-1);

	assert(p->len == sizeof(struct vdp_chksha256_cfg_s));
	CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKSHA256_CFG_MAGIC);
	*priv = vdps;

	return (0);
}

static int v_matchproto_(vdp_init_f)
xyzzy_chkcrc32_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{
	struct vdp_chkcrc32_s *vdps;
	struct vmod_priv *p;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);
	AN(priv);

	WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKCRC32_MAGIC);
	if (vdps == NULL)
		return (-1);
	vdps->crc = crc32(0L, Z_NULL, 0);

	p = VRT_priv_task_get(ctx, chkcrc32_priv_id);
	if (p == NULL)
		return (-1);

	assert(p->len == sizeof(struct vdp_chkcrc32_cfg_s));
	CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKCRC32_CFG_MAGIC);
	*priv = vdps;

	return (0);
}

static int v_matchproto_(vdp_bytes_f)
xyzzy_chksha256_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
    const void *ptr, ssize_t len)
{
	struct vdp_chksha256_s *vdps;

	CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKSHA256_MAGIC);
	if (len != 0)
		VSHA256_Update(vdps->cx, ptr, len);
	vdps->called++;
	vdps->bytes += len;
	return (VDP_bytes(vdc, act, ptr, len));
}

static int v_matchproto_(vdp_bytes_f)
xyzzy_chkcrc32_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
    const void *ptr, ssize_t len)
{
	struct vdp_chkcrc32_s *vdps;

	CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKCRC32_MAGIC);
	if (len > 0)
		vdps->crc = crc32(vdps->crc, ptr, len);
	vdps->called++;
	vdps->bytes += len;
	return (VDP_bytes(vdc, act, ptr, len));
}

static int v_matchproto_(vdp_fini_f)
xyzzy_chksha256_fini(struct vdp_ctx *vdc, void **priv)
{
	unsigned char digest[VSHA256_DIGEST_LENGTH];
	enum vdp_chk_mode_e mode;
	struct vdp_chksha256_s *vdps;
	struct vsb *vsb;
	int r;

	(void) vdc;
	AN(priv);
	if (*priv == NULL)
		return (0);
	TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKSHA256_MAGIC);

	VSHA256_Final(digest, vdps->cx);
	r = memcmp(digest, vdps->cfg->expected, sizeof digest);
	if (r == 0)
		return (0);

	mode = vdps->cfg->mode;
	if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
		mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;

	if (mode == VDP_CHK_LOG) {
		VSLb(vdc->vsl, SLT_Debug, "sha256 checksum mismatch");

		vsb = VSB_new_auto();
		AN(vsb);
		VSB_quote(vsb, digest, sizeof digest, VSB_QUOTE_HEX);
		AZ(VSB_finish(vsb));
		VSLb(vdc->vsl, SLT_Debug, "got: %s", VSB_data(vsb));

		VSB_clear(vsb);
		VSB_quote(vsb, vdps->cfg->expected, sizeof digest, VSB_QUOTE_HEX);
		AZ(VSB_finish(vsb));
		VSLb(vdc->vsl, SLT_Debug, "exp: %s", VSB_data(vsb));
		VSB_destroy(&vsb);
	}
	else if (mode == VDP_CHK_PANIC)
		WRONG("body checksum");
	else
		WRONG("mode");

	return (0);
}

static int v_matchproto_(vdp_fini_f)
xyzzy_chkcrc32_fini(struct vdp_ctx *vdc, void **priv)
{
	enum vdp_chk_mode_e mode;
	struct vdp_chkcrc32_s *vdps;

	(void) vdc;
	AN(priv);
	if (*priv == NULL)
		return (0);
	TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKCRC32_MAGIC);

	if (vdps->crc == vdps->cfg->expected)
		return (0);

	mode = vdps->cfg->mode;
	if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
		mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;

	if (mode == VDP_CHK_LOG) {
		VSLb(vdc->vsl, SLT_Debug, "crc32 checksum mismatch");
		VSLb(vdc->vsl, SLT_Debug, "got: %08x", vdps->crc);
		VSLb(vdc->vsl, SLT_Debug, "exp: %08x", vdps->cfg->expected);
	}
	else if (mode == VDP_CHK_PANIC)
		WRONG("body checksum");
	else
		WRONG("mode");

	return (0);
}

static const struct vdp xyzzy_vdp_chksha256 = {
	.name  = "debug.chksha256",
	.init  = xyzzy_chksha256_init,
	.bytes = xyzzy_chksha256_bytes,
	.fini  = xyzzy_chksha256_fini,
};

static const struct vdp xyzzy_vdp_chkcrc32 = {
	.name  = "debug.chkcrc32",
	.init  = xyzzy_chkcrc32_init,
	.bytes = xyzzy_chkcrc32_bytes,
	.fini  = xyzzy_chkcrc32_fini,
};

#define chkcfg(ws, cfg, magic, id, mode_e) do {							\
	struct vmod_priv *p = VRT_priv_task(ctx, id);						\
												\
	XXXAN(p);										\
	if (p->priv == NULL) {									\
		p->priv = WS_Alloc(ws, sizeof *cfg);						\
		p->len = sizeof *cfg;								\
	}											\
	AN(p->priv);										\
	cfg = p->priv;										\
	INIT_OBJ(cfg, magic);									\
	if (mode_e == VENUM(log))								\
		cfg->mode = VDP_CHK_LOG;							\
	else if (mode_e == VENUM(panic))							\
		cfg->mode = VDP_CHK_PANIC;							\
	else if (mode_e == VENUM(panic_unless_error))						\
		cfg->mode = VDP_CHK_PANIC_UNLESS_ERROR;						\
	else											\
		WRONG("mode");									\
} while(0)

VCL_VOID v_matchproto_(td_xyzzy_debug_chksha256)
xyzzy_chksha256(VRT_CTX, VCL_BLOB blob, VCL_ENUM mode_e)
{
	struct vdp_chksha256_cfg_s *cfg;
	size_t l;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	AN(blob);
	XXXAN(blob->blob);
	XXXAN(blob->len);

	chkcfg(ctx->ws, cfg, VDP_CHKSHA256_CFG_MAGIC, chksha256_priv_id, mode_e);

	l = blob->len;
	if (l > sizeof cfg->expected)
		l = sizeof cfg->expected;
	memcpy(cfg->expected, blob->blob, l);

}

VCL_VOID v_matchproto_(td_xyzzy_debug_chkcrc32)
xyzzy_chkcrc32(VRT_CTX, VCL_INT expected, VCL_ENUM mode_e)
{
	struct vdp_chkcrc32_cfg_s *cfg;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);

	chkcfg(ctx->ws, cfg, VDP_CHKCRC32_CFG_MAGIC, chkcrc32_priv_id, mode_e);

	if (expected < 0)
		expected = 0;
	cfg->expected = (uintmax_t)expected % UINT32_MAX;
}

/**********************************************************************
 * reserve thread_workspace
 */

static int v_matchproto_(vdp_init_f)
xyzzy_awshog_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{
	struct ws *aws;
	unsigned u;

	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
	CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
	CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
	CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
	AN(vdc->clen);
	AN(priv);

	if (ctx->req != NULL)
		aws = ctx->req->wrk->aws;
	else if (ctx->bo != NULL)
		aws = ctx->bo->wrk->aws;
	else
		WRONG("neither req nor bo");

	u = WS_ReserveAll(aws);
	WS_Release(aws, 0);
	(void) WS_Alloc(aws, u);
	return (1);
}

static const struct vdp xyzzy_vdp_awshog = {
	.name  = "debug.awshog",
	.init  = xyzzy_awshog_init
};

void
debug_add_filters(VRT_CTX)
{
	AZ(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
	AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_pedantic));
	AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chunked));
	AZ(VRT_AddFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow));
	AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chksha256));
	AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chkcrc32));
	AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_awshog));
}

void
debug_remove_filters(VRT_CTX)
{
	VRT_RemoveFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow);
	VRT_RemoveFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13);
	VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_pedantic);
	VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chunked);
	VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chksha256);
	VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chkcrc32);
	VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_awshog);
}