[master] 011944d Change the C-symbols a VMOD exports to contain the vmod name, to eliminate any chance of collissions.

Poul-Henning Kamp phk at varnish-cache.org
Wed Mar 6 21:53:30 CET 2013


commit 011944d3575cefb726cc341458a9def4df4fb3cf
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Mar 6 20:52:30 2013 +0000

    Change the C-symbols a VMOD exports to contain the vmod name,
    to eliminate any chance of collissions.
    
    Check the ABI.  This check may be too strong, but at least its safe.

diff --git a/bin/varnishd/cache/cache_vrt_vmod.c b/bin/varnishd/cache/cache_vrt_vmod.c
index 93d82f9..3a90a4d 100644
--- a/bin/varnishd/cache/cache_vrt_vmod.c
+++ b/bin/varnishd/cache/cache_vrt_vmod.c
@@ -32,12 +32,14 @@
 #include "config.h"
 
 #include <dlfcn.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 #include "cache.h"
 
 #include "vcli_priv.h"
 #include "vrt.h"
+#include "vmod_abi.h"
 
 /*--------------------------------------------------------------------
  * Modules stuff
@@ -56,7 +58,6 @@ struct vmod {
 	void			*hdl;
 	const void		*funcs;
 	int			funclen;
-	const void		*idptr;
 };
 
 static VTAILQ_HEAD(,vmod)	vmods = VTAILQ_HEAD_INITIALIZER(vmods);
@@ -67,6 +68,7 @@ VRT_Vmod_Init(void **hdl, void *ptr, int len, const char *nm,
 {
 	struct vmod *v;
 	void *x, *y, *z, *w;
+	char buf[256];
 
 	ASSERT_CLI();
 
@@ -86,10 +88,14 @@ VRT_Vmod_Init(void **hdl, void *ptr, int len, const char *nm,
 			return (1);
 		}
 
-		x = dlsym(v->hdl, "Vmod_Name");
-		y = dlsym(v->hdl, "Vmod_Len");
-		z = dlsym(v->hdl, "Vmod_Func");
-		w = dlsym(v->hdl, "Vmod_Id");
+		bprintf(buf, "Vmod_%s_Name", nm);
+		x = dlsym(v->hdl, buf);
+		bprintf(buf, "Vmod_%s_Len", nm);
+		y = dlsym(v->hdl, buf);
+		bprintf(buf, "Vmod_%s_Func", nm);
+		z = dlsym(v->hdl, buf);
+		bprintf(buf, "Vmod_%s_ABI", nm);
+		w = dlsym(v->hdl, buf);
 		if (x == NULL || y == NULL || z == NULL || w == NULL) {
 			VCLI_Out(cli, "Loading VMOD %s from %s:\n", nm, path);
 			VCLI_Out(cli, "VMOD symbols not found\n");
@@ -112,6 +118,18 @@ VRT_Vmod_Init(void **hdl, void *ptr, int len, const char *nm,
 			return (1);
 		}
 
+		if (strcmp(w, VMOD_ABI_Version)) {
+			VCLI_Out(cli, "Loading VMOD %s from %s:\n", nm, path);
+			VCLI_Out(cli, "VMOD ABI (%s)", w);
+			VCLI_Out(cli, " incompatible with varnish ABI (%s)\n",
+			    VMOD_ABI_Version);
+			(void)dlclose(v->hdl);
+			FREE_OBJ(v);
+			return (1);
+		}
+
+		// XXX: Check w for ABI version compatibility
+
 		v->funclen = *(const int *)y;
 		v->funcs = z;
 
@@ -120,7 +138,6 @@ VRT_Vmod_Init(void **hdl, void *ptr, int len, const char *nm,
 
 		VSC_C_main->vmods++;
 		VTAILQ_INSERT_TAIL(&vmods, v, list);
-		v->idptr = w;
 	}
 
 	assert(len == v->funclen);
diff --git a/bin/varnishd/flint.lnt b/bin/varnishd/flint.lnt
index 0d810d2..f12f128 100644
--- a/bin/varnishd/flint.lnt
+++ b/bin/varnishd/flint.lnt
@@ -56,19 +56,19 @@
 -efunc(838, VRT_purge_string)
 
 // Stuff in VMODs which is used through dl*(3) functions
--esym(754, Vmod_Func*)
--esym(765, Vmod_Func)
--esym(552, Vmod_Func)
--esym(765, Vmod_Len)
--esym(714, Vmod_Len)
--esym(765, Vmod_Name)
--esym(714, Vmod_Name)
--esym(765, Vmod_Proto)
--esym(714, Vmod_Proto)
--esym(765, Vmod_Spec)
--esym(714, Vmod_Spec)
--esym(765, Vmod_Varnish_ABI)
--esym(714, Vmod_Varnish_ABI)
+-esym(754, Vmod_*_Func::*)
+-esym(765, Vmod_*_Func)
+-esym(552, Vmod_*_Func)
+-esym(765, Vmod_*_Len)
+-esym(714, Vmod_*_Len)
+-esym(765, Vmod_*_Name)
+-esym(714, Vmod_*_Name)
+-esym(765, Vmod_*_Proto)
+-esym(714, Vmod_*_Proto)
+-esym(765, Vmod_*_Spec)
+-esym(714, Vmod_*_Spec)
+-esym(765, Vmod_*_ABI)
+-esym(714, Vmod_*_ABI)
 
 
 //-sem (pthread_mutex_lock, thread_lock)
@@ -159,16 +159,6 @@
 
 -e441	//  for clause irregularity: loop variable '___' not found in 2nd for expression
 
-// Vmod instantiation symbols are defined in all vmods
-
--esym(14, Vmod_Name)
--esym(14, Vmod_Func)
--esym(14, Vmod_Len)
--esym(14, Vmod_Proto)
--esym(14, Vmod_Spec)
--esym(14, Vmod_Varnish_ABI)
--esym(14, Vmod_Id)
-
 // Review all below this line ///////////////////////////////////////////////
 
 -e732   // 183 Loss of sign (___) (___ to ___)
diff --git a/lib/libvcl/vcc_parse.c b/lib/libvcl/vcc_parse.c
index f656cbd..af97241 100644
--- a/lib/libvcl/vcc_parse.c
+++ b/lib/libvcl/vcc_parse.c
@@ -111,7 +111,6 @@ vcc_IfStmt(struct vcc *tl)
 			ERRCHK(tl);
 			L(tl, vcc_Compound(tl));
 			ERRCHK(tl);
-			
 		} else if (vcc_IdIs(tl->t, "elseif") ||
 		     vcc_IdIs(tl->t, "elsif") ||
 		     vcc_IdIs(tl->t, "elif")) {
diff --git a/lib/libvcl/vcc_vmod.c b/lib/libvcl/vcc_vmod.c
index 874c899..23a3171 100644
--- a/lib/libvcl/vcc_vmod.c
+++ b/lib/libvcl/vcc_vmod.c
@@ -41,6 +41,7 @@ vcc_ParseImport(struct vcc *tl)
 {
 	void *hdl;
 	char fn[1024];
+	char buf[256];
 	struct token *mod, *t1;
 	const char *modname;
 	const char *proto;
@@ -106,8 +107,8 @@ vcc_ParseImport(struct vcc *tl)
 	Fh(tl, 0, "static void *VGC_vmod_%.*s;\n", PF(mod));
 
 	Fi(tl, 0, "\tif (VRT_Vmod_Init(&VGC_vmod_%.*s,\n", PF(mod));
-	Fi(tl, 0, "\t    &Vmod_Func_%.*s,\n", PF(mod));
-	Fi(tl, 0, "\t    sizeof(Vmod_Func_%.*s),\n", PF(mod));
+	Fi(tl, 0, "\t    &Vmod_%.*s_Func,\n", PF(mod));
+	Fi(tl, 0, "\t    sizeof(Vmod_%.*s_Func),\n", PF(mod));
 	Fi(tl, 0, "\t    \"%.*s\",\n", PF(mod));
 	Fi(tl, 0, "\t    ");
 	EncString(tl->fi, fn, NULL, 0);
@@ -125,7 +126,8 @@ vcc_ParseImport(struct vcc *tl)
 		return;
 	}
 
-	modname = dlsym(hdl, "Vmod_Name");
+	bprintf(buf, "Vmod_%.*s_Name", PF(mod));
+	modname = dlsym(hdl, buf);
 	if (modname == NULL) {
 		VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n",
 		    PF(mod), fn, "Symbol Vmod_Name not found");
@@ -140,7 +142,8 @@ vcc_ParseImport(struct vcc *tl)
 		return;
 	}
 
-	abi = dlsym(hdl, "Vmod_Varnish_ABI");
+	bprintf(buf, "Vmod_%.*s_ABI", PF(mod));
+	abi = dlsym(hdl, buf);
 	if (abi == NULL || strcmp(abi, VMOD_ABI_Version) != 0) {
 		VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n",
 		    PF(mod), fn);
@@ -150,14 +153,16 @@ vcc_ParseImport(struct vcc *tl)
 		return;
 	}
 
-	proto = dlsym(hdl, "Vmod_Proto");
+	bprintf(buf, "Vmod_%.*s_Proto", PF(mod));
+	proto = dlsym(hdl, buf);
 	if (proto == NULL) {
 		VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n",
 		    PF(mod), fn, "Symbol Vmod_Proto not found");
 		vcc_ErrWhere(tl, mod);
 		return;
 	}
-	spec = dlsym(hdl, "Vmod_Spec");
+	bprintf(buf, "Vmod_%.*s_Spec", PF(mod));
+	spec = dlsym(hdl, buf);
 	if (spec == NULL) {
 		VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n",
 		    PF(mod), fn, "Symbol Vmod_Spec not found");
diff --git a/lib/libvcl/vmodtool.py b/lib/libvcl/vmodtool.py
index f20362f..0a8bb12 100755
--- a/lib/libvcl/vmodtool.py
+++ b/lib/libvcl/vmodtool.py
@@ -125,8 +125,8 @@ class vmod(object):
 			fo.write("int " + self.init)
 			fo.write(
 			    "(struct vmod_priv *, const struct VCL_conf *);\n")
-		fo.write("\n")
-		fo.write("extern const void * const Vmod_Id;\n")
+		#fo.write("\n")
+		#fo.write("extern const void * const Vmod_" + self.nam + "_Id;\n")
 
 	def c_typedefs_(self):
 		l = list()
@@ -145,33 +145,36 @@ class vmod(object):
 			fo.write(i + "\n")
 
 	def c_vmod(self, fo):
-		fo.write('const char Vmod_Name[] = \"' + self.nam + '";\n')
+		fo.write('const char Vmod_' + self.nam + '_Name[] =')
+		fo.write(' \"' + self.nam + '";\n')
 		fo.write("\n")
 
 		cs = self.c_struct()
-		fo.write("const " + cs + ' Vmod_Func = ')
+		fo.write("const " + cs + ' Vmod_' + self.nam + '_Func = ')
 		fo.write(self.c_initializer())
 		fo.write("\n")
 
 		fo.write("\n")
-		fo.write("const int Vmod_Len = sizeof(Vmod_Func);\n")
+		fo.write("const int Vmod_" + self.nam + '_Len =')
+		fo.write(" sizeof(Vmod_" + self.nam + "_Func);\n")
 		fo.write("\n")
 
-
-		fo.write("const char Vmod_Proto[] =\n")
+		fo.write("const char Vmod_" + self.nam + "_Proto[] =\n")
 		for t in self.c_typedefs_():
 			fo.write('\t"' + t + '\\n"\n')
 		fo.write('\t"\\n"\n')
-		for i in (cs + " Vmod_Func_" + self.nam + ';').split("\n"):
+		for i in (cs + " Vmod_" + self.nam + '_Func;').split("\n"):
 			fo.write('\n\t"' + i + '\\n"')
 		fo.write(";\n\n")
 
 		fo.write(self.c_strspec())
 
 		fo.write("\n")
-		fo.write('const char Vmod_Varnish_ABI[] = VMOD_ABI_Version;\n')
-		fo.write("\n")
-		fo.write('const void * const Vmod_Id = &Vmod_Id;\n')
+		fo.write('const char Vmod_' + self.nam + '_ABI[] =')
+		fo.write(' VMOD_ABI_Version;\n')
+		#fo.write("\n")
+		#fo.write('const void * const Vmod_' + self.nam + '_Id =')
+		#fo.write(' &Vmod_' + self.nam + '_Id;\n')
 
 	def c_initializer(self):
 		s = '{\n'
@@ -190,7 +193,7 @@ class vmod(object):
 		return s
 
 	def c_struct(self):
-		s = 'struct Vmod_Func_' + self.nam + ' {\n'
+		s = 'struct Vmod_' + self.nam + '_Func {\n'
 		for o in self.objs:
 			s += o.c_struct(self.nam)
 
@@ -205,7 +208,7 @@ class vmod(object):
 		return s
 
 	def c_strspec(self):
-		s = "const char * const Vmod_Spec[] = {\n"
+		s = "const char * const Vmod_" + self.nam + "_Spec[] = {\n"
 
 		for o in self.objs:
 			s += o.c_strspec(self.nam)
@@ -216,7 +219,7 @@ class vmod(object):
 
 		s += "\n\t/* Init/Fini */\n"
 		if self.init != None:
-			s += '\t"INIT\\0Vmod_Func_' + self.nam + '._init",\n'
+			s += '\t"INIT\\0Vmod_' + self.nam + '_Func._init",\n'
 
 		s += "\t0\n"
 		s += "};\n"
@@ -280,7 +283,7 @@ class func(object):
 	def c_strspec(self, modnam):
 		s = modnam + "." + self.nam
 		s += "\\0"
-		s += "Vmod_Func_" + modnam + "." + self.cnam + "\\0"
+		s += "Vmod_" + modnam + "_Func." + self.cnam + "\\0"
 		s += self.retval + "\\0"
 		for a in self.al:
 			s += a.c_strspec()



More information about the varnish-commit mailing list