varnish-cache/lib/libvcc/vcc_vmod_sym.c
0
/*-
1
 * Copyright (c) 2010-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8 46908
 * Redistribution and use in source and binary forms, with or without
9 46908
 * modification, are permitted provided that the following conditions
10 46908
 * are met:
11 46908
 * 1. Redistributions of source code must retain the above copyright
12 46908
 *    notice, this list of conditions and the following disclaimer.
13 46908
 * 2. Redistributions in binary form must reproduce the above copyright
14 46908
 *    notice, this list of conditions and the following disclaimer in the
15 46908
 *    documentation and/or other materials provided with the distribution.
16 46908
 *
17 46908
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 46908
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 46908
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 46908
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 46908
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 46908
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 46908
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 46908
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Turn vmod JSON spec into symbols
30
 *
31
 */
32
33
#include "config.h"
34
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include "vcc_compile.h"
39
40
#include "libvcc.h"
41
#include "vjsn.h"
42
43
#include "vcc_vmod.h"
44
45
struct vmod_obj {
46
        unsigned                magic;
47
#define VMOD_OBJ_MAGIC          0x349885f8
48
        char                    *name;
49
        struct type             type[1];
50
        VTAILQ_ENTRY(vmod_obj)  list;
51
};
52
53
static void
54 16404
vcc_VmodObject(struct vcc *tl, struct symbol *sym)
55
{
56
        struct vmod_obj *obj;
57
        struct vsb *buf;
58
59 16404
        buf = VSB_new_auto();
60 16404
        AN(buf);
61
62 16404
        VSB_printf(buf, "%s.%s", sym->vmod_name, sym->name);
63 16404
        AZ(VSB_finish(buf));
64
65 16404
        ALLOC_OBJ(obj, VMOD_OBJ_MAGIC);
66 16404
        AN(obj);
67 16404
        REPLACE(obj->name, VSB_data(buf));
68
69 16404
        INIT_OBJ(obj->type, TYPE_MAGIC);
70 16404
        obj->type->name = obj->name;
71 16404
        sym->type = obj->type;
72 16404
        VTAILQ_INSERT_TAIL(&tl->vmod_objects, obj, list);
73 16404
        VSB_destroy(&buf);
74 16404
}
75
76
static void
77 1572
alias_sym(struct vcc *tl, const struct symbol *psym, const struct vjsn_val *v)
78
{
79 1572
        char *alias = NULL, *func = NULL;
80
        struct symbol *sym;
81
        struct vsb *buf;
82
83 1572
        buf = VSB_new_auto();
84 1572
        AN(buf);
85
86 1572
        VCC_SymName(buf, psym);
87 1572
        VSB_printf(buf, ".%s", v->value);
88 1572
        AZ(VSB_finish(buf));
89 1572
        REPLACE(alias, VSB_data(buf));
90
91 1572
        v = VTAILQ_NEXT(v, list);
92 1572
        assert(vjsn_is_string(v));
93
94 1572
        VSB_clear(buf);
95 1572
        VCC_SymName(buf, psym);
96 1572
        VSB_printf(buf, ".%s", v->value);
97 1572
        AZ(VSB_finish(buf));
98 1572
        REPLACE(func, VSB_data(buf));
99
100 1572
        sym = VCC_MkSymAlias(tl, alias, func);
101 1572
        AN(sym);
102 1572
        assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD);
103 1572
        VSB_destroy(&buf);
104 1572
        free(alias);
105 1572
        free(func);
106 1572
}
107
108
static void
109 227388
func_restrict(struct vcc *tl, struct symbol *sym, vcc_kind_t kind, const struct vjsn_val *v)
110
{
111
        struct vjsn_val *vv;
112
113 227388
        AN(v);
114 227388
        AN(sym);
115
116 227388
        if (kind != SYM_FUNC && kind != SYM_METHOD)
117 0
                return;
118
119 227388
        v = VTAILQ_NEXT(v, list);
120 227388
        if (!v || !vjsn_is_array(v))
121 17340
                return;
122 210048
        vv = VTAILQ_FIRST(&v->children);
123 210048
        AN(vv);
124 210048
        assert(vjsn_is_string(vv));
125 210048
        if (strcmp(vv->value, "$RESTRICT"))
126 189288
                return;
127 20760
        vv = VTAILQ_NEXT(vv, list);
128 20760
        AN(vv);
129 20760
        assert(vjsn_is_array(vv));
130 20760
        sym->r_methods = 0;
131 20760
        vv = VTAILQ_FIRST(&vv->children);
132
        unsigned s;
133 67656
        while (vv) {
134 46908
                s = 0;
135
#define VCL_CTX(l,H)                                                    \
136
                if (strcmp(vv->value, #l) == 0) s = VCL_MET_##H;
137
#include "tbl/vcl_context.h"
138 46908
                if (!s) {
139 12
                        VSB_printf(tl->sb, "Error in vmod \"%s\", invalid scope for $Restrict: %s\n",sym->vmod_name, vv->value);
140 12
                        tl->err = 1;
141 12
                        break;
142
                }
143 46896
                sym->r_methods |= s;
144 46896
                vv = VTAILQ_NEXT(vv,list);
145
        }
146 227388
}
147
148
static void
149 245364
func_sym(struct vcc *tl, vcc_kind_t kind, const struct symbol *psym,
150
    const struct vjsn_val *v, const struct vjsn_val *vv)
151
{
152
        struct symbol *sym;
153
        struct vsb *buf;
154
155 245364
        if (kind == SYM_ALIAS) {
156 1572
                alias_sym(tl, psym, v);
157 1572
                return;
158
        }
159
160 243792
        buf = VSB_new_auto();
161 243792
        AN(buf);
162
163 243792
        VCC_SymName(buf, psym);
164 243792
        VSB_printf(buf, ".%s", v->value);
165 243792
        AZ(VSB_finish(buf));
166 243792
        sym = VCC_MkSym(tl, VSB_data(buf), SYM_MAIN, kind, VCL_LOW, VCL_HIGH);
167 243792
        AN(sym);
168 243792
        VSB_destroy(&buf);
169
170 243792
        if (kind == SYM_OBJECT) {
171 16404
                sym->eval_priv = v;
172 16404
                sym->vmod_name = psym->vmod_name;
173 16404
                sym->r_methods = VCL_MET_INIT;
174 16404
                vcc_VmodObject(tl, sym);
175 16404
                vcc_VmodSymbols(tl, sym);
176 16404
                return;
177
        }
178
179 227388
        if (kind == SYM_METHOD)
180 52644
                sym->extra = psym->rname;
181
182 227388
        v = VTAILQ_NEXT(v, list);
183
184 227388
        assert(vjsn_is_array(v));
185 227388
        sym->action = vcc_Act_Call;
186 227388
        sym->vmod_name = psym->vmod_name;
187 227388
        sym->eval = vcc_Eval_SymFunc;
188 227388
        sym->eval_priv = v;
189 227388
        v = VTAILQ_FIRST(&v->children);
190 227388
        assert(vjsn_is_array(v));
191 227388
        v = VTAILQ_FIRST(&v->children);
192 227388
        assert(vjsn_is_string(v));
193 227388
        sym->type = VCC_Type(v->value);
194 227388
        AN(sym->type);
195 227388
        sym->r_methods = VCL_MET_TASK_ALL;
196 227388
        func_restrict(tl, sym, kind, vv);
197 245364
}
198
199
void
200 22980
vcc_VmodSymbols(struct vcc *tl, const struct symbol *sym)
201
{
202
        const struct vjsn *vj;
203
        const struct vjsn_val *vv, *vv1, *vv2;
204
        vcc_kind_t kind;
205
206 22980
        if (sym->kind == SYM_VMOD) {
207 6576
                CAST_OBJ_NOTNULL(vj, sym->eval_priv, VJSN_MAGIC);
208 6576
                vv = VTAILQ_FIRST(&vj->value->children);
209 22980
        } else if (sym->kind == SYM_OBJECT) {
210 16404
                CAST_OBJ_NOTNULL(vv, sym->eval_priv, VJSN_VAL_MAGIC);
211 16404
        } else {
212 0
                WRONG("symbol kind");
213
        }
214
215 385620
        for (; vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
216 362652
                if (!vjsn_is_array(vv))
217 49212
                        continue;
218 313440
                vv1 = VTAILQ_FIRST(&vv->children);
219 313440
                AN(vv1);
220 313440
                assert(vjsn_is_string(vv1));
221 313440
                vv2 = VTAILQ_NEXT(vv1, list);
222 313440
                AN(vv2);
223 313440
                if (!vjsn_is_string(vv2))
224 53556
                        continue;
225
226 259884
                kind = SYM_NONE;
227
#define STANZA(UU, ll, ss) if (!strcmp(vv1->value, "$" #UU)) kind = ss;
228 259884
        STANZA_TBL
229
#undef STANZA
230 259884
                if (kind != SYM_NONE) {
231 245364
                        func_sym(tl, kind, sym, vv2, vv);
232 245364
                        ERRCHK(tl);
233 245352
                }
234 259872
        }
235 22980
}
236
237
void v_matchproto_(sym_act_f)
238 2484
vcc_Act_New(struct vcc *tl, struct token *t, struct symbol *sym)
239
{
240
        struct symbol *isym, *osym;
241
        struct inifin *ifp;
242
        struct vsb *buf;
243
        const struct vjsn_val *vv, *vf;
244 2484
        int null_ok = 0;
245
246 2484
        (void)sym;
247 2484
        (void)t;
248
249 2484
        ExpectErr(tl, ID);
250 2484
        vcc_ExpectVid(tl, "VCL object");
251 2484
        ERRCHK(tl);
252 2472
        isym = VCC_HandleSymbol(tl, INSTANCE);
253 2472
        ERRCHK(tl);
254 2448
        AN(isym);
255 2448
        isym->noref = 1;
256 2448
        isym->action = vcc_Act_Obj;
257
258 2448
        SkipToken(tl, '=');
259 2448
        ExpectErr(tl, ID);
260 2448
        osym = VCC_SymbolGet(tl, SYM_MAIN, SYM_OBJECT, SYMTAB_EXISTING,
261
            XREF_NONE);
262 2448
        ERRCHK(tl);
263 2412
        AN(osym);
264
265
        /* Scratch the generic INSTANCE type */
266 2412
        isym->type = osym->type;
267
268 2412
        CAST_OBJ_NOTNULL(vv, osym->eval_priv, VJSN_VAL_MAGIC);
269
        // vv = object name
270
271 2412
        isym->vmod_name = osym->vmod_name;
272 2412
        isym->eval_priv = vv;
273
274 2412
        vv = VTAILQ_NEXT(vv, list);
275
        // vv = flags
276 2412
        assert(vjsn_is_object(vv));
277 4824
        VTAILQ_FOREACH(vf, &vv->children, list)
278 2460
                if (!strcmp(vf->name, "NULL_OK") && vjsn_is_true(vf))
279 48
                        null_ok = 1;
280 2412
        if (!null_ok)
281 2364
                VTAILQ_INSERT_TAIL(&tl->sym_objects, isym, sideways);
282
283 2412
        vv = VTAILQ_NEXT(vv, list);
284
        // vv = struct name
285
286 2412
        Fh(tl, 0, "static %s *%s;\n\n", vv->value, isym->rname);
287 2412
        vv = VTAILQ_NEXT(vv, list);
288
289 2412
        vf = VTAILQ_FIRST(&vv->children);
290 2412
        vv = VTAILQ_NEXT(vv, list);
291 2412
        assert(vjsn_is_string(vf));
292 2412
        assert(!strcmp(vf->value, "$INIT"));
293
294 2412
        vf = VTAILQ_NEXT(vf, list);
295
296 2412
        buf = VSB_new_auto();
297 2412
        AN(buf);
298 2412
        VSB_printf(buf, "&%s, \"%s\"", isym->rname, isym->name);
299 2412
        AZ(VSB_finish(buf));
300 2412
        vcc_Eval_Func(tl, vf, VSB_data(buf), osym);
301 2412
        VSB_destroy(&buf);
302 2412
        ERRCHK(tl);
303 2412
        SkipToken(tl, ';');
304 2412
        isym->def_e = tl->t;
305
306 2412
        vf = VTAILQ_FIRST(&vv->children);
307 2412
        assert(vjsn_is_string(vf));
308 2412
        assert(!strcmp(vf->value, "$FINI"));
309
310 2412
        vf = VTAILQ_NEXT(vf, list);
311 2412
        vf = VTAILQ_FIRST(&vf->children);
312 2412
        vf = VTAILQ_NEXT(vf, list);
313 2412
        ifp = New_IniFin(tl);
314 2412
        VSB_printf(ifp->fin, "\t\tif (%s)\n", isym->rname);
315 2412
        VSB_printf(ifp->fin, "\t\t\t\t%s(&%s);", vf->value, isym->rname);
316 2484
}