[master] 343e680a9 ws: New WS_Dump() facility

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Aug 17 06:57:05 UTC 2021


commit 343e680a900f7553c2d701da9fd596c0497e70ee
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Thu Jul 8 08:03:39 2021 +0200

    ws: New WS_Dump() facility
    
    Getting rid of the very last direct access inside struct ws from
    vmod_vtc.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index abfff698d..7c12d7875 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -781,6 +781,7 @@ uintptr_t WS_Snapshot(struct ws *ws);
 int WS_Overflowed(const struct ws *ws);
 const char *WS_Printf(struct ws *ws, const char *fmt, ...) v_printflike_(2, 3);
 int WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len);
+unsigned WS_Dump(const struct ws *ws, char, size_t off, void *buf, size_t len);
 
 void WS_VSB_new(struct vsb *, struct ws *);
 char *WS_VSB_finish(struct vsb *, struct ws *, size_t *);
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index 784e3b493..091179d47 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -411,6 +411,50 @@ WS_VSB_finish(struct vsb *vsb, struct ws *ws, size_t *szp)
 
 /*--------------------------------------------------------------------*/
 
+unsigned
+WS_Dump(const struct ws *ws, char where, size_t off, void *buf, size_t len)
+{
+	char *b, *p;
+	size_t l;
+
+	WS_Assert(ws);
+	AN(buf);
+	AN(len);
+
+	switch (where) {
+	case 's': p = ws->s; break;
+	case 'f': p = ws->f; break;
+	case 'r': p = ws->r; break;
+	default:
+		errno = EINVAL;
+		return (0);
+	}
+
+	if (p == NULL) {
+		errno = EAGAIN;
+		return (0);
+	}
+
+	p += off;
+	if (p >= ws->e) {
+		errno = EFAULT;
+		return (0);
+	}
+
+	l = pdiff(p, ws->e);
+	if (len <= l) {
+		memcpy(buf, p, len);
+		return (len);
+	}
+
+	b = buf;
+	memcpy(b, p, l);
+	memset(b + l, WS_REDZONE_END, len - l);
+	return (l);
+}
+
+/*--------------------------------------------------------------------*/
+
 void
 WS_Panic(struct vsb *vsb, const struct ws *ws)
 {
diff --git a/include/vrt.h b/include/vrt.h
index 180bbc8e3..4019f7e57 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -64,6 +64,7 @@
  *	[cache.h] WS_Inside() removed
  *	[cache.h] WS_Assert_Allocated() removed
  *	[cache.h] WS_Allocated() added
+ *	[cache.h] WS_Dump() added
  * 13.0 (2021-03-15)
  *	Move VRT_synth_page() to deprecated status
  *	Add VRT_synth_strands() and VRT_synth_blob()
diff --git a/vmod/vmod_vtc.c b/vmod/vmod_vtc.c
index 2b445d82b..7c09e79c3 100644
--- a/vmod/vmod_vtc.c
+++ b/vmod/vmod_vtc.c
@@ -244,9 +244,10 @@ vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
 	struct ws *ws;
 	VCL_BYTES l, maxlen = 1024;
 	unsigned char buf[maxlen];
-	const char *p;
+	const char *p, *err;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	AN(where);
 
 	ws = vtc_ws_find(ctx, which);
 	if (ws == NULL)
@@ -259,32 +260,20 @@ vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
 		return (NULL);
 	}
 
-	if (where == VENUM(s))
-		p = ws->s;
-	else if (where == VENUM(f))
-		p = ws->f;
-	else if (where == VENUM(r))
-		p = ws->r;
-	else
-		INCOMPL();
+	l = WS_Dump(ws, *where, off, buf, len);
 
-	if (p == NULL) {
-		VSLb(ctx->vsl, SLT_Error, "workspace_dump: NULL");
-		return (NULL);
-	}
-
-	p += off;
-	if (p >= ws->e) {
-		VSLb(ctx->vsl, SLT_Error, "workspace_dump: off limit");
+	if (l == 0) {
+		switch (errno) {
+		case EINVAL: WRONG(where); break;
+		case EAGAIN: err = "NULL"; break;
+		case EFAULT: err = "off limit"; break;
+		default: err = "unknown error"; break;
+		}
+		VRT_fail(ctx, "workspace_dump: %s", err);
 		return (NULL);
 	}
 
-	l = pdiff(p, ws->e);
-	if (len < l)
-		l = len;
-
 	assert(l < maxlen);
-	memcpy(buf, p, l);
 	p = WS_Copy(ctx->ws, buf, l);
 	if (p == NULL) {
 		VRT_fail(ctx, "workspace_dump: copy failed");


More information about the varnish-commit mailing list