Code Coverage: vinyl-cache/bin/vinyld/cache/cache_vrt_priv.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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_int.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 1093
VRBT_GENERATE_INSERT_COLOR(vrt_privs, vrt_priv, entry, static)
62 11110388
VRBT_GENERATE_FIND(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static)
63 1434
VRBT_GENERATE_INSERT_FINISH(vrt_privs, vrt_priv, entry, static)
64 1930
VRBT_GENERATE_INSERT(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static)
65 19055
VRBT_GENERATE_MINMAX(vrt_privs, vrt_priv, entry, static)
66
VRBT_GENERATE_NEXT(vrt_privs, vrt_priv, entry, static)
67
68
/*--------------------------------------------------------------------
69
 */
70
71
void
72
pan_privs(struct vsb *vsb, const struct vrt_privs *privs)
73 12
{
74 12
        struct vrt_priv *vp;
75 12
        const struct vmod_priv *p;
76 12
        const struct vmod_priv_methods *m;
77
78 12
        if (privs == NULL) {
79 0
                VSB_cat(vsb, "privs = NULL\n");
80 0
                return;
81 0
        }
82 12
        VSB_printf(vsb, "privs = %p {\n", privs);
83 12
        VSB_indent(vsb, 2);
84 12
        VRBT_FOREACH(vp, vrt_privs, privs) {
85 3
                if (PAN_dump_oneline(vsb, vp, VRT_PRIV_MAGIC, "priv"))
86 0
                        continue;
87 3
                p = vp->priv;
88
                //lint -e{774}
89 3
                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 0
                }
95 3
                m = p->methods;
96 3
                VSB_printf(vsb,
97 3
                    "{p %p l %ld m %p t \"%s\"} vmod %jx},\n",
98 3
                    p->priv, p->len, m,
99 3
                    m != NULL ? m->type : "",
100 3
                    (uintmax_t)vp->vmod_id
101 3
                );
102 3
        }
103 12
        VSB_indent(vsb, -2);
104 12
        VSB_cat(vsb, "},\n");
105 12
}
106
107
/*--------------------------------------------------------------------
108
 */
109
110
static void
111
VRTPRIV_init(struct vrt_privs *privs)
112 19056
{
113
114 19056
        VRBT_INIT(privs);
115 19056
}
116
117
static inline int
118
vrt_priv_dyncmp(const struct vrt_priv *vp1, const struct vrt_priv *vp2)
119 99930983
{
120 99930983
        if (vp1->vmod_id < vp2->vmod_id)
121 47061772
                return (-1);
122 52869211
        if (vp1->vmod_id > vp2->vmod_id)
123 41758487
                return (1);
124 11110724
        return (0);
125 52869211
}
126
127
static struct vmod_priv *
128
vrt_priv_dynamic_get(const struct vrt_privs *privs, uintptr_t vmod_id)
129 11110388
{
130 11110388
        struct vrt_priv *vp;
131
132 11110388
        const struct vrt_priv needle = {.vmod_id = vmod_id};
133
134 11110388
        vp = VRBT_FIND(vrt_privs, privs, &needle);
135 11110388
        if (vp == NULL)
136 160
                return (NULL);
137
138 11110228
        CHECK_OBJ(vp, VRT_PRIV_MAGIC);
139 11110228
        assert(vp->vmod_id == vmod_id);
140 11110228
        return (vp->priv);
141 11110228
}
142
143
static struct vmod_priv *
144
vrt_priv_dynamic(struct ws *ws, struct vrt_privs *privs, uintptr_t vmod_id)
145 1930
{
146
        //lint --e{593} vp allocated, vp->priv returned
147 1930
        struct vrt_priv *vp, *ovp;
148 1930
        enum vrt_priv_storage_e storage;
149
150 1930
        AN(vmod_id);
151
152 1930
        if (LIKELY(WS_ReserveSize(ws, sizeof *vp) != 0)) {
153 3730
                vp = WS_Reservation(ws);
154 0
                storage = VRT_PRIV_ST_WS;
155 3730
        }
156 65
        else {
157 65
                vp = malloc(sizeof *vp);
158 65
                storage = VRT_PRIV_ST_HEAP;
159 65
        }
160 1930
        AN(vp);
161
162 1930
        INIT_OBJ(vp, VRT_PRIV_MAGIC);
163 1930
        vp->storage = storage;
164 1930
        vp->vmod_id = vmod_id;
165 1930
        ovp = VRBT_INSERT(vrt_privs, privs, vp);
166 1930
        if (ovp == NULL) {
167 1434
                if (storage == VRT_PRIV_ST_WS)
168 1369
                        WS_Release(ws, sizeof *vp);
169 1434
                return (vp->priv);
170 1434
        }
171 496
        if (storage == VRT_PRIV_ST_WS)
172 496
                WS_Release(ws, 0);
173 0
        else if (storage == VRT_PRIV_ST_HEAP)
174 0
                free(vp);
175 0
        else
176 0
                WRONG("priv storage");
177 496
        return (ovp->priv);
178 496
}
179
180
static struct vrt_privs *
181
vrt_priv_task_context(VRT_CTX)
182 11112281
{
183
184 11112281
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
185
186
        /* In pipe mode, both req and bo are set. We use req */
187
188 11112281
        assert(ctx->req == NULL || ctx->bo == NULL ||
189 11112281
            ctx->method == VCL_MET_PIPE || ctx->method == 0);
190
191 11112281
        if (ctx->req) {
192 11111769
                CHECK_OBJ(ctx->req, REQ_MAGIC);
193 11111769
                return (ctx->req->privs);
194 11111769
        }
195 512
        if (ctx->bo) {
196 343
                CHECK_OBJ(ctx->bo, BUSYOBJ_MAGIC);
197 343
                return (ctx->bo->privs);
198 343
        }
199 169
        ASSERT_CLI();
200 169
        return (cli_task_privs);
201 169
}
202
203
struct vmod_priv *
204
VRT_priv_task_get(VRT_CTX, const void *vmod_id)
205 11110378
{
206 11110378
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
207
208 11110378
        return (vrt_priv_dynamic_get(
209 11110378
            vrt_priv_task_context(ctx),
210 11110378
            (uintptr_t)vmod_id));
211 11110378
}
212
213
struct vmod_priv *
214
VRT_priv_task(VRT_CTX, const void *vmod_id)
215 1903
{
216
217 1903
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
218
219 1903
        return (vrt_priv_dynamic(
220 1903
            ctx->ws,
221 1903
            vrt_priv_task_context(ctx),
222 1903
            (uintptr_t)vmod_id));
223 1903
}
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 37
#define VRT_PRIV_TOP_PREP(ctx, req, sp, top)    do {                    \
233 37
                CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);                  \
234 37
                req = (ctx)->req;                                       \
235 37
                if (req == NULL) {                                      \
236 0
                        VRT_fail(ctx, "PRIV_TOP is only accessible "    \
237 0
                            "in client VCL context");                   \
238 0
                        return (NULL);                                  \
239 0
                }                                                       \
240 37
                CHECK_OBJ(req, REQ_MAGIC);                              \
241 37
                sp = (ctx)->sp;                                         \
242 37
                CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);                      \
243 37
                top = (req)->top;                                       \
244 37
                CHECK_OBJ_NOTNULL(top, REQTOP_MAGIC);                   \
245 37
                req = (top)->topreq;                                    \
246 37
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);                      \
247 37
        } while(0)
248
249
struct vmod_priv *
250
VRT_priv_top_get(VRT_CTX, const void *vmod_id)
251 10
{
252 10
        struct req *req;
253 10
        struct sess *sp;
254 10
        struct reqtop *top;
255 10
        struct vmod_priv *priv;
256
257 10
        VRT_PRIV_TOP_PREP(ctx, req, sp, top);
258
259 10
        Lck_Lock(&sp->mtx);
260 10
        priv = vrt_priv_dynamic_get(top->privs, (uintptr_t)vmod_id);
261 10
        Lck_Unlock(&sp->mtx);
262 10
        return (priv);
263 10
}
264
265
struct vmod_priv *
266
VRT_priv_top(VRT_CTX, const void *vmod_id)
267 27
{
268 27
        struct req *req;
269 27
        struct sess *sp;
270 27
        struct reqtop *top;
271 27
        struct vmod_priv *priv;
272
273 27
        VRT_PRIV_TOP_PREP(ctx, req, sp, top);
274
275 27
        Lck_Lock(&sp->mtx);
276 27
        priv = vrt_priv_dynamic(req->ws, top->privs, (uintptr_t)vmod_id);
277 27
        Lck_Unlock(&sp->mtx);
278 27
        return (priv);
279 27
}
280
281
/*--------------------------------------------------------------------
282
 */
283
284
void
285
VRT_priv_fini(VRT_CTX, const struct vmod_priv *p)
286 1519
{
287 1519
        const struct vmod_priv_methods *m;
288
289 1519
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
290
291 1519
        m = p->methods;
292 1519
        if (m == NULL)
293 1171
                return;
294
295 348
        CHECK_OBJ(m, VMOD_PRIV_METHODS_MAGIC);
296 348
        if (p->priv == NULL || m->fini == NULL)
297 0
                return;
298
299
        // XXX remove me after soak in
300 348
        VRT_CTX_Assert(ctx);
301
302 348
        m->fini(ctx, p->priv);
303 348
        assert(ctx->vpi->handling == 0 || ctx->vpi->handling == VCL_RET_FAIL);
304 348
}
305
306
/*--------------------------------------------------------------------*/
307
308
void
309
VCL_TaskEnter(struct vrt_privs *privs)
310 19054
{
311
312 19054
        VRTPRIV_init(privs);
313 19054
}
314
315
void
316
VCL_TaskLeave(VRT_CTX, struct vrt_privs *privs)
317 19044
{
318 19044
        struct vrt_priv *vp, *vp1;
319
320 19044
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
321 19044
        AN(ctx->vpi);
322 19044
        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 19044
        VRBT_FOREACH_SAFE(vp, vrt_privs, privs, vp1) {
330 1431
                CHECK_OBJ(vp, VRT_PRIV_MAGIC);
331 1431
                VRT_priv_fini(ctx, vp->priv);
332 1431
                if (vp->storage == VRT_PRIV_ST_HEAP)
333 65
                        free(vp);
334 1431
        }
335 19044
        ZERO_OBJ(privs, sizeof *privs);
336 19044
}