-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.c
705 lines (601 loc) · 14.6 KB
/
type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
* This file is part of ltrace.
* Copyright (C) 2011,2012,2013,2015 Petr Machata, Red Hat Inc.
* Copyright (C) 2007,2008 Juan Cespedes
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <assert.h>
#include <stdlib.h>
#include <limits.h>
#include "type.h"
#include "sysdep.h"
#include "expr.h"
#include "lens.h"
struct arg_type_info *
type_get_simple(enum arg_type type)
{
#define HANDLE(T) { \
static struct arg_type_info t = { T }; \
case T: \
return &t; \
}
switch (type) {
HANDLE(ARGTYPE_VOID)
HANDLE(ARGTYPE_INT)
HANDLE(ARGTYPE_UINT)
HANDLE(ARGTYPE_LONG)
HANDLE(ARGTYPE_ULONG)
HANDLE(ARGTYPE_CHAR)
HANDLE(ARGTYPE_SHORT)
HANDLE(ARGTYPE_USHORT)
HANDLE(ARGTYPE_FLOAT)
HANDLE(ARGTYPE_DOUBLE)
#undef HANDLE
case ARGTYPE_ARRAY:
case ARGTYPE_STRUCT:
case ARGTYPE_POINTER:
assert(!"Not a simple type!");
};
abort();
}
struct arg_type_info *
type_get_voidptr(void)
{
struct arg_type_info *void_info = type_get_simple(ARGTYPE_VOID);
static struct arg_type_info *ret;
if (ret == NULL) {
static struct arg_type_info ptr_info;
type_init_pointer(&ptr_info, void_info, 0);
ret = &ptr_info;
}
return ret;
}
static void
type_init_common(struct arg_type_info *info, enum arg_type type)
{
info->type = type;
info->lens = NULL;
info->own_lens = 0;
}
struct struct_field {
struct arg_type_info *info;
int own_info;
};
void
type_init_struct(struct arg_type_info *info)
{
type_init_common(info, ARGTYPE_STRUCT);
VECT_INIT(&info->u.entries, struct struct_field);
}
int
type_struct_add(struct arg_type_info *info,
struct arg_type_info *field_info, int own)
{
assert(info->type == ARGTYPE_STRUCT);
assert(field_info->type != ARGTYPE_VOID);
struct struct_field field = { field_info, own };
return VECT_PUSHBACK(&info->u.entries, &field);
}
struct arg_type_info *
type_struct_get(struct arg_type_info *info, size_t idx)
{
assert(info->type == ARGTYPE_STRUCT);
return VECT_ELEMENT(&info->u.entries, struct struct_field, idx)->info;
}
size_t
type_struct_size(struct arg_type_info *info)
{
assert(info->type == ARGTYPE_STRUCT);
return vect_size(&info->u.entries);
}
static void
struct_field_dtor(struct struct_field *field, void *data)
{
if (field->own_info) {
type_destroy(field->info);
free(field->info);
}
}
static void
type_struct_destroy(struct arg_type_info *info)
{
VECT_DESTROY(&info->u.entries, struct struct_field,
struct_field_dtor, NULL);
}
static int
layout_struct(struct process *proc, struct arg_type_info *info,
size_t *sizep, size_t *alignmentp, size_t *offsetofp)
{
size_t sz = 0;
size_t max_alignment = 0;
size_t i;
size_t offsetof_field = (size_t)-1;
if (offsetofp != NULL)
offsetof_field = *offsetofp;
assert(info->type == ARGTYPE_STRUCT);
for (i = 0; i < vect_size(&info->u.entries); ++i) {
struct struct_field *field
= VECT_ELEMENT(&info->u.entries,
struct struct_field, i);
size_t alignment = type_alignof(proc, field->info);
if (alignment == (size_t)-1)
return -1;
/* Add padding to SZ to align the next element. */
sz = align(sz, alignment);
if (i == offsetof_field) {
*offsetofp = sz;
if (sizep == NULL && alignmentp == NULL)
return 0;
}
size_t size = type_sizeof(proc, field->info);
if (size == (size_t)-1)
return -1;
sz += size;
if (alignment > max_alignment)
max_alignment = alignment;
}
if (max_alignment > 0)
sz = align(sz, max_alignment);
if (sizep != NULL)
*sizep = sz;
if (alignmentp != NULL)
*alignmentp = max_alignment;
return 0;
}
void
type_init_array(struct arg_type_info *info,
struct arg_type_info *element_info, int own_info,
struct expr_node *length_expr, int own_length)
{
type_init_common(info, ARGTYPE_ARRAY);
info->u.array_info.elt_type = element_info;
info->u.array_info.own_info = own_info;
info->u.array_info.length = length_expr;
info->u.array_info.own_length = own_length;
}
static void
type_array_destroy(struct arg_type_info *info)
{
if (info->u.array_info.own_info) {
type_destroy(info->u.array_info.elt_type);
free(info->u.array_info.elt_type);
}
if (info->u.array_info.own_length) {
expr_destroy(info->u.array_info.length);
free(info->u.array_info.length);
}
}
void
type_init_pointer(struct arg_type_info *info,
struct arg_type_info *pointee_info, int own_info)
{
type_init_common(info, ARGTYPE_POINTER);
info->u.ptr_info.info = pointee_info;
info->u.ptr_info.own_info = own_info;
}
static void
type_pointer_destroy(struct arg_type_info *info)
{
if (info->u.ptr_info.own_info) {
type_destroy(info->u.ptr_info.info);
free(info->u.ptr_info.info);
}
}
void
type_destroy(struct arg_type_info *info)
{
if (info == NULL)
return;
switch (info->type) {
case ARGTYPE_STRUCT:
type_struct_destroy(info);
break;
case ARGTYPE_ARRAY:
type_array_destroy(info);
break;
case ARGTYPE_POINTER:
type_pointer_destroy(info);
break;
case ARGTYPE_VOID:
case ARGTYPE_INT:
case ARGTYPE_UINT:
case ARGTYPE_LONG:
case ARGTYPE_ULONG:
case ARGTYPE_CHAR:
case ARGTYPE_SHORT:
case ARGTYPE_USHORT:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
break;
}
if (info->own_lens) {
lens_destroy(info->lens);
free(info->lens);
}
}
static int
type_alloc_and_clone(struct arg_type_info **retpp,
struct arg_type_info *info, int own)
{
*retpp = info;
if (own) {
*retpp = malloc(sizeof **retpp);
if (*retpp == NULL || type_clone(*retpp, info) < 0) {
free(*retpp);
return -1;
}
}
return 0;
}
static enum callback_status
clone_struct_add_field(const struct struct_field *field, void *data)
{
struct arg_type_info *retp = data;
struct arg_type_info *info;
if (type_alloc_and_clone(&info, field->info, field->own_info) < 0) {
fail:
if (info != field->info)
free(info);
return CBS_STOP;
}
if (type_struct_add(retp, info, field->own_info) < 0) {
if (field->own_info)
type_destroy(info);
goto fail;
}
return CBS_CONT;
}
int
type_clone(struct arg_type_info *retp, const struct arg_type_info *info)
{
switch (info->type) {
case ARGTYPE_STRUCT:
type_init_struct(retp);
if (VECT_EACH_CST(&info->u.entries, struct struct_field, NULL,
clone_struct_add_field, retp) != NULL) {
type_destroy(retp);
return -1;
}
break;
case ARGTYPE_ARRAY:;
struct arg_type_info *elt_type;
if (type_alloc_and_clone(&elt_type, info->u.array_info.elt_type,
info->u.array_info.own_info) < 0)
return -1;
assert(!info->u.array_info.own_length); // XXXXXXX
type_init_array(retp, elt_type, info->u.array_info.own_info,
info->u.array_info.length,
info->u.array_info.own_length);
break;
case ARGTYPE_POINTER:;
struct arg_type_info *ninfo;
if (type_alloc_and_clone(&ninfo, info->u.ptr_info.info,
info->u.ptr_info.own_info) < 0)
return -1;
type_init_pointer(retp, ninfo, info->u.ptr_info.own_info);
break;
case ARGTYPE_VOID:
case ARGTYPE_INT:
case ARGTYPE_UINT:
case ARGTYPE_LONG:
case ARGTYPE_ULONG:
case ARGTYPE_CHAR:
case ARGTYPE_SHORT:
case ARGTYPE_USHORT:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
*retp = *info;
break;
}
assert(!info->own_lens);
retp->lens = info->lens;
retp->own_lens = info->own_lens;
return 0;
}
#ifdef ARCH_HAVE_SIZEOF
size_t arch_type_sizeof(struct process *proc, struct arg_type_info *arg);
#else
size_t
arch_type_sizeof(struct process *proc, struct arg_type_info *arg)
{
/* Use default value. */
return (size_t)-2;
}
#endif
#ifdef ARCH_HAVE_ALIGNOF
size_t arch_type_alignof(struct process *proc, struct arg_type_info *arg);
#else
size_t
arch_type_alignof(struct process *proc, struct arg_type_info *arg)
{
/* Use default value. */
return (size_t)-2;
}
#endif
/* We need to support alignments that are not power of two. E.g. long
* double on x86 has alignment of 12. */
size_t
align(size_t sz, size_t alignment)
{
assert(alignment != 0);
if ((sz % alignment) != 0)
sz = ((sz / alignment) + 1) * alignment;
return sz;
}
size_t
type_sizeof(struct process *proc, struct arg_type_info *type)
{
size_t arch_size = arch_type_sizeof(proc, type);
if (arch_size != (size_t)-2)
return arch_size;
switch (type->type) {
size_t size;
case ARGTYPE_CHAR:
return sizeof(char);
case ARGTYPE_SHORT:
case ARGTYPE_USHORT:
return sizeof(short);
case ARGTYPE_INT:
case ARGTYPE_UINT:
return sizeof(int);
case ARGTYPE_LONG:
case ARGTYPE_ULONG:
return sizeof(long);
case ARGTYPE_FLOAT:
return sizeof(float);
case ARGTYPE_DOUBLE:
return sizeof(double);
case ARGTYPE_STRUCT:
if (layout_struct(proc, type, &size, NULL, NULL) < 0)
return (size_t)-1;
return size;
case ARGTYPE_POINTER:
return sizeof(void *);
case ARGTYPE_ARRAY:
if (expr_is_compile_constant(type->u.array_info.length)) {
long l;
if (expr_eval_constant(type->u.array_info.length,
&l) < 0)
return -1;
struct arg_type_info *elt_ti
= type->u.array_info.elt_type;
size_t elt_size = type_sizeof(proc, elt_ti);
if (elt_size == (size_t)-1)
return (size_t)-1;
return ((size_t)l) * elt_size;
} else {
/* Flexible arrays don't count into the
* sizeof. */
return 0;
}
case ARGTYPE_VOID:
return 0;
}
abort();
}
#undef alignof
#define alignof(field,st) ((size_t) ((char*) &st.field - (char*) &st))
size_t
type_alignof(struct process *proc, struct arg_type_info *type)
{
size_t arch_alignment = arch_type_alignof(proc, type);
if (arch_alignment != (size_t)-2)
return arch_alignment;
struct { char c; char C; } cC;
struct { char c; short s; } cs;
struct { char c; int i; } ci;
struct { char c; long l; } cl;
struct { char c; void* p; } cp;
struct { char c; float f; } cf;
struct { char c; double d; } cd;
static size_t char_alignment = alignof(C, cC);
static size_t short_alignment = alignof(s, cs);
static size_t int_alignment = alignof(i, ci);
static size_t long_alignment = alignof(l, cl);
static size_t ptr_alignment = alignof(p, cp);
static size_t float_alignment = alignof(f, cf);
static size_t double_alignment = alignof(d, cd);
switch (type->type) {
size_t alignment;
case ARGTYPE_LONG:
case ARGTYPE_ULONG:
return long_alignment;
case ARGTYPE_CHAR:
return char_alignment;
case ARGTYPE_SHORT:
case ARGTYPE_USHORT:
return short_alignment;
case ARGTYPE_FLOAT:
return float_alignment;
case ARGTYPE_DOUBLE:
return double_alignment;
case ARGTYPE_POINTER:
return ptr_alignment;
case ARGTYPE_ARRAY:
return type_alignof(proc, type->u.array_info.elt_type);
case ARGTYPE_STRUCT:
if (layout_struct(proc, type, NULL, &alignment, NULL) < 0)
return (size_t)-1;
return alignment;
default:
return int_alignment;
}
}
size_t
type_offsetof(struct process *proc, struct arg_type_info *type, size_t emt)
{
assert(type->type == ARGTYPE_STRUCT
|| type->type == ARGTYPE_ARRAY);
switch (type->type) {
size_t alignment;
size_t size;
case ARGTYPE_ARRAY:
alignment = type_alignof(proc, type->u.array_info.elt_type);
if (alignment == (size_t)-1)
return (size_t)-1;
size = type_sizeof(proc, type->u.array_info.elt_type);
if (size == (size_t)-1)
return (size_t)-1;
return emt * align(size, alignment);
case ARGTYPE_STRUCT:
if (layout_struct(proc, type, NULL, NULL, &emt) < 0)
return (size_t)-1;
return emt;
default:
abort();
}
}
struct arg_type_info *
type_element(struct arg_type_info *info, size_t emt)
{
assert(info->type == ARGTYPE_STRUCT
|| info->type == ARGTYPE_ARRAY);
switch (info->type) {
case ARGTYPE_ARRAY:
return info->u.array_info.elt_type;
case ARGTYPE_STRUCT:
assert(emt < type_struct_size(info));
return type_struct_get(info, emt);
default:
abort();
}
}
size_t
type_aggregate_size(struct arg_type_info *info)
{
assert(info->type == ARGTYPE_STRUCT
|| info->type == ARGTYPE_ARRAY);
switch (info->type) {
long ret;
case ARGTYPE_ARRAY:
if (expr_eval_constant(info->u.array_info.length, &ret) < 0)
return (size_t)-1;
return (size_t)ret;
case ARGTYPE_STRUCT:
return type_struct_size(info);
default:
abort();
}
}
int
type_is_integral(enum arg_type type)
{
switch (type) {
case ARGTYPE_INT:
case ARGTYPE_UINT:
case ARGTYPE_LONG:
case ARGTYPE_ULONG:
case ARGTYPE_CHAR:
case ARGTYPE_SHORT:
case ARGTYPE_USHORT:
return 1;
case ARGTYPE_VOID:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
case ARGTYPE_ARRAY:
case ARGTYPE_STRUCT:
case ARGTYPE_POINTER:
return 0;
}
abort();
}
int
type_is_signed(enum arg_type type)
{
assert(type_is_integral(type));
switch (type) {
case ARGTYPE_CHAR:
return CHAR_MIN != 0;
case ARGTYPE_SHORT:
case ARGTYPE_INT:
case ARGTYPE_LONG:
return 1;
case ARGTYPE_UINT:
case ARGTYPE_ULONG:
case ARGTYPE_USHORT:
return 0;
case ARGTYPE_VOID:
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
case ARGTYPE_ARRAY:
case ARGTYPE_STRUCT:
case ARGTYPE_POINTER:
abort();
}
abort();
}
struct arg_type_info *
type_get_fp_equivalent(struct arg_type_info *info)
{
/* Extract innermost structure. Give up early if any
* component has more than one element. */
while (info->type == ARGTYPE_STRUCT) {
if (type_struct_size(info) != 1)
return NULL;
info = type_element(info, 0);
}
switch (info->type) {
case ARGTYPE_CHAR:
case ARGTYPE_SHORT:
case ARGTYPE_INT:
case ARGTYPE_LONG:
case ARGTYPE_UINT:
case ARGTYPE_ULONG:
case ARGTYPE_USHORT:
case ARGTYPE_VOID:
case ARGTYPE_ARRAY:
case ARGTYPE_POINTER:
return NULL;
case ARGTYPE_FLOAT:
case ARGTYPE_DOUBLE:
return info;
case ARGTYPE_STRUCT:
abort();
}
abort();
}
struct arg_type_info *
type_get_hfa_type(struct arg_type_info *info, size_t *countp)
{
assert(info != NULL);
if (info->type != ARGTYPE_STRUCT
&& info->type != ARGTYPE_ARRAY)
return NULL;
size_t n = type_aggregate_size(info);
if (n == (size_t)-1)
return NULL;
struct arg_type_info *ret = NULL;
*countp = 0;
while (n-- > 0) {
struct arg_type_info *emt = type_element(info, n);
size_t emt_count = 1;
if (emt->type == ARGTYPE_STRUCT || emt->type == ARGTYPE_ARRAY)
emt = type_get_hfa_type(emt, &emt_count);
if (emt == NULL)
return NULL;
if (ret == NULL) {
if (emt->type != ARGTYPE_FLOAT
&& emt->type != ARGTYPE_DOUBLE)
return NULL;
ret = emt;
}
if (emt->type != ret->type)
return NULL;
*countp += emt_count;
}
return ret;
}