1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <assert.h>
27 #include <strings.h>
28 #include <alloca.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 
32 #include <dt_parser.h>
33 #include <dt_impl.h>
34 #include <dt_provider.h>
35 #include <dt_module.h>
36 
37 /*
38  * This callback function is installed in a given identifier hash to search for
39  * and apply deferred pragmas that are pending for a given new identifier name.
40  * Multiple pragmas may be pending for a given name; we processs all of them.
41  */
42 /*ARGSUSED*/
43 static void
44 dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
45 {
46 	dt_idhash_t *php;
47 	dt_ident_t *pdp;
48 
49 	if ((php = yypcb->pcb_pragmas) == NULL)
50 		return; /* no pragmas pending for current compilation pass */
51 
52 	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
53 		switch (pdp->di_kind) {
54 		case DT_IDENT_PRAGAT:
55 			idp->di_attr = pdp->di_attr;
56 			break;
57 		case DT_IDENT_PRAGBN:
58 			idp->di_vers = pdp->di_vers;
59 			break;
60 		}
61 		dt_idhash_delete(php, pdp);
62 	}
63 }
64 
65 /*
66  * The #pragma attributes directive can be used to reset stability attributes
67  * on a global identifier or inline definition.  If the identifier is already
68  * defined, we can just change di_attr.  If not, we insert the pragma into a
69  * hash table of the current pcb's deferred pragmas for later processing.
70  */
71 static void
72 dt_pragma_attributes(const char *prname, dt_node_t *dnp)
73 {
74 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
75 	dtrace_attribute_t attr, *a;
76 	dt_provider_t *pvp;
77 	const char *name, *part;
78 	dt_ident_t *idp;
79 
80 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
81 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
82 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
83 		    "<attributes> <ident>\n", prname);
84 	}
85 
86 	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
87 		xyerror(D_PRAGMA_INVAL, "invalid attributes "
88 		    "specified by #pragma %s\n", prname);
89 	}
90 
91 	dnp = dnp->dn_list;
92 	name = dnp->dn_string;
93 
94 	if (strcmp(name, "provider") == 0) {
95 		dnp = dnp->dn_list;
96 		name = dnp->dn_string;
97 
98 		dnp = dnp->dn_list;
99 		part = dnp->dn_string;
100 
101 		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
102 			if (strcmp(part, "provider") == 0) {
103 				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
104 			} else if (strcmp(part, "module") == 0) {
105 				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
106 			} else if (strcmp(part, "function") == 0) {
107 				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
108 			} else if (strcmp(part, "name") == 0) {
109 				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
110 			} else if (strcmp(part, "args") == 0) {
111 				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
112 			} else {
113 				xyerror(D_PRAGMA_INVAL, "invalid component "
114 				    "\"%s\" in attribute #pragma "
115 				    "for provider %s\n", name, part);
116 			}
117 
118 			*a = attr;
119 			return;
120 		}
121 
122 	} else if ((idp = dt_idstack_lookup(
123 	    &yypcb->pcb_globals, name)) != NULL) {
124 
125 		if (idp->di_gen != dtp->dt_gen) {
126 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
127 			    "entity defined outside program scope\n", prname);
128 		}
129 
130 		idp->di_attr = attr;
131 		return;
132 	}
133 
134 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
135 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
136 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
137 
138 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
139 	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
140 
141 	if (idp == NULL)
142 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
143 
144 	if (dtp->dt_globals->dh_defer == NULL)
145 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
146 }
147 
148 /*
149  * The #pragma binding directive can be used to reset the version binding
150  * on a global identifier or inline definition.  If the identifier is already
151  * defined, we can just change di_vers.  If not, we insert the pragma into a
152  * hash table of the current pcb's deferred pragmas for later processing.
153  */
154 static void
155 dt_pragma_binding(const char *prname, dt_node_t *dnp)
156 {
157 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
158 	dt_version_t vers;
159 	const char *name;
160 	dt_ident_t *idp;
161 
162 	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
163 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
164 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
165 		    "\"version\" <ident>\n", prname);
166 	}
167 
168 	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
169 		xyerror(D_PRAGMA_INVAL, "invalid version string "
170 		    "specified by #pragma %s\n", prname);
171 	}
172 
173 	name = dnp->dn_list->dn_string;
174 	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
175 
176 	if (idp != NULL) {
177 		if (idp->di_gen != dtp->dt_gen) {
178 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
179 			    "entity defined outside program scope\n", prname);
180 		}
181 		idp->di_vers = vers;
182 		return;
183 	}
184 
185 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
186 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
187 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
188 
189 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
190 	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
191 
192 	if (idp == NULL)
193 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
194 
195 	if (dtp->dt_globals->dh_defer == NULL)
196 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
197 }
198 
199 /*
200  * The #pragma depends_on directive can be used to express a dependency on a
201  * module, provider or library which if not present will cause processing to
202  * abort.
203  */
204 static void
205 dt_pragma_depends(const char *prname, dt_node_t *cnp)
206 {
207 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
208 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
209 	int found;
210 	dt_lib_depend_t *dld;
211 	char lib[MAXPATHLEN];
212 
213 	if (cnp == NULL || nnp == NULL ||
214 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
215 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
216 		    "<class> <name>\n", prname);
217 	}
218 
219 	if (strcmp(cnp->dn_string, "provider") == 0)
220 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
221 	else if (strcmp(cnp->dn_string, "module") == 0) {
222 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
223 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
224 	} else if (strcmp(cnp->dn_string, "library") == 0) {
225 		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
226 			assert(dtp->dt_filetag != NULL);
227 
228 			/*
229 			 * We have the file we are working on in dtp->dt_filetag
230 			 * so find that node and add the dependency in.
231 			 */
232 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
233 			    dtp->dt_filetag);
234 			assert(dld != NULL);
235 
236 			(void) snprintf(lib, sizeof (lib), "%s%s",
237 			    dld->dtld_libpath, nnp->dn_string);
238 			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
239 			    lib)) != 0) {
240 				xyerror(D_PRAGMA_DEPEND,
241 				    "failed to add dependency %s:%s\n", lib,
242 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
243 			}
244 		} else {
245 			/*
246 			 * By this point we have already performed a topological
247 			 * sort of the dependencies; we process this directive
248 			 * as satisfied as long as the dependency was properly
249 			 * loaded.
250 			 */
251 			if (dtp->dt_filetag == NULL)
252 				xyerror(D_PRAGMA_DEPEND, "main program may "
253 				    "not explicitly depend on a library");
254 
255 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
256 			    dtp->dt_filetag);
257 			assert(dld != NULL);
258 
259 			(void) snprintf(lib, sizeof (lib), "%s%s",
260 			    dld->dtld_libpath, nnp->dn_string);
261 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
262 			    lib);
263 			assert(dld != NULL);
264 
265 			if (!dld->dtld_loaded)
266 				xyerror(D_PRAGMA_DEPEND, "program requires "
267 				    "library \"%s\" which failed to load",
268 				    lib);
269 		}
270 
271 		found = B_TRUE;
272 	} else {
273 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
274 		    "specified by #pragma %s\n", cnp->dn_string, prname);
275 	}
276 
277 	if (!found) {
278 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
279 		    cnp->dn_string, nnp->dn_string);
280 	}
281 }
282 
283 /*
284  * The #pragma error directive can be followed by any list of tokens, which we
285  * just concatenate and print as part of our error message.
286  */
287 static void
288 dt_pragma_error(const char *prname, dt_node_t *dnp)
289 {
290 	dt_node_t *enp;
291 	size_t n = 0;
292 	char *s;
293 
294 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
295 		if (enp->dn_kind == DT_NODE_IDENT ||
296 		    enp->dn_kind == DT_NODE_STRING)
297 			n += strlen(enp->dn_string) + 1;
298 	}
299 
300 	s = alloca(n + 1);
301 	s[0] = '\0';
302 
303 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
304 		if (enp->dn_kind == DT_NODE_IDENT ||
305 		    enp->dn_kind == DT_NODE_STRING) {
306 			(void) strcat(s, enp->dn_string);
307 			(void) strcat(s, " ");
308 		}
309 	}
310 
311 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
312 }
313 
314 /*ARGSUSED*/
315 static void
316 dt_pragma_ident(const char *prname, dt_node_t *dnp)
317 {
318 	/* ignore any #ident or #pragma ident lines */
319 }
320 
321 static void
322 dt_pragma_option(const char *prname, dt_node_t *dnp)
323 {
324 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
325 	char *opt, *val;
326 
327 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
328 		xyerror(D_PRAGMA_MALFORM,
329 		    "malformed #pragma %s <option>=<val>\n", prname);
330 	}
331 
332 	if (dnp->dn_list != NULL) {
333 		xyerror(D_PRAGMA_MALFORM,
334 		    "superfluous arguments specified for #pragma %s\n", prname);
335 	}
336 
337 	opt = strdupa(dnp->dn_string);
338 
339 	if ((val = strchr(opt, '=')) != NULL)
340 		*val++ = '\0';
341 
342 	if (dtrace_setopt(dtp, opt, val) == -1) {
343 		if (val == NULL) {
344 			xyerror(D_PRAGMA_OPTSET,
345 			    "failed to set option '%s': %s\n", opt,
346 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
347 		} else {
348 			xyerror(D_PRAGMA_OPTSET,
349 			    "failed to set option '%s' to '%s': %s\n",
350 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
351 		}
352 	}
353 }
354 
355 /*
356  * The #line directive is used to reset the input line number and to optionally
357  * note the file name for use in error messages.  Sun cpp(1) also produces a
358  * third integer token after the filename which is one of the following:
359  *
360  * 0 - line change has nothing to do with an #include file
361  * 1 - line change because we just entered a #include file
362  * 2 - line change because we just exited a #include file
363  *
364  * We use these state tokens to adjust pcb_idepth, which in turn controls
365  * whether type lookups access the global type space or not.
366  */
367 static void
368 dt_pragma_line(const char *prname, dt_node_t *dnp)
369 {
370 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
371 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
372 
373 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
374 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
375 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
376 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
377 		    "<line> [ [\"file\"] state ]\n", prname);
378 	}
379 
380 	/*
381 	 * If a file is specified, free any old pcb_filetag and swap fnp's
382 	 * dn_string into pcb_filetag as the new filename for error messages.
383 	 */
384 	if (fnp != NULL) {
385 		if (yypcb->pcb_filetag != NULL)
386 			free(yypcb->pcb_filetag);
387 
388 		/*
389 		 * This is not pretty, but is a necessary evil until we either
390 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
391 		 * the filename begins with /dev/fd, we know it's the master
392 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
393 		 * dt_filetag pointer so error messages refer to the main file.
394 		 */
395 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
396 			yypcb->pcb_filetag = fnp->dn_string;
397 			fnp->dn_string = NULL;
398 		} else
399 			yypcb->pcb_filetag = NULL;
400 	}
401 
402 	if (inp != NULL) {
403 		if (inp->dn_value == 1)
404 			yypcb->pcb_idepth++;
405 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
406 			yypcb->pcb_idepth--;
407 	}
408 
409 	yylineno = dnp->dn_value;
410 }
411 
412 /*
413  * D compiler pragma types range from control directives to common pragmas to
414  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
415  * as a special prefix for our pragmas so they can be used in mixed headers.
416  */
417 #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
418 #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
419 #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
420 
421 static const struct dt_pragmadesc {
422 	const char *dpd_name;
423 	void (*dpd_func)(const char *, dt_node_t *);
424 	int dpd_kind;
425 } dt_pragmas[] = {
426 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
427 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
428 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
429 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
430 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
431 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
432 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
433 	{ NULL, NULL }
434 };
435 
436 /*
437  * Process a control line #directive by looking up the directive name in our
438  * lookup table and invoking the corresponding function with the token list.
439  * According to K&R[A12.9], we silently ignore null directive lines.
440  */
441 void
442 dt_pragma(dt_node_t *pnp)
443 {
444 	const struct dt_pragmadesc *dpd;
445 	dt_node_t *dnp;
446 	int kind = DT_PRAGMA_DIR;
447 
448 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
449 		if (dnp->dn_kind == DT_NODE_INT) {
450 			dt_pragma_line("line", dnp);
451 			break;
452 		}
453 
454 		if (dnp->dn_kind != DT_NODE_IDENT)
455 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
456 
457 		if (kind == DT_PRAGMA_DIR &&
458 		    strcmp(dnp->dn_string, "pragma") == 0) {
459 			kind = DT_PRAGMA_SUB;
460 			continue;
461 		}
462 
463 		if (kind == DT_PRAGMA_SUB &&
464 		    strcmp(dnp->dn_string, "D") == 0) {
465 			kind = DT_PRAGMA_DCP;
466 			continue;
467 		}
468 
469 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
470 			if (dpd->dpd_kind <= kind &&
471 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
472 				break;
473 		}
474 
475 		yylineno--; /* since we've already seen \n */
476 
477 		if (dpd->dpd_name != NULL) {
478 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
479 			yylineno++;
480 			break;
481 		}
482 
483 		switch (kind) {
484 		case DT_PRAGMA_DIR:
485 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
486 			    "#%s\n", dnp->dn_string);
487 			/*NOTREACHED*/
488 		case DT_PRAGMA_SUB:
489 			break; /* K&R[A12.8] says to ignore unknown pragmas */
490 		case DT_PRAGMA_DCP:
491 		default:
492 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
493 			    dnp->dn_string);
494 		}
495 
496 		yylineno++;
497 		break;
498 	}
499 
500 	dt_node_list_free(&pnp);
501 }
502