raising the head after a three-weeks refactoring
[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
28 #if __GNUC__ >= 5
29 # pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
30 #endif
31 #include "php_psi_macros.h"
32 #include "php_psi_redirs.h"
33 #if __GNUC__ >= 5
34 # pragma GCC diagnostic pop
35 #endif
36
37 #include <dlfcn.h>
38
39 #include "data.h"
40
41 struct psi_decl *psi_decl_init(struct psi_decl_abi *abi,
42 struct psi_decl_arg *func, struct psi_plist *args)
43 {
44 struct psi_decl *d = calloc(1, sizeof(*d));
45 d->abi = abi;
46 d->func = func;
47 d->args = args;
48 return d;
49 }
50
51 void psi_decl_free(struct psi_decl **d_ptr)
52 {
53 if (*d_ptr) {
54 struct psi_decl *d = *d_ptr;
55
56 *d_ptr = NULL;
57
58 psi_decl_abi_free(&d->abi);
59 psi_decl_arg_free(&d->func);
60 if (d->args) {
61 psi_plist_free(d->args);
62 }
63 free(d);
64 }
65 }
66
67 void psi_decl_dump(int fd, struct psi_decl *decl)
68 {
69 psi_decl_abi_dump(fd, decl->abi);
70 dprintf(fd, " ");
71 psi_decl_arg_dump(fd, decl->func, 0);
72 dprintf(fd, "(");
73 if (decl->args) {
74 size_t i;
75 struct psi_decl_arg *arg;
76
77 for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
78 if (i) {
79 dprintf(fd, ", ");
80 }
81 psi_decl_arg_dump(fd, arg, 0);
82 }
83 if (decl->varargs) {
84 dprintf(fd, ", ...");
85 }
86 }
87 dprintf(fd, ");");
88 }
89
90 static inline bool psi_decl_validate_func(struct psi_data *data,
91 struct psi_decl *decl, struct psi_decl_arg *func, void *dl)
92 {
93 struct psi_func_redir *redir;
94
95 if (!strcmp(func->var->name, "dlsym")) {
96 data->error(data, func->token, PSI_WARNING,
97 "Cannot dlsym dlsym (sic!)");
98 return false;
99 }
100
101 for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
102 if (!strcmp(func->var->name, redir->name)) {
103 decl->sym = redir->func;
104 }
105 }
106 if (!decl->sym) {
107 #ifndef RTLD_NEXT
108 # define RTLD_NEXT ((void *) -1l)
109 #endif
110 decl->sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
111 if (!decl->sym) {
112 data->error(data, func->token, PSI_WARNING,
113 "Failed to locate symbol '%s': %s", func->var->name,
114 dlerror() ?: "not found");
115 return false;
116 }
117 }
118 return true;
119 }
120
121 bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl)
122 {
123 if (!psi_decl_validate_nodl(data, decl)) {
124 return false;
125 }
126 if (!psi_decl_validate_func(data, decl, decl->func, dl)) {
127 return false;
128 }
129
130 return true;
131 }
132
133 bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl)
134 {
135 if (!psi_decl_abi_validate(data, decl->abi)) {
136 data->error(data, decl->abi->token, PSI_WARNING,
137 "Invalid calling convention: '%s'", decl->abi->token->text);
138 return false;
139 }
140 if (!psi_decl_arg_validate(data, decl->func)) {
141 return false;
142 }
143 if (decl->args) {
144 size_t i = 0;
145 struct psi_decl_arg *arg;
146
147 while (psi_plist_get(decl->args, i++, &arg)) {
148 if (!psi_decl_arg_validate(data, arg)) {
149 return false;
150 }
151 }
152 }
153
154 return true;
155 }