Code Coverage: vinyl-cache/vmod/vmod_debug_filters.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2012-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <sys/socket.h>
36
#include <unistd.h>
37
38
#include "cache/cache_int.h"
39
#include "cache/cache_filter.h"
40
41
#include "vgz.h"
42
#include "vsha256.h"
43
#include "vtim.h"
44
#include "vcc_debug_if.h"
45
46
#include "vmod_debug.h"
47
48
/**********************************************************************/
49
50
static enum vfp_status v_matchproto_(vfp_pull_f)
51
xyzzy_vfp_rot13_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
52
    ssize_t *lp)
53 9
{
54 9
        enum vfp_status vp;
55 9
        char *q;
56 9
        ssize_t l;
57
58 9
        (void)vfe;
59 9
        vp = VFP_Suck(vc, p, lp);
60 9
        if (vp == VFP_ERROR)
61 0
                return (vp);
62 9
        q = p;
63 261
        for (l = 0; l < *lp; l++, q++) {
64 252
                if (*q >= 'A' && *q <= 'Z')
65 36
                        *q = (((*q - 'A') + 13) % 26) + 'A';
66 252
                if (*q >= 'a' && *q <= 'z')
67 171
                        *q = (((*q - 'a') + 13) % 26) + 'a';
68 252
        }
69 9
        return (vp);
70 9
}
71
72
static const struct vfp xyzzy_vfp_rot13 = {
73
        .name = "rot13",
74
        .pull = xyzzy_vfp_rot13_pull,
75
};
76
77
/**********************************************************************/
78
79
// deliberately fragmenting the stream to make testing more interesting
80 118677
#define ROT13_BUFSZ 8
81
82
static int rot13_init_fail = 0;
83
84
void
85
xyzzy_rot13_fail_init(VRT_CTX)
86 2
{
87
88 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
89 2
        rot13_init_fail = 1;
90 2
}
91
92
static int v_matchproto_(vdp_init_f)
93
xyzzy_vdp_rot13_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
94 29
{
95
96 29
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
97 29
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
98 29
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
99 29
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
100 29
        AN(vdc->clen);
101
102 29
        AN(priv);
103
104 29
        if (rot13_init_fail)
105 2
                return (-1);
106 27
        *priv = malloc(ROT13_BUFSZ);
107 27
        if (*priv == NULL)
108 0
                return (-1);
109
110 27
        return (0);
111 27
}
112
113
static int v_matchproto_(vdp_bytes_f)
114
xyzzy_vdp_rot13_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
115
    const void *ptr, ssize_t len)
116 42
{
117 42
        char *q;
118 42
        const char *pp;
119 42
        int i, j, retval = 0;
120
121 42
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
122 42
        AN(priv);
123 42
        AN(*priv);
124 42
        if (len <= 0)
125 8
                return (VDP_bytes(vdc, act, ptr, len));
126 34
        AN(ptr);
127 34
        if (act != VDP_END)
128 12
                act = VDP_FLUSH;
129 34
        q = *priv;
130 34
        pp = ptr;
131
132 105516
        for (i = 0, j = 0; j < len; i++, j++) {
133 105482
                if (pp[j] >= 'A' && pp[j] <= 'Z')
134 27946
                        q[i] = (((pp[j] - 'A') + 13) % 26) + 'A';
135 77536
                else if (pp[j] >= 'a' && pp[j] <= 'z')
136 28936
                        q[i] = (((pp[j] - 'a') + 13) % 26) + 'a';
137 48600
                else
138 48600
                        q[i] = pp[j];
139 105482
                if (i == ROT13_BUFSZ - 1 && j < len - 1) {
140 13168
                        retval = VDP_bytes(vdc, VDP_FLUSH, q, ROT13_BUFSZ);
141 13168
                        if (retval != 0)
142 0
                                return (retval);
143 13168
                        i = -1;
144 13168
                }
145 105482
        }
146 34
        if (i >= 0)
147 34
                retval = VDP_bytes(vdc, act, q, i);
148 34
        return (retval);
149 34
}
150
151
static int v_matchproto_(vdp_fini_f)
152
xyzzy_vdp_rot13_fini(struct vdp_ctx *vdc, void **priv)
153 27
{
154 27
        (void)vdc;
155 27
        AN(priv);
156 27
        AN(*priv);
157 27
        free(*priv);
158 27
        *priv = NULL;
159 27
        return (0);
160 27
}
161
162
static const struct vdp xyzzy_vdp_rot13 = {
163
        .name  = "rot13",
164
        .init  = xyzzy_vdp_rot13_init,
165
        .bytes = xyzzy_vdp_rot13_bytes,
166
        .fini  = xyzzy_vdp_rot13_fini,
167
};
168
169
VCL_VOID v_matchproto_(td_debug_rot104)
170
xyzzy_rot104(VRT_CTX)
171 1
{
172
173 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
174
        // This should fail
175 1
        AN(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
176 1
}
177
178
/**********************************************************************
179
 * vdp debug_chunked: force http1 chunked encoding by removing the
180
 * Content-Length header
181
 *
182
 * this happens in a VDP because cnt_transmit() runs after VCL and
183
 * restores it
184
 */
185
186
static int v_matchproto_(vdp_init_f)
187
xyzzy_vdp_chunked_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
188 32
{
189
190 32
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
191 32
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
192 32
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
193 32
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
194 32
        AN(vdc->clen);
195 32
        AN(priv);
196
197 32
        *vdc->clen = -1;
198
199 32
        return (1);
200 32
}
201
202
static const struct vdp xyzzy_vdp_chunked = {
203
        .name  = "debug.chunked",
204
        .init  = xyzzy_vdp_chunked_init,
205
};
206
207
/**********************************************************************
208
 * pedantic tests of the VDP API:
209
 * - assert that we see a VDP_END
210
 * - assert that _fini gets called before the task ends
211
 *
212
 * note:
213
 * we could lookup our own vdpe in _fini and check for vdpe->end == VDP_END
214
 * yet that would cross the API
215
 */
216
217
enum vdp_state_e {
218
        VDPS_NULL = 0,
219
        VDPS_INIT,      // _init called
220
        VDPS_BYTES,     // _bytes called act != VDP_END
221
        VDPS_END,       // _bytes called act == VDP_END
222
        VDPS_FINI       // _fini called
223
};
224
225
struct vdp_state_s {
226
        unsigned                magic;
227
#define VDP_STATE_MAGIC 0x57c8d309
228
        enum vdp_state_e        state;
229
};
230
231
static void v_matchproto_(vmod_priv_fini_f)
232
priv_pedantic_fini(VRT_CTX, void *priv)
233 200
{
234 200
        struct vdp_state_s *vdps;
235
236 200
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
237 200
        CAST_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
238
239 200
        assert(vdps->state == VDPS_FINI);
240 200
}
241
242
static const struct vmod_priv_methods priv_pedantic_methods[1] = {{
243
        .magic = VMOD_PRIV_METHODS_MAGIC,
244
        .type = "debug_vdp_pedantic",
245
        .fini = priv_pedantic_fini
246
}};
247
248
static int v_matchproto_(vdp_init_f)
249
xyzzy_pedantic_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
250 208
{
251 208
        struct vdp_state_s *vdps;
252 208
        struct vmod_priv *p;
253
254 208
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
255 208
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
256 208
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
257 208
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
258 208
        AN(vdc->clen);
259 208
        AN(priv);
260
261 208
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_STATE_MAGIC);
262 208
        if (vdps == NULL)
263 8
                return (-1);
264 200
        assert(vdps->state == VDPS_NULL);
265
266 200
        p = VRT_priv_task(ctx, (void *)vdc);
267 200
        if (p == NULL)
268 0
                return (-1);
269 200
        p->priv = vdps;
270 200
        p->methods = priv_pedantic_methods;
271
272 200
        *priv = vdps;
273
274 200
        vdps->state = VDPS_INIT;
275
276 200
        return (0);
277 200
}
278
279
static int v_matchproto_(vdp_bytes_f)
280
xyzzy_pedantic_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
281
    const void *ptr, ssize_t len)
282 321
{
283 321
        struct vdp_state_s *vdps;
284
285 321
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_STATE_MAGIC);
286 321
        assert(vdps->state >= VDPS_INIT);
287 321
        assert(vdps->state < VDPS_END);
288
289 321
        if (act == VDP_END)
290 70
                vdps->state = VDPS_END;
291 251
        else
292 251
                vdps->state = VDPS_BYTES;
293
294 321
        return (VDP_bytes(vdc, act, ptr, len));
295 321
}
296
297
static int v_matchproto_(vdp_fini_f)
298
xyzzy_pedantic_fini(struct vdp_ctx *vdc, void **priv)
299 200
{
300 200
        struct vdp_state_s *vdps;
301
302 200
        (void) vdc;
303 200
        AN(priv);
304 200
        if (*priv == NULL)
305 0
                return (0);
306 200
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_STATE_MAGIC);
307 200
        assert(vdps->state == VDPS_INIT || vdps->state == VDPS_END);
308 200
        vdps->state = VDPS_FINI;
309
310 200
        return (0);
311 200
}
312
313
static const struct vdp xyzzy_vdp_pedantic = {
314
        .name  = "debug.pedantic",
315
        .init  = xyzzy_pedantic_init,
316
        .bytes = xyzzy_pedantic_bytes,
317
        .fini  = xyzzy_pedantic_fini,
318
};
319
320
/**********************************************************************
321
 *
322
 * this trivial copy/paste/edit filter (of rot13) was specifically made for
323
 * someone who added a DBG_SLOW_BEREQ debug flag. It should actually be turned
324
 * in a proper "bandwidth control" filter, but that exceeds an evening's work,
325
 * so it's kept for later
326
 */
327
328
static enum vfp_status v_matchproto_(vfp_pull_f)
329
xyzzy_vfp_slow_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
330
    ssize_t *lp)
331 0
{
332
333 0
        (void)vfe;
334 0
        VTIM_sleep(1.0);
335 0
        return (VFP_Suck(vc, p, lp));
336 0
}
337
338
static const struct vfp xyzzy_vfp_slow = {
339
        .name = "debug.slow",
340
        .pull = xyzzy_vfp_slow_pull,
341
};
342
343
/**********************************************************************/
344
345
static int v_matchproto_(vdp_bytes_f)
346
xyzzy_vdp_slow_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
347
    const void *ptr, ssize_t len)
348 4
{
349
350 4
        (void)priv;
351 4
        VTIM_sleep(1.0);
352 4
        return (VDP_bytes(vdc, act, ptr, len));
353 4
}
354
355
static const struct vdp xyzzy_vdp_slow = {
356
        .name  = "debug.slow",
357
        .bytes = xyzzy_vdp_slow_bytes
358
};
359
360
/*
361
 * check VDPs:
362
 *
363
 * test that the stream of bytes has a certain checksum or length and either log
364
 * or panic
365
 *
366
 * The sha256 and crc32 variants are basically identical, but the amount of
367
 * code does not justify generalizing. (slink)
368
 */
369
370
enum vdp_chk_mode_e {
371
        //lint -esym(749, vdp_chk_mode_e::VDP_CHK_INVAL) deliberately not referenced
372
        VDP_CHK_INVAL = 0,
373
        VDP_CHK_LOG,
374
        VDP_CHK_PANIC,
375
        VDP_CHK_PANIC_UNLESS_ERROR
376
};
377
378
struct vdp_chksha256_cfg_s {
379
        unsigned                        magic;
380
#define VDP_CHKSHA256_CFG_MAGIC         0x624f5b32
381
        enum vdp_chk_mode_e             mode;
382
        unsigned char                   expected[VSHA256_DIGEST_LENGTH];
383
};
384
385
struct vdp_chkcrc32_cfg_s {
386
        unsigned                        magic;
387
#define VDP_CHKCRC32_CFG_MAGIC          0x5a7a835c
388
        enum vdp_chk_mode_e             mode;
389
        uint32_t                        expected;
390
};
391
392
struct vdp_chklen_cfg_s {
393
        unsigned                        magic;
394
#define VDP_CHKLEN_CFG_MAGIC            0x08cf3426
395
        enum vdp_chk_mode_e             mode;
396
        size_t                          expected;
397
};
398
399
struct vdp_chksha256_s {
400
        unsigned                        magic;
401
#define VDP_CHKSHA256_MAGIC             0x6856e913
402
        unsigned                        called;
403
        size_t                          bytes;
404
        struct VSHA256Context           cx[1];
405
        struct vdp_chksha256_cfg_s      *cfg;
406
};
407
408
struct vdp_chkcrc32_s {
409
        unsigned                        magic;
410
#define VDP_CHKCRC32_MAGIC              0x15c03d3c
411
        unsigned                        called;
412
        size_t                          bytes;
413
        uint32_t                        crc;
414
        struct vdp_chkcrc32_cfg_s       *cfg;
415
};
416
417
struct vdp_chklen_s {
418
        unsigned                        magic;
419
#define VDP_CHKLEN_MAGIC                0x029811f5
420
        unsigned                        called;
421
        size_t                          bytes;
422
        struct vdp_chklen_cfg_s         *cfg;
423
};
424
425
static const void * const chksha256_priv_id = &chksha256_priv_id;
426
static const void * const chkcrc32_priv_id = &chkcrc32_priv_id;
427
static const void * const chklen_priv_id = &chklen_priv_id;
428
429
static int v_matchproto_(vdp_init_f)
430
xyzzy_chksha256_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
431 4
{
432 4
        struct vdp_chksha256_s *vdps;
433 4
        struct vmod_priv *p;
434
435 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
436 4
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
437 4
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
438 4
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
439 4
        AN(vdc->clen);
440 4
        AN(priv);
441
442 4
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKSHA256_MAGIC);
443 4
        if (vdps == NULL)
444 0
                return (-1);
445 4
        VSHA256_Init(vdps->cx);
446
447 4
        p = VRT_priv_task_get(ctx, chksha256_priv_id);
448 4
        if (p == NULL)
449 0
                return (-1);
450
451 4
        assert(p->len == sizeof(struct vdp_chksha256_cfg_s));
452 4
        CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKSHA256_CFG_MAGIC);
453 4
        *priv = vdps;
454
455 4
        return (0);
456 4
}
457
458
static int v_matchproto_(vdp_init_f)
459
xyzzy_chkcrc32_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
460 4
{
461 4
        struct vdp_chkcrc32_s *vdps;
462 4
        struct vmod_priv *p;
463
464 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
465 4
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
466 4
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
467 4
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
468 4
        AN(vdc->clen);
469 4
        AN(priv);
470
471 4
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKCRC32_MAGIC);
472 4
        if (vdps == NULL)
473 0
                return (-1);
474 4
        vdps->crc = crc32(0L, Z_NULL, 0);
475
476 4
        p = VRT_priv_task_get(ctx, chkcrc32_priv_id);
477 4
        if (p == NULL)
478 0
                return (-1);
479
480 4
        assert(p->len == sizeof(struct vdp_chkcrc32_cfg_s));
481 4
        CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKCRC32_CFG_MAGIC);
482 4
        *priv = vdps;
483
484 4
        return (0);
485 4
}
486
487
static int v_matchproto_(vdp_init_f)
488
xyzzy_chklen_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
489 4
{
490 4
        struct vdp_chklen_s *vdps;
491 4
        struct vmod_priv *p;
492
493 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
494 4
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
495 4
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
496 4
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
497 4
        AN(vdc->clen);
498 4
        AN(priv);
499
500 4
        WS_TASK_ALLOC_OBJ(ctx, vdps, VDP_CHKLEN_MAGIC);
501 4
        if (vdps == NULL)
502 0
                return (-1);
503 4
        AZ(vdps->bytes);
504
505 4
        p = VRT_priv_task_get(ctx, chklen_priv_id);
506 4
        if (p == NULL)
507 0
                return (-1);
508
509 4
        assert(p->len == sizeof(struct vdp_chklen_cfg_s));
510 4
        CAST_OBJ_NOTNULL(vdps->cfg, p->priv, VDP_CHKLEN_CFG_MAGIC);
511 4
        *priv = vdps;
512
513 4
        return (0);
514 4
}
515
516
static int v_matchproto_(vdp_bytes_f)
517
xyzzy_chksha256_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
518
    const void *ptr, ssize_t len)
519 12
{
520 12
        struct vdp_chksha256_s *vdps;
521
522 12
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKSHA256_MAGIC);
523 12
        if (len != 0)
524 6
                VSHA256_Update(vdps->cx, ptr, len);
525 12
        vdps->called++;
526 12
        vdps->bytes += len;
527 12
        return (VDP_bytes(vdc, act, ptr, len));
528 12
}
529
530
static int v_matchproto_(vdp_bytes_f)
531
xyzzy_chkcrc32_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
532
    const void *ptr, ssize_t len)
533 12
{
534 12
        struct vdp_chkcrc32_s *vdps;
535
536 12
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKCRC32_MAGIC);
537 12
        if (len > 0)
538 6
                vdps->crc = crc32(vdps->crc, ptr, len);
539 12
        vdps->called++;
540 12
        vdps->bytes += len;
541 12
        return (VDP_bytes(vdc, act, ptr, len));
542 12
}
543
544
static int v_matchproto_(vdp_bytes_f)
545
xyzzy_chklen_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
546
    const void *ptr, ssize_t len)
547 12
{
548 12
        struct vdp_chklen_s *vdps;
549
550 12
        CAST_OBJ_NOTNULL(vdps, *priv, VDP_CHKLEN_MAGIC);
551 12
        vdps->called++;
552 12
        vdps->bytes += len;
553 12
        return (VDP_bytes(vdc, act, ptr, len));
554 12
}
555
556
static int v_matchproto_(vdp_fini_f)
557
xyzzy_chksha256_fini(struct vdp_ctx *vdc, void **priv)
558 4
{
559 4
        unsigned char digest[VSHA256_DIGEST_LENGTH];
560 4
        enum vdp_chk_mode_e mode;
561 4
        struct vdp_chksha256_s *vdps;
562 4
        struct vsb *vsb;
563 4
        int r;
564
565 4
        (void) vdc;
566 4
        AN(priv);
567 4
        if (*priv == NULL)
568 0
                return (0);
569 4
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKSHA256_MAGIC);
570
571 4
        VSHA256_Final(digest, vdps->cx);
572 4
        r = memcmp(digest, vdps->cfg->expected, sizeof digest);
573 4
        if (r == 0)
574 2
                return (0);
575
576 2
        mode = vdps->cfg->mode;
577 2
        if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
578 0
                mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;
579
580 2
        if (mode == VDP_CHK_LOG) {
581 1
                VSLb(vdc->vsl, SLT_Debug, "sha256 checksum mismatch");
582
583 1
                vsb = VSB_new_auto();
584 1
                AN(vsb);
585 1
                VSB_quote(vsb, digest, sizeof digest, VSB_QUOTE_HEX);
586 1
                AZ(VSB_finish(vsb));
587 1
                VSLb(vdc->vsl, SLT_Debug, "got: %s", VSB_data(vsb));
588
589 1
                VSB_clear(vsb);
590 1
                VSB_quote(vsb, vdps->cfg->expected, sizeof digest, VSB_QUOTE_HEX);
591 1
                AZ(VSB_finish(vsb));
592 1
                VSLb(vdc->vsl, SLT_Debug, "exp: %s", VSB_data(vsb));
593 1
                VSB_destroy(&vsb);
594 1
        }
595 1
        else if (mode == VDP_CHK_PANIC)
596 1
                WRONG("body checksum");
597 0
        else
598 0
                WRONG("mode");
599
600 1
        return (0);
601 2
}
602
603
static int v_matchproto_(vdp_fini_f)
604
xyzzy_chkcrc32_fini(struct vdp_ctx *vdc, void **priv)
605 3
{
606 3
        enum vdp_chk_mode_e mode;
607 3
        struct vdp_chkcrc32_s *vdps;
608
609 3
        (void) vdc;
610 3
        AN(priv);
611 3
        if (*priv == NULL)
612 0
                return (0);
613 3
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKCRC32_MAGIC);
614
615 3
        if (vdps->crc == vdps->cfg->expected)
616 2
                return (0);
617
618 1
        mode = vdps->cfg->mode;
619 1
        if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
620 0
                mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;
621
622 1
        if (mode == VDP_CHK_LOG) {
623 1
                VSLb(vdc->vsl, SLT_Debug, "crc32 checksum mismatch");
624 1
                VSLb(vdc->vsl, SLT_Debug, "got: %08x", vdps->crc);
625 1
                VSLb(vdc->vsl, SLT_Debug, "exp: %08x", vdps->cfg->expected);
626 1
        }
627 0
        else if (mode == VDP_CHK_PANIC)
628 0
                WRONG("body checksum");
629 0
        else
630 0
                WRONG("mode");
631
632 1
        return (0);
633 1
}
634
635
static int v_matchproto_(vdp_fini_f)
636
xyzzy_chklen_fini(struct vdp_ctx *vdc, void **priv)
637 3
{
638 3
        enum vdp_chk_mode_e mode;
639 3
        struct vdp_chklen_s *vdps;
640
641 3
        (void) vdc;
642 3
        AN(priv);
643 3
        if (*priv == NULL)
644 0
                return (0);
645 3
        TAKE_OBJ_NOTNULL(vdps, priv, VDP_CHKLEN_MAGIC);
646
647 3
        if (vdps->bytes == vdps->cfg->expected)
648 2
                return (0);
649
650 1
        mode = vdps->cfg->mode;
651 1
        if (mode == VDP_CHK_PANIC_UNLESS_ERROR)
652 0
                mode = (vdps->called == 0 || vdc->retval != 0) ? VDP_CHK_LOG : VDP_CHK_PANIC;
653
654 1
        if (mode == VDP_CHK_LOG) {
655 1
                VSLb(vdc->vsl, SLT_Debug, "length mismatch");
656 1
                VSLb(vdc->vsl, SLT_Debug, "got: %zd", vdps->bytes);
657 1
                VSLb(vdc->vsl, SLT_Debug, "exp: %zd", vdps->cfg->expected);
658 1
        }
659 0
        else if (mode == VDP_CHK_PANIC)
660 0
                WRONG("body length");
661 0
        else
662 0
                WRONG("mode");
663
664 1
        return (0);
665 1
}
666
667
static const struct vdp xyzzy_vdp_chksha256 = {
668
        .name  = "debug.chksha256",
669
        .init  = xyzzy_chksha256_init,
670
        .bytes = xyzzy_chksha256_bytes,
671
        .fini  = xyzzy_chksha256_fini,
672
};
673
674
static const struct vdp xyzzy_vdp_chkcrc32 = {
675
        .name  = "debug.chkcrc32",
676
        .init  = xyzzy_chkcrc32_init,
677
        .bytes = xyzzy_chkcrc32_bytes,
678
        .fini  = xyzzy_chkcrc32_fini,
679
};
680
681
static const struct vdp xyzzy_vdp_chklen = {
682
        .name  = "debug.chklen",
683
        .init  = xyzzy_chklen_init,
684
        .bytes = xyzzy_chklen_bytes,
685
        .fini  = xyzzy_chklen_fini,
686
};
687
688 12
#define chkcfg(ws, cfg, magic, id, mode_e) do {                                                 \
689 12
        struct vmod_priv *p = VRT_priv_task(ctx, id);                                           \
690 12
                                                                                                \
691 12
        XXXAN(p);                                                                               \
692 12
        if (p->priv == NULL) {                                                                  \
693 12
                p->priv = WS_Alloc(ws, sizeof *cfg);                                            \
694 12
                p->len = sizeof *cfg;                                                           \
695 12
        }                                                                                       \
696 12
        AN(p->priv);                                                                            \
697 12
        cfg = p->priv;                                                                          \
698 12
        INIT_OBJ(cfg, magic);                                                                   \
699 12
        if (mode_e == VENUM(log))                                                               \
700 12
                cfg->mode = VDP_CHK_LOG;                                                        \
701 12
        else if (mode_e == VENUM(panic))                                                        \
702 3
                cfg->mode = VDP_CHK_PANIC;                                                      \
703 3
        else if (mode_e == VENUM(panic_unless_error))                                           \
704 0
                cfg->mode = VDP_CHK_PANIC_UNLESS_ERROR;                                         \
705 0
        else                                                                                    \
706 0
                WRONG("mode");                                                                  \
707 12
} while(0)
708
709
VCL_VOID v_matchproto_(td_xyzzy_debug_chksha256)
710
xyzzy_chksha256(VRT_CTX, VCL_BLOB blob, VCL_ENUM mode_e)
711 4
{
712 4
        struct vdp_chksha256_cfg_s *cfg;
713 4
        size_t l;
714
715 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
716 4
        CHECK_OBJ_NOTNULL(blob, VRT_BLOB_MAGIC);
717 4
        XXXAN(blob->blob);
718 4
        XXXAN(blob->len);
719
720 4
        chkcfg(ctx->ws, cfg, VDP_CHKSHA256_CFG_MAGIC, chksha256_priv_id, mode_e);
721
722 4
        l = blob->len;
723 4
        if (l > sizeof cfg->expected)
724 0
                l = sizeof cfg->expected;
725 4
        vmemcpy(cfg->expected, blob->blob, l);
726
727 4
}
728
729
VCL_VOID v_matchproto_(td_xyzzy_debug_chkcrc32)
730
xyzzy_chkcrc32(VRT_CTX, VCL_INT expected, VCL_ENUM mode_e)
731 4
{
732 4
        struct vdp_chkcrc32_cfg_s *cfg;
733
734 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
735
736 4
        chkcfg(ctx->ws, cfg, VDP_CHKCRC32_CFG_MAGIC, chkcrc32_priv_id, mode_e);
737
738 4
        if (expected < 0)
739 0
                expected = 0;
740 4
        cfg->expected = (uintmax_t)expected % UINT32_MAX;
741 4
}
742
743
VCL_VOID v_matchproto_(td_xyzzy_debug_chklen)
744
xyzzy_chklen(VRT_CTX, VCL_BYTES expected, VCL_ENUM mode_e)
745 4
{
746 4
        struct vdp_chklen_cfg_s *cfg;
747
748 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
749
750 4
        chkcfg(ctx->ws, cfg, VDP_CHKLEN_CFG_MAGIC, chklen_priv_id, mode_e);
751
752 4
        cfg->expected = expected;
753 4
}
754
755
/**********************************************************************
756
 * reserve thread_workspace
757
 */
758
759
static int v_matchproto_(vdp_init_f)
760
xyzzy_awshog_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
761 1
{
762 1
        struct ws *aws;
763 1
        unsigned u;
764
765 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
766 1
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
767 1
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
768 1
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
769 1
        AN(vdc->clen);
770 1
        AN(priv);
771
772 1
        if (ctx->req != NULL)
773 0
                aws = ctx->req->wrk->aws;
774 1
        else if (ctx->bo != NULL)
775 1
                aws = ctx->bo->wrk->aws;
776 0
        else
777 0
                WRONG("neither req nor bo");
778
779 1
        u = WS_ReserveAll(aws);
780 1
        WS_Release(aws, 0);
781 1
        (void) WS_Alloc(aws, u);
782 1
        return (1);
783 1
}
784
785
static const struct vdp xyzzy_vdp_awshog = {
786
        .name  = "debug.awshog",
787
        .init  = xyzzy_awshog_init
788
};
789
790
/**********************************************************************
791
 * send a fixed string
792
 */
793
794
static const void * const string_priv_id = &string_priv_id;
795
796
static int v_matchproto_(vdp_init_f)
797
xyzzy_string_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
798 8
{
799 8
        struct vmod_priv *p;
800
801 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
802 8
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
803 8
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
804 8
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
805 8
        AN(vdc->clen);
806
807 8
        AN(priv);
808 8
        p = VRT_priv_task_get(ctx, string_priv_id);
809 8
        if (p == NULL || p->priv == NULL) {
810 4
                *priv = TRUST_ME("");
811 4
                *vdc->clen = 0;
812 4
                return (0);
813 4
        }
814 4
        AN(p->priv);
815 4
        *priv = p->priv;
816 4
        *vdc->clen = vstrlen(*priv);
817 4
        return (0);
818 4
}
819
820
static int v_matchproto_(vdp_bytes_f)
821
xyzzy_string_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
822
    const void *ptr, ssize_t len)
823 6
{
824 6
        int r;
825
826 6
        (void)act;
827 6
        AN(priv);
828 6
        (void)ptr;
829 6
        (void)len;
830
831 6
        r = VDP_bytes(vdc, VDP_END, *priv, vstrlen(*priv));
832 6
        if (r != 0)
833 0
                return (r);
834
        // only to be called once
835 6
        return (1);
836 6
}
837
838
static int v_matchproto_(vdp_fini_f)
839
xyzzy_string_fini(struct vdp_ctx *vdc, void **priv)
840 8
{
841 8
        (void)vdc;
842 8
        AN(priv);
843 8
        *priv = NULL;
844 8
        return (0);
845 8
}
846
847
static const struct vdp xyzzy_vdp_string = {
848
        .name  = "debug.string",
849
        .init  = xyzzy_string_init,
850
        .bytes = xyzzy_string_bytes,
851
        .fini = xyzzy_string_fini,
852
};
853
854
VCL_VOID v_matchproto_(td_xyzzy_debug_body_string)
855
xyzzy_body_string(VRT_CTX, VCL_STRING s)
856 4
{
857 4
        struct vmod_priv *p;
858
859 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
860 4
        p = VRT_priv_task(ctx, string_priv_id);
861 4
        if (p == NULL)
862 0
                return;
863 4
        p->priv = TRUST_ME(s);
864 4
}
865
866
/**********************************************************************/
867
868
static int v_matchproto_(vdp_init_f)
869
xyzzy_vdp_body_prefix_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
870 2
{
871 2
        struct xyzzy_bp_string *bps;
872 2
        struct xyzzy_bp *bp;
873 2
        struct vmod_priv *task;
874 2
        intmax_t l;
875
876 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
877 2
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
878 2
        AN(vdc->clen);
879
880 2
        task = VRT_priv_task_get(ctx, priv_task_id_bp);
881 2
        if (task == NULL)
882 0
                return (1);
883 2
        CAST_OBJ_NOTNULL(bp, task->priv, XYZZY_BP_MAGIC);
884 2
        AN(priv);
885 2
        AZ(*priv);
886 2
        *priv = bp;
887
888 2
        if (*vdc->clen < 0)
889 0
                return (0);
890
891 2
        l = 0;
892 2
        VSTAILQ_FOREACH(bps, &bp->head, list)
893 4
                l += vstrlen(bps->s);
894 2
        *vdc->clen += l;
895
896 2
        return (0);
897 2
}
898
899
static int v_matchproto_(vdp_bytes_f)
900
xyzzy_vdp_body_prefix_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
901
    const void *ptr, ssize_t len)
902 4
{
903 4
        struct xyzzy_bp_string *bps;
904 4
        struct xyzzy_bp *bp;
905
906 4
        AN(priv);
907 4
        CAST_OBJ_NOTNULL(bp, *priv, XYZZY_BP_MAGIC);
908
909 8
        while ((bps = VSTAILQ_FIRST(&bp->head)) != NULL) {
910 4
                VSTAILQ_REMOVE_HEAD(&bp->head, list);
911 4
                if (VDP_bytes(vdc, VDP_NULL, bps->s, vstrlen(bps->s)))
912 0
                        return (vdc->retval);
913 4
        }
914
915 4
        return (VDP_bytes(vdc, act, ptr, len));
916 4
}
917
918
static int v_matchproto_(vdp_fini_f)
919
xyzzy_vdp_body_prefix_fini(struct vdp_ctx *vdc, void **priv)
920 2
{
921 2
        (void)vdc;
922 2
        AN(priv);
923 2
        *priv = NULL;
924 2
        return (0);
925 2
}
926
927
static const struct vdp xyzzy_vdp_body_prefix = {
928
        .name  = "debug.body_prefix",
929
        .init  = xyzzy_vdp_body_prefix_init,
930
        .bytes = xyzzy_vdp_body_prefix_bytes,
931
        .fini = xyzzy_vdp_body_prefix_fini
932
};
933
934
void
935
debug_add_filters(VRT_CTX)
936 114
{
937 114
        AZ(VRT_AddFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow));
938 114
        AZ(VRT_AddFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13));
939 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_pedantic));
940 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chunked));
941 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chksha256));
942 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chkcrc32));
943 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_chklen));
944 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_awshog));
945 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_string));
946 114
        AZ(VRT_AddFilter(ctx, NULL, &xyzzy_vdp_body_prefix));
947 114
}
948
949
void
950
debug_remove_filters(VRT_CTX)
951 23
{
952 23
        VRT_RemoveFilter(ctx, &xyzzy_vfp_slow, &xyzzy_vdp_slow);
953 23
        VRT_RemoveFilter(ctx, &xyzzy_vfp_rot13, &xyzzy_vdp_rot13);
954 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_pedantic);
955 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chunked);
956 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chksha256);
957 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chkcrc32);
958 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_chklen);
959 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_awshog);
960 23
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_string);
961
        VRT_RemoveFilter(ctx, NULL, &xyzzy_vdp_body_prefix);
962 23
}