flush
[m6w6/ext-psi] / src / types / decl.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #if __GNUC__ >= 5
33 # pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
34 #endif
35 #include "php_psi_macros.h"
36 #include "php_psi_redirs.h"
37 #if __GNUC__ >= 5
38 # pragma GCC diagnostic pop
39 #endif
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <sys/param.h>
44 #include <dlfcn.h>
45
46 #include "data.h"
47
48 decl *init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
49 decl *d = calloc(1, sizeof(*d));
50 d->abi = abi;
51 d->func = func;
52 d->args = args;
53 return d;
54 }
55
56 void free_decl(decl *d) {
57 free_decl_abi(d->abi);
58 free_decl_arg(d->func);
59 if (d->args) {
60 free_decl_args(d->args);
61 }
62 free(d);
63 }
64
65 void dump_decl(int fd, decl *decl) {
66 dump_decl_abi(fd, decl->abi);
67 dprintf(fd, " ");
68 dump_decl_arg(fd, decl->func, 0);
69 dprintf(fd, "(");
70 if (decl->args) {
71 dump_decl_args(fd, decl->args, 0);
72 }
73 dprintf(fd, ");");
74 }
75
76 static inline int validate_decl_func(struct psi_data *data, void *dl, decl *decl, decl_arg *func)
77 {
78 struct psi_func_redir *redir;
79
80 if (!strcmp(func->var->name, "dlsym")) {
81 data->error(data, func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)");
82 return 0;
83 }
84
85 for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
86 if (!strcmp(func->var->name, redir->name)) {
87 decl->call.sym = redir->func;
88 }
89 }
90 if (!decl->call.sym) {
91 #ifndef RTLD_NEXT
92 # define RTLD_NEXT ((void *) -1l)
93 #endif
94 decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
95 if (!decl->call.sym) {
96 data->error(data, func->token, PSI_WARNING,
97 "Failed to locate symbol '%s': %s",
98 func->var->name, dlerror() ?: "not found");
99 }
100 }
101 return 1;
102 }
103
104 int validate_decl(struct psi_data *data, void *dl, decl *decl) {
105 if (!validate_decl_nodl(data, decl)) {
106 return 0;
107 }
108 if (!validate_decl_func(data, dl, decl, decl->func)) {
109 return 0;
110 }
111 return 1;
112 }
113
114 int validate_decl_nodl(struct psi_data *data, decl *decl) {
115 if (!validate_decl_abi(data, decl->abi)) {
116 data->error(data, decl->abi->token, PSI_WARNING,
117 "Invalid calling convention: '%s'", decl->abi->token->text);
118 return 0;
119 }
120 if (!validate_decl_arg(data, decl->func)) {
121 return 0;
122 }
123 if (decl->args) {
124 size_t i;
125
126 for (i = 0; i < decl->args->count; ++i) {
127 if (!validate_decl_arg(data, decl->args->args[i])) {
128 return 0;
129 }
130 }
131 }
132 return 1;
133 }