250aa509bd87412c2c3d292b3a321ca63d00a755
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
29 # include "php_config.h"
33 #include <Zend/zend_smart_str.h>
39 #define PSI_FUNC_REDIRS
40 #include "php_psi_predef.h"
42 struct psi_decl
*psi_decl_init(struct psi_decl_arg
*func
, struct psi_plist
*args
)
44 struct psi_decl
*d
= pecalloc(1, sizeof(*d
), 1);
52 void psi_decl_free(struct psi_decl
**d_ptr
)
55 struct psi_decl
*d
= *d_ptr
;
59 psi_decl_abi_free(&d
->abi
);
60 psi_decl_arg_free(&d
->func
);
62 psi_plist_free(d
->args
);
65 zend_string_release(d
->redir
);
71 void psi_decl_dump(struct psi_dump
*dump
, struct psi_decl
*decl
)
74 psi_decl_abi_dump(dump
, decl
->abi
);
78 psi_decl_type_dump(dump
, decl
->func
->type
, 0);
80 PSI_DUMP(dump
, "%s%s",
81 psi_t_indirection(decl
->func
->var
->pointer_level
- !!decl
->func
->var
->array_size
),
82 decl
->func
->var
->name
->val
);
87 struct psi_decl_arg
*arg
;
89 for (i
= 0; psi_plist_get(decl
->args
, i
, &arg
); ++i
) {
93 psi_decl_arg_dump(dump
, arg
, 0);
96 PSI_DUMP(dump
, ", ...");
99 if (decl
->func
->var
->array_size
) {
100 PSI_DUMP(dump
, ")[%u]", decl
->func
->var
->array_size
);
105 PSI_DUMP(dump
, " __asm__ (\"%s\");", decl
->redir
->val
);
111 static inline bool psi_decl_validate_func(struct psi_data
*data
,
112 struct psi_decl
*decl
, struct psi_decl_arg
*func
)
114 struct psi_func_redir
*redir
;
116 if (!func
->var
->name
) {
117 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot load anonymous decl");
121 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
122 if (!strcmp(func
->var
->name
->val
, redir
->name
)) {
123 decl
->sym
= redir
->func
;
128 decl
->sym
= psi_dlsym(data
->file
.dlopened
, func
->var
->name
->val
,
129 decl
->redir
? decl
->redir
->val
: NULL
);
132 data
->error(data
, func
->token
, PSI_WARNING
,
133 "Failed to locate symbol '%s(%s)': %s",
134 func
->var
->name
->val
,
135 decl
->redir
? decl
->redir
->val
: "",
136 psi_dlerror() ?: "not found");
142 bool psi_decl_validate(struct psi_data
*data
, struct psi_decl
*decl
,
143 struct psi_validate_scope
*scope
)
145 if (!psi_decl_validate_nodl(data
, decl
, scope
)) {
148 if (!psi_decl_validate_func(data
, decl
, decl
->func
)) {
155 bool psi_decl_validate_nodl(struct psi_data
*data
, struct psi_decl
*decl
,
156 struct psi_validate_scope
*scope
)
159 decl
->abi
= psi_decl_abi_init(NULL
);
160 } else if (!psi_decl_abi_validate(data
, decl
->abi
)) {
161 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
162 "Invalid calling convention: '%s'",
163 decl
->abi
->token
->text
->val
);
166 if (!psi_decl_arg_validate(data
, decl
->func
, scope
)) {
171 struct psi_decl_arg
*arg
;
173 while (psi_plist_get(decl
->args
, i
++, &arg
)) {
174 if (!arg
->var
->name
) {
175 smart_str name
= {0};
177 smart_str_appendl_ex(&name
, ZEND_STRL("arg"), 1);
178 smart_str_append_unsigned_ex(&name
, i
, 1);
180 arg
->var
->name
= smart_str_extract(&name
);
181 arg
->var
->fqn
= zend_string_copy(arg
->var
->name
);
183 if (!psi_decl_arg_validate(data
, arg
, scope
)) {
192 bool psi_decl_is_blacklisted(const char *name
)
197 while (psi_plist_get(PSI_G(blacklist
).decls
, i
++, &blacklisted
)) {
198 if (!fnmatch(blacklisted
, name
, 0)) {
205 struct psi_decl_arg
*psi_decl_get_arg(struct psi_decl
*decl
, struct psi_decl_var
*var
) {
208 struct psi_decl_arg
*arg
= decl
->func
;
211 if (var
->arg
== arg
) {
214 } while (psi_plist_get(decl
->args
, i
++, &arg
));
217 return psi_decl_arg_get_by_var(var
, decl
->args
, decl
->func
);