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"
31 #define PSI_FUNC_REDIRS
32 #include "php_psi_posix.h"
34 struct psi_decl
*psi_decl_init(struct psi_decl_arg
*func
, struct psi_plist
*args
)
36 struct psi_decl
*d
= calloc(1, sizeof(*d
));
44 void psi_decl_free(struct psi_decl
**d_ptr
)
47 struct psi_decl
*d
= *d_ptr
;
51 psi_decl_abi_free(&d
->abi
);
52 psi_decl_arg_free(&d
->func
);
54 psi_plist_free(d
->args
);
60 void psi_decl_dump(int fd
, struct psi_decl
*decl
)
62 psi_decl_abi_dump(fd
, decl
->abi
);
64 psi_decl_arg_dump(fd
, decl
->func
, 0);
68 struct psi_decl_arg
*arg
;
70 for (i
= 0; psi_plist_get(decl
->args
, i
, &arg
); ++i
) {
74 psi_decl_arg_dump(fd
, arg
, 0);
83 static inline bool psi_decl_validate_func(struct psi_data
*data
,
84 struct psi_decl
*decl
, struct psi_decl_arg
*func
, void *dl
)
86 struct psi_func_redir
*redir
;
88 if (!func
->var
->name
) {
89 data
->error(data
, func
->token
, PSI_WARNING
, "Anonymous decl!");
92 if (!strcmp(func
->var
->name
, "dlsym")) {
93 data
->error(data
, func
->token
, PSI_WARNING
,
94 "Cannot dlsym dlsym (sic!)");
98 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
99 if (!strcmp(func
->var
->name
, redir
->name
)) {
100 decl
->sym
= redir
->func
;
105 # define RTLD_NEXT ((void *) -1l)
108 # define RTLD_DEFAULT ((void *) 0)
110 decl
->sym
= dlsym(dl
?: RTLD_DEFAULT
, func
->var
->name
);
112 data
->error(data
, func
->token
, PSI_WARNING
,
113 "Failed to locate symbol '%s': %s", func
->var
->name
,
114 dlerror() ?: "not found");
121 bool psi_decl_validate(struct psi_data
*data
, struct psi_decl
*decl
, void *dl
,
122 struct psi_validate_stack
*type_stack
)
124 if (!psi_decl_validate_nodl(data
, decl
, type_stack
)) {
127 if (!psi_decl_validate_func(data
, decl
, decl
->func
, dl
)) {
134 bool psi_decl_validate_nodl(struct psi_data
*data
, struct psi_decl
*decl
,
135 struct psi_validate_stack
*type_stack
)
138 decl
->abi
= psi_decl_abi_init("default");
139 } else if (!psi_decl_abi_validate(data
, decl
->abi
)) {
140 data
->error(data
, decl
->abi
->token
, PSI_WARNING
,
141 "Invalid calling convention: '%s'", decl
->abi
->token
->text
);
144 if (!psi_decl_arg_validate(data
, decl
->func
, type_stack
)) {
149 struct psi_decl_arg
*arg
;
151 while (psi_plist_get(decl
->args
, i
++, &arg
)) {
152 if (!arg
->var
->name
) {
153 arg
->var
->name
= malloc(7);
154 snprintf(arg
->var
->name
, 6, "arg%zu", i
);
155 arg
->var
->fqn
= strdup(arg
->var
->name
);
157 if (!psi_decl_arg_validate(data
, arg
, type_stack
)) {