94f43368c978c306f04f7f6c0456550330e5e2bc
[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 #include "php_psi.h"
32
33 #include <dlfcn.h>
34 #include <fnmatch.h>
35
36 #include <Zend/zend_smart_str.h>
37
38 #include "data.h"
39
40 #define PSI_FUNC_REDIRS
41 #include "php_psi_predef.h"
42
43 struct psi_decl *psi_decl_init(struct psi_decl_arg *func, struct psi_plist *args)
44 {
45 struct psi_decl *d = pecalloc(1, sizeof(*d), 1);
46
47 d->func = func;
48 d->args = args;
49
50 return d;
51 }
52
53 void psi_decl_free(struct psi_decl **d_ptr)
54 {
55 if (*d_ptr) {
56 struct psi_decl *d = *d_ptr;
57
58 *d_ptr = NULL;
59
60 psi_decl_abi_free(&d->abi);
61 psi_decl_arg_free(&d->func);
62 if (d->args) {
63 psi_plist_free(d->args);
64 }
65 if (d->redir) {
66 zend_string_release(d->redir);
67 }
68 free(d);
69 }
70 }
71
72 void psi_decl_dump(struct psi_dump *dump, struct psi_decl *decl)
73 {
74 if (decl->abi) {
75 psi_decl_abi_dump(dump, decl->abi);
76 }
77 PSI_DUMP(dump, " ");
78 /* FIXME: functions returning arrays */
79 psi_decl_arg_dump(dump, decl->func, 0);
80 PSI_DUMP(dump, "(");
81 if (decl->args) {
82 size_t i;
83 struct psi_decl_arg *arg;
84
85 for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
86 if (i) {
87 PSI_DUMP(dump, ", ");
88 }
89 psi_decl_arg_dump(dump, arg, 0);
90 }
91 if (decl->varargs) {
92 PSI_DUMP(dump, ", ...");
93 }
94 }
95 if (decl->func->var->array_size) {
96 PSI_DUMP(dump, ")[%u]", decl->func->var->array_size);
97 }
98 if (decl->redir) {
99 PSI_DUMP(dump, ") __asm__ (\"%s\");", decl->redir->val);
100 } else {
101 PSI_DUMP(dump, ");");
102 }
103 }
104
105 static inline bool psi_decl_validate_func(struct psi_data *data,
106 struct psi_decl *decl, struct psi_decl_arg *func)
107 {
108 struct psi_func_redir *redir;
109
110 if (!func->var->name) {
111 data->error(data, func->token, PSI_WARNING, "Cannot load anonymous decl");
112 return false;
113 }
114
115 for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
116 if (!strcmp(func->var->name->val, redir->name)) {
117 decl->sym = redir->func;
118 break;
119 }
120 }
121 if (!decl->sym) {
122 decl->sym = psi_dlsym(data->file.dlopened, func->var->name->val,
123 decl->redir ? decl->redir->val : NULL);
124 }
125 if (!decl->sym) {
126 data->error(data, func->token, PSI_WARNING,
127 "Failed to locate symbol '%s(%s)': %s",
128 func->var->name->val,
129 decl->redir ? decl->redir->val : "",
130 dlerror() ?: "not found");
131 return false;
132 }
133 return true;
134 }
135
136 bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl,
137 struct psi_validate_scope *scope)
138 {
139 if (!psi_decl_validate_nodl(data, decl, scope)) {
140 return false;
141 }
142 if (!psi_decl_validate_func(data, decl, decl->func)) {
143 return false;
144 }
145
146 return true;
147 }
148
149 bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl,
150 struct psi_validate_scope *scope)
151 {
152 if (!decl->abi) {
153 decl->abi = psi_decl_abi_init(NULL);
154 } else if (!psi_decl_abi_validate(data, decl->abi)) {
155 data->error(data, decl->abi->token, PSI_WARNING,
156 "Invalid calling convention: '%s'",
157 decl->abi->token->text->val);
158 return false;
159 }
160 if (!psi_decl_arg_validate(data, decl->func, scope)) {
161 return false;
162 }
163 if (decl->args) {
164 size_t i = 0;
165 struct psi_decl_arg *arg;
166
167 while (psi_plist_get(decl->args, i++, &arg)) {
168 if (!arg->var->name) {
169 smart_str name = {0};
170
171 smart_str_appendl_ex(&name, ZEND_STRL("arg"), 1);
172 smart_str_append_unsigned_ex(&name, i, 1);
173
174 arg->var->name = smart_str_extract(&name);
175 arg->var->fqn = zend_string_copy(arg->var->name);
176 }
177 if (!psi_decl_arg_validate(data, arg, scope)) {
178 return false;
179 }
180 }
181 }
182
183 return true;
184 }
185
186 bool psi_decl_is_blacklisted(const char *name)
187 {
188 char *blacklisted;
189 size_t i = 0;
190
191 while (psi_plist_get(PSI_G(blacklist).decls, i++, &blacklisted)) {
192 if (!fnmatch(blacklisted, name, 0)) {
193 return true;
194 }
195 }
196 return false;
197 }
198
199 struct psi_decl_arg *psi_decl_get_arg(struct psi_decl *decl, struct psi_decl_var *var) {
200 if (var->arg) {
201 size_t i = 0;
202 struct psi_decl_arg *arg = decl->func;
203
204 do {
205 if (var->arg == arg) {
206 return arg;
207 }
208 } while (psi_plist_get(decl->args, i++, &arg));
209 }
210
211 return psi_decl_arg_get_by_var(var, decl->args, decl->func);
212 }