[master] 8c2ed01 Propagate the VCL syntax to VRT_CTX.

Poul-Henning Kamp phk at FreeBSD.org
Tue Feb 6 09:34:13 UTC 2018


commit 8c2ed01282a157eaf776e3d1106a93f8d850f5ea
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Feb 6 09:33:09 2018 +0000

    Propagate the VCL syntax to VRT_CTX.
    
    More work on vcl4.1 reserved words etc.

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index aa11668..ef05b9c 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -106,6 +106,8 @@ static uintptr_t ws_snapshot_cli;
 static struct vrt_ctx *
 vcl_get_ctx(unsigned method, int msg)
 {
+
+	ASSERT_CLI();
 	AZ(ctx_cli.handling);
 	INIT_OBJ(&ctx_cli, VRT_CTX_MAGIC);
 	handling_cli = 0;
@@ -124,6 +126,8 @@ vcl_get_ctx(unsigned method, int msg)
 static void
 vcl_rel_ctx(struct vrt_ctx **ctx)
 {
+
+	ASSERT_CLI();
 	assert(*ctx == &ctx_cli);
 	AN((*ctx)->handling);
 	if (ctx_cli.msg)
@@ -877,6 +881,7 @@ VCL_Poll(void)
 		if (vcl->temp == VCL_TEMP_BUSY ||
 		    vcl->temp == VCL_TEMP_COOLING) {
 			ctx->vcl = vcl;
+			ctx->syntax = ctx->vcl->conf->syntax;
 			ctx->method = 0;
 			(void)vcl_set_state(ctx, "0");
 		}
@@ -887,6 +892,7 @@ VCL_Poll(void)
 			VTAILQ_REMOVE(&vcl_head, vcl, list);
 			ctx->method = VCL_MET_FINI;
 			ctx->vcl = vcl;
+			ctx->syntax = ctx->vcl->conf->syntax;
 			AZ(vcl_send_event(ctx, VCL_EVENT_DISCARD));
 			vcl_KillBackends(vcl);
 			free(vcl->loaded_name);
@@ -1132,6 +1138,7 @@ vcl_call_method(struct worker *wrk, struct req *req, struct busyobj *bo,
 		ctx.ws = bo->ws;
 	}
 	assert(ctx.now != 0);
+	ctx.syntax = ctx.vcl->conf->syntax;
 	ctx.vsl = vsl;
 	ctx.specific = specific;
 	ctx.method = method;
diff --git a/bin/varnishtest/tests/v00020.vtc b/bin/varnishtest/tests/v00020.vtc
index dbfa07a..39defaa 100644
--- a/bin/varnishtest/tests/v00020.vtc
+++ b/bin/varnishtest/tests/v00020.vtc
@@ -203,7 +203,7 @@ varnish v1 -errvcl {Name of function, 'foo.bar', contains illegal character '.'}
 	}
 }
 
-varnish v1 -errvcl {VCL sub's named 'vcl*' are reserved names.} {
+varnish v1 -errvcl {The names 'vcl*' are reserved for methods.} {
 	sub vcl_bar {
 	}
 	sub vcl_recv {
diff --git a/bin/varnishtest/tests/v00021.vtc b/bin/varnishtest/tests/v00021.vtc
index 7db8afd..8394ce8 100644
--- a/bin/varnishtest/tests/v00021.vtc
+++ b/bin/varnishtest/tests/v00021.vtc
@@ -95,3 +95,29 @@ varnish v1 -errvcl {Unknown variable: 'req.foobar'} {
 	backend foo { .host = "127.0.0.1"; }
 	sub vcl_recv { set req.foobar = 3; }
 }
+
+varnish v1 -errvcl {Symbol 'anacl' has wrong type (acl):} {
+	sub vcl_recv {
+		if (client.ip ~ anacl) { }
+	}
+	sub anacl { }
+}
+
+varnish v1 -errvcl {Symbols named 'vcl_*' are reserved.} {
+	sub vcl_recv {
+		if (client.ip ~ vcl_foo) { }
+	}
+}
+
+varnish v1 -errvcl {Symbol 'true' has wrong type (func):} {
+	sub vcl_recv {
+		if (client.ip ~ true) { }
+	}
+}
+
+varnish v1 -errvcl {Symbol 'default' is a reserved word.} {
+	sub vcl_recv {
+		if (client.ip ~ default) { }
+	}
+}
+
diff --git a/include/vrt.h b/include/vrt.h
index 662399d..c2dab1e 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -162,8 +162,10 @@ struct vrt_ctx {
 	unsigned			magic;
 #define VRT_CTX_MAGIC			0x6bb8f0db
 
+	unsigned			syntax;
 	unsigned			method;
 	unsigned			*handling;
+	unsigned			vclver;
 
 	struct vsb			*msg;	// Only in ...init()
 	struct vsl_log			*vsl;
diff --git a/lib/libvcc/generate.py b/lib/libvcc/generate.py
index 6ae254d..b75ae57 100755
--- a/lib/libvcc/generate.py
+++ b/lib/libvcc/generate.py
@@ -1179,6 +1179,7 @@ struct VCL_conf {
 	unsigned			magic;
 #define VCL_CONF_MAGIC			0x7406c509	/* from /dev/random */
 
+	unsigned			syntax;
 	struct director			**default_director;
 	const struct vrt_backend_probe	*default_probe;
 	unsigned			nref;
diff --git a/lib/libvcc/vcc_acl.c b/lib/libvcc/vcc_acl.c
index ba45ee2..d3536ab 100644
--- a/lib/libvcc/vcc_acl.c
+++ b/lib/libvcc/vcc_acl.c
@@ -470,7 +470,6 @@ vcc_ParseAcl(struct vcc *tl)
 	vcc_ExpectVid(tl, "ACL");
 	ERRCHK(tl);
 	sym = VCC_HandleSymbol(tl, ACL, ACL_SYMBOL_PREFIX);
-
 	ERRCHK(tl);
 	AN(sym);
 
diff --git a/lib/libvcc/vcc_compile.c b/lib/libvcc/vcc_compile.c
index 3f9393b..55bcd7c 100644
--- a/lib/libvcc/vcc_compile.c
+++ b/lib/libvcc/vcc_compile.c
@@ -414,6 +414,7 @@ EmitStruct(const struct vcc *tl)
 {
 	Fc(tl, 0, "\nconst struct VCL_conf VCL_conf = {\n");
 	Fc(tl, 0, "\t.magic = VCL_CONF_MAGIC,\n");
+	Fc(tl, 0, "\t.syntax = %u,\n", tl->syntax);
 	Fc(tl, 0, "\t.event_vcl = VGC_Event,\n");
 	Fc(tl, 0, "\t.default_director = &%s,\n", tl->default_director);
 	if (tl->default_probe != NULL)
@@ -561,6 +562,8 @@ vcc_CompileSource(struct vcc *tl, struct source *sp)
 	Fh(tl, 0, "/* ---===### VCC generated .h code ###===---*/\n");
 	Fc(tl, 0, "\n/* ---===### VCC generated .c code ###===---*/\n");
 
+	vcc_Parse_Init(tl);
+
 	vcc_Expr_Init(tl);
 
 	vcc_Action_Init(tl);
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index 776a52f..d3f6b7a 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -294,7 +294,8 @@ struct symbol *VCC_HandleSymbol(struct vcc *, vcc_type_t , const char *);
 void vcc_Var_Init(struct vcc *);
 
 /* vcc_parse.c */
-void vcc_Parse(struct vcc *tl);
+void vcc_Parse(struct vcc *);
+void vcc_Parse_Init(struct vcc *);
 sym_act_f vcc_Act_If;
 
 /* vcc_utils.c */
diff --git a/lib/libvcc/vcc_parse.c b/lib/libvcc/vcc_parse.c
index e3e69cb..0cd13cc 100644
--- a/lib/libvcc/vcc_parse.c
+++ b/lib/libvcc/vcc_parse.c
@@ -220,6 +220,7 @@ vcc_ParseFunction(struct vcc *tl)
 
 	t = tl->t;
 	sym = VCC_SymbolGet(tl, SYM_SUB, SYMTAB_CREATE, XREF_DEF);
+	ERRCHK(tl);
 	AN(sym);
 	p = sym->proc;
 	if (p == NULL) {
@@ -227,7 +228,7 @@ vcc_ParseFunction(struct vcc *tl)
 		    (t->b[1] == 'c'|| t->b[1] == 'C') &&
 		    (t->b[2] == 'l'|| t->b[2] == 'L')) {
 			VSB_printf(tl->sb,
-			    "VCL sub's named 'vcl*' are reserved names.\n");
+			    "The names 'vcl*' are reserved for methods.\n");
 			vcc_ErrWhere(tl, t);
 			VSB_printf(tl->sb, "Valid vcl_* methods are:\n");
 			VTAILQ_FOREACH(p, &tl->procs, list) {
@@ -343,6 +344,7 @@ static struct toplev {
 	{ "probe",		vcc_ParseProbe },
 	{ "import",		vcc_ParseImport },
 	{ "vcl",		vcc_ParseVcl },
+	{ "default",		NULL },
 	{ NULL, NULL }
 };
 
@@ -392,6 +394,8 @@ vcc_Parse(struct vcc *tl)
 			break;
 		case ID:
 			for (tp = toplev; tp->name != NULL; tp++) {
+				if (tp->func == NULL)
+					continue;
 				if (!vcc_IdIs(tl->t, tp->name))
 					continue;
 				tp->func(tl);
@@ -419,3 +423,12 @@ vcc_Parse(struct vcc *tl)
 	}
 	AZ(tl->indent);
 }
+
+void
+vcc_Parse_Init(struct vcc *tl)
+{
+	struct toplev *tp;
+
+	for (tp = toplev; tp->name != NULL; tp++)
+		VCC_MkSym(tl, tp->name, SYM_NONE);
+}
diff --git a/lib/libvcc/vcc_symb.c b/lib/libvcc/vcc_symb.c
index d2cd1bc..1a39ea7 100644
--- a/lib/libvcc/vcc_symb.c
+++ b/lib/libvcc/vcc_symb.c
@@ -200,6 +200,16 @@ VCC_SymbolGet(struct vcc *tl, enum symkind kind, const char *e, const char *x)
 	struct symbol *sym;
 
 	AN(e);
+	if (tl->syntax >= 41 && e == SYMTAB_CREATE && kind != SYM_SUB &&
+	    (tl->t->b[0] == 'v'|| tl->t->b[0] == 'V') &&
+	    (tl->t->b[1] == 'c'|| tl->t->b[1] == 'C') &&
+	    (tl->t->b[2] == 'l'|| tl->t->b[2] == 'L')) {
+		VSB_printf(tl->sb,
+		    "Symbols named 'vcl_*' are reserved.\nAt:");
+		vcc_ErrWhere(tl, tl->t);
+		return (NULL);
+	}
+
 	sym = VCC_Symbol(tl, NULL, tl->t->b, tl->t->e, kind,
 	    e == SYMTAB_CREATE ? 1 : 0);
 	if (sym == NULL && e == SYMTAB_NOERR)
@@ -214,8 +224,11 @@ VCC_SymbolGet(struct vcc *tl, enum symkind kind, const char *e, const char *x)
 	if (kind != SYM_NONE && kind != sym->kind) {
 		VSB_printf(tl->sb, "Symbol ");
 		vcc_ErrToken(tl, tl->t);
-		VSB_printf(tl->sb, " has wrong type (%s): ",
-			VCC_SymKind(tl, sym));
+		if (sym->kind == SYM_NONE)
+			VSB_printf(tl->sb, " is a reserved word.");
+		else
+			VSB_printf(tl->sb, " has wrong type (%s): ",
+				VCC_SymKind(tl, sym));
 		VSB_cat(tl->sb, "\nAt: ");
 		vcc_ErrWhere(tl, tl->t);
 		if (sym->def_b != NULL) {


More information about the varnish-commit mailing list