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 1038
vcc_path_dlopen(void *priv, const char *fn)
44
{
45
        void *hdl, **pp;
46
47 1038
        AN(priv);
48 1038
        AN(fn);
49
50 1038
        hdl = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
51 1038
        if (hdl == NULL)
52 0
                return (-1);
53 1038
        pp = priv;
54 1038
        *pp = hdl;
55 1038
        return (0);
56
}
57
58
static void
59 3126
func_sym(struct symbol *sym, const char *vmod, const struct vjsn_val *v)
60
{
61
62 3126
        assert(v->type == VJSN_ARRAY);
63 3126
        sym->action = vcc_Act_Call;
64 3126
        sym->vmod = vmod;
65 3126
        sym->eval = vcc_Eval_SymFunc;
66 3126
        sym->eval_priv = v;
67 3126
        v = VTAILQ_FIRST(&v->children);
68 3126
        assert(v->type == VJSN_ARRAY);
69 3126
        v = VTAILQ_FIRST(&v->children);
70 3126
        assert(v->type == VJSN_STRING);
71 3126
        sym->type = VCC_Type(v->value);
72 3126
        AN(sym->type);
73 3126
}
74
75
static void
76 1035
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 1035
        double vmod_syntax = 0.0;
82
83 1035
        ifp = NULL;
84
85 1035
        CAST_OBJ_NOTNULL(vj, msym->eval_priv, VJSN_MAGIC);
86
87 19485
        VTAILQ_FOREACH(vv, &vj->value->children, list) {
88 18450
                assert(vv->type == VJSN_ARRAY);
89 18450
                vv2 = VTAILQ_FIRST(&vv->children);
90 18450
                assert(vv2->type == VJSN_STRING);
91 18450
                if (!strcmp(vv2->value, "$VMOD")) {
92 1035
                        vmod_syntax =
93 1035
                            strtod(VTAILQ_NEXT(vv2, list)->value, NULL);
94 1035
                        continue;
95
                }
96 17415
                assert (vmod_syntax == 1.0);
97 17415
                if (!strcmp(vv2->value, "$EVENT")) {
98
                        /* XXX: What about the rest of the events ? */
99 186
                        if (ifp == NULL)
100 186
                                ifp = New_IniFin(tl);
101 186
                        vv2 = VTAILQ_NEXT(vv2, list);
102 186
                        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 186
                        VSB_printf(ifp->fin,
107
                            "\t\t(void)%s(ctx, &vmod_priv_%s,\n"
108
                            "\t\t\t    VCL_EVENT_DISCARD);\n",
109
                            vv2->value, msym->name);
110 186
                        VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%s, ev)",
111
                            vv2->value, msym->name);
112 17229
                } else if (!strcmp(vv2->value, "$FUNC")) {
113 1659
                } 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 1035
}
122
123
static void v_matchproto_(sym_wildcard_t)
124 1251
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 1251
        assert(msym->kind == SYM_VMOD);
130 1251
        CAST_OBJ_NOTNULL(vj, msym->eval_priv, VJSN_MAGIC);
131 10281
        VTAILQ_FOREACH(vv, &vj->value->children, list) {
132 10281
                assert(vv->type == VJSN_ARRAY);
133 10281
                vv1 = VTAILQ_FIRST(&vv->children);
134 10281
                assert(vv1->type == VJSN_STRING);
135 10281
                vv2 = VTAILQ_NEXT(vv1, list);
136 10281
                assert(vv2->type == VJSN_STRING);
137 18075
                if (!strcmp(vv1->value, "$FUNC") &&
138 7794
                    !strcmp(vv2->value, tsym->name)) {
139 930
                        tsym->kind = SYM_FUNC;
140 930
                        tsym->noref = 1;
141 930
                        func_sym(tsym, msym->name, VTAILQ_NEXT(vv2, list));
142 930
                        return;
143 10383
                } else if (!strcmp(vv1->value, "$OBJ") &&
144 1032
                           !strcmp(vv2->value, tsym->name)) {
145 321
                        tsym->kind = SYM_OBJECT;
146 321
                        tsym->eval_priv = vv2;
147 321
                        tsym->vmod = msym->name;
148 321
                        return;
149
                }
150
        }
151 0
        tl->err = 1;
152
}
153
154
void
155 1050
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 1050
        int again = 0;
167
168 1050
        t1 = tl->t;
169 1065
        SkipToken(tl, ID);              /* "import" */
170
171
172 1050
        ExpectErr(tl, ID);
173 1050
        mod = tl->t;
174 1050
        msym = VCC_SymbolGet(tl, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
175
176 1050
        if (msym != NULL && msym->kind != SYM_VMOD) {
177
                /*
178
                 * We need to make sure the entire std.* namespace is empty
179
                 */
180 6
                VSB_printf(tl->sb, "Module %.*s conflicts with other symbol.\n",
181 3
                    PF(mod));
182 3
                vcc_ErrWhere2(tl, t1, tl->t);
183 3
                return;
184
        }
185 1047
        if (msym != NULL) {
186 0
                again = 1;
187
        } else {
188
189 1047
                msym = VCC_SymbolGet(tl, SYM_VMOD, SYMTAB_CREATE, XREF_NONE);
190 1047
                ERRCHK(tl);
191 1047
                AN(msym);
192 1047
                msym->def_b = t1;
193 1047
                msym->def_e = tl->t;
194
        }
195
196 1047
        if (tl->t->tok == ID) {
197 12
                if (!vcc_IdIs(tl->t, "from")) {
198 3
                        VSB_printf(tl->sb, "Expected 'from path ...'\n");
199 3
                        vcc_ErrWhere(tl, tl->t);
200 3
                        return;
201
                }
202 9
                vcc_NextToken(tl);
203 9
                if (!tl->unsafe_path && strchr(tl->t->dec, '/')) {
204 3
                        VSB_printf(tl->sb,
205
                            "'import ... from path ...' is unsafe.\nAt:");
206 3
                        vcc_ErrToken(tl, tl->t);
207 3
                        vcc_ErrWhere(tl, tl->t);
208 3
                        return;
209
                }
210 6
                ExpectErr(tl, CSTR);
211 6
                p = strrchr(tl->t->dec, '/');
212 6
                if (p != NULL && p[1] == '\0')
213 3
                        bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod));
214
                else
215 3
                        bprintf(fn, "%s", tl->t->dec);
216 6
                vcc_NextToken(tl);
217
        } else {
218 1035
                bprintf(fn, "libvmod_%.*s.so", PF(mod));
219
        }
220
221 1041
        SkipToken(tl, ';');
222
223 1041
        if (!again)
224 1041
                msym->def_e = tl->t;
225
226
227 1041
        if (VFIL_searchpath(tl->vmod_path,
228
            vcc_path_dlopen, &hdl, fn, &fnpx)) {
229 3
                VSB_printf(tl->sb, "Could not load VMOD %.*s\n", PF(mod));
230 3
                VSB_printf(tl->sb, "\tFile name: %s\n",
231 3
                    fnpx != NULL ? fnpx : fn);
232 3
                VSB_printf(tl->sb, "\tdlerror: %s\n", dlerror());
233 3
                vcc_ErrWhere(tl, mod);
234 3
                free(fnpx);
235 3
                return;
236
        }
237
238 1038
        AN(fnpx);
239 1038
        fnp = TlDup(tl, fnpx);
240 1038
        free(fnpx);
241
242 1038
        bprintf(buf, "Vmod_%.*s_Data", PF(mod));
243 1038
        vmd = dlsym(hdl, buf);
244 1038
        if (vmd == NULL) {
245 3
                VSB_printf(tl->sb, "Malformed VMOD %.*s\n", PF(mod));
246 3
                VSB_printf(tl->sb, "\tFile name: %s\n", fnp);
247 3
                VSB_printf(tl->sb, "\t(no Vmod_Data symbol)\n");
248 3
                vcc_ErrWhere(tl, mod);
249 3
                return;
250
        }
251 2070
        if (vmd->vrt_major == 0 && vmd->vrt_minor == 0 &&
252 1035
            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 1035
        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 2070
        if (vmd->name == NULL ||
272 2070
            vmd->func == NULL ||
273 2070
            vmd->func_len <= 0 ||
274 2070
            vmd->proto == NULL ||
275 1035
            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 1035
        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 1035
        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 1035
        if (again) {
300 0
                AZ(dlclose(hdl));
301 0
                return;
302
        }
303
304 1035
        ifp = New_IniFin(tl);
305
306 1035
        VSB_printf(ifp->ini, "\tif (VRT_Vmod_Init(ctx,\n");
307 1035
        VSB_printf(ifp->ini, "\t    &VGC_vmod_%.*s,\n", PF(mod));
308 1035
        VSB_printf(ifp->ini, "\t    &Vmod_%.*s_Func,\n", PF(mod));
309 1035
        VSB_printf(ifp->ini, "\t    sizeof(Vmod_%.*s_Func),\n", PF(mod));
310 1035
        VSB_printf(ifp->ini, "\t    \"%.*s\",\n", PF(mod));
311 1035
        VSB_printf(ifp->ini, "\t    ");
312 1035
        VSB_quote(ifp->ini, fnp, -1, VSB_QUOTE_CSTR);
313 1035
        VSB_printf(ifp->ini, ",\n");
314 1035
        AN(vmd);
315 1035
        AN(vmd->file_id);
316 1035
        VSB_printf(ifp->ini, "\t    \"%s\",\n", vmd->file_id);
317 2070
        VSB_printf(ifp->ini, "\t    \"./vmod_cache/_vmod_%.*s.%s\"\n",
318 1035
            PF(mod), vmd->file_id);
319 1035
        VSB_printf(ifp->ini, "\t    ))\n");
320 1035
        VSB_printf(ifp->ini, "\t\treturn(1);");
321
322 2070
        VSB_printf(tl->fi, "%s VMOD %s ./vmod_cache/_vmod_%.*s.%s */\n",
323 1035
            VCC_INFO_PREFIX, fnp, PF(mod), vmd->file_id);
324
325
        /* XXX: zero the function pointer structure ?*/
326 1035
        VSB_printf(ifp->fin, "\t\tVRT_priv_fini(&vmod_priv_%.*s);\n", PF(mod));
327 1035
        VSB_printf(ifp->fin, "\t\t\tVRT_Vmod_Fini(&VGC_vmod_%.*s);", PF(mod));
328
329 1035
        vj = vjsn_parse(vmd->json, &p);
330 1035
        XXXAZ(p);
331 1035
        AN(vj);
332 1035
        msym->eval_priv = vj;
333 1035
        msym->wildcard = vcc_json_wildcard;
334 1035
        msym->extra = TlDup(tl, vmd->file_id);
335
336 1035
        vcc_json_always(tl, msym);
337
338 1035
        Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod));
339 1035
        Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod));
340 1035
        Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod));
341 1035
        Fh(tl, 0, "\n%s\n", vmd->proto);
342 1035
        Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod));
343
}
344
345
void v_matchproto_(sym_act_f)
346 480
vcc_Act_New(struct vcc *tl, struct token *t, struct symbol *sym)
347
{
348
        struct symbol *sy1, *sy2, *sy3;
349
        struct inifin *ifp;
350
        char buf1[128];
351
        char buf2[128];
352
        const struct vjsn_val *vv, *vf;
353
        const char *p;
354
355
        (void)sym;
356 495
        ExpectErr(tl, ID);
357 480
        vcc_ExpectVid(tl, "VCL object");
358 480
        ERRCHK(tl);
359 477
        sy1 = VCC_HandleSymbol(tl, INSTANCE, "vo");
360 477
        ERRCHK(tl);
361 471
        AN(sy1);
362 471
        sy1->noref = 1;
363
364 471
        ExpectErr(tl, '=');
365 471
        vcc_NextToken(tl);
366
367 471
        ExpectErr(tl, ID);
368 471
        t = tl->t;
369 471
        sy2 = VCC_SymbolGet(tl, SYM_OBJECT, "Symbol not found", XREF_NONE);
370 471
        ERRCHK(tl);
371 465
        AN(sy2);
372 465
        if (sy2->eval_priv == NULL) {
373 0
                VSB_printf(tl->sb, "Constructor not found: ");
374 0
                vcc_ErrToken(tl, t);
375 0
                VSB_printf(tl->sb, " at ");
376 0
                vcc_ErrWhere(tl, t);
377 0
                return;
378
        }
379
380 465
        CAST_OBJ_NOTNULL(vv, sy2->eval_priv, VJSN_VAL_MAGIC);
381
382 465
        vv = VTAILQ_NEXT(vv, list);
383
384 465
        Fh(tl, 0, "static %s *%s;\n\n", vv->value, sy1->rname);
385 465
        vv = VTAILQ_NEXT(vv, list);
386
387 465
        vf = VTAILQ_FIRST(&vv->children);
388 465
        vv = VTAILQ_NEXT(vv, list);
389 465
        assert(vf->type == VJSN_STRING);
390 465
        assert(!strcmp(vf->value, "$INIT"));
391
392 465
        vf = VTAILQ_NEXT(vf, list);
393
394 465
        bprintf(buf1, ", &%s, \"%s\"", sy1->rname, sy1->name);
395 465
        vcc_Eval_Func(tl, vf, buf1, sy2);
396 465
        ERRCHK(tl);
397 465
        SkipToken(tl, ';');
398 465
        sy1->def_e = tl->t;
399
400 465
        vf = VTAILQ_FIRST(&vv->children);
401 465
        vv = VTAILQ_NEXT(vv, list);
402 465
        assert(vf->type == VJSN_STRING);
403 465
        assert(!strcmp(vf->value, "$FINI"));
404
405 465
        vf = VTAILQ_NEXT(vf, list);
406 465
        vf = VTAILQ_FIRST(&vf->children);
407 465
        vf = VTAILQ_NEXT(vf, list);
408 465
        ifp = New_IniFin(tl);
409 465
        VSB_printf(ifp->fin, "\t\t%s(&%s);", vf->value, sy1->rname);
410
411
        /* Instantiate symbols for the methods */
412 465
        bprintf(buf1, ", %s", sy1->rname);
413 465
        p = TlDup(tl, buf1);
414 3126
        while (vv != NULL) {
415 2196
                vf = VTAILQ_FIRST(&vv->children);
416 2196
                assert(vf->type == VJSN_STRING);
417 2196
                assert(!strcmp(vf->value, "$METHOD"));
418 2196
                vf = VTAILQ_NEXT(vf, list);
419 2196
                assert(vf->type == VJSN_STRING);
420
421 2196
                bprintf(buf2, "%s.%s", sy1->name, vf->value);
422 2196
                sy3 = VCC_MkSym(tl, buf2, SYM_FUNC, VCL_LOW, VCL_HIGH);
423 2196
                AN(sy3);
424 2196
                func_sym(sy3, sy2->vmod, VTAILQ_NEXT(vf, list));
425 2196
                sy3->extra = p;
426 2196
                vv = VTAILQ_NEXT(vv, list);
427
        }
428
}