fix invalid free on syntax error
[m6w6/ext-psi] / src / builtin.c
1 /*******************************************************************************
2 Copyright (c) 2018, 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include <stdbool.h>
33 #include <stdarg.h>
34
35 #include "php_psi.h"
36 #include "builtin.h"
37 #include "parser.h"
38 #include "cpp.h"
39
40 HashTable psi_builtins;
41
42 static bool has_include(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
43 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
44 static bool has_feature(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
45 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
46 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
47
48 static inline struct psi_plist *builtin_sig(token_t typ, ...)
49 {
50 struct psi_plist *sig;
51 size_t n = 0;
52 va_list args;
53
54 if (typ == (token_t) - 1) {
55 return NULL;
56 }
57
58 sig = psi_plist_init((psi_plist_dtor) psi_token_free);
59 va_start(args, typ);
60 while (typ) {
61 char a = 'a' + n++;
62 struct psi_token *arg;
63
64 arg = psi_token_init(typ, &a, 1, 0, 0, zend_empty_string);
65 sig = psi_plist_add(sig, &arg);
66 typ = va_arg(args, token_t);
67 }
68 va_end(args);
69
70 return sig;
71 }
72
73 static void free_builtin(zval *p)
74 {
75 struct psi_builtin *b = Z_PTR_P(p);
76
77 if (b) {
78 zend_string_release(b->name);
79 psi_cpp_macro_decl_free(&b->decl);
80 pefree(b, 1);
81 }
82 }
83
84 PHP_MINIT_FUNCTION(psi_builtin);
85 PHP_MINIT_FUNCTION(psi_builtin)
86 {
87 #define PSI_BUILTIN(builtin, ...) do { \
88 struct psi_builtin entry; \
89 struct psi_plist *sig = builtin_sig(__VA_ARGS__, 0); \
90 struct psi_cpp_macro_decl *decl = psi_cpp_macro_decl_init(sig, NULL, NULL); \
91 decl->token = psi_token_init(PSI_T_NAME, "__" #builtin, sizeof("__" #builtin)-1, \
92 0, 0, zend_empty_string); \
93 entry.name = zend_string_copy(decl->token->text); \
94 entry.func = &builtin; \
95 entry.decl = decl; \
96 zend_hash_add_mem(&psi_builtins, entry.name, &entry, sizeof(entry)); \
97 } while(0)
98
99 zend_hash_init(&psi_builtins, 0, NULL, free_builtin, 1);
100 PSI_BUILTIN(has_include, PSI_T_CPP_HEADER);
101 PSI_BUILTIN(has_include_next, PSI_T_CPP_HEADER);
102 PSI_BUILTIN(has_feature, PSI_T_NAME);
103 PSI_BUILTIN(builtin_constant_p, PSI_T_NAME);
104
105 PSI_BUILTIN(COUNTER__, -1);
106
107 return SUCCESS;
108 }
109
110 PHP_MSHUTDOWN_FUNCTION(psi_builtin);
111 PHP_MSHUTDOWN_FUNCTION(psi_builtin)
112 {
113 zend_hash_destroy(&psi_builtins);
114 return SUCCESS;
115 }
116
117 bool psi_builtin_exists(zend_string *name)
118 {
119 return zend_hash_exists(&psi_builtins, name);
120 }
121
122 struct psi_builtin *psi_builtin_get(zend_string *name)
123 {
124 return zend_hash_find_ptr(&psi_builtins, name);
125 }
126
127 static bool has_include(struct psi_cpp *cpp, struct psi_token *target,
128 struct psi_plist **args, struct psi_plist **res)
129 {
130 struct psi_plist *arg = args[0];
131 struct psi_token *tok;
132
133 switch (psi_plist_count(arg)) {
134 case 1:
135 if (psi_plist_get(arg, 0, &tok)) {
136 const char *cpp_search = cpp->search;
137 bool has = psi_cpp_has_include(cpp, tok, 0, NULL);
138 cpp->search = cpp_search;
139 return has;
140 }
141 /* no break */
142 default:
143 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
144 "Erroneous usage of builtin __%s", __FUNCTION__);
145 }
146 return false;
147 }
148
149 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target,
150 struct psi_plist **args, struct psi_plist **res)
151 {
152 struct psi_plist *arg = args[0];
153 struct psi_token *tok;
154
155 switch (psi_plist_count(arg)) {
156 case 1:
157 if (psi_plist_get(arg, 0, &tok)) {
158 const char *cpp_search = cpp->search;
159 bool has = psi_cpp_has_include(cpp, tok, PSI_CPP_INCLUDE_NEXT, NULL);
160 cpp->search = cpp_search;
161 return has;
162 }
163 /* no break */
164 default:
165 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
166 "Erroneous usage of builtin __%s", __FUNCTION__);
167 }
168 return false;
169 }
170
171 static bool has_feature(struct psi_cpp *cpp, struct psi_token *target,
172 struct psi_plist **args, struct psi_plist **res_ptr)
173 {
174 return false;
175 }
176
177 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target,
178 struct psi_plist **args, struct psi_plist **res_ptr)
179 {
180 /* we want functions, not macros for e.g. htonl() */
181 return false;
182 }
183
184 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target,
185 struct psi_plist **args, struct psi_plist **res)
186 {
187 struct psi_token *tok;
188 char buf[0x20];
189 size_t len = sprintf(buf, "%u", cpp->counter++);
190
191 tok = psi_token_init(PSI_T_NUMBER, buf, len, target->col, target->line, target->file);
192 *res = psi_plist_init((psi_plist_dtor) psi_token_free);
193 *res = psi_plist_add(*res, &tok);
194
195 return true;
196 }
197
198 #ifdef __APPLE__
199 #include <libkern/OSByteOrder.h>
200 # define bswap_16(u) _OSSwapInt16(u)
201 # define bswap_32(u) _OSSwapInt32(u)
202 # define bswap_64(u) _OSSwapInt64(u)
203 #elif defined(__FreeBSD__)
204 # include <sys/endian.h>
205 # define bswap_16(u) bswap16(u)
206 # define bswap_32(u) bswap32(u)
207 # define bswap_64(u) bswap64(u)
208 #elif defined(__OpenBSD__)
209 # include <sys/types.h>
210 # define bswap_16(u) swap16(u)
211 # define bswap_32(u) swap32(u)
212 # define bswap_64(u) swap64(u)
213 #elif defined(__NetBSD__)
214 # include <sys/types.h>
215 # include <machine/bswap.h>
216 # define bswap_16(u) bswap16(u)
217 # define bswap_32(u) bswap32(u)
218 # define bswap_64(u) bswap64(u)
219 #else
220 # include <byteswap.h>
221 #endif
222
223 uint16_t psi_swap16(uint16_t u)
224 {
225 return bswap_16(u);
226 }
227
228 uint32_t psi_swap32(uint32_t u)
229 {
230 return bswap_32(u);
231 }
232
233 uint64_t psi_swap64(uint64_t u)
234 {
235 return bswap_64(u);
236 }