Code Coverage: vinyl-cache/lib/libvcc/vcc_xref.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 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
 * This file contains code for two cross-reference or consistency checks.
31
 *
32
 * The first check is simply that all subroutine, acls and backends are
33
 * both defined and referenced.  Complaints about referenced but undefined
34
 * or defined but unreferenced objects will be emitted.
35
 *
36
 * The second check recursively descends through subroutine calls to make
37
 * sure that action actions are correct for the methods through which
38
 * they are called.
39
 */
40
41
#include "config.h"
42
43
#include <string.h>
44
#include "vcc_compile.h"
45
46
/*--------------------------------------------------------------------*/
47
48
struct proccall {
49
        VTAILQ_ENTRY(proccall)  list;
50
        struct symbol           *sym;
51
        struct token            *t;
52
        struct proc             *fm;
53
};
54
55
struct procuse {
56
        VTAILQ_ENTRY(procuse)   list;
57
        const struct token      *t1;
58
        const struct token      *t2;
59
        const struct symbol     *sym;
60
        const struct xrefuse    *use;
61
        unsigned                mask;
62
        struct proc             *fm;
63
};
64
65
/*--------------------------------------------------------------------*/
66
67
static void
68
vcc_checkref(struct vcc *tl, const struct symbol *sym)
69 784532
{
70
71 784532
        if (sym->noref)
72 696013
                return;
73 88519
        if (sym->ndef == 0 && sym->nref != 0) {
74 3
                AN(sym->ref_b);
75 3
                VSB_printf(tl->sb, "Undefined %s %.*s, first reference:\n",
76 3
                    sym->kind->name, PF(sym->ref_b));
77 3
                vcc_ErrWhere(tl, sym->ref_b);
78 88516
        } else if (sym->ndef != 0 && sym->nref == 0) {
79 131
                AN(sym->def_b);
80 131
                VSB_printf(tl->sb, "Unused %s %.*s, defined:\n",
81 131
                    sym->kind->name, PF(sym->def_b));
82 131
                vcc_ErrWhere(tl, sym->def_b);
83 131
                if (!tl->err_unref)
84 123
                        vcc_Warn(tl);
85 131
        }
86 88519
}
87
88
int
89
vcc_CheckReferences(struct vcc *tl)
90 3029
{
91
92 3029
        VCC_WalkSymbols(tl, vcc_checkref, SYM_MAIN, SYM_NONE);
93 3029
        return (tl->err);
94 3029
}
95
96
/*--------------------------------------------------------------------
97
 * Returns checks
98
 */
99
100
const struct xrefuse XREF_READ[1] = {{"xref_read", "Not available"}};
101
const struct xrefuse XREF_WRITE[1] = {{"xref_write", "Cannot be set"}};
102
const struct xrefuse XREF_UNSET[1] = {{"xref_unset", "Cannot be unset"}};
103
const struct xrefuse XREF_ACTION[1] = {{"xref_action", "Not a valid action"}};
104
105
void
106
vcc_AddUses(struct vcc *tl, const struct token *t1, const struct token *t2,
107
    const struct symbol *sym, const struct xrefuse *use)
108 226757
{
109 226757
        struct procuse *pu;
110
111 226757
        AN(tl->curproc);
112 226757
        pu = TlAlloc(tl, sizeof *pu);
113 226757
        AN(pu);
114 226757
        AN(sym);
115 226757
        AN(use);
116 226757
        AN(use->name);
117 226757
        pu->t1 = t1;
118 226757
        pu->t2 = t2;
119 226757
        if (pu->t2 == NULL) {
120 184267
                pu->t2 = vcc_PeekTokenFrom(tl, t1);
121 184267
                AN(pu->t2);
122 184267
        }
123 226757
        pu->sym = sym;
124 226757
        pu->use = use;
125 226757
        pu->fm = tl->curproc;
126
127 226757
        if (pu->use == XREF_READ)
128 174955
                pu->mask = sym->r_methods;
129 51802
        else if (pu->use == XREF_WRITE)
130 33333
                pu->mask = sym->w_methods;
131 18469
        else if (pu->use == XREF_UNSET)
132 9157
                pu->mask = sym->u_methods;
133 9312
        else if (pu->use == XREF_ACTION)
134 9312
                pu->mask = sym->action_mask;
135 0
        else
136 0
                WRONG("wrong xref use");
137
138 226757
        VTAILQ_INSERT_TAIL(&tl->curproc->uses, pu, list);
139 226757
}
140
141
void
142
vcc_AddCall(struct vcc *tl, struct token *t, struct symbol *sym)
143 97077
{
144 97077
        struct proccall *pc;
145
146 97077
        AN(sym);
147 97077
        pc = TlAlloc(tl, sizeof *pc);
148 97077
        AN(pc);
149 97077
        pc->sym = sym;
150 97077
        pc->t = t;
151 97077
        pc->fm = tl->curproc;
152 97077
        VTAILQ_INSERT_TAIL(&tl->curproc->calls, pc, list);
153 97077
}
154
155
void
156
vcc_ProcAction(struct proc *p, unsigned returns, unsigned mask, struct token *t)
157 82520
{
158
159 82520
        assert(returns < VCL_RET_MAX);
160 82520
        p->ret_bitmap |= (1U << returns);
161 82520
        p->okmask &= mask;
162
        /* Record the first instance of this return */
163 82520
        if (p->return_tok[returns] == NULL)
164 82303
                p->return_tok[returns] = t;
165 82520
}
166
167
static int
168
vcc_CheckActionRecurse(struct vcc *tl, struct proc *p, unsigned bitmap)
169 298871
{
170 298871
        unsigned u;
171 298871
        struct proccall *pc;
172
173 298871
        AN(p);
174 298871
        if (p->active) {
175 2
                VSB_cat(tl->sb, "Subroutine recurses on\n");
176 2
                vcc_ErrWhere(tl, p->name);
177 2
                return (1);
178 2
        }
179 298869
        u = p->ret_bitmap & ~bitmap;
180 298869
        if (u) {
181
182 1
#define VCL_RET_MAC(l, U, B)                                            \
183 18
                if (u & (1 << (VCL_RET_##U))) {                         \
184 1
                        VSB_printf(tl->sb, "Invalid return \"" #l "\"\n");\
185 1
                        vcc_ErrWhere(tl, p->return_tok[VCL_RET_##U]);   \
186 1
                }
187 1
#include "tbl/vcl_returns.h"
188
189 1
                VSB_printf(tl->sb, "\n...in subroutine \"%.*s\"\n",
190 1
                    PF(p->name));
191 1
                vcc_ErrWhere(tl, p->name);
192 1
                return (1);
193 1
        }
194
195
        // more references than calls -> sub is referenced for dynamic calls
196 298868
        u = (p->sym->nref > p->called);
197
198 298868
        p->active = 1;
199 298868
        VTAILQ_FOREACH(pc, &p->calls, list) {
200 166094
                if (pc->sym->proc == NULL) {
201 0
                        VSB_printf(tl->sb, "Subroutine %s does not exist\n",
202 0
                            pc->sym->name);
203 0
                        vcc_ErrWhere(tl, pc->t);
204 0
                        return (1);
205 0
                }
206 166094
                pc->sym->proc->calledfrom |= p->calledfrom;
207 166094
                pc->sym->proc->called++;
208 166094
                pc->sym->nref += u;
209 166094
                if (vcc_CheckActionRecurse(tl, pc->sym->proc, bitmap)) {
210 4
                        VSB_printf(tl->sb, "\n...called from \"%.*s\"\n",
211 4
                            PF(p->name));
212 4
                        vcc_ErrWhere(tl, pc->t);
213 4
                        return (1);
214 4
                }
215 166090
                p->okmask &= pc->sym->proc->okmask;
216 166090
        }
217 298864
        p->active = 0;
218 298864
        return (0);
219 298868
}
220
221
/*--------------------------------------------------------------------*/
222
223
static void
224
vcc_checkaction(struct vcc *tl, const struct symbol *sym)
225 132777
{
226 132777
        struct proc *p;
227 132777
        unsigned bitmap;
228
229 132777
        p = sym->proc;
230 132777
        AN(p);
231 132777
        AN(p->name);
232
233 132777
        if (p->method == NULL) {
234 87538
                bitmap = ~0U;
235 87538
        } else {
236 45239
                bitmap = p->method->ret_bitmap;
237 45239
                p->calledfrom = p->method->bitval;
238 45239
        }
239
240 132777
        if (! vcc_CheckActionRecurse(tl, p, bitmap))
241 132774
                return;
242
243 3
        tl->err = 1;
244 3
        if (p->method == NULL)
245 2
                return;
246
247 1
        VSB_printf(tl->sb,
248 1
                   "\n...which is the \"%s\" subroutine\n", p->method->name);
249 1
        VSB_cat(tl->sb, "Legal returns are:");
250 1
#define VCL_RET_MAC(l, U, B)                                            \
251 18
        if (p->method->ret_bitmap & ((1 << VCL_RET_##U)))               \
252 18
                VSB_printf(tl->sb, " \"%s\"", #l);
253
254 1
#include "tbl/vcl_returns.h"
255 1
        VSB_cat(tl->sb, "\n");
256 1
}
257
258
int
259
vcc_CheckAction(struct vcc *tl)
260 3018
{
261
262 3018
        VCC_WalkSymbols(tl, vcc_checkaction, SYM_MAIN, SYM_SUB);
263 3018
        return (tl->err);
264 3018
}
265
266
/*--------------------------------------------------------------------*/
267
268
static struct procuse *
269
vcc_illegal_write(struct vcc *tl, struct procuse *pu, const struct method *m)
270 259518
{
271
272 259518
        if (pu->mask || pu->use != XREF_WRITE)
273 259505
                return (NULL);
274
275 13
        if (pu->sym->r_methods == 0) {
276 1
                vcc_ErrWhere2(tl, pu->t1, pu->t2);
277 1
                VSB_cat(tl->sb, "Variable cannot be set.\n");
278 1
                return (NULL);
279 1
        }
280
281 12
        if (!(pu->sym->r_methods & m->bitval)) {
282 2
                pu->use = XREF_READ; /* NB: change the error message. */
283 2
                return (pu);
284 2
        }
285
286 10
        vcc_ErrWhere2(tl, pu->t1, pu->t2);
287 10
        VSB_cat(tl->sb, "Variable is read only.\n");
288 10
        return (NULL);
289 12
}
290
291
static struct procuse *
292
vcc_FindIllegalUse(struct vcc *tl, struct proc *p, const struct method *m)
293 429253
{
294 429253
        struct procuse *pu, *pw, *r = NULL;
295
296 886943
        VTAILQ_FOREACH(pu, &p->uses, list) {
297 886943
                p->okmask &= pu->mask;
298 886943
                if (m == NULL)
299 627425
                        continue;
300 259518
                pw = vcc_illegal_write(tl, pu, m);
301 259518
                if (r != NULL)
302 7
                        continue;
303 259511
                if (tl->err)
304 11
                        r = pw;
305 259500
                else if (!(pu->mask & m->bitval))
306 29
                        r = pu;
307 259511
        }
308 429253
        return (r);
309 429253
}
310
311
static int
312
vcc_CheckUseRecurse(struct vcc *tl, struct proc *p,
313
    const struct method *m)
314 297256
{
315 297256
        struct proccall *pc;
316 297256
        struct procuse *pu;
317
318 297256
        pu = vcc_FindIllegalUse(tl, p, m);
319 297256
        if (pu != NULL) {
320 3
                vcc_ErrWhere2(tl, pu->t1, pu->t2);
321 3
                VSB_printf(tl->sb, "%s from subroutine '%s'.\n",
322 3
                    pu->use->err, m->name);
323 3
                VSB_printf(tl->sb, "\n...in subroutine \"%.*s\"\n",
324 3
                    PF(pu->fm->name));
325 3
                vcc_ErrWhere(tl, p->name);
326 3
                return (1);
327 3
        }
328 297253
        if (tl->err)
329 1
                return (1);
330 297252
        VTAILQ_FOREACH(pc, &p->calls, list) {
331 165295
                if (vcc_CheckUseRecurse(tl, pc->sym->proc, m)) {
332 5
                        VSB_printf(tl->sb, "\n...called from \"%.*s\"\n",
333 5
                            PF(p->name));
334 5
                        vcc_ErrWhere(tl, pc->t);
335 5
                        return (1);
336 5
                }
337 165290
                p->okmask &= pc->sym->proc->okmask;
338 165290
        }
339 297247
        return (0);
340 297252
}
341
342
static void
343
vcc_checkuses(struct vcc *tl, const struct symbol *sym)
344 131997
{
345 131997
        struct proc *p;
346 131997
        struct procuse *pu;
347
348 131997
        p = sym->proc;
349 131997
        AN(p);
350 131997
        pu = vcc_FindIllegalUse(tl, p, p->method);
351 131997
        if (pu != NULL) {
352 26
                vcc_ErrWhere2(tl, pu->t1, pu->t2);
353 26
                VSB_printf(tl->sb, "%s in subroutine '%.*s'.",
354 26
                    pu->use->err, PF(p->name));
355 26
                VSB_cat(tl->sb, "\nAt: ");
356 26
                return;
357 26
        }
358 131971
        ERRCHK(tl);
359 131961
        if (vcc_CheckUseRecurse(tl, p, p->method)) {
360 4
                VSB_printf(tl->sb,
361 4
                    "\n...which is the \"%s\" subroutine\n", p->method->name);
362 4
                return;
363 4
        }
364 131961
}
365
366
/*
367
 * Used from a second symbol walk because vcc_checkuses is more precise for
368
 * subroutines called from methods. We catch here subs used for dynamic calls
369
 * and with vcc_err_unref = off
370
 */
371
static void
372
vcc_checkpossible(struct vcc *tl, const struct symbol *sym)
373 130887
{
374 130887
        struct proc *p;
375
376 130887
        p = sym->proc;
377 130887
        AN(p);
378
379 130887
        if (p->okmask != 0)
380 130885
                return;
381
382 2
        VSB_cat(tl->sb, "Impossible Subroutine");
383 2
        vcc_ErrWhere(tl, p->name);
384 2
}
385
386
int
387
vcc_CheckUses(struct vcc *tl)
388 3015
{
389
390 3015
        VCC_WalkSymbols(tl, vcc_checkuses, SYM_MAIN, SYM_SUB);
391 3015
        if (tl->err)
392 40
                return (tl->err);
393 2975
        VCC_WalkSymbols(tl, vcc_checkpossible, SYM_MAIN, SYM_SUB);
394 2975
        return (tl->err);
395 3015
}
396
397
/*---------------------------------------------------------------------*/
398
399
static void v_matchproto_(symwalk_f)
400
vcc_instance_info(struct vcc *tl, const struct symbol *sym)
401 194
{
402
403 194
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
404 194
        AN(sym->rname);
405 194
        Fc(tl, 0, "\t{ .p = (uintptr_t *)&%s, .name = \"", sym->rname);
406 194
        VCC_SymName(tl->fc, sym);
407 194
        Fc(tl, 0, "\" },\n");
408 194
}
409
410
void
411
VCC_InstanceInfo(struct vcc *tl)
412 1329
{
413 1329
        Fc(tl, 0, "\nstatic const struct vpi_ii VGC_instance_info[] = {\n");
414 1329
        VCC_WalkSymbols(tl, vcc_instance_info, SYM_MAIN, SYM_INSTANCE);
415 1329
        Fc(tl, 0, "\t{ .p = NULL, .name = \"\" }\n");
416 1329
        Fc(tl, 0, "};\n");
417 1329
}
418
419
/*---------------------------------------------------------------------*/
420
421
static int sym_type_len;
422
423
static void v_matchproto_(symwalk_f)
424
vcc_xreftable_len(struct vcc *tl, const struct symbol *sym)
425 373854
{
426 373854
        int len;
427
428 373854
        (void)tl;
429 373854
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
430 373854
        CHECK_OBJ_NOTNULL(sym->type, TYPE_MAGIC);
431 373854
        len = vstrlen(sym->type->name);
432 373854
        if (sym_type_len < len)
433 8296
                sym_type_len = len;
434 373854
}
435
436
static void v_matchproto_(symwalk_f)
437
vcc_xreftable(struct vcc *tl, const struct symbol *sym)
438 373854
{
439
440 373854
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
441 373854
        CHECK_OBJ_NOTNULL(sym->kind, KIND_MAGIC);
442 373854
        CHECK_OBJ_NOTNULL(sym->type, TYPE_MAGIC);
443 373854
        Fc(tl, 0, " * %-8s ", sym->kind->name);
444 373854
        Fc(tl, 0, " %-*s ", sym_type_len, sym->type->name);
445 373854
        Fc(tl, 0, " %2u %2u ", sym->lorev, sym->hirev);
446 373854
        VCC_SymName(tl->fc, sym);
447 373854
        if (sym->wildcard != NULL)
448 10632
                Fc(tl, 0, "*");
449 373854
        Fc(tl, 0, "\n");
450 373854
}
451
452
void
453
VCC_XrefTable(struct vcc *tl)
454 1329
{
455
456 1329
#define VCC_NAMESPACE(U, l)                                             \
457 2658
        Fc(tl, 0, "\n/*\n * Symbol Table " #U "\n *\n");                \
458 2658
        sym_type_len = 0;                                               \
459 2658
        VCC_WalkSymbols(tl, vcc_xreftable_len, SYM_##U, SYM_NONE);      \
460 2658
        VCC_WalkSymbols(tl, vcc_xreftable, SYM_##U, SYM_NONE);          \
461 2658
        Fc(tl, 0, "*/\n\n");
462 1329
#include "vcc_namespace.h"
463 1329
}