varnish-cache/bin/varnishd/cache/cache_vrt_priv.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2021 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Runtime support for compiled VCL programs: private variables
31
 */
32
33
#include "config.h"
34
35
36
#include <stdlib.h>
37
38
#include "cache_varnishd.h"
39
#include "vcl.h"
40
#include "vcc_interface.h"
41
42
enum vrt_priv_storage_e {
43
        VRT_PRIV_ST_WS = 1,
44
        VRT_PRIV_ST_HEAP
45
};
46
47
struct vrt_priv {
48
        unsigned                        magic;
49
#define VRT_PRIV_MAGIC                  0x24157a52
50
        enum vrt_priv_storage_e         storage;
51
        VRBT_ENTRY(vrt_priv)            entry;
52
        struct vmod_priv                priv[1];
53
        uintptr_t                       vmod_id;
54
};
55
56
struct vrt_privs cli_task_privs[1];
57
58
static inline int vrt_priv_dyncmp(const struct vrt_priv *,
59
    const struct vrt_priv *);
60
61 16420
VRBT_GENERATE_INSERT_COLOR(vrt_privs, vrt_priv, entry, static)
62 399682216
VRBT_GENERATE_FIND(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static)
63 5644
VRBT_GENERATE_INSERT_FINISH(vrt_privs, vrt_priv, entry, static)
64 47856
VRBT_GENERATE_INSERT(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static)
65 49902
VRBT_GENERATE_MINMAX(vrt_privs, vrt_priv, entry, static)
66 9864
VRBT_GENERATE_NEXT(vrt_privs, vrt_priv, entry, static)
67
68
/*--------------------------------------------------------------------
69
 */
70
71
void
72 48
pan_privs(struct vsb *vsb, const struct vrt_privs *privs)
73
{
74
        struct vrt_priv *vp;
75
        const struct vmod_priv *p;
76
        const struct vmod_priv_methods *m;
77
78 48
        if (privs == NULL) {
79 0
                VSB_cat(vsb, "privs = NULL\n");
80 0
                return;
81
        }
82 48
        VSB_printf(vsb, "privs = %p {\n", privs);
83 48
        VSB_indent(vsb, 2);
84 56
        VRBT_FOREACH(vp, vrt_privs, privs) {
85 8
                if (PAN_dump_oneline(vsb, vp, VRT_PRIV_MAGIC, "priv"))
86 0
                        continue;
87 8
                p = vp->priv;
88
                //lint -e{774}
89 8
                if (p == NULL) {
90
                        // should never happen
91 0
                        VSB_printf(vsb, "NULL vmod %jx},\n",
92 0
                            (uintmax_t)vp->vmod_id);
93 0
                        continue;
94
                }
95 8
                m = p->methods;
96 16
                VSB_printf(vsb,
97
                    "{p %p l %ld m %p t \"%s\"} vmod %jx},\n",
98 8
                    p->priv, p->len, m,
99 8
                    m != NULL ? m->type : "",
100 8
                    (uintmax_t)vp->vmod_id
101
                );
102 8
        }
103 48
        VSB_indent(vsb, -2);
104 48
        VSB_cat(vsb, "},\n");
105 48
}
106
107
/*--------------------------------------------------------------------
108
 */
109
110
static void
111 48479
VRTPRIV_init(struct vrt_privs *privs)
112
{
113
114 48479
        VRBT_INIT(privs);
115 48479
}
116
117
static inline int
118 399723804
vrt_priv_dyncmp(const struct vrt_priv *vp1, const struct vrt_priv *vp2)
119
{
120 399723804
        if (vp1->vmod_id < vp2->vmod_id)
121 188247056
                return (-1);
122 211476748
        if (vp1->vmod_id > vp2->vmod_id)
123 167033908
                return (1);
124 44442840
        return (0);
125 399723804
}
126
127
static struct vmod_priv *
128 44441488
vrt_priv_dynamic_get(const struct vrt_privs *privs, uintptr_t vmod_id)
129
{
130
        struct vrt_priv *vp;
131
132 44441488
        const struct vrt_priv needle = {.vmod_id = vmod_id};
133
134 44441488
        vp = VRBT_FIND(vrt_privs, privs, &needle);
135 44441488
        if (vp == NULL)
136 624
                return (NULL);
137
138 44440864
        CHECK_OBJ(vp, VRT_PRIV_MAGIC);
139 44440864
        assert(vp->vmod_id == vmod_id);
140 44440864
        return (vp->priv);
141 44441488
}
142
143
static struct vmod_priv *
144 7620
vrt_priv_dynamic(struct ws *ws, struct vrt_privs *privs, uintptr_t vmod_id)
145
{
146
        //lint --e{593} vp allocated, vp->priv returned
147
        struct vrt_priv *vp, *ovp;
148
        enum vrt_priv_storage_e storage;
149
150 7620
        AN(vmod_id);
151
152 7620
        if (LIKELY(WS_ReserveSize(ws, sizeof *vp) != 0)) {
153 7360
                vp = WS_Reservation(ws);
154 7360
                storage = VRT_PRIV_ST_WS;
155 7360
        }
156
        else {
157 260
                vp = malloc(sizeof *vp);
158 260
                storage = VRT_PRIV_ST_HEAP;
159
        }
160 7620
        AN(vp);
161
162 7620
        INIT_OBJ(vp, VRT_PRIV_MAGIC);
163 7620
        vp->storage = storage;
164 7620
        vp->vmod_id = vmod_id;
165 7620
        ovp = VRBT_INSERT(vrt_privs, privs, vp);
166 7620
        if (ovp == NULL) {
167 5644
                if (storage == VRT_PRIV_ST_WS)
168 5384
                        WS_Release(ws, sizeof *vp);
169 5644
                return (vp->priv);
170
        }
171 1976
        if (storage == VRT_PRIV_ST_WS)
172 1976
                WS_Release(ws, 0);
173 0
        else if (storage == VRT_PRIV_ST_HEAP)
174 0
                free(vp);
175
        else
176 0
                WRONG("priv storage");
177 1976
        return (ovp->priv);
178 7620
}
179
180
static struct vrt_privs *
181 44448960
vrt_priv_task_context(VRT_CTX)
182
{
183
184 44448960
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
185
186
        /* In pipe mode, both req and bo are set. We use req */
187
188 44448960
        assert(ctx->req == NULL || ctx->bo == NULL ||
189
            ctx->method == VCL_MET_PIPE || ctx->method == 0);
190
191 44448960
        if (ctx->req) {
192 44447004
                CHECK_OBJ(ctx->req, REQ_MAGIC);
193 44447004
                return (ctx->req->privs);
194
        }
195 1956
        if (ctx->bo) {
196 1280
                CHECK_OBJ(ctx->bo, BUSYOBJ_MAGIC);
197 1280
                return (ctx->bo->privs);
198
        }
199 676
        ASSERT_CLI();
200 676
        return (cli_task_privs);
201 44448960
}
202
203
struct vmod_priv *
204 44441448
VRT_priv_task_get(VRT_CTX, const void *vmod_id)
205
{
206 44441448
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
207
208 44441448
        return (vrt_priv_dynamic_get(
209 44441448
            vrt_priv_task_context(ctx),
210 44441448
            (uintptr_t)vmod_id));
211
}
212
213
struct vmod_priv *
214 7512
VRT_priv_task(VRT_CTX, const void *vmod_id)
215
{
216
217 7512
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
218
219 7512
        return (vrt_priv_dynamic(
220 7512
            ctx->ws,
221 7512
            vrt_priv_task_context(ctx),
222 7512
            (uintptr_t)vmod_id));
223
}
224
225
/*
226
 * XXX #3498 on VRT_fail(): Would be better to move the PRIV_TOP check to VCC
227
 *
228
 * This will fail in the preamble of any VCL SUB containing a call to a vmod
229
 * function with a PRIV_TOP argument, which might not exactly be pola
230
 */
231
232
#define VRT_PRIV_TOP_PREP(ctx, req, sp, top)    do {                    \
233
                CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);                  \
234
                req = (ctx)->req;                                       \
235
                if (req == NULL) {                                      \
236
                        VRT_fail(ctx, "PRIV_TOP is only accessible "    \
237
                            "in client VCL context");                   \
238
                        return (NULL);                                  \
239
                }                                                       \
240
                CHECK_OBJ(req, REQ_MAGIC);                              \
241
                sp = (ctx)->sp;                                         \
242
                CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);                      \
243
                top = (req)->top;                                       \
244
                CHECK_OBJ_NOTNULL(top, REQTOP_MAGIC);                   \
245
                req = (top)->topreq;                                    \
246
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);                      \
247
        } while(0)
248
249
struct vmod_priv *
250 40
VRT_priv_top_get(VRT_CTX, const void *vmod_id)
251
{
252
        struct req *req;
253
        struct sess *sp;
254
        struct reqtop *top;
255
        struct vmod_priv *priv;
256
257 40
        VRT_PRIV_TOP_PREP(ctx, req, sp, top);
258
259 40
        Lck_Lock(&sp->mtx);
260 40
        priv = vrt_priv_dynamic_get(top->privs, (uintptr_t)vmod_id);
261 40
        Lck_Unlock(&sp->mtx);
262 40
        return (priv);
263 40
}
264
265
struct vmod_priv *
266 108
VRT_priv_top(VRT_CTX, const void *vmod_id)
267
{
268
        struct req *req;
269
        struct sess *sp;
270
        struct reqtop *top;
271
        struct vmod_priv *priv;
272
273 108
        VRT_PRIV_TOP_PREP(ctx, req, sp, top);
274
275 108
        Lck_Lock(&sp->mtx);
276 108
        priv = vrt_priv_dynamic(req->ws, top->privs, (uintptr_t)vmod_id);
277 108
        Lck_Unlock(&sp->mtx);
278 108
        return (priv);
279 108
}
280
281
/*--------------------------------------------------------------------
282
 */
283
284
void
285 5998
VRT_priv_fini(VRT_CTX, const struct vmod_priv *p)
286
{
287
        const struct vmod_priv_methods *m;
288
289 5998
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
290
291 5998
        m = p->methods;
292 5998
        if (m == NULL)
293 4638
                return;
294
295 1360
        CHECK_OBJ(m, VMOD_PRIV_METHODS_MAGIC);
296 1360
        if (p->priv == NULL || m->fini == NULL)
297 0
                return;
298
299
        // XXX remove me after soak in
300 1360
        VRT_CTX_Assert(ctx);
301
302 1360
        m->fini(ctx, p->priv);
303 1360
        assert(ctx->vpi->handling == 0 || ctx->vpi->handling == VCL_RET_FAIL);
304 5998
}
305
306
/*--------------------------------------------------------------------*/
307
308
void
309 48479
VCL_TaskEnter(struct vrt_privs *privs)
310
{
311
312 48479
        VRTPRIV_init(privs);
313 48479
}
314
315
void
316 48430
VCL_TaskLeave(VRT_CTX, struct vrt_privs *privs)
317
{
318
        struct vrt_priv *vp, *vp1;
319
320 48430
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
321 48430
        AN(ctx->vpi);
322 48430
        assert(ctx->vpi->handling == 0 || ctx->vpi->handling == VCL_RET_FAIL);
323
324
        /*
325
         * NB: We don't bother removing entries as we finish them because it's
326
         * a costly operation. Instead we safely walk the whole tree and clear
327
         * the head at the very end.
328
         */
329 54066
        VRBT_FOREACH_SAFE(vp, vrt_privs, privs, vp1) {
330 5636
                CHECK_OBJ(vp, VRT_PRIV_MAGIC);
331 5636
                VRT_priv_fini(ctx, vp->priv);
332 5636
                if (vp->storage == VRT_PRIV_ST_HEAP)
333 260
                        free(vp);
334 5636
        }
335 48430
        ZERO_OBJ(privs, sizeof *privs);
336 48430
}