85d93d40cd63243cb93c6e3a15c240f27a8b314c
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
35 #define PSI_FUNC_REDIRS
36 #include "php_psi_posix.h"
38 struct psi_decl
*psi_decl_init(struct psi_decl_arg
*func
, struct psi_plist
*args
)
40 struct psi_decl
*d
= calloc(1, sizeof(*d
));
48 void psi_decl_free(struct psi_decl
**d_ptr
)
51 struct psi_decl
*d
= *d_ptr
;
55 psi_decl_abi_free(&d
->abi
);
56 psi_decl_arg_free(&d
->func
);
58 psi_plist_free(d
->args
);
67 void psi_decl_dump(int fd
, struct psi_decl
*decl
)
69 psi_decl_abi_dump(fd
, decl
->abi
);
71 /* FIXME: functions returning arrays */
72 psi_decl_arg_dump(fd
, decl
->func
, 0);
76 struct psi_decl_arg
*arg
;
78 for (i
= 0; psi_plist_get(decl
->args
, i
, &arg
); ++i
) {
82 psi_decl_arg_dump(fd
, arg
, 0);
89 dprintf(fd
, ") __asm__ (\"%s\");", decl
->redir
);
95 static inline bool psi_decl_validate_func(struct psi_data
*data
,
96 struct psi_decl
*decl
, struct psi_decl_arg
*func
, void *dl
)
98 struct psi_func_redir
*redir
;
100 if (!func
->var
->name
) {
101 data
->error(data
, func
->token
, PSI_WARNING
, "Cannot load anonymous decl");
105 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
106 if (!strcmp(func
->var
->name
, redir
->name
)) {
107 decl
->sym
= redir
->func
;
112 # define RTLD_NEXT ((void *) -1l)
115 # define RTLD_DEFAULT ((void *) 0)
117 decl
->sym
= dlsym(dl
?: RTLD_DEFAULT
, decl
->redir
?: func
->var
->name
);
119 data
->error(data
, func
->token
, PSI_WARNING
,
120 "Failed to locate symbol '%s(%s)': %s",
121 func
->var
->name
, decl
->redir
?: "",
122 dlerror() ?: "not found");
129 bool psi_decl_validate(struct psi_data
*data
, struct psi_decl
*decl
,
130 struct psi_validate_scope
*scope
)
132 if (!psi_decl_validate_nodl(data
, decl
, scope
)) {
135 if (!psi_decl_validate_func(data
, decl
, decl
->func
, scope
->dlopened
)) {
142 bool psi_decl_validate_nodl(struct psi_data
*data
, struct psi_decl
*decl
,
143 struct psi_validate_scope
*scope
)
146 decl
->abi
= psi_decl_abi_init("default");
147 } else if (!psi_decl_abi_validate(data
, decl
->abi
)) {
148 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
149 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
152 if (!psi_decl_arg_validate(data
, decl
->func
, scope
)) {
157 struct psi_decl_arg
*arg
;
159 while (psi_plist_get(decl
->args
, i
++, &arg
)) {
160 if (!arg
->var
->name
) {
161 arg
->var
->name
= malloc(7);
162 snprintf(arg
->var
->name
, 6, "arg%zu", i
);
163 arg
->var
->fqn
= strdup(arg
->var
->name
);
165 if (!psi_decl_arg_validate(data
, arg
, scope
)) {
174 bool psi_decl_is_blacklisted(const char *name
)
179 while (psi_plist_get(PSI_G(blacklist
).decls
, i
++, &blacklisted
)) {
180 if (!fnmatch(blacklisted
, name
, 0)) {
187 struct psi_decl_arg
*psi_decl_get_arg(struct psi_decl
*decl
, struct psi_decl_var
*var
) {
190 struct psi_decl_arg
*arg
= decl
->func
;
193 if (var
->arg
== arg
) {
196 } while (psi_plist_get(decl
->args
, i
++, &arg
));
199 return psi_decl_arg_get_by_var(var
, decl
->args
, decl
->func
);