| | 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 |
164560 |
VRBT_GENERATE_INSERT_COLOR(vrt_privs, vrt_priv, entry, static) |
| 62 |
3996822600 |
VRBT_GENERATE_FIND(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static) |
| 63 |
56720 |
VRBT_GENERATE_INSERT_FINISH(vrt_privs, vrt_priv, entry, static) |
| 64 |
479199 |
VRBT_GENERATE_INSERT(vrt_privs, vrt_priv, entry, vrt_priv_dyncmp, static) |
| 65 |
512849 |
VRBT_GENERATE_MINMAX(vrt_privs, vrt_priv, entry, static) |
| 66 |
98960 |
VRBT_GENERATE_NEXT(vrt_privs, vrt_priv, entry, static) |
| 67 |
|
|
| 68 |
|
/*-------------------------------------------------------------------- |
| 69 |
|
*/ |
| 70 |
|
|
| 71 |
|
void |
| 72 |
480 |
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 |
480 |
if (privs == NULL) { |
| 79 |
0 |
VSB_cat(vsb, "privs = NULL\n"); |
| 80 |
0 |
return; |
| 81 |
|
} |
| 82 |
480 |
VSB_printf(vsb, "privs = %p {\n", privs); |
| 83 |
480 |
VSB_indent(vsb, 2); |
| 84 |
600 |
VRBT_FOREACH(vp, vrt_privs, privs) { |
| 85 |
120 |
if (PAN_dump_oneline(vsb, vp, VRT_PRIV_MAGIC, "priv")) |
| 86 |
0 |
continue; |
| 87 |
120 |
p = vp->priv; |
| 88 |
|
//lint -e{774} |
| 89 |
120 |
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 |
120 |
m = p->methods; |
| 96 |
240 |
VSB_printf(vsb, |
| 97 |
|
"{p %p l %ld m %p t \"%s\"} vmod %jx},\n", |
| 98 |
120 |
p->priv, p->len, m, |
| 99 |
120 |
m != NULL ? m->type : "", |
| 100 |
120 |
(uintmax_t)vp->vmod_id |
| 101 |
|
); |
| 102 |
120 |
} |
| 103 |
480 |
VSB_indent(vsb, -2); |
| 104 |
480 |
VSB_cat(vsb, "},\n"); |
| 105 |
480 |
} |
| 106 |
|
|
| 107 |
|
/*-------------------------------------------------------------------- |
| 108 |
|
*/ |
| 109 |
|
|
| 110 |
|
static void |
| 111 |
498358 |
VRTPRIV_init(struct vrt_privs *privs) |
| 112 |
|
{ |
| 113 |
|
|
| 114 |
498358 |
VRBT_INIT(privs); |
| 115 |
498358 |
} |
| 116 |
|
|
| 117 |
|
static inline int |
| 118 |
3997238840 |
vrt_priv_dyncmp(const struct vrt_priv *vp1, const struct vrt_priv *vp2) |
| 119 |
|
{ |
| 120 |
3997238840 |
if (vp1->vmod_id < vp2->vmod_id) |
| 121 |
1882470720 |
return (-1); |
| 122 |
2114768120 |
if (vp1->vmod_id > vp2->vmod_id) |
| 123 |
1670339480 |
return (1); |
| 124 |
444428640 |
return (0); |
| 125 |
3997238840 |
} |
| 126 |
|
|
| 127 |
|
static struct vmod_priv * |
| 128 |
444415120 |
vrt_priv_dynamic_get(const struct vrt_privs *privs, uintptr_t vmod_id) |
| 129 |
|
{ |
| 130 |
|
struct vrt_priv *vp; |
| 131 |
|
|
| 132 |
444415120 |
const struct vrt_priv needle = {.vmod_id = vmod_id}; |
| 133 |
|
|
| 134 |
444415120 |
vp = VRBT_FIND(vrt_privs, privs, &needle); |
| 135 |
444415120 |
if (vp == NULL) |
| 136 |
6240 |
return (NULL); |
| 137 |
|
|
| 138 |
444408880 |
CHECK_OBJ(vp, VRT_PRIV_MAGIC); |
| 139 |
444408880 |
assert(vp->vmod_id == vmod_id); |
| 140 |
444408880 |
return (vp->priv); |
| 141 |
444415120 |
} |
| 142 |
|
|
| 143 |
|
static struct vmod_priv * |
| 144 |
76480 |
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 |
76480 |
AN(vmod_id); |
| 151 |
|
|
| 152 |
76480 |
if (LIKELY(WS_ReserveSize(ws, sizeof *vp) != 0)) { |
| 153 |
73880 |
vp = WS_Reservation(ws); |
| 154 |
73880 |
storage = VRT_PRIV_ST_WS; |
| 155 |
73880 |
} |
| 156 |
|
else { |
| 157 |
2600 |
vp = malloc(sizeof *vp); |
| 158 |
2600 |
storage = VRT_PRIV_ST_HEAP; |
| 159 |
|
} |
| 160 |
76480 |
AN(vp); |
| 161 |
|
|
| 162 |
76480 |
INIT_OBJ(vp, VRT_PRIV_MAGIC); |
| 163 |
76480 |
vp->storage = storage; |
| 164 |
76480 |
vp->vmod_id = vmod_id; |
| 165 |
76480 |
ovp = VRBT_INSERT(vrt_privs, privs, vp); |
| 166 |
76480 |
if (ovp == NULL) { |
| 167 |
56720 |
if (storage == VRT_PRIV_ST_WS) |
| 168 |
54120 |
WS_Release(ws, sizeof *vp); |
| 169 |
56720 |
return (vp->priv); |
| 170 |
|
} |
| 171 |
19760 |
if (storage == VRT_PRIV_ST_WS) |
| 172 |
19760 |
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 |
19760 |
return (ovp->priv); |
| 178 |
76480 |
} |
| 179 |
|
|
| 180 |
|
static struct vrt_privs * |
| 181 |
444490120 |
vrt_priv_task_context(VRT_CTX) |
| 182 |
|
{ |
| 183 |
|
|
| 184 |
444490120 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 185 |
|
|
| 186 |
|
/* In pipe mode, both req and bo are set. We use req */ |
| 187 |
|
|
| 188 |
444490120 |
assert(ctx->req == NULL || ctx->bo == NULL || |
| 189 |
|
ctx->method == VCL_MET_PIPE || ctx->method == 0); |
| 190 |
|
|
| 191 |
444490120 |
if (ctx->req) { |
| 192 |
444470520 |
CHECK_OBJ(ctx->req, REQ_MAGIC); |
| 193 |
444470520 |
return (ctx->req->privs); |
| 194 |
|
} |
| 195 |
19600 |
if (ctx->bo) { |
| 196 |
12840 |
CHECK_OBJ(ctx->bo, BUSYOBJ_MAGIC); |
| 197 |
12840 |
return (ctx->bo->privs); |
| 198 |
|
} |
| 199 |
6760 |
ASSERT_CLI(); |
| 200 |
6760 |
return (cli_task_privs); |
| 201 |
444490120 |
} |
| 202 |
|
|
| 203 |
|
struct vmod_priv * |
| 204 |
444414720 |
VRT_priv_task_get(VRT_CTX, const void *vmod_id) |
| 205 |
|
{ |
| 206 |
444414720 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 207 |
|
|
| 208 |
444414720 |
return (vrt_priv_dynamic_get( |
| 209 |
444414720 |
vrt_priv_task_context(ctx), |
| 210 |
444414720 |
(uintptr_t)vmod_id)); |
| 211 |
|
} |
| 212 |
|
|
| 213 |
|
struct vmod_priv * |
| 214 |
75400 |
VRT_priv_task(VRT_CTX, const void *vmod_id) |
| 215 |
|
{ |
| 216 |
|
|
| 217 |
75400 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 218 |
|
|
| 219 |
75400 |
return (vrt_priv_dynamic( |
| 220 |
75400 |
ctx->ws, |
| 221 |
75400 |
vrt_priv_task_context(ctx), |
| 222 |
75400 |
(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 |
400 |
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 |
400 |
VRT_PRIV_TOP_PREP(ctx, req, sp, top); |
| 258 |
|
|
| 259 |
400 |
Lck_Lock(&sp->mtx); |
| 260 |
400 |
priv = vrt_priv_dynamic_get(top->privs, (uintptr_t)vmod_id); |
| 261 |
400 |
Lck_Unlock(&sp->mtx); |
| 262 |
400 |
return (priv); |
| 263 |
400 |
} |
| 264 |
|
|
| 265 |
|
struct vmod_priv * |
| 266 |
1080 |
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 |
1080 |
VRT_PRIV_TOP_PREP(ctx, req, sp, top); |
| 274 |
|
|
| 275 |
1080 |
Lck_Lock(&sp->mtx); |
| 276 |
1080 |
priv = vrt_priv_dynamic(req->ws, top->privs, (uintptr_t)vmod_id); |
| 277 |
1080 |
Lck_Unlock(&sp->mtx); |
| 278 |
1080 |
return (priv); |
| 279 |
1080 |
} |
| 280 |
|
|
| 281 |
|
/*-------------------------------------------------------------------- |
| 282 |
|
*/ |
| 283 |
|
|
| 284 |
|
void |
| 285 |
60238 |
VRT_priv_fini(VRT_CTX, const struct vmod_priv *p) |
| 286 |
|
{ |
| 287 |
|
const struct vmod_priv_methods *m; |
| 288 |
|
|
| 289 |
60238 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 290 |
|
|
| 291 |
60238 |
m = p->methods; |
| 292 |
60238 |
if (m == NULL) |
| 293 |
46586 |
return; |
| 294 |
|
|
| 295 |
13652 |
CHECK_OBJ(m, VMOD_PRIV_METHODS_MAGIC); |
| 296 |
13652 |
if (p->priv == NULL || m->fini == NULL) |
| 297 |
0 |
return; |
| 298 |
|
|
| 299 |
|
// XXX remove me after soak in |
| 300 |
13652 |
VRT_CTX_Assert(ctx); |
| 301 |
|
|
| 302 |
13652 |
m->fini(ctx, p->priv); |
| 303 |
13652 |
assert(ctx->vpi->handling == 0 || ctx->vpi->handling == VCL_RET_FAIL); |
| 304 |
60238 |
} |
| 305 |
|
|
| 306 |
|
/*--------------------------------------------------------------------*/ |
| 307 |
|
|
| 308 |
|
void |
| 309 |
498350 |
VCL_TaskEnter(struct vrt_privs *privs) |
| 310 |
|
{ |
| 311 |
|
|
| 312 |
498350 |
VRTPRIV_init(privs); |
| 313 |
498350 |
} |
| 314 |
|
|
| 315 |
|
void |
| 316 |
497882 |
VCL_TaskLeave(VRT_CTX, struct vrt_privs *privs) |
| 317 |
|
{ |
| 318 |
|
struct vrt_priv *vp, *vp1; |
| 319 |
|
|
| 320 |
497882 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 321 |
497882 |
AN(ctx->vpi); |
| 322 |
497882 |
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 |
554482 |
VRBT_FOREACH_SAFE(vp, vrt_privs, privs, vp1) { |
| 330 |
56600 |
CHECK_OBJ(vp, VRT_PRIV_MAGIC); |
| 331 |
56600 |
VRT_priv_fini(ctx, vp->priv); |
| 332 |
56600 |
if (vp->storage == VRT_PRIV_ST_HEAP) |
| 333 |
2600 |
free(vp); |
| 334 |
56600 |
} |
| 335 |
497882 |
ZERO_OBJ(privs, sizeof *privs); |
| 336 |
497882 |
} |