varnish-cache/lib/libvcc/vcc_vmod.c
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
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <dlfcn.h>
32
#include <stdlib.h>
33
#include <string.h>
34
35
#include "vcc_compile.h"
36
37
#include "libvcc.h"
38
#include "vfil.h"
39
#include "vjsn.h"
40
#include "vmod_abi.h"
41
42
static int
43 1775
vcc_path_dlopen(void *priv, const char *fn)
44
{
45
        void *hdl, **pp;
46
47 1775
        AN(priv);
48 1775
        AN(fn);
49
50 1775
        hdl = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
51 1775
        if (hdl == NULL)
52 0
                return (-1);
53 1775
        pp = priv;
54 1775
        *pp = hdl;
55 1775
        return (0);
56
}
57
58
static void
59 5275
func_sym(struct symbol *sym, const char *vmod, const struct vjsn_val *v)
60
{
61
62 5275
        assert(v->type == VJSN_ARRAY);
63 5275
        sym->action = vcc_Act_Call;
64 5275
        sym->vmod = vmod;
65 5275
        sym->eval = vcc_Eval_SymFunc;
66 5275
        sym->eval_priv = v;
67 5275
        v = VTAILQ_FIRST(&v->children);
68 5275
        assert(v->type == VJSN_ARRAY);
69 5275
        v = VTAILQ_FIRST(&v->children);
70 5275
        assert(v->type == VJSN_STRING);
71 5275
        sym->type = VCC_Type(v->value);
72 5275
        AN(sym->type);
73 5275
}
74
75
static void
76 1770
vcc_json_always(struct vcc *tl, const struct symbol *msym)
77
{
78
        struct inifin *ifp;
79
        const struct vjsn *vj;
80
        const struct vjsn_val *vv, *vv2;
81 1770
        double vmod_syntax = 0.0;
82
83 1770
        ifp = NULL;
84
85 1770
        CAST_OBJ_NOTNULL(vj, msym->eval_priv, VJSN_MAGIC);
86
87 35385
        VTAILQ_FOREACH(vv, &vj->value->children, list) {
88 33615
                assert(vv->type == VJSN_ARRAY);
89 33615
                vv2 = VTAILQ_FIRST(&vv->children);
90 33615
                assert(vv2->type == VJSN_STRING);
91 33615
                if (!strcmp(vv2->value, "$VMOD")) {
92 1770
                        vmod_syntax =
93 1770
                            strtod(VTAILQ_NEXT(vv2, list)->value, NULL);
94 1770
                        continue;
95
                }
96 31845
                assert (vmod_syntax == 1.0);
97 31845
                if (!strcmp(vv2->value, "$EVENT")) {
98
                        /* XXX: What about the rest of the events ? */
99 335
                        if (ifp == NULL)
100 335
                                ifp = New_IniFin(tl);
101 335
                        vv2 = VTAILQ_NEXT(vv2, list);
102 335
                        VSB_printf(ifp->ini,
103
                            "\tif (%s(ctx, &vmod_priv_%s, VCL_EVENT_LOAD))\n"
104
                            "\t\treturn(1);",
105
                            vv2->value, msym->name);
106 335
                        VSB_printf(ifp->fin,
107
                            "\t\t(void)%s(ctx, &vmod_priv_%s,\n"
108
                            "\t\t\t    VCL_EVENT_DISCARD);",
109
                            vv2->value, msym->name);
110 335
                        VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%s, ev)",
111
                            vv2->value, msym->name);
112 31510
                } else if (!strcmp(vv2->value, "$FUNC")) {
113 3175
                } else if (!strcmp(vv2->value, "$OBJ")) {
114
                } else {
115 0
                        VTAILQ_FOREACH(vv2, &vv->children, list)
116 0
                                fprintf(stderr, "\tt %s n %s v %s\n",
117
                                    vv2->type, vv2->name, vv2->value);
118 0
                        WRONG("Vmod JSON syntax error");
119
                }
120
        }
121 1770
}
122
123
static void v_matchproto_(sym_wildcard_t)
124 2145
vcc_json_wildcard(struct vcc *tl, struct symbol *msym, struct symbol *tsym)
125
{
126
        const struct vjsn *vj;
127
        const struct vjsn_val *vv, *vv1, *vv2;
128
129 2145
        assert(msym->kind == SYM_VMOD);
130 2145
        CAST_OBJ_NOTNULL(vj, msym->eval_priv, VJSN_MAGIC);
131 18370
        VTAILQ_FOREACH(vv, &vj->value->children, list) {
132 18370
                assert(vv->type == VJSN_ARRAY);
133 18370
                vv1 = VTAILQ_FIRST(&vv->children);
134 18370
                assert(vv1->type == VJSN_STRING);
135 18370
                vv2 = VTAILQ_NEXT(vv1, list);
136 18370
                assert(vv2->type == VJSN_STRING);
137 32360
                if (!strcmp(vv1->value, "$FUNC") &&
138 13990
                    !strcmp(vv2->value, tsym->name)) {
139 1605
                        tsym->kind = SYM_FUNC;
140 1605
                        tsym->noref = 1;
141 1605
                        func_sym(tsym, msym->name, VTAILQ_NEXT(vv2, list));
142 1605
                        return;
143 18625
                } else if (!strcmp(vv1->value, "$OBJ") &&
144 1860
                           !strcmp(vv2->value, tsym->name)) {
145 540
                        tsym->kind = SYM_OBJECT;
146 540
                        tsym->eval_priv = vv2;
147 540
                        tsym->vmod = msym->name;
148 540
                        return;
149
                }
150
        }
151 0
        tl->err = 1;
152
}
153
154
void
155 1795
vcc_ParseImport(struct vcc *tl)
156
{
157
        void *hdl;
158
        char fn[1024], *fnp, *fnpx;
159
        char buf[256];
160
        const char *p;
161
        struct token *mod, *t1;
162
        struct inifin *ifp;
163
        struct symbol *msym;
164
        const struct vmod_data *vmd;
165
        struct vjsn *vj;
166 1795
        int again = 0;
167
168 1795
        t1 = tl->t;
169 1820
        SkipToken(tl, ID);              /* "import" */
170
171
172 1795
        ExpectErr(tl, ID);
173 1795
        mod = tl->t;
174 1795
        msym = VCC_SymbolGet(tl, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
175
176 1795
        if (msym != NULL && msym->kind != SYM_VMOD) {
177
                /*
178
                 * We need to make sure the entire std.* namespace is empty
179
                 */
180 10
                VSB_printf(tl->sb, "Module %.*s conflicts with other symbol.\n",
181 5
                    PF(mod));
182 5
                vcc_ErrWhere2(tl, t1, tl->t);
183 5
                return;
184
        }
185 1790
        if (msym != NULL) {
186 0
                again = 1;
187
        } else {
188
189 1790
                msym = VCC_SymbolGet(tl, SYM_VMOD, SYMTAB_CREATE, XREF_NONE);
190 1790
                ERRCHK(tl);
191 1790
                AN(msym);
192 1790
                msym->def_b = t1;
193 1790
                msym->def_e = tl->t;
194
        }
195
196 1790
        if (tl->t->tok == ID) {
197 20
                if (!vcc_IdIs(tl->t, "from")) {
198 5
                        VSB_printf(tl->sb, "Expected 'from path ...'\n");
199 5
                        vcc_ErrWhere(tl, tl->t);
200 5
                        return;
201
                }
202 15
                vcc_NextToken(tl);
203 15
                if (!tl->unsafe_path && strchr(tl->t->dec, '/')) {
204 5
                        VSB_printf(tl->sb,
205
                            "'import ... from path ...' is unsafe.\nAt:");
206 5
                        vcc_ErrToken(tl, tl->t);
207 5
                        vcc_ErrWhere(tl, tl->t);
208 5
                        return;
209
                }
210 10
                ExpectErr(tl, CSTR);
211 10
                p = strrchr(tl->t->dec, '/');
212 10
                if (p != NULL && p[1] == '\0')
213 5
                        bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod));
214
                else
215 5
                        bprintf(fn, "%s", tl->t->dec);
216 10
                vcc_NextToken(tl);
217
        } else {
218 1770
                bprintf(fn, "libvmod_%.*s.so", PF(mod));
219
        }
220
221 1780
        SkipToken(tl, ';');
222
223 1780
        if (!again)
224 1780
                msym->def_e = tl->t;
225
226
227 1780
        if (VFIL_searchpath(tl->vmod_path,
228
            vcc_path_dlopen, &hdl, fn, &fnpx)) {
229 5
                VSB_printf(tl->sb, "Could not load VMOD %.*s\n", PF(mod));
230 5
                VSB_printf(tl->sb, "\tFile name: %s\n",
231 5
                    fnpx != NULL ? fnpx : fn);
232 5
                VSB_printf(tl->sb, "\tdlerror: %s\n", dlerror());
233 5
                vcc_ErrWhere(tl, mod);
234 5
                free(fnpx);
235 5
                return;
236
        }
237
238 1775
        AN(fnpx);
239 1775
        fnp = TlDup(tl, fnpx);
240 1775
        free(fnpx);
241
242 1775
        bprintf(buf, "Vmod_%.*s_Data", PF(mod));
243 1775
        vmd = dlsym(hdl, buf);
244 1775
        if (vmd == NULL) {
245 5
                VSB_printf(tl->sb, "Malformed VMOD %.*s\n", PF(mod));
246 5
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
247 5
                VSB_printf(tl->sb, "\t(no Vmod_Data symbol)\n");
248 5
                vcc_ErrWhere(tl, mod);
249 5
                return;
250
        }
251 3540
        if (vmd->vrt_major == 0 && vmd->vrt_minor == 0 &&
252 1770
            strcmp(vmd->abi, VMOD_ABI_Version) != 0) {
253 0
                VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod));
254 0
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
255 0
                VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n",
256
                           VMOD_ABI_Version, vmd->abi);
257 0
                vcc_ErrWhere(tl, mod);
258 0
                return;
259
        }
260 1770
        if (vmd->vrt_major != 0 && (vmd->vrt_major != VRT_MAJOR_VERSION ||
261 0
            vmd->vrt_minor > VRT_MINOR_VERSION)) {
262 0
                VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod));
263 0
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
264 0
                VSB_printf(tl->sb, "\tVMOD wants ABI version %u.%u\n",
265
                    vmd->vrt_major, vmd->vrt_minor);
266 0
                VSB_printf(tl->sb, "\tvarnishd provices ABI version %u.%u\n",
267
                    VRT_MAJOR_VERSION, VRT_MINOR_VERSION);
268 0
                vcc_ErrWhere(tl, mod);
269 0
                return;
270
        }
271 3540
        if (vmd->name == NULL ||
272 3540
            vmd->func == NULL ||
273 3540
            vmd->func_len <= 0 ||
274 3540
            vmd->proto == NULL ||
275 1770
            vmd->abi == NULL) {
276 0
                VSB_printf(tl->sb, "Mangled VMOD %.*s\n", PF(mod));
277 0
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
278 0
                VSB_printf(tl->sb, "\tInconsistent metadata\n");
279 0
                vcc_ErrWhere(tl, mod);
280 0
                return;
281
        }
282
283 1770
        if (!vcc_IdIs(mod, vmd->name)) {
284 0
                VSB_printf(tl->sb, "Wrong VMOD file %.*s\n", PF(mod));
285 0
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
286 0
                VSB_printf(tl->sb, "\tContains vmod \"%s\"\n", vmd->name);
287 0
                vcc_ErrWhere(tl, mod);
288 0
                return;
289
        }
290
291 1770
        if (again && strcmp(vmd->file_id, msym->extra)) {
292 0
                VSB_printf(tl->sb,
293
                    "Different version of module %.*s already imported.\n",
294 0
                    PF(mod));
295 0
                vcc_ErrWhere2(tl, t1, tl->t);
296 0
                VSB_printf(tl->sb, "Previous import was here:\n");
297 0
                vcc_ErrWhere2(tl, msym->def_b, msym->def_e);
298
        }
299 1770
        if (again) {
300 0
                AZ(dlclose(hdl));
301 0
                return;
302
        }
303
304 1770
        ifp = New_IniFin(tl);
305
306 1770
        VSB_printf(ifp->ini, "\tif (VRT_Vmod_Init(ctx,\n");
307 1770
        VSB_printf(ifp->ini, "\t    &VGC_vmod_%.*s,\n", PF(mod));
308 1770
        VSB_printf(ifp->ini, "\t    %u,\n", tl->vmod_count++);
309 1770
        VSB_printf(ifp->ini, "\t    &Vmod_%.*s_Func,\n", PF(mod));
310 1770
        VSB_printf(ifp->ini, "\t    sizeof(Vmod_%.*s_Func),\n", PF(mod));
311 1770
        VSB_printf(ifp->ini, "\t    \"%.*s\",\n", PF(mod));
312 1770
        VSB_printf(ifp->ini, "\t    ");
313 1770
        VSB_quote(ifp->ini, fnp, -1, VSB_QUOTE_CSTR);
314 1770
        VSB_printf(ifp->ini, ",\n");
315 1770
        AN(vmd);
316 1770
        AN(vmd->file_id);
317 1770
        VSB_printf(ifp->ini, "\t    \"%s\",\n", vmd->file_id);
318 3540
        VSB_printf(ifp->ini, "\t    \"./vmod_cache/_vmod_%.*s.%s\"\n",
319 1770
            PF(mod), vmd->file_id);
320 1770
        VSB_printf(ifp->ini, "\t    ))\n");
321 1770
        VSB_printf(ifp->ini, "\t\treturn(1);");
322
323 3540
        VSB_printf(tl->fi, "%s VMOD %s ./vmod_cache/_vmod_%.*s.%s */\n",
324 1770
            VCC_INFO_PREFIX, fnp, PF(mod), vmd->file_id);
325
326
        /* XXX: zero the function pointer structure ?*/
327 1770
        VSB_printf(ifp->fin, "\t\tVRT_priv_fini(&vmod_priv_%.*s);", PF(mod));
328 3540
        VSB_printf(ifp->final,
329 1770
            "\t\tVRT_Vmod_Unload(ctx, &VGC_vmod_%.*s);", PF(mod));
330
331 1770
        vj = vjsn_parse(vmd->json, &p);
332 1770
        XXXAZ(p);
333 1770
        AN(vj);
334 1770
        msym->eval_priv = vj;
335 1770
        msym->wildcard = vcc_json_wildcard;
336 1770
        msym->extra = TlDup(tl, vmd->file_id);
337
338 1770
        vcc_json_always(tl, msym);
339
340 1770
        Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod));
341 1770
        Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod));
342 1770
        Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod));
343 1770
        Fh(tl, 0, "\n%s\n", vmd->proto);
344 1770
        Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod));
345
}
346
347
void v_matchproto_(sym_act_f)
348 810
vcc_Act_New(struct vcc *tl, struct token *t, struct symbol *sym)
349
{
350
        struct symbol *sy1, *sy2, *sy3;
351
        struct inifin *ifp;
352
        char buf1[128];
353
        char buf2[128];
354
        const struct vjsn_val *vv, *vf;
355
        const char *p;
356
357
        (void)sym;
358 835
        ExpectErr(tl, ID);
359 810
        vcc_ExpectVid(tl, "VCL object");
360 810
        ERRCHK(tl);
361 805
        sy1 = VCC_HandleSymbol(tl, INSTANCE, "vo");
362 805
        ERRCHK(tl);
363 795
        AN(sy1);
364 795
        sy1->noref = 1;
365
366 795
        ExpectErr(tl, '=');
367 795
        vcc_NextToken(tl);
368
369 795
        ExpectErr(tl, ID);
370 795
        t = tl->t;
371 795
        sy2 = VCC_SymbolGet(tl, SYM_OBJECT, "Symbol not found", XREF_NONE);
372 795
        ERRCHK(tl);
373 785
        AN(sy2);
374 785
        if (sy2->eval_priv == NULL) {
375 0
                VSB_printf(tl->sb, "Constructor not found: ");
376 0
                vcc_ErrToken(tl, t);
377 0
                VSB_printf(tl->sb, " at ");
378 0
                vcc_ErrWhere(tl, t);
379 0
                return;
380
        }
381
382 785
        CAST_OBJ_NOTNULL(vv, sy2->eval_priv, VJSN_VAL_MAGIC);
383
384 785
        vv = VTAILQ_NEXT(vv, list);
385
386 785
        Fh(tl, 0, "static %s *%s;\n\n", vv->value, sy1->rname);
387 785
        vv = VTAILQ_NEXT(vv, list);
388
389 785
        vf = VTAILQ_FIRST(&vv->children);
390 785
        vv = VTAILQ_NEXT(vv, list);
391 785
        assert(vf->type == VJSN_STRING);
392 785
        assert(!strcmp(vf->value, "$INIT"));
393
394 785
        vf = VTAILQ_NEXT(vf, list);
395
396 785
        bprintf(buf1, ", &%s, \"%s\"", sy1->rname, sy1->name);
397 785
        vcc_Eval_Func(tl, vf, buf1, sy2);
398 785
        ERRCHK(tl);
399 785
        SkipToken(tl, ';');
400 785
        sy1->def_e = tl->t;
401
402 785
        vf = VTAILQ_FIRST(&vv->children);
403 785
        vv = VTAILQ_NEXT(vv, list);
404 785
        assert(vf->type == VJSN_STRING);
405 785
        assert(!strcmp(vf->value, "$FINI"));
406
407 785
        vf = VTAILQ_NEXT(vf, list);
408 785
        vf = VTAILQ_FIRST(&vf->children);
409 785
        vf = VTAILQ_NEXT(vf, list);
410 785
        ifp = New_IniFin(tl);
411 785
        VSB_printf(ifp->fin, "\t\t%s(&%s);", vf->value, sy1->rname);
412
413
        /* Instantiate symbols for the methods */
414 785
        bprintf(buf1, ", %s", sy1->rname);
415 785
        p = TlDup(tl, buf1);
416 5240
        while (vv != NULL) {
417 3670
                vf = VTAILQ_FIRST(&vv->children);
418 3670
                assert(vf->type == VJSN_STRING);
419 3670
                assert(!strcmp(vf->value, "$METHOD"));
420 3670
                vf = VTAILQ_NEXT(vf, list);
421 3670
                assert(vf->type == VJSN_STRING);
422
423 3670
                bprintf(buf2, "%s.%s", sy1->name, vf->value);
424 3670
                sy3 = VCC_MkSym(tl, buf2, SYM_FUNC, VCL_LOW, VCL_HIGH);
425 3670
                AN(sy3);
426 3670
                func_sym(sy3, sy2->vmod, VTAILQ_NEXT(vf, list));
427 3670
                sy3->extra = p;
428 3670
                vv = VTAILQ_NEXT(vv, list);
429
        }
430
}