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