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"
29 #include "php_globals.h"
34 struct psi_data
*psi_data_ctor_with_dtors(struct psi_data
*data
,
35 psi_error_cb error
, unsigned flags
)
38 data
= calloc(1, sizeof(*data
));
45 data
->consts
= psi_plist_init((psi_plist_dtor
) psi_const_free
);
48 data
->types
= psi_plist_init((psi_plist_dtor
) psi_decl_arg_free
);
51 data
->structs
= psi_plist_init((psi_plist_dtor
) psi_decl_struct_free
);
54 data
->unions
= psi_plist_init((psi_plist_dtor
) psi_decl_union_free
);
57 data
->enums
= psi_plist_init((psi_plist_dtor
) psi_decl_enum_free
);
60 data
->decls
= psi_plist_init((psi_plist_dtor
) psi_decl_free
);
63 data
->impls
= psi_plist_init((psi_plist_dtor
) psi_impl_free
);
66 data
->libs
= psi_plist_init((psi_plist_dtor
) psi_libs_free
);
71 struct psi_data
*psi_data_ctor(struct psi_data
*data
, psi_error_cb error
,
75 data
= calloc(1, sizeof(*data
));
82 data
->consts
= psi_plist_init(NULL
);
85 data
->types
= psi_plist_init(NULL
);
88 data
->structs
= psi_plist_init(NULL
);
91 data
->unions
= psi_plist_init(NULL
);
94 data
->enums
= psi_plist_init(NULL
);
97 data
->decls
= psi_plist_init(NULL
);
100 data
->impls
= psi_plist_init(NULL
);
103 data
->libs
= psi_plist_init(NULL
);
108 struct psi_data
*psi_data_exchange(struct psi_data
*dest
, struct psi_data
*src
)
111 dest
= malloc(sizeof(*dest
));
114 memset(src
, 0, sizeof(*src
));
118 void psi_data_dtor(struct psi_data
*data
)
121 psi_plist_free(data
->consts
);
124 psi_plist_free(data
->types
);
127 psi_plist_free(data
->structs
);
130 psi_plist_free(data
->unions
);
133 psi_plist_free(data
->enums
);
136 psi_plist_free(data
->decls
);
139 psi_plist_free(data
->impls
);
142 psi_plist_free(data
->libs
);
145 psi_decl_file_dtor(&data
->file
);
148 void psi_data_dump(int fd
, struct psi_data
*D
)
151 dprintf(fd
, "// filename=%s (%u errors)\n", D
->file
.fn
, D
->errors
);
153 dprintf(fd
, "lib \"%s\";\n", D
->file
.ln
);
156 dprintf(fd
, "// builtin predef\n");
158 if (psi_plist_count(D
->types
)) {
160 struct psi_decl_arg
*def
;
162 while (psi_plist_get(D
->types
, i
++, &def
)) {
163 dprintf(fd
, "typedef ");
164 psi_decl_arg_dump(fd
, def
, 0);
169 if (psi_plist_count(D
->unions
)) {
171 struct psi_decl_union
*unn
;
173 while (psi_plist_get(D
->unions
, i
++, &unn
)) {
174 if (!psi_decl_type_is_anon(unn
->name
, "union")) {
175 psi_decl_union_dump(fd
, unn
);
181 if (psi_plist_count(D
->structs
)) {
183 struct psi_decl_struct
*strct
;
185 while (psi_plist_get(D
->structs
, i
++, &strct
)) {
186 if (!psi_decl_type_is_anon(strct
->name
, "struct")) {
187 psi_decl_struct_dump(fd
, strct
);
193 if (psi_plist_count(D
->enums
)) {
195 struct psi_decl_enum
*enm
;
197 while (psi_plist_get(D
->enums
, i
++, &enm
)) {
198 if (!psi_decl_type_is_anon(enm
->name
, "enum")) {
199 psi_decl_enum_dump(fd
, enm
, 0);
205 if (psi_plist_count(D
->consts
)) {
209 while (psi_plist_get(D
->consts
, i
++, &c
)) {
210 psi_const_dump(fd
, c
);
215 if (psi_plist_count(D
->decls
)) {
217 struct psi_decl
*decl
;
219 while (psi_plist_get(D
->decls
, i
++, &decl
)) {
220 psi_decl_dump(fd
, decl
);
225 if (psi_plist_count(D
->impls
)) {
227 struct psi_impl
*impl
;
229 while (psi_plist_get(D
->impls
, i
++, &impl
)) {
230 psi_impl_dump(fd
, impl
);
237 bool psi_data_validate(struct psi_data
*dst
, struct psi_data
*src
)
239 void *dlopened
= NULL
;
240 size_t check_count
= ~0;
241 struct psi_plist
*check_types
= src
->types
;
242 struct psi_plist
*check_structs
= src
->structs
;
243 struct psi_plist
*check_unions
= src
->unions
;
244 struct psi_plist
*check_enums
= src
->enums
;
245 struct psi_plist
*check_decls
= src
->decls
;
246 unsigned flags
= dst
->flags
;
247 unsigned errors
= src
->errors
;
248 struct psi_validate_stack type_stack
;
250 /* fail early if library is not found */
251 if (!psi_decl_file_validate(dst
, src
, &dlopened
)) {
255 psi_validate_stack_ctor(&type_stack
);
257 dst
->flags
|= PSI_SILENT
;
259 while (check_count
) {
260 struct psi_plist
*recheck_types
;
261 struct psi_plist
*recheck_structs
;
262 struct psi_plist
*recheck_unions
;
263 struct psi_plist
*recheck_enums
;
264 struct psi_plist
*recheck_decls
;
265 size_t count_types
= psi_plist_count(check_types
);
266 size_t count_structs
= psi_plist_count(check_structs
);
267 size_t count_unions
= psi_plist_count(check_unions
);
268 size_t count_enums
= psi_plist_count(check_enums
);
269 size_t count_decls
= psi_plist_count(check_decls
);
270 size_t count_all
= count_types
+ count_structs
+ count_unions
271 + count_enums
+ count_decls
;
273 if (check_count
== count_all
) {
274 /* nothing changed; bail out */
275 if (count_all
&& (dst
->flags
& PSI_SILENT
) && !(flags
& PSI_SILENT
)) {
276 /* one last error-spitting round, if not explicitly suppressed */
277 dst
->flags
^= PSI_SILENT
;
280 PSI_DEBUG_PRINT(dst
, "PSI: validation bail out with %zu"
281 " type checks remaining, errors follow\n", count_all
);
286 recheck_types
= count_types
? psi_plist_init(NULL
) : NULL
;
287 recheck_structs
= count_structs
? psi_plist_init(NULL
) : NULL
;
288 recheck_unions
= count_unions
? psi_plist_init(NULL
) : NULL
;
289 recheck_enums
= count_enums
? psi_plist_init(NULL
) : NULL
;
290 recheck_decls
= count_decls
? psi_plist_init(NULL
) : NULL
;
292 check_count
= count_all
;
293 src
->errors
= errors
+ check_count
;
295 PSI_DEBUG_PRINT(dst
, "PSI: validate data(%p) %zu type checks remaining\n",
300 struct psi_decl_arg
*def
;
302 while (psi_plist_get(check_types
, i
++, &def
)) {
303 *dst
->last_error
= 0;
304 dst
->types
= psi_plist_add(dst
->types
, &def
);
305 PSI_DEBUG_PRINT(dst
, "PSI: validate typedef %s ", def
->var
->name
);
306 if (psi_decl_arg_validate_typedef(PSI_DATA(dst
), def
, &type_stack
)) {
307 PSI_DEBUG_PRINT(dst
, "%s\n", "✔");
309 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
310 recheck_types
= psi_plist_add(recheck_types
, &def
);
311 psi_plist_pop(dst
->types
, NULL
);
317 struct psi_decl_struct
*str
;
319 while (psi_plist_get(check_structs
, i
++, &str
)) {
320 *dst
->last_error
= 0;
321 dst
->structs
= psi_plist_add(dst
->structs
, &str
);
322 PSI_DEBUG_PRINT(dst
, "PSI: validate struct %s ", str
->name
);
323 if (psi_decl_struct_validate(PSI_DATA(dst
), str
, &type_stack
)) {
324 PSI_DEBUG_PRINT(dst
, "%s ::(%zu, %zu)\n", "✔", str
->align
, str
->size
);
326 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
327 recheck_structs
= psi_plist_add(recheck_structs
, &str
);
328 psi_plist_pop(dst
->structs
, NULL
);
334 struct psi_decl_union
*unn
;
336 while (psi_plist_get(check_unions
, i
++, &unn
)) {
337 *dst
->last_error
= 0;
338 dst
->unions
= psi_plist_add(dst
->unions
, &unn
);
339 PSI_DEBUG_PRINT(dst
, "PSI: validate union %s ", unn
->name
);
340 if (psi_decl_union_validate(PSI_DATA(dst
), unn
, &type_stack
)) {
341 PSI_DEBUG_PRINT(dst
, "%s ::(%zu, %zu)\n", "✔", unn
->align
, unn
->size
);
343 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
344 recheck_unions
= psi_plist_add(recheck_unions
, &unn
);
345 psi_plist_pop(dst
->unions
, NULL
);
351 struct psi_decl_enum
*enm
;
353 while (psi_plist_get(check_enums
, i
++, &enm
)) {
354 *dst
->last_error
= 0;
355 PSI_DEBUG_PRINT(dst
, "PSI: validate enum %s ", enm
->name
);
356 if (psi_decl_enum_validate(PSI_DATA(dst
), enm
)) {
357 PSI_DEBUG_PRINT(dst
, "%s\n", "✔");
358 dst
->enums
= psi_plist_add(dst
->enums
, &enm
);
360 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
361 recheck_enums
= psi_plist_add(recheck_enums
, &enm
);
367 struct psi_decl
*decl
;
369 while (psi_plist_get(check_decls
, i
++, &decl
)) {
370 *dst
->last_error
= 0;
371 PSI_DEBUG_PRINT(dst
, "PSI: validate decl %s ", decl
->func
->var
->name
);
372 if (psi_decl_validate(PSI_DATA(dst
), decl
, dlopened
, &type_stack
)) {
373 PSI_DEBUG_PRINT(dst
, "%s\n", "✔");
374 dst
->decls
= psi_plist_add(dst
->decls
, &decl
);
376 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
377 recheck_decls
= psi_plist_add(recheck_decls
, &decl
);
383 if (check_types
&& check_types
!= src
->types
) {
384 psi_plist_free(check_types
);
386 check_types
= recheck_types
;
387 if (check_structs
&& check_structs
!= src
->structs
) {
388 psi_plist_free(check_structs
);
390 check_structs
= recheck_structs
;
391 if (check_unions
&& check_unions
!= src
->unions
) {
392 psi_plist_free(check_unions
);
394 check_unions
= recheck_unions
;
395 if (check_enums
&& check_enums
!= src
->enums
) {
396 psi_plist_free(check_enums
);
398 check_enums
= recheck_enums
;
399 if (check_decls
&& check_decls
!= src
->decls
) {
400 psi_plist_free(check_decls
);
402 check_decls
= recheck_decls
;
405 /* reset original flags */
410 struct psi_decl_struct
*str
;
412 while (psi_plist_get(dst
->structs
, i
++, &str
)) {
413 size_t nlen
= strlen(str
->name
);
414 size_t slen
= sizeof("psi\\SIZEOF_STRUCT_");
415 size_t alen
= sizeof("psi\\ALIGNOF_STRUCT_");
416 char *nptr
= str
->name
, *sname
, *aname
;
417 struct psi_const
*cnst
;
418 struct psi_const_type
*ctyp
;
419 struct psi_impl_def_val
*cval
;
421 sname
= malloc(slen
+ nlen
+ 1);
422 strcpy(sname
, "psi\\SIZEOF_STRUCT_");
423 aname
= malloc(alen
+ nlen
+ 1);
424 strcpy(aname
, "psi\\ALIGNOF_STRUCT_");
428 size_t off
= nptr
- str
->name
;
429 sname
[slen
- 1 + off
] = aname
[alen
- 1 + off
] = toupper(*nptr
++);
431 sname
[slen
- 1 + nlen
] = aname
[alen
- 1 + nlen
] = 0;
433 ctyp
= psi_const_type_init(PSI_T_INT
, "int");
434 cval
= psi_impl_def_val_init(PSI_T_INT
, NULL
);
435 cval
->ival
.zend
.lval
= str
->size
;
436 cnst
= psi_const_init(ctyp
, sname
, cval
);
437 dst
->consts
= psi_plist_add(dst
->consts
, &cnst
);
440 ctyp
= psi_const_type_init(PSI_T_INT
, "int");
441 cval
= psi_impl_def_val_init(PSI_T_INT
, NULL
);
442 cval
->ival
.zend
.lval
= str
->align
;
443 cnst
= psi_const_init(ctyp
, aname
, cval
);
444 dst
->consts
= psi_plist_add(dst
->consts
, &cnst
);
451 struct psi_const
*cnst
;
453 while (psi_plist_get(src
->consts
, i
++, &cnst
)) {
454 *dst
->last_error
= 0;
455 PSI_DEBUG_PRINT(dst
, "PSI: validate const %s ", cnst
->name
);
456 if (psi_const_validate(PSI_DATA(dst
), cnst
)) {
457 PSI_DEBUG_PRINT(dst
, "%s\n", "✔");
458 dst
->consts
= psi_plist_add(dst
->consts
, &cnst
);
460 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
468 struct psi_impl
*impl
;
470 while (psi_plist_get(src
->impls
, i
++, &impl
)) {
471 *dst
->last_error
= 0;
472 PSI_DEBUG_PRINT(dst
, "PSI: validate impl %s ", impl
->func
->name
);
473 if (psi_impl_validate(PSI_DATA(dst
), impl
)) {
474 PSI_DEBUG_PRINT(dst
, "%s\n", "✔");
475 dst
->impls
= psi_plist_add(dst
->impls
, &impl
);
477 PSI_DEBUG_PRINT(dst
, "%s (%s)\n", "✘", dst
->last_error
);
483 psi_validate_stack_dtor(&type_stack
);