1 /*-
2 * Copyright (c) 2010-2015 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
31 #include "config.h"
32
33 #include <dlfcn.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "vcc_compile.h"
38
39 #include "libvcc.h"
40 #include "vfil.h"
41 #include "vjsn.h"
42 #include "vmod_abi.h"
43 #include "vsb.h"
44
45 struct vmod_open {
46 unsigned magic;
47 #define VMOD_OPEN_MAGIC 0x9995b1f3
48 void *hdl;
49 const char *err;
50 };
51
52 struct vmod_obj {
53 unsigned magic;
54 #define VMOD_OBJ_MAGIC 0x349885f8
55 char *name;
56 struct type type[1];
57 VTAILQ_ENTRY(vmod_obj) list;
58 };
59
60 static int
vcc_path_dlopen(void * priv,const char * fn)61 vcc_path_dlopen(void *priv, const char *fn)
62 {
63 struct vmod_open *vop;
64
65 CAST_OBJ_NOTNULL(vop, priv, VMOD_OPEN_MAGIC);
66 AN(fn);
67
68 vop->hdl = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
69 if (vop->hdl == NULL) {
70 vop->err = dlerror();
71 return (-1);
72 }
73 return (0);
74 }
75
76 static void vcc_VmodObject(struct vcc *tl, struct symbol *sym);
77 static void vcc_VmodSymbols(struct vcc *tl, const struct symbol *sym);
78
79 static void
func_sym(struct vcc * tl,vcc_kind_t kind,const struct symbol * psym,const struct vjsn_val * v)80 func_sym(struct vcc *tl, vcc_kind_t kind, const struct symbol *psym,
81 const struct vjsn_val *v)
82 {
83 struct symbol *sym;
84 struct vsb *buf;
85
86 buf = VSB_new_auto();
87 AN(buf);
88
89 VSB_clear(buf);
90 VCC_SymName(buf, psym);
91 VSB_printf(buf, ".%s", v->value);
92 AZ(VSB_finish(buf));
93 sym = VCC_MkSym(tl, VSB_data(buf), SYM_MAIN, kind, VCL_LOW, VCL_HIGH);
94 AN(sym);
95 VSB_destroy(&buf);
96
97 if (kind == SYM_OBJECT) {
98 sym->eval_priv = v;
99 sym->vmod_name = psym->vmod_name;
100 sym->r_methods = VCL_MET_INIT;
101 vcc_VmodObject(tl, sym);
102 vcc_VmodSymbols(tl, sym);
103 return;
104 }
105
106 if (kind == SYM_METHOD)
107 sym->extra = psym->rname;
108
109 v = VTAILQ_NEXT(v, list);
110
111 assert(v->type == VJSN_ARRAY);
112 sym->action = vcc_Act_Call;
113 sym->vmod_name = psym->vmod_name;
114 sym->eval = vcc_Eval_SymFunc;
115 sym->eval_priv = v;
116 v = VTAILQ_FIRST(&v->children);
117 assert(v->type == VJSN_ARRAY);
118 v = VTAILQ_FIRST(&v->children);
119 assert(v->type == VJSN_STRING);
120 sym->type = VCC_Type(v->value);
121 AN(sym->type);
122 sym->r_methods = VCL_MET_TASK_ALL;
123 }
124
125 static void
vcc_json_always(struct vcc * tl,const struct vjsn * vj,const char * vmod_name)126 vcc_json_always(struct vcc *tl, const struct vjsn *vj, const char *vmod_name)
127 {
128 struct inifin *ifp;
129 const struct vjsn_val *vv, *vv2;
130 double vmod_syntax = 0.0;
131
132 AN(vj);
133 AN(vmod_name);
134 ifp = NULL;
135
136 VTAILQ_FOREACH(vv, &vj->value->children, list) {
137 assert(vv->type == VJSN_ARRAY);
138 vv2 = VTAILQ_FIRST(&vv->children);
139 assert(vv2->type == VJSN_STRING);
140 if (!strcmp(vv2->value, "$VMOD")) {
141 vmod_syntax =
142 strtod(VTAILQ_NEXT(vv2, list)->value, NULL);
143 continue;
144 }
145 assert (vmod_syntax == 1.0);
146 if (!strcmp(vv2->value, "$EVENT")) {
147 /* XXX: What about the rest of the events ? */
148 if (ifp == NULL)
149 ifp = New_IniFin(tl);
150 vv2 = VTAILQ_NEXT(vv2, list);
151 VSB_printf(ifp->ini,
152 "\tif (%s(ctx, &vmod_priv_%s, VCL_EVENT_LOAD))\n"
153 "\t\treturn(1);",
154 vv2->value, vmod_name);
155 VSB_printf(ifp->fin,
156 "\t\t(void)%s(ctx, &vmod_priv_%s,\n"
157 "\t\t\t VCL_EVENT_DISCARD);",
158 vv2->value, vmod_name);
159 VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%s, ev)",
160 vv2->value, vmod_name);
161 } else if (!strcmp(vv2->value, "$FUNC")) {
162 } else if (!strcmp(vv2->value, "$OBJ")) {
163 } else {
164 VTAILQ_FOREACH(vv2, &vv->children, list)
165 fprintf(stderr, "\tt %s n %s v %s\n",
166 vv2->type, vv2->name, vv2->value);
167 WRONG("Vmod JSON syntax error");
168 }
169 }
170 }
171
172 static const struct vmod_data *
vcc_VmodSanity(struct vcc * tl,void * hdl,const struct token * mod,char * fnp)173 vcc_VmodSanity(struct vcc *tl, void *hdl, const struct token *mod, char *fnp)
174 {
175 char buf[256];
176 const struct vmod_data *vmd;
177
178 bprintf(buf, "Vmod_%.*s_Data", PF(mod));
179 vmd = dlsym(hdl, buf);
180 if (vmd == NULL) {
181 VSB_printf(tl->sb, "Malformed VMOD %.*s\n", PF(mod));
182 VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
183 VSB_cat(tl->sb, "\t(no Vmod_Data symbol)\n");
184 vcc_ErrWhere(tl, mod);
185 return (NULL);
186 }
187 if (vmd->vrt_major == 0 && vmd->vrt_minor == 0 &&
188 (vmd->abi == NULL || strcmp(vmd->abi, VMOD_ABI_Version) != 0)) {
189 VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod));
190 VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
191 VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n",
192 VMOD_ABI_Version, vmd->abi);
193 vcc_ErrWhere(tl, mod);
194 return (NULL);
195 }
196 if (vmd->vrt_major != 0 && (vmd->vrt_major != VRT_MAJOR_VERSION ||
197 vmd->vrt_minor > VRT_MINOR_VERSION)) {
198 VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod));
199 VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
200 VSB_printf(tl->sb, "\tVMOD wants ABI version %u.%u\n",
201 vmd->vrt_major, vmd->vrt_minor);
202 VSB_printf(tl->sb, "\tvarnishd provides ABI version %u.%u\n",
203 VRT_MAJOR_VERSION, VRT_MINOR_VERSION);
204 vcc_ErrWhere(tl, mod);
205 return (NULL);
206 }
207 if (vmd->name == NULL ||
208 vmd->func == NULL ||
209 vmd->func_len <= 0 ||
210 vmd->proto == NULL ||
211 vmd->abi == NULL) {
212 VSB_printf(tl->sb, "Mangled VMOD %.*s\n", PF(mod));
213 VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
214 VSB_cat(tl->sb, "\tInconsistent metadata\n");
215 vcc_ErrWhere(tl, mod);
216 return (NULL);
217 }
218 if (!vcc_IdIs(mod, vmd->name)) {
219 VSB_printf(tl->sb, "Wrong file for VMOD %.*s\n", PF(mod));
220 VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
221 VSB_printf(tl->sb, "\tContains vmod \"%s\"\n", vmd->name);
222 vcc_ErrWhere(tl, mod);
223 return (NULL);
224 }
225 return (vmd);
226 }
227
228 static vcc_kind_t
vcc_vmod_kind(const char * type)229 vcc_vmod_kind(const char *type)
230 {
231
232 #define VMOD_KIND(str, kind) \
233 do { \
234 if (!strcmp(str, type)) \
235 return (kind); \
236 } while (0)
237 VMOD_KIND("$OBJ", SYM_OBJECT);
238 VMOD_KIND("$METHOD", SYM_METHOD);
239 VMOD_KIND("$FUNC", SYM_FUNC);
240 #undef VMOD_KIND
241 return (SYM_NONE);
242 }
243
244 static void
vcc_VmodObject(struct vcc * tl,struct symbol * sym)245 vcc_VmodObject(struct vcc *tl, struct symbol *sym)
246 {
247 struct vmod_obj *obj;
248 struct vsb *buf;
249
250 buf = VSB_new_auto();
251 AN(buf);
252
253 VSB_printf(buf, "%s.%s", sym->vmod_name, sym->name);
254 AZ(VSB_finish(buf));
255
256 ALLOC_OBJ(obj, VMOD_OBJ_MAGIC);
257 AN(obj);
258 REPLACE(obj->name, VSB_data(buf));
259
260 INIT_OBJ(obj->type, TYPE_MAGIC);
261 obj->type->name = obj->name;
262 sym->type = obj->type;
263 VTAILQ_INSERT_TAIL(&tl->vmod_objects, obj, list);
264 VSB_destroy(&buf);
265 }
266
267 static void
vcc_VmodSymbols(struct vcc * tl,const struct symbol * sym)268 vcc_VmodSymbols(struct vcc *tl, const struct symbol *sym)
269 {
270 const struct vjsn *vj;
271 const struct vjsn_val *vv, *vv1, *vv2;
272 vcc_kind_t kind;
273
274 if (sym->kind == SYM_VMOD) {
275 CAST_OBJ_NOTNULL(vj, sym->eval_priv, VJSN_MAGIC);
276 vv = VTAILQ_FIRST(&vj->value->children);
277 } else if (sym->kind == SYM_OBJECT) {
278 CAST_OBJ_NOTNULL(vv, sym->eval_priv, VJSN_VAL_MAGIC);
279 } else {
280 WRONG("symbol kind");
281 }
282
283 for (; vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
284 if (vv->type != VJSN_ARRAY)
285 continue;
286 vv1 = VTAILQ_FIRST(&vv->children);
287 assert(vv1->type == VJSN_STRING);
288 vv2 = VTAILQ_NEXT(vv1, list);
289 if (vv2->type != VJSN_STRING)
290 continue;
291
292 kind = vcc_vmod_kind(vv1->value);
293 if (kind == SYM_NONE)
294 continue;
295
296 func_sym(tl, kind, sym, vv2);
297 }
298 }
299
300 void
vcc_ParseImport(struct vcc * tl)301 vcc_ParseImport(struct vcc *tl)
302 {
303 char fn[1024], *fnpx;
304 const char *p;
305 struct token *mod, *tmod, *t1;
306 struct inifin *ifp;
307 struct symbol *msym, *vsym;
308 const struct vmod_data *vmd;
309 struct vjsn *vj;
310 struct vmod_open vop[1];
311
312 INIT_OBJ(vop, VMOD_OPEN_MAGIC);
313 t1 = tl->t;
314 SkipToken(tl, ID); /* "import" */
315
316 ExpectErr(tl, ID); /* "vmod_name" */
317 mod = tl->t;
318 tmod = VTAILQ_NEXT(mod, list);
319 if (tmod->tok == ID && vcc_IdIs(tmod, "as")) {
320 vcc_NextToken(tl); /* "vmod_name" */
321 vcc_NextToken(tl); /* "as" */
322 ExpectErr(tl, ID); /* "vcl_name" */
323 }
324 tmod = tl->t;
325
326 msym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VMOD, SYMTAB_CREATE, XREF_NONE);
327 ERRCHK(tl);
328 AN(msym);
329
330 if (tl->t->tok == ID) {
331 if (!vcc_IdIs(tl->t, "from")) {
332 VSB_cat(tl->sb, "Expected 'from path ...'\n");
333 vcc_ErrWhere(tl, tl->t);
334 return;
335 }
336 vcc_NextToken(tl);
337 if (!tl->unsafe_path && strchr(tl->t->dec, '/')) {
338 VSB_cat(tl->sb,
339 "'import ... from path ...' is unsafe.\nAt:");
340 vcc_ErrToken(tl, tl->t);
341 vcc_ErrWhere(tl, tl->t);
342 return;
343 }
344 ExpectErr(tl, CSTR);
345 p = strrchr(tl->t->dec, '/');
346 if (p != NULL && p[1] == '\0')
347 bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod));
348 else
349 bprintf(fn, "%s", tl->t->dec);
350 vcc_NextToken(tl);
351 } else {
352 bprintf(fn, "libvmod_%.*s.so", PF(mod));
353 }
354
355 SkipToken(tl, ';');
356
357 if (VFIL_searchpath(tl->vmod_path, vcc_path_dlopen, vop, fn, &fnpx)) {
358 if (vop->err == NULL) {
359 VSB_printf(tl->sb,
360 "Could not find VMOD %.*s\n", PF(mod));
361 } else {
362 VSB_printf(tl->sb,
363 "Could not open VMOD %.*s\n", PF(mod));
364 VSB_printf(tl->sb, "\tFile name: %s\n",
365 fnpx != NULL ? fnpx : fn);
366 VSB_printf(tl->sb, "\tdlerror: %s\n", vop->err);
367 }
368 vcc_ErrWhere(tl, mod);
369 free(fnpx);
370 return;
371 }
372
373 vmd = vcc_VmodSanity(tl, vop->hdl, mod, fnpx);
374 if (vmd == NULL || tl->err) {
375 AZ(dlclose(vop->hdl));
376 free(fnpx);
377 return;
378 }
379
380 if (msym->extra != NULL) {
381 if (!strcmp(msym->extra, vmd->file_id)) {
382 /* Identical import is OK */
383 } else {
384 VSB_printf(tl->sb,
385 "Another module already imported as %.*s.\n",
386 PF(tmod));
387 vcc_ErrWhere2(tl, t1, tl->t);
388 }
389 AZ(dlclose(vop->hdl));
390 free(fnpx);
391 return;
392 }
393 msym->def_b = t1;
394 msym->def_e = tl->t;
395
396 VTAILQ_FOREACH(vsym, &tl->sym_vmods, sideways) {
397 assert(vsym->kind == SYM_VMOD);
398 if (!strcmp(vsym->extra, vmd->file_id)) {
399 /* Already loaded under different name */
400 msym->eval_priv = vsym->eval_priv;
401 msym->extra = vsym->extra;
402 msym->vmod_name = vsym->vmod_name;
403 vcc_VmodSymbols(tl, msym);
404 AZ(dlclose(vop->hdl));
405 free(fnpx);
406 return;
407 }
408 }
409
410 VTAILQ_INSERT_TAIL(&tl->sym_vmods, msym, sideways);
411
412 ifp = New_IniFin(tl);
413
414 VSB_cat(ifp->ini, "\tif (VPI_Vmod_Init(ctx,\n");
415 VSB_printf(ifp->ini, "\t &VGC_vmod_%.*s,\n", PF(mod));
416 VSB_printf(ifp->ini, "\t %u,\n", tl->vmod_count++);
417 VSB_printf(ifp->ini, "\t &%s,\n", vmd->func_name);
418 VSB_printf(ifp->ini, "\t sizeof(%s),\n", vmd->func_name);
419 VSB_printf(ifp->ini, "\t \"%.*s\",\n", PF(mod));
420 VSB_cat(ifp->ini, "\t ");
421 VSB_quote(ifp->ini, fnpx, -1, VSB_QUOTE_CSTR);
422 VSB_cat(ifp->ini, ",\n");
423 AN(vmd);
424 AN(vmd->file_id);
425 VSB_printf(ifp->ini, "\t \"%s\",\n", vmd->file_id);
426 VSB_printf(ifp->ini, "\t \"./vmod_cache/_vmod_%.*s.%s\"\n",
427 PF(mod), vmd->file_id);
428 VSB_cat(ifp->ini, "\t ))\n");
429 VSB_cat(ifp->ini, "\t\treturn(1);");
430
431 VSB_cat(tl->symtab, ",\n {\n");
432 VSB_cat(tl->symtab, "\t\"dir\": \"import\",\n");
433 VSB_cat(tl->symtab, "\t\"type\": \"$VMOD\",\n");
434 VSB_printf(tl->symtab, "\t\"name\": \"%.*s\",\n", PF(mod));
435 VSB_printf(tl->symtab, "\t\"file\": \"%s\",\n", fnpx);
436 VSB_printf(tl->symtab, "\t\"dst\": \"./vmod_cache/_vmod_%.*s.%s\"\n",
437 PF(mod), vmd->file_id);
438 VSB_cat(tl->symtab, " }");
439
440 /* XXX: zero the function pointer structure ?*/
441 VSB_printf(ifp->fin, "\t\tVRT_priv_fini(ctx, &vmod_priv_%.*s);", PF(mod));
442 VSB_printf(ifp->final, "\t\tVPI_Vmod_Unload(ctx, &VGC_vmod_%.*s);", PF(mod));
443
444 vj = vjsn_parse(vmd->json, &p);
445 XXXAZ(p);
446 AN(vj);
447 msym->eval_priv = vj;
448 msym->extra = TlDup(tl, vmd->file_id);
449 msym->vmod_name = TlDup(tl, vmd->name);
450 vcc_VmodSymbols(tl, msym);
451
452 vcc_json_always(tl, vj, msym->vmod_name);
453
454 Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod));
455 Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod));
456 Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod));
457 Fh(tl, 0, "\n%s\n", vmd->proto);
458 Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod));
459 free(fnpx);
460 }
461
v_matchproto_(sym_act_f)462 void v_matchproto_(sym_act_f)
463 vcc_Act_New(struct vcc *tl, struct token *t, struct symbol *sym)
464 {
465 struct symbol *isym, *osym;
466 struct inifin *ifp;
467 struct vsb *buf;
468 const struct vjsn_val *vv, *vf;
469 int null_ok = 0;
470
471 (void)sym;
472 (void)t;
473
474 ExpectErr(tl, ID);
475 vcc_ExpectVid(tl, "VCL object");
476 ERRCHK(tl);
477 isym = VCC_HandleSymbol(tl, INSTANCE, "vo");
478 ERRCHK(tl);
479 AN(isym);
480 isym->noref = 1;
481 isym->action = vcc_Act_Obj;
482
483 SkipToken(tl, '=');
484 ExpectErr(tl, ID);
485 osym = VCC_SymbolGet(tl, SYM_MAIN, SYM_OBJECT, SYMTAB_EXISTING,
486 XREF_NONE);
487 ERRCHK(tl);
488 AN(osym);
489
490 /* Scratch the generic INSTANCE type */
491 isym->type = osym->type;
492
493 CAST_OBJ_NOTNULL(vv, osym->eval_priv, VJSN_VAL_MAGIC);
494 // vv = object name
495
496 isym->vmod_name = osym->vmod_name;
497 isym->eval_priv = vv;
498
499 vv = VTAILQ_NEXT(vv, list);
500 // vv = flags
501 assert(vv->type == VJSN_OBJECT);
502 VTAILQ_FOREACH(vf, &vv->children, list)
503 if (!strcmp(vf->name, "NULL_OK") && vf->type == VJSN_TRUE)
504 null_ok = 1;
505 if (!null_ok)
506 VTAILQ_INSERT_TAIL(&tl->sym_objects, isym, sideways);
507
508 vv = VTAILQ_NEXT(vv, list);
509 // vv = struct name
510
511 Fh(tl, 0, "static %s *%s;\n\n", vv->value, isym->rname);
512 vv = VTAILQ_NEXT(vv, list);
513
514 vf = VTAILQ_FIRST(&vv->children);
515 vv = VTAILQ_NEXT(vv, list);
516 assert(vf->type == VJSN_STRING);
517 assert(!strcmp(vf->value, "$INIT"));
518
519 vf = VTAILQ_NEXT(vf, list);
520
521 buf = VSB_new_auto();
522 AN(buf);
523 VSB_printf(buf, "&%s, \"%s\"", isym->rname, isym->name);
524 AZ(VSB_finish(buf));
525 vcc_Eval_Func(tl, vf, VSB_data(buf), osym);
526 VSB_destroy(&buf);
527 ERRCHK(tl);
528 SkipToken(tl, ';');
529 isym->def_e = tl->t;
530
531 vf = VTAILQ_FIRST(&vv->children);
532 assert(vf->type == VJSN_STRING);
533 assert(!strcmp(vf->value, "$FINI"));
534
535 vf = VTAILQ_NEXT(vf, list);
536 vf = VTAILQ_FIRST(&vf->children);
537 vf = VTAILQ_NEXT(vf, list);
538 ifp = New_IniFin(tl);
539 VSB_printf(ifp->fin, "\t\tif (%s)\n", isym->rname);
540 VSB_printf(ifp->fin, "\t\t\t\t%s(&%s);", vf->value, isym->rname);
541 }
542