validate: fix type stack and leaks
[m6w6/ext-psi] / src / types / decl_struct.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 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_decl_struct* psi_decl_struct_init(const char *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_struct *s = calloc(1, sizeof(*s));
35 s->name = strdup(name);
36 s->args = args;
37 return s;
38 }
39
40 void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
41 {
42 if (*s_ptr) {
43 struct psi_decl_struct *s = *s_ptr;
44
45 *s_ptr = NULL;
46 if (s->token) {
47 free(s->token);
48 }
49 if (s->args) {
50 psi_plist_free(s->args);
51 }
52 if (s->engine.type && s->engine.dtor) {
53 s->engine.dtor(s->engine.type);
54 }
55 free(s->name);
56 free(s);
57 }
58 }
59
60 void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct)
61 {
62 dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align,
63 strct->size);
64 if (psi_plist_count(strct->args)) {
65 psi_decl_type_dump_args_with_layout(fd, strct->args, 0);
66 } else {
67 dprintf(fd, ";");
68 }
69 }
70
71 struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
72 struct psi_decl_var *var)
73 {
74 if (s->args) {
75 return psi_decl_arg_get_by_var(var, s->args, NULL);
76 }
77
78 return NULL;
79 }
80
81 bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
82 struct psi_validate_stack *type_stack)
83 {
84 size_t i, pos, len, size, align;
85 struct psi_decl_arg *darg, *prev_arg;
86
87 if (!s) {
88 return false;
89 }
90 if (psi_validate_stack_has_struct(type_stack, s->name)) {
91 return true;
92 }
93
94 if (!s->size && !psi_plist_count(s->args)) {
95 data->error(data, s->token, PSI_WARNING,
96 "Cannot compute size of empty struct '%s'", s->name);
97 return false;
98 }
99
100 psi_validate_stack_add_struct(type_stack, s->name, s);
101
102 for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
103 darg->var->arg = darg;
104
105 if (!psi_decl_arg_validate(data, darg, type_stack)) {
106 psi_validate_stack_del_struct(type_stack, s->name);
107 return false;
108 }
109
110 if (darg->layout && darg->layout->len) {
111 pos = darg->layout->pos;
112 align = psi_decl_arg_align(darg, &pos, &len);
113
114 if (!align) {
115 data->error(data, darg->token, PSI_WARNING,
116 "Computed zero alignment of %s.%s of type '%s'",
117 len, s->name, darg->var->name, darg->type->name);
118 psi_validate_stack_del_struct(type_stack, s->name);
119 return false;
120 }
121
122 if (darg->layout->len != len) {
123 data->error(data, darg->token, PSI_WARNING,
124 "Computed size %zu of %s.%s does not match"
125 " pre-defined size %zu of type '%s'",
126 len, s->name, darg->var->name, darg->layout->len,
127 darg->type->name);
128 }
129 if (darg->layout->pos != pos) {
130 data->error(data, darg->token, PSI_WARNING,
131 "Computed offset %zu of %s.%s does not match"
132 " pre-defined offset %zu",
133 pos, s->name, darg->var->name, darg->layout->pos);
134 }
135 } else {
136 if (i) {
137 if (prev_arg->layout && prev_arg->layout->bfw && darg->layout && darg->layout->bfw) {
138 struct psi_decl_type *real = NULL;
139 size_t max_bfw = 8 * psi_decl_type_get_size(prev_arg->type, &real);
140
141 switch (real->type) {
142 case PSI_T_INT8:
143 case PSI_T_UINT8:
144 case PSI_T_INT16:
145 case PSI_T_UINT16:
146 case PSI_T_INT32:
147 case PSI_T_UINT32:
148 case PSI_T_INT64:
149 case PSI_T_UINT64:
150 break;
151 default:
152 data->error(data, darg->token, PSI_WARNING,
153 "Unsupported type for bit field: %s", real->name);
154 psi_validate_stack_del_struct(type_stack, s->name);
155 return false;
156 }
157 darg->layout->bfw->pos = prev_arg->layout->bfw->pos + prev_arg->layout->bfw->len;
158 if (max_bfw >= darg->layout->bfw->pos + darg->layout->bfw->len) {
159 pos = prev_arg->layout->pos;
160 } else {
161 darg->layout->bfw->pos = 0;
162 pos = prev_arg->layout->pos + prev_arg->layout->len;
163 }
164 } else {
165 pos = prev_arg->layout->pos + prev_arg->layout->len;
166 }
167 } else {
168 pos = 0;
169 }
170
171 align = psi_decl_arg_align(darg, &pos, &len);
172
173 if (darg->layout) {
174 if (darg->layout->pos != pos) {
175 data->error(data, darg->token, PSI_WARNING,
176 "Computed offset %zu of %s.%s does not match"
177 " pre-defined offset %zu",
178 pos, s->name, darg->var->name, darg->layout->pos);
179 }
180 darg->layout->pos = pos;
181 darg->layout->len = len;
182 } else {
183 darg->layout = psi_layout_init(pos, len, NULL);
184 }
185 }
186
187 if (align > s->align) {
188 s->align = align;
189 }
190 prev_arg = darg;
191 }
192
193 if (psi_plist_count(s->args)) {
194 psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
195 psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
196
197 size = darg->layout->pos + darg->layout->len;
198 if (s->size < size) {
199 s->size = psi_align(size, s->align);
200 }
201 }
202
203 assert(s->size);
204
205 return true;
206 }
207
208 size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
209 {
210 if (!s) {
211 return 0;
212 }
213 if (!s->align) {
214 s->align = psi_decl_type_get_args_align(s->args);
215 }
216 return s->align;
217 }