| | varnish-cache/bin/varnishd/cache/cache_ws.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 |
|
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com> |
| 7 |
|
* |
| 8 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 9 |
|
* |
| 10 |
|
* Redistribution and use in source and binary forms, with or without |
| 11 |
|
* modification, are permitted provided that the following conditions |
| 12 |
|
* are met: |
| 13 |
|
* 1. Redistributions of source code must retain the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer. |
| 15 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
|
* notice, this list of conditions and the following disclaimer in the |
| 17 |
|
* documentation and/or other materials provided with the distribution. |
| 18 |
|
* |
| 19 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 20 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 23 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
|
* SUCH DAMAGE. |
| 30 |
|
* |
| 31 |
|
*/ |
| 32 |
|
|
| 33 |
|
#include "config.h" |
| 34 |
|
|
| 35 |
|
#include "cache_varnishd.h" |
| 36 |
|
|
| 37 |
|
#define WS_REDZONE_END '\x15' |
| 38 |
|
|
| 39 |
|
static const uintptr_t snap_overflowed = (uintptr_t)&snap_overflowed; |
| 40 |
|
|
| 41 |
|
void |
| 42 |
33902292 |
WS_Assert(const struct ws *ws) |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
33902292 |
CHECK_OBJ_NOTNULL(ws, WS_MAGIC); |
| 46 |
33902292 |
DSLb(DBG_WORKSPACE, "WS(%s, %p) = {%p %zu %zu %zu}", |
| 47 |
|
ws->id, ws, ws->s, pdiff(ws->s, ws->f), |
| 48 |
|
ws->r == NULL ? 0 : pdiff(ws->f, ws->r), |
| 49 |
|
pdiff(ws->s, ws->e)); |
| 50 |
33902292 |
assert(ws->s != NULL); |
| 51 |
33902292 |
assert(PAOK(ws->s)); |
| 52 |
33902292 |
assert(ws->e != NULL); |
| 53 |
33902292 |
assert(PAOK(ws->e)); |
| 54 |
33902292 |
assert(ws->s < ws->e); |
| 55 |
33902292 |
assert(ws->f >= ws->s); |
| 56 |
33902292 |
assert(ws->f <= ws->e); |
| 57 |
33902292 |
assert(PAOK(ws->f)); |
| 58 |
33902292 |
if (ws->r) { |
| 59 |
11172193 |
assert(ws->r >= ws->f); |
| 60 |
11172193 |
assert(ws->r <= ws->e); |
| 61 |
11172193 |
} |
| 62 |
33902292 |
assert(*ws->e == WS_REDZONE_END); |
| 63 |
33902292 |
} |
| 64 |
|
|
| 65 |
|
int |
| 66 |
2117230 |
WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len) |
| 67 |
|
{ |
| 68 |
2117230 |
const char *p = ptr; |
| 69 |
|
|
| 70 |
2117230 |
WS_Assert(ws); |
| 71 |
2117230 |
if (len < 0) |
| 72 |
12880 |
len = strlen(p) + 1; |
| 73 |
2117230 |
assert(!(p > ws->f && p <= ws->e)); |
| 74 |
2117230 |
return (p >= ws->s && (p + len) <= ws->f); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
/* |
| 78 |
|
* NB: The id must be max 3 char and lower-case. |
| 79 |
|
* (upper-case the first char to indicate overflow) |
| 80 |
|
*/ |
| 81 |
|
|
| 82 |
|
void |
| 83 |
1112787 |
WS_Init(struct ws *ws, const char *id, void *space, unsigned len) |
| 84 |
|
{ |
| 85 |
|
unsigned l; |
| 86 |
|
|
| 87 |
1112787 |
DSLb(DBG_WORKSPACE, |
| 88 |
|
"WS_Init(%s, %p, %p, %u)", id, ws, space, len); |
| 89 |
1112787 |
assert(space != NULL); |
| 90 |
1112787 |
INIT_OBJ(ws, WS_MAGIC); |
| 91 |
1112787 |
ws->s = space; |
| 92 |
1112787 |
assert(PAOK(space)); |
| 93 |
1112787 |
l = PRNDDN(len - 1); |
| 94 |
1112787 |
ws->e = ws->s + l; |
| 95 |
1112787 |
memset(ws->e, WS_REDZONE_END, len - l); |
| 96 |
1112787 |
ws->f = ws->s; |
| 97 |
1112787 |
assert(id[0] & 0x20); // cheesy islower() |
| 98 |
1112787 |
bstrcpy(ws->id, id); |
| 99 |
1112787 |
WS_Assert(ws); |
| 100 |
1112787 |
} |
| 101 |
|
|
| 102 |
|
/* |
| 103 |
|
* Reset a WS to a cookie from WS_Snapshot |
| 104 |
|
* |
| 105 |
|
* for use by any code using cache.h |
| 106 |
|
* |
| 107 |
|
* does not reset the overflow bit and asserts that, if WS_Snapshot had found |
| 108 |
|
* the workspace overflown, the marker is intact |
| 109 |
|
*/ |
| 110 |
|
|
| 111 |
|
void |
| 112 |
1807952 |
WS_Reset(struct ws *ws, uintptr_t pp) |
| 113 |
|
{ |
| 114 |
|
char *p; |
| 115 |
|
|
| 116 |
1807952 |
WS_Assert(ws); |
| 117 |
1807952 |
AN(pp); |
| 118 |
1807952 |
if (pp == snap_overflowed) { |
| 119 |
40 |
DSLb(DBG_WORKSPACE, "WS_Reset(%s, %p, overflowed)", ws->id, ws); |
| 120 |
40 |
AN(WS_Overflowed(ws)); |
| 121 |
40 |
return; |
| 122 |
|
} |
| 123 |
1807912 |
p = (char *)pp; |
| 124 |
1807912 |
DSLb(DBG_WORKSPACE, "WS_Reset(%s, %p, %p)", ws->id, ws, p); |
| 125 |
1807912 |
assert(ws->r == NULL); |
| 126 |
1807912 |
assert(p >= ws->s); |
| 127 |
1807912 |
assert(p <= ws->e); |
| 128 |
1807912 |
ws->f = p; |
| 129 |
1807912 |
WS_Assert(ws); |
| 130 |
1807952 |
} |
| 131 |
|
|
| 132 |
|
/* |
| 133 |
|
* Make a reservation and optionally pipeline a memory region that may or |
| 134 |
|
* may not originate from the same workspace. |
| 135 |
|
*/ |
| 136 |
|
|
| 137 |
|
int |
| 138 |
336088 |
WS_Pipeline(struct ws *ws, const void *b, const void *e, unsigned rollback) |
| 139 |
|
{ |
| 140 |
|
unsigned r, l; |
| 141 |
|
|
| 142 |
336088 |
WS_Assert(ws); |
| 143 |
|
|
| 144 |
336088 |
if (rollback) |
| 145 |
251796 |
WS_Rollback(ws, 0); |
| 146 |
|
|
| 147 |
336088 |
r = WS_ReserveAll(ws); |
| 148 |
|
|
| 149 |
336088 |
if (b == NULL) { |
| 150 |
322622 |
AZ(e); |
| 151 |
322622 |
return (0); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
13466 |
AN(e); |
| 155 |
13466 |
l = pdiff(b, e); |
| 156 |
13466 |
if (l > r) |
| 157 |
0 |
return (-1); |
| 158 |
13466 |
memmove(ws->f, b, l); |
| 159 |
13466 |
return (l); |
| 160 |
336088 |
} |
| 161 |
|
|
| 162 |
|
void * |
| 163 |
728414 |
WS_Alloc(struct ws *ws, unsigned bytes) |
| 164 |
|
{ |
| 165 |
|
char *r; |
| 166 |
|
|
| 167 |
728414 |
WS_Assert(ws); |
| 168 |
728414 |
assert(bytes > 0); |
| 169 |
728414 |
bytes = PRNDUP(bytes); |
| 170 |
|
|
| 171 |
728414 |
assert(ws->r == NULL); |
| 172 |
728414 |
if (ws->f + bytes > ws->e) { |
| 173 |
13760 |
WS_MarkOverflow(ws); |
| 174 |
13760 |
return (NULL); |
| 175 |
|
} |
| 176 |
714654 |
r = ws->f; |
| 177 |
714654 |
ws->f += bytes; |
| 178 |
714654 |
DSLb(DBG_WORKSPACE, "WS_Alloc(%s, %p, %u) = %p", ws->id, ws, bytes, r); |
| 179 |
714654 |
WS_Assert(ws); |
| 180 |
714654 |
return (r); |
| 181 |
728414 |
} |
| 182 |
|
|
| 183 |
|
void * |
| 184 |
675209 |
WS_Copy(struct ws *ws, const void *str, int len) |
| 185 |
|
{ |
| 186 |
|
char *r; |
| 187 |
|
unsigned bytes; |
| 188 |
|
|
| 189 |
675209 |
WS_Assert(ws); |
| 190 |
675209 |
assert(ws->r == NULL); |
| 191 |
|
|
| 192 |
675209 |
if (len == -1) |
| 193 |
14709 |
len = strlen(str) + 1; |
| 194 |
675209 |
assert(len > 0); |
| 195 |
|
|
| 196 |
675209 |
bytes = PRNDUP((unsigned)len); |
| 197 |
675209 |
if (ws->f + bytes > ws->e) { |
| 198 |
160 |
WS_MarkOverflow(ws); |
| 199 |
160 |
return (NULL); |
| 200 |
|
} |
| 201 |
675049 |
r = ws->f; |
| 202 |
675049 |
ws->f += bytes; |
| 203 |
675049 |
memcpy(r, str, len); |
| 204 |
675049 |
DSLb(DBG_WORKSPACE, "WS_Copy(%s, %p, %d) = %p", ws->id, ws, len, r); |
| 205 |
675049 |
WS_Assert(ws); |
| 206 |
675049 |
return (r); |
| 207 |
675209 |
} |
| 208 |
|
|
| 209 |
|
uintptr_t |
| 210 |
2073109 |
WS_Snapshot(struct ws *ws) |
| 211 |
|
{ |
| 212 |
|
|
| 213 |
2073109 |
WS_Assert(ws); |
| 214 |
2073109 |
assert(ws->r == NULL); |
| 215 |
2073109 |
if (WS_Overflowed(ws)) { |
| 216 |
120 |
DSLb(DBG_WORKSPACE, "WS_Snapshot(%s, %p) = overflowed", |
| 217 |
|
ws->id, ws); |
| 218 |
120 |
return (snap_overflowed); |
| 219 |
|
} |
| 220 |
2072989 |
DSLb(DBG_WORKSPACE, "WS_Snapshot(%s, %p) = %p", ws->id, ws, ws->f); |
| 221 |
2072989 |
return ((uintptr_t)ws->f); |
| 222 |
2073109 |
} |
| 223 |
|
|
| 224 |
|
/* |
| 225 |
|
* WS_Release() must be called in all cases |
| 226 |
|
*/ |
| 227 |
|
unsigned |
| 228 |
2297892 |
WS_ReserveAll(struct ws *ws) |
| 229 |
|
{ |
| 230 |
|
unsigned b; |
| 231 |
|
|
| 232 |
2297892 |
WS_Assert(ws); |
| 233 |
2297892 |
assert(ws->r == NULL); |
| 234 |
|
|
| 235 |
2297892 |
ws->r = ws->e; |
| 236 |
2297892 |
b = pdiff(ws->f, ws->r); |
| 237 |
|
|
| 238 |
2297892 |
WS_Assert(ws); |
| 239 |
2297892 |
DSLb(DBG_WORKSPACE, "WS_ReserveAll(%s, %p) = %u", ws->id, ws, b); |
| 240 |
|
|
| 241 |
2297892 |
return (b); |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
/* |
| 245 |
|
* WS_Release() must be called for retval > 0 only |
| 246 |
|
*/ |
| 247 |
|
unsigned |
| 248 |
620082 |
WS_ReserveSize(struct ws *ws, unsigned bytes) |
| 249 |
|
{ |
| 250 |
|
unsigned l; |
| 251 |
|
|
| 252 |
620082 |
WS_Assert(ws); |
| 253 |
620082 |
assert(ws->r == NULL); |
| 254 |
620082 |
assert(bytes > 0); |
| 255 |
|
|
| 256 |
620082 |
l = pdiff(ws->f, ws->e); |
| 257 |
620082 |
if (bytes > l) { |
| 258 |
2680 |
WS_MarkOverflow(ws); |
| 259 |
2680 |
return (0); |
| 260 |
|
} |
| 261 |
617402 |
ws->r = ws->f + bytes; |
| 262 |
617402 |
DSLb(DBG_WORKSPACE, "WS_ReserveSize(%s, %p, %u/%u) = %u", ws->id, |
| 263 |
|
ws, bytes, l, bytes); |
| 264 |
617402 |
WS_Assert(ws); |
| 265 |
617402 |
return (bytes); |
| 266 |
620082 |
} |
| 267 |
|
|
| 268 |
|
void |
| 269 |
2376202 |
WS_Release(struct ws *ws, unsigned bytes) |
| 270 |
|
{ |
| 271 |
2376202 |
WS_Assert(ws); |
| 272 |
2376202 |
assert(bytes <= ws->e - ws->f); |
| 273 |
2376202 |
DSLb(DBG_WORKSPACE, "WS_Release(%s, %p, %u)", ws->id, ws, bytes); |
| 274 |
2376202 |
assert(ws->r != NULL); |
| 275 |
2376202 |
assert(ws->f + bytes <= ws->r); |
| 276 |
2376202 |
ws->f += PRNDUP(bytes); |
| 277 |
2376202 |
ws->r = NULL; |
| 278 |
2376202 |
WS_Assert(ws); |
| 279 |
2376202 |
} |
| 280 |
|
|
| 281 |
|
void |
| 282 |
538465 |
WS_ReleaseP(struct ws *ws, const char *ptr) |
| 283 |
|
{ |
| 284 |
538465 |
WS_Assert(ws); |
| 285 |
538465 |
DSLb(DBG_WORKSPACE, "WS_ReleaseP(%s, %p, %p (%zd))", ws->id, ws, ptr, |
| 286 |
|
ptr - ws->f); |
| 287 |
538465 |
assert(ws->r != NULL); |
| 288 |
538465 |
assert(ptr >= ws->f); |
| 289 |
538465 |
assert(ptr <= ws->r); |
| 290 |
538465 |
ws->f += PRNDUP(ptr - ws->f); |
| 291 |
538465 |
ws->r = NULL; |
| 292 |
538465 |
WS_Assert(ws); |
| 293 |
538465 |
} |
| 294 |
|
|
| 295 |
|
void * |
| 296 |
1385644 |
WS_AtOffset(const struct ws *ws, unsigned off, unsigned len) |
| 297 |
|
{ |
| 298 |
|
char *ptr; |
| 299 |
|
|
| 300 |
1385644 |
WS_Assert(ws); |
| 301 |
1385644 |
ptr = ws->s + off; |
| 302 |
1385644 |
AN(WS_Allocated(ws, ptr, len)); |
| 303 |
1385644 |
return (ptr); |
| 304 |
|
} |
| 305 |
|
|
| 306 |
|
unsigned |
| 307 |
440260 |
WS_ReservationOffset(const struct ws *ws) |
| 308 |
|
{ |
| 309 |
|
|
| 310 |
440260 |
AN(ws->r); |
| 311 |
440260 |
return (ws->f - ws->s); |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
/*--------------------------------------------------------------------*/ |
| 315 |
|
|
| 316 |
|
unsigned |
| 317 |
478 |
WS_Dump(const struct ws *ws, char where, size_t off, void *buf, size_t len) |
| 318 |
|
{ |
| 319 |
|
char *b, *p; |
| 320 |
|
size_t l; |
| 321 |
|
|
| 322 |
478 |
WS_Assert(ws); |
| 323 |
478 |
AN(buf); |
| 324 |
478 |
AN(len); |
| 325 |
|
|
| 326 |
478 |
switch (where) { |
| 327 |
0 |
case 's': p = ws->s; break; |
| 328 |
478 |
case 'f': p = ws->f; break; |
| 329 |
0 |
case 'r': p = ws->r; break; |
| 330 |
|
default: |
| 331 |
0 |
errno = EINVAL; |
| 332 |
0 |
return (0); |
| 333 |
|
} |
| 334 |
|
|
| 335 |
478 |
if (p == NULL) { |
| 336 |
1 |
errno = EAGAIN; |
| 337 |
1 |
return (0); |
| 338 |
|
} |
| 339 |
|
|
| 340 |
477 |
p += off; |
| 341 |
477 |
if (p >= ws->e) { |
| 342 |
0 |
errno = EFAULT; |
| 343 |
0 |
return (0); |
| 344 |
|
} |
| 345 |
|
|
| 346 |
477 |
l = pdiff(p, ws->e); |
| 347 |
477 |
if (len <= l) { |
| 348 |
477 |
memcpy(buf, p, len); |
| 349 |
477 |
return (len); |
| 350 |
|
} |
| 351 |
|
|
| 352 |
0 |
b = buf; |
| 353 |
0 |
memcpy(b, p, l); |
| 354 |
0 |
memset(b + l, WS_REDZONE_END, len - l); |
| 355 |
0 |
return (l); |
| 356 |
478 |
} |
| 357 |
|
|
| 358 |
|
/*--------------------------------------------------------------------*/ |
| 359 |
|
|
| 360 |
|
static inline void |
| 361 |
2520 |
ws_printptr(struct vsb *vsb, const char *s, const char *p) |
| 362 |
|
{ |
| 363 |
2520 |
if (p >= s) |
| 364 |
1680 |
VSB_printf(vsb, ", +%ld", (long) (p - s)); |
| 365 |
|
else |
| 366 |
840 |
VSB_printf(vsb, ", %p", p); |
| 367 |
2520 |
} |
| 368 |
|
|
| 369 |
|
void |
| 370 |
1520 |
WS_Panic(struct vsb *vsb, const struct ws *ws) |
| 371 |
|
{ |
| 372 |
|
|
| 373 |
1520 |
if (PAN_dump_struct(vsb, ws, WS_MAGIC, "ws")) |
| 374 |
680 |
return; |
| 375 |
840 |
if (ws->id[0] != '\0' && (!(ws->id[0] & 0x20))) // cheesy islower() |
| 376 |
0 |
VSB_cat(vsb, "OVERFLOWED "); |
| 377 |
840 |
VSB_printf(vsb, "id = \"%s\",\n", ws->id); |
| 378 |
840 |
VSB_printf(vsb, "{s, f, r, e} = {%p", ws->s); |
| 379 |
840 |
ws_printptr(vsb, ws->s, ws->f); |
| 380 |
840 |
ws_printptr(vsb, ws->s, ws->r); |
| 381 |
840 |
ws_printptr(vsb, ws->s, ws->e); |
| 382 |
840 |
VSB_cat(vsb, "},\n"); |
| 383 |
840 |
VSB_indent(vsb, -2); |
| 384 |
840 |
VSB_cat(vsb, "},\n"); |
| 385 |
1520 |
} |