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