-
Notifications
You must be signed in to change notification settings - Fork 17
/
value.c
500 lines (431 loc) · 10.7 KB
/
value.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
/*
* This file is part of ltrace.
* Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
*
* 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 <string.h>
#include <assert.h>
#include <stdlib.h>
#include "value.h"
#include "type.h"
#include "common.h"
#include "expr.h"
#include "backend.h"
static void
value_common_init(struct value *valp, struct process *inferior,
struct value *parent, struct arg_type_info *type,
int own_type)
{
valp->type = type;
valp->own_type = own_type;
valp->inferior = inferior;
memset(&valp->u, 0, sizeof(valp->u));
valp->where = VAL_LOC_NODATA;
valp->parent = parent;
valp->size = (size_t)-1;
}
void
value_init(struct value *valp, struct process *inferior, struct value *parent,
struct arg_type_info *type, int own_type)
{
assert(inferior != NULL);
value_common_init(valp, inferior, parent, type, own_type);
}
void
value_init_detached(struct value *valp, struct value *parent,
struct arg_type_info *type, int own_type)
{
value_common_init(valp, NULL, parent, type, own_type);
}
void
value_set_type(struct value *value, struct arg_type_info *type, int own_type)
{
if (value->own_type) {
type_destroy(value->type);
free(value->type);
}
value->type = type;
value->own_type = own_type;
}
void
value_take_type(struct value *value, struct arg_type_info **type,
int *own_type)
{
*type = value->type;
*own_type = value->own_type;
value->own_type = 0;
}
void
value_release(struct value *val)
{
if (val == NULL)
return;
if (val->where == VAL_LOC_COPY) {
free(val->u.address);
val->where = VAL_LOC_NODATA;
}
}
void
value_destroy(struct value *val)
{
if (val == NULL)
return;
value_release(val);
value_set_type(val, NULL, 0);
}
unsigned char *
value_reserve(struct value *valp, size_t size)
{
value_release(valp);
if (size <= sizeof(valp->u.value)) {
valp->where = VAL_LOC_WORD;
valp->u.value = 0;
} else {
valp->where = VAL_LOC_COPY;
valp->u.address = calloc(size, 1);
if (valp->u.address == 0)
return NULL;
}
return value_get_raw_data(valp);
}
void
value_in_inferior(struct value *valp, arch_addr_t address)
{
value_release(valp);
valp->where = VAL_LOC_INFERIOR;
valp->u.address = address;
}
int
value_reify(struct value *val, struct value_dict *arguments)
{
if (val->where != VAL_LOC_INFERIOR)
return 0;
assert(val->inferior != NULL);
size_t size = value_size(val, arguments);
if (size == (size_t)-1)
return -1;
void *data;
enum value_location_t nloc;
if (size <= sizeof(val->u.value)) {
data = &val->u.value;
nloc = VAL_LOC_WORD;
} else {
data = malloc(size);
if (data == NULL)
return -1;
nloc = VAL_LOC_COPY;
}
if (umovebytes(val->inferior, val->u.inf_address, data, size) < size) {
if (nloc == VAL_LOC_COPY)
free(data);
return -1;
}
val->where = nloc;
if (nloc == VAL_LOC_COPY)
val->u.address = data;
return 0;
}
unsigned char *
value_get_data(struct value *val, struct value_dict *arguments)
{
if (value_reify(val, arguments) < 0)
return NULL;
return value_get_raw_data(val);
}
unsigned char *
value_get_raw_data(struct value *val)
{
switch (val->where) {
case VAL_LOC_INFERIOR:
abort();
case VAL_LOC_NODATA:
return NULL;
case VAL_LOC_COPY:
case VAL_LOC_SHARED:
return val->u.address;
case VAL_LOC_WORD:
return val->u.buf;
}
assert(!"Unexpected value of val->where");
abort();
}
int
value_clone(struct value *retp, const struct value *val)
{
*retp = *val;
if (val->own_type) {
retp->type = malloc(sizeof(struct arg_type_info));
if (type_clone (retp->type, val->type) < 0) {
free(retp->type);
return -1;
}
}
if (val->where == VAL_LOC_COPY) {
assert(val->inferior != NULL);
size_t size = type_sizeof(val->inferior, val->type);
if (size == (size_t)-1) {
fail:
if (retp->own_type) {
type_destroy(retp->type);
free(retp->type);
}
return -1;
}
retp->u.address = malloc(size);
if (retp->u.address == NULL)
goto fail;
memcpy(retp->u.address, val->u.address, size);
}
return 0;
}
size_t
value_size(struct value *val, struct value_dict *arguments)
{
if (val->size != (size_t)-1)
return val->size;
if (val->type->type != ARGTYPE_ARRAY)
return val->size = type_sizeof(val->inferior, val->type);
struct value length;
if (expr_eval(val->type->u.array_info.length, val,
arguments, &length) < 0)
return (size_t)-1;
size_t l;
int o = value_extract_word(&length, (long *)&l, arguments);
value_destroy(&length);
if (o < 0)
return (size_t)-1;
size_t elt_size = type_sizeof(val->inferior,
val->type->u.array_info.elt_type);
if (elt_size == (size_t)-1)
return (size_t)-1;
return val->size = elt_size * l;
}
int
value_init_element(struct value *ret_val, struct value *val, size_t element)
{
size_t off = type_offsetof(val->inferior, val->type, element);
if (off == (size_t)-1)
return -1;
struct arg_type_info *e_info = type_element(val->type, element);
if (e_info == NULL)
return -1;
value_common_init(ret_val, val->inferior, val, e_info, 0);
switch (val->where) {
case VAL_LOC_COPY:
case VAL_LOC_SHARED:
ret_val->u.address = val->u.address + off;
ret_val->where = VAL_LOC_SHARED;
return 0;
case VAL_LOC_WORD:
ret_val->u.address = value_get_raw_data(val) + off;
ret_val->where = VAL_LOC_SHARED;
return 0;
case VAL_LOC_INFERIOR:
ret_val->u.inf_address = val->u.inf_address + off;
ret_val->where = VAL_LOC_INFERIOR;
return 0;
case VAL_LOC_NODATA:
assert(!"Can't offset NODATA.");
abort();
}
abort();
}
int
value_init_deref(struct value *ret_val, struct value *valp)
{
assert(valp->type->type == ARGTYPE_POINTER);
/* Note: extracting a pointer value should not need value_dict
* with function arguments. */
long l;
if (value_extract_word(valp, &l, NULL) < 0)
return -1;
/* We need "long" to be long enough to hold platform
* pointers. */
(void)sizeof(char[1 - 2*(sizeof(l) < sizeof(void *))]);
value_common_init(ret_val, valp->inferior, valp,
valp->type->u.ptr_info.info, 0);
ret_val->u.value = l; /* Set the address. */
ret_val->where = VAL_LOC_INFERIOR;
return 0;
}
/* The functions value_extract_buf and value_extract_word assume that
* data in VALUE is stored at the start of the internal buffer. For
* value_extract_buf in particular there's no other reasonable
* default. If we need to copy out four bytes, they need to be the
* bytes pointed to by the buffer pointer.
*
* But actually the situation is similar for value_extract_word as
* well. This function is used e.g. to extract characters from
* strings. Those weren't stored by value_set_word, they might still
* be in client for all we know. So value_extract_word has to assume
* that the whole of data is data is stored at the buffer pointer.
*
* This is a problem on big endian machines, where 2-byte quantity
* carried in 4- or 8-byte long is stored at the end of that long.
* (Though that quantity itself is still big endian.) So we need to
* make a little dance to shift the value to the right part of the
* buffer. */
union word_data {
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
long l;
unsigned char buf[0];
} u;
void
value_set_word(struct value *value, long word)
{
size_t sz = type_sizeof(value->inferior, value->type);
assert(sz != (size_t)-1);
assert(sz <= sizeof(value->u.value));
value->where = VAL_LOC_WORD;
union word_data u = {};
switch (sz) {
case 0:
u.l = 0;
break;
case 1:
u.u8 = word;
break;
case 2:
u.u16 = word;
break;
case 4:
u.u32 = word;
break;
case 8:
u.u64 = word;
break;
default:
assert(sz != sz);
abort();
}
value->u.value = u.l;
}
static int
value_extract_buf_sz(struct value *value, unsigned char *tgt, size_t sz,
struct value_dict *arguments)
{
unsigned char *data = value_get_data(value, arguments);
if (data == NULL)
return -1;
memcpy(tgt, data, sz);
return 0;
}
int
value_extract_word(struct value *value, long *retp,
struct value_dict *arguments)
{
size_t sz = type_sizeof(value->inferior, value->type);
if (sz == (size_t)-1)
return -1;
assert(sz <= sizeof(value->u.value));
if (sz == 0) {
*retp = 0;
return 0;
}
union word_data u = {};
if (value_extract_buf_sz(value, u.buf, sz, arguments) < 0)
return -1;
switch (sz) {
case 1:
*retp = (long)u.u8;
return 0;
case 2:
*retp = (long)u.u16;
return 0;
case 4:
*retp = (long)u.u32;
return 0;
case 8:
*retp = (long)u.u64;
return 0;
default:
assert(sz != sz);
abort();
}
}
int
value_extract_buf(struct value *value, unsigned char *tgt,
struct value_dict *arguments)
{
size_t sz = type_sizeof(value->inferior, value->type);
if (sz == (size_t)-1)
return -1;
return value_extract_buf_sz(value, tgt, sz, arguments);
}
struct value *
value_get_parental_struct(struct value *val)
{
struct value *parent;
for (parent = val->parent; parent != NULL; parent = parent->parent)
if (parent->type->type == ARGTYPE_STRUCT)
return parent;
return NULL;
}
int
value_is_zero(struct value *val, struct value_dict *arguments)
{
unsigned char *data = value_get_data(val, arguments);
if (data == NULL)
return -1;
size_t sz = type_sizeof(val->inferior, val->type);
if (sz == (size_t)-1)
return -1;
int zero = 1;
size_t j;
for (j = 0; j < sz; ++j) {
if (data[j] != 0) {
zero = 0;
break;
}
}
return zero;
}
int
value_equal(struct value *val1, struct value *val2,
struct value_dict *arguments)
{
size_t sz1 = type_sizeof(val1->inferior, val1->type);
size_t sz2 = type_sizeof(val2->inferior, val2->type);
if (sz1 == (size_t)-1 || sz2 == (size_t)-1)
return -1;
if (sz1 != sz2)
return 0;
unsigned char *data1 = value_get_data(val1, arguments);
unsigned char *data2 = value_get_data(val2, arguments);
if (data1 == NULL || data2 == NULL)
return -1;
return memcmp(data1, data2, sz1) == 0 ? 1 : 0;
}
int
value_pass_by_reference(struct value *value)
{
assert(value != NULL);
assert(value->type->type == ARGTYPE_STRUCT);
struct arg_type_info *new_info = calloc(sizeof(*new_info), 1);
if (new_info == NULL)
return -1;
int own;
struct arg_type_info *orig;
value_take_type(value, &orig, &own);
type_init_pointer(new_info, orig, own);
new_info->lens = orig->lens;
value_set_type(value, new_info, 1);
return 0;
}