1 /*- 2 * Copyright (c) 2006 Verdens Gang AS 3 * Copyright (c) 2006-2015 Varnish Software AS 4 * All rights reserved. 5 * 6 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk> 7 * 8 * SPDX-License-Identifier: BSD-2-Clause 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 #include <stdio.h> 34 #include <stdint.h> 35 36 #include "miniobj.h" 37 #include "vdef.h" 38 #include "vas.h" 39 #include "vrt.h" 40 #include "vcl.h" 41 #include "vqueue.h" 42 #include "vsb.h" 43 44 #include "vcc_token_defs.h" 45 46 /*--------------------------------------------------------------------- 47 * VCL version stuff 48 */ 49 50 #define VCL_LOW 40 // Lowest VCC supports 51 #define VCL_HIGH 41 // Highest VCC supports 52 53 // Specific VCL versions 54 #define VCL_40 40 55 #define VCL_41 41 56 57 /*---------------------------------------------------------------------*/ 58 59 struct vsb; 60 struct token; 61 struct sockaddr_storage; 62 struct method; 63 struct symtab; 64 65 unsigned vcl_fixed_token(const char *p, const char **q); 66 extern const char * const vcl_tnames[256]; 67 void vcl_output_lang_h(struct vsb *sb); 68 69 #define PF(t) (int)((t)->e - (t)->b), (t)->b 70 71 #define INDENT 2 72 73 struct acl_e; 74 struct proc; 75 struct expr; 76 struct vcc; 77 struct vjsn_val; 78 struct symbol; 79 struct vmod_obj; 80 81 struct source { 82 VTAILQ_ENTRY(source) list; 83 char *name; 84 const char *b; 85 const char *e; 86 unsigned idx; 87 char *freeit; 88 const struct source *parent; 89 const struct token *parent_tok; 90 }; 91 92 struct token { 93 unsigned tok; 94 const char *b; 95 const char *e; 96 const struct source *src; 97 VTAILQ_ENTRY(token) list; 98 unsigned cnt; 99 char *dec; 100 }; 101 102 /*---------------------------------------------------------------------*/ 103 104 typedef const struct type *vcc_type_t; 105 106 struct type { 107 unsigned magic; 108 #define TYPE_MAGIC 0xfae932d9 109 110 const char *name; 111 const struct vcc_method *methods; 112 113 const char *tostring; 114 vcc_type_t multype; 115 int stringform; 116 }; 117 118 #define VCC_TYPE(UC, lc) extern const struct type UC[1]; 119 #include "vcc_types.h" 120 121 /*---------------------------------------------------------------------*/ 122 123 typedef const struct kind *vcc_kind_t; 124 125 struct kind { 126 unsigned magic; 127 #define KIND_MAGIC 0xfad72443 128 129 const char *name; 130 }; 131 132 #define VCC_KIND(U,l) extern const struct kind SYM_##U[1]; 133 #include "tbl/symbol_kind.h" 134 135 /*---------------------------------------------------------------------*/ 136 137 typedef const struct vcc_namespace *const vcc_ns_t; 138 139 #define VCC_NAMESPACE(U, l) extern vcc_ns_t SYM_##U; 140 #include "vcc_namespace.h" 141 142 enum vcc_namespace_e { 143 #define VCC_NAMESPACE(U, l) VCC_NAMESPACE_##U, 144 #include "vcc_namespace.h" 145 VCC_NAMESPACE__MAX 146 }; 147 148 /*---------------------------------------------------------------------*/ 149 150 typedef void sym_expr_t(struct vcc *tl, struct expr **, 151 struct token *, struct symbol *sym, vcc_type_t); 152 typedef void sym_wildcard_t(struct vcc *, struct symbol *, struct symbol *); 153 154 typedef void sym_act_f(struct vcc *, struct token *, struct symbol *); 155 156 struct symbol { 157 unsigned magic; 158 #define SYMBOL_MAGIC 0x3368c9fb 159 VTAILQ_ENTRY(symbol) list; 160 VTAILQ_ENTRY(symbol) sideways; 161 162 const char *name; 163 164 int lorev; 165 int hirev; 166 167 const struct symtab *symtab; 168 const char *vmod_name; 169 170 sym_wildcard_t *wildcard; 171 vcc_kind_t kind; 172 173 sym_act_f *action; 174 unsigned action_mask; 175 176 const struct token *def_b, *def_e, *ref_b; 177 178 vcc_type_t type; 179 180 sym_expr_t *eval; 181 const void *eval_priv; 182 183 /* xref.c */ 184 struct proc *proc; 185 unsigned noref, nref, ndef; 186 187 const char *extra; 188 189 /* SYM_VAR */ 190 const char *rname; 191 unsigned r_methods; 192 const char *lname; 193 unsigned w_methods; 194 const char *uname; 195 unsigned u_methods; 196 }; 197 198 VTAILQ_HEAD(tokenhead, token); 199 VTAILQ_HEAD(procprivhead, procpriv); 200 201 struct proc { 202 unsigned magic; 203 #define PROC_MAGIC 0xd1d98499 204 const struct method *method; 205 VTAILQ_HEAD(,proccall) calls; 206 VTAILQ_HEAD(,procuse) uses; 207 struct procprivhead priv_tasks; 208 struct procprivhead priv_tops; 209 VTAILQ_ENTRY(proc) list; 210 struct token *name; 211 unsigned ret_bitmap; 212 unsigned called; 213 unsigned active; 214 unsigned okmask; 215 struct token *return_tok[VCL_RET_MAX]; 216 struct vsb *cname; 217 struct vsb *prologue; 218 struct vsb *body; 219 struct symbol *sym; 220 }; 221 222 struct inifin { 223 unsigned magic; 224 #define INIFIN_MAGIC 0x583c274c 225 unsigned n; 226 unsigned ignore_errors; 227 struct vsb *ini; 228 struct vsb *fin; 229 struct vsb *final; 230 struct vsb *event; 231 VTAILQ_ENTRY(inifin) list; 232 }; 233 234 VTAILQ_HEAD(inifinhead, inifin); 235 236 struct vcc { 237 unsigned magic; 238 #define VCC_MAGIC 0x24ad719d 239 int syntax; 240 241 char *builtin_vcl; 242 struct vfil_path *vcl_path; 243 struct vfil_path *vmod_path; 244 #define MGT_VCC(t, n, cc) t n; 245 #include <tbl/mgt_vcc.h> 246 247 struct symtab *syms[VCC_NAMESPACE__MAX]; 248 249 struct inifinhead inifin; 250 unsigned ninifin; 251 252 /* Instance section */ 253 struct tokenhead tokens; 254 VTAILQ_HEAD(, source) sources; 255 unsigned nsources; 256 struct token *t; 257 int indent; 258 int hindent; 259 unsigned cnt; 260 261 struct vsb *symtab; /* VCC info to MGT */ 262 struct vsb *fc; /* C-code */ 263 struct vsb *fh; /* H-code (before C-code) */ 264 struct vsb *fb; /* Body of current sub 265 * NULL otherwise 266 */ 267 struct vsb *sb; 268 int err; 269 unsigned nsub; 270 unsigned subref; // SUB arguments present 271 struct proc *curproc; 272 VTAILQ_HEAD(, proc) procs; 273 274 VTAILQ_HEAD(, acl_e) acl; 275 276 int nprobe; 277 278 int ndirector; 279 struct symbol *first_director; 280 const char *default_director; 281 const char *default_probe; 282 283 VTAILQ_HEAD(, symbol) sym_objects; 284 VTAILQ_HEAD(, symbol) sym_vmods; 285 VTAILQ_HEAD(, vmod_obj) vmod_objects; 286 287 unsigned unique; 288 unsigned vmod_count; 289 }; 290 291 extern struct vcc *vcc_builtin; 292 293 struct method { 294 const char *name; 295 unsigned ret_bitmap; 296 unsigned bitval; 297 }; 298 299 /*--------------------------------------------------------------------*/ 300 301 /* vcc_acl.c */ 302 303 void vcc_ParseAcl(struct vcc *tl); 304 305 /* vcc_action.c */ 306 void vcc_Action_Init(struct vcc *); 307 308 /* vcc_backend.c */ 309 struct fld_spec; 310 311 const char *vcc_default_probe(struct vcc *); 312 void vcc_Backend_Init(struct vcc *tl); 313 void vcc_ParseProbe(struct vcc *tl); 314 void vcc_ParseBackend(struct vcc *tl); 315 struct fld_spec * vcc_FldSpec(struct vcc *tl, const char *first, ...); 316 void vcc_IsField(struct vcc *tl, const struct token **t, struct fld_spec *fs); 317 void vcc_FieldsOk(struct vcc *tl, const struct fld_spec *fs); 318 319 /* vcc_compile.c */ 320 struct inifin *New_IniFin(struct vcc *); 321 struct proc *vcc_NewProc(struct vcc*, struct symbol*); 322 323 /* 324 * H -> Header, before the C code 325 * C -> C-code 326 * B -> Body of function, ends up in C once function is completed 327 * I -> Initializer function 328 * F -> Finish function 329 */ 330 void Fh(const struct vcc *tl, int indent, const char *fmt, ...) 331 v_printflike_(3, 4); 332 void Fc(const struct vcc *tl, int indent, const char *fmt, ...) 333 v_printflike_(3, 4); 334 void Fb(const struct vcc *tl, int indent, const char *fmt, ...) 335 v_printflike_(3, 4); 336 void EncToken(struct vsb *sb, const struct token *t); 337 void *TlAlloc(struct vcc *tl, unsigned len); 338 char *TlDup(struct vcc *tl, const char *s); 339 340 /* vcc_expr.c */ 341 void vcc_Expr(struct vcc *tl, vcc_type_t typ); 342 sym_act_f vcc_Act_Call; 343 sym_act_f vcc_Act_Obj; 344 void vcc_Expr_Init(struct vcc *tl); 345 sym_expr_t vcc_Eval_Var; 346 sym_expr_t vcc_Eval_Handle; 347 sym_expr_t vcc_Eval_SymFunc; 348 sym_expr_t vcc_Eval_TypeMethod; 349 void vcc_Eval_Func(struct vcc *, const struct vjsn_val *, 350 const char *, struct symbol *); 351 void VCC_GlobalSymbol(struct symbol *, vcc_type_t fmt, const char *pfx); 352 struct symbol *VCC_HandleSymbol(struct vcc *, vcc_type_t , const char *); 353 void VCC_SymName(struct vsb *, const struct symbol *); 354 355 /* vcc_obj.c */ 356 void vcc_Var_Init(struct vcc *); 357 358 /* vcc_parse.c */ 359 void vcc_Parse(struct vcc *); 360 void vcc_Parse_Init(struct vcc *); 361 sym_act_f vcc_Act_If; 362 363 /* vcc_utils.c */ 364 void vcc_regexp(struct vcc *tl, struct vsb *vgc_name); 365 void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport, 366 const char **ipv4, const char **ipv4_ascii, const char **ipv6, 367 const char **ipv6_ascii, const char **p_ascii, int maxips, 368 const struct token *t_err, const char *errid); 369 double vcc_DurationUnit(struct vcc *); 370 void vcc_ByteVal(struct vcc *, double *); 371 void vcc_Duration(struct vcc *tl, double *); 372 unsigned vcc_UintVal(struct vcc *tl); 373 374 /* vcc_storage.c */ 375 void vcc_stevedore(struct vcc *vcc, const char *stv_name); 376 377 /* vcc_symb.c */ 378 void VCC_PrintCName(struct vsb *vsb, const char *b, const char *e); 379 struct symbol *VCC_MkSym(struct vcc *tl, const char *b, vcc_ns_t, vcc_kind_t, 380 int, int); 381 382 struct symxref { const char *name; }; 383 extern const struct symxref XREF_NONE[1]; 384 extern const struct symxref XREF_DEF[1]; 385 extern const struct symxref XREF_REF[1]; 386 387 struct symmode { 388 const char *name; 389 unsigned noerr; 390 unsigned partial; 391 }; 392 extern const struct symmode SYMTAB_NOERR[1]; 393 extern const struct symmode SYMTAB_CREATE[1]; 394 extern const struct symmode SYMTAB_EXISTING[1]; 395 extern const struct symmode SYMTAB_PARTIAL[1]; 396 extern const struct symmode SYMTAB_PARTIAL_NOERR[1]; 397 398 struct symbol *VCC_SymbolGet(struct vcc *, vcc_ns_t, vcc_kind_t, 399 const struct symmode *, const struct symxref *); 400 401 struct symbol *VCC_TypeSymbol(struct vcc *, vcc_kind_t, vcc_type_t); 402 403 typedef void symwalk_f(struct vcc *tl, const struct symbol *s); 404 void VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_ns_t, vcc_kind_t); 405 406 /* vcc_token.c */ 407 void vcc_Coord(const struct vcc *tl, struct vsb *vsb, 408 const struct token *t); 409 void vcc_ErrToken(const struct vcc *tl, const struct token *t); 410 void vcc_ErrWhere(struct vcc *, const struct token *); 411 void vcc_ErrWhere2(struct vcc *, const struct token *, const struct token *); 412 void vcc_Warn(struct vcc *); 413 414 void vcc__Expect(struct vcc *tl, unsigned tok, unsigned line); 415 int vcc_IdIs(const struct token *t, const char *p); 416 void vcc_ExpectVid(struct vcc *tl, const char *what); 417 void vcc_Lexer(struct vcc *tl, const struct source *sp, int eoi); 418 void vcc_NextToken(struct vcc *tl); 419 void vcc__ErrInternal(struct vcc *tl, const char *func, 420 unsigned line); 421 422 /* vcc_types.c */ 423 vcc_type_t VCC_Type(const char *p); 424 const char * VCC_Type_EvalMethod(struct vcc *, const struct symbol *); 425 void vcc_Type_Init(struct vcc *tl); 426 427 /* vcc_var.c */ 428 sym_wildcard_t vcc_Var_Wildcard; 429 430 /* vcc_vmod.c */ 431 void vcc_ParseImport(struct vcc *tl); 432 sym_act_f vcc_Act_New; 433 434 /* vcc_xref.c */ 435 int vcc_CheckReferences(struct vcc *tl); 436 void VCC_InstanceInfo(struct vcc *tl); 437 void VCC_XrefTable(struct vcc *); 438 439 void vcc_AddCall(struct vcc *, struct token *, struct symbol *); 440 void vcc_ProcAction(struct proc *, unsigned, unsigned, struct token *); 441 int vcc_CheckAction(struct vcc *tl); 442 443 444 struct xrefuse { const char *name, *err; }; 445 extern const struct xrefuse XREF_READ[1]; 446 extern const struct xrefuse XREF_WRITE[1]; 447 extern const struct xrefuse XREF_UNSET[1]; 448 extern const struct xrefuse XREF_ACTION[1]; 449 450 void vcc_AddUses(struct vcc *, const struct token *, const struct token *, 451 const struct symbol *sym, const struct xrefuse *use); 452 int vcc_CheckUses(struct vcc *tl); 453 const char *vcc_MarkPriv(struct vcc *, struct procprivhead *, 454 const char *); 455 456 #define ERRCHK(tl) do { if ((tl)->err) return; } while (0) 457 #define ErrInternal(tl) vcc__ErrInternal(tl, __func__, __LINE__) 458 #define Expect(a, b) vcc__Expect(a, b, __LINE__) 459 #define ExpectErr(a, b) \ 460 do { vcc__Expect(a, b, __LINE__); ERRCHK(a);} while (0) 461 #define SkipToken(a, b) \ 462 do { vcc__Expect(a, b, __LINE__); ERRCHK(a); vcc_NextToken(a); } while (0) 463 464 #define ACL_SYMBOL_PREFIX "vrt_acl_named" 465