Debian Codegraph @

src/OVAL/probes/SEAP/sexp-value.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 //#ifdef HAVE_CONFIG_H
24 #include <config.h>
25 //#endif
26
27 #include <stdint.h>
28 #include <string.h>
29
30 #include "_sexp-atomic.h"
31 #include "_sexp-value.h"
32 #include "debug_priv.h"
33
34 int SEXP_val_new (SEXP_val_t *dst, size_t vmemsize, SEXP_type_t type)
35 {
36 void *s_val = oscap_aligned_malloc(sizeof(SEXP_valhdr_t) + vmemsize, SEXP_VALP_ALIGN);
37
38 SEXP_val_dsc (dst, (uintptr_t) s_val);
39
40 dst->hdr->refs = 1;
41 dst->hdr->size = vmemsize;
42 dst->type = type;
43 dst->ptr = SEXP_val_ptr (dst);
44 #if defined(SEAP_VERBOSE_DEBUG)
45 dD("new value: hdr->refs = %u, hdr->size = %zu, type = %hhu, ptr = %p",
46 dst->hdr->refs, dst->hdr->size, dst->type, (void *)dst->ptr);
47 #endif
48 return (0);
49 }
50
51 void SEXP_val_dsc (SEXP_val_t *dst, uintptr_t ptr)
52 {
53 dst->ptr = ptr;
54 dst->hdr = (SEXP_valhdr_t *)(ptr & SEXP_VALP_MASK);
55 dst->mem = (void *)(((uint8_t *)(dst->hdr)) + sizeof (SEXP_valhdr_t));
56 dst->type = ptr & SEXP_VALT_MASK;
57 }
58
59 uintptr_t SEXP_val_ptr (SEXP_val_t *dsc)
60 {
61 return ((dsc->ptr & SEXP_VALP_MASK) | (dsc->type & SEXP_VALT_MASK));
62 }
63
64 /*
65 * Return values:
66 * (uintptr_t)NULL - empty value
67 * ...other values mean success.
68 */
69 uintptr_t SEXP_rawval_incref (uintptr_t valp)
70 {
71 return SEXP_atomic_inc_u32 (&(SEXP_VALP_HDR(valp)->refs)) > 0 ? valp : (uintptr_t) NULL;
72 }
73
74 /*
75 * Return values:
76 * 0 - refs > 0
77 * 1 - refs == 0
78 */
79 int SEXP_rawval_decref (uintptr_t valp)
80 {
81 return (SEXP_atomic_dec_u32 (&(SEXP_VALP_HDR(valp)->refs)) == 0);
82 }
83
84 SEXP_numtype_t SEXP_rawval_number_type (SEXP_val_t *dsc)
85 {
86 return SEXP_NTYPEP(dsc->hdr->size, dsc->mem);
87 }
88
89 size_t SEXP_rawval_list_length (struct SEXP_val_list *list)
90 {
91 size_t length;
92 register struct SEXP_val_lblk *lblk;
93
94 length = 0;
95 lblk = SEXP_VALP_LBLK(list->b_addr);
96
97 while (lblk != NULL) {
98 length += lblk->real;
99 lblk = SEXP_VALP_LBLK(lblk->nxsz);
100 }
101
102 return (length - list->offset);
103 }
104
105 uintptr_t SEXP_rawval_lblk_new (uint8_t sz)
106 {
107 _A(sz < 16);
108
109 struct SEXP_val_lblk *lblk = oscap_aligned_malloc(
110 sizeof(struct SEXP_val_lblk),
111 SEXP_LBLK_ALIGN);
112 lblk->memb = malloc(sizeof(SEXP_t) * (1 << sz));
113
114 lblk->nxsz = ((uintptr_t)(NULL) & SEXP_LBLKP_MASK) | ((uintptr_t)sz & SEXP_LBLKS_MASK);
115 lblk->refs = 1;
116 lblk->real = 0;
117
118 return ((uintptr_t)lblk);
119 }
120
121 uintptr_t SEXP_rawval_lblk_incref (uintptr_t lblkp)
122 {
123 struct SEXP_val_lblk *lblk;
124 uint16_t refs;
125
126 lblk = SEXP_VALP_LBLK(lblkp);
127
128 /*
129 * Atomicaly update the reference counter.
130 * If the reference counter can't be updated
131 * (it would overflow), create a copy of the
132 * list.
133 */
134 for (;;) {
135 refs = lblk->refs;
136
137 if (refs < UINT16_MAX) {
138 if (SEXP_atomic_cas_u16 (&lblk->refs, refs, refs + 1))
139 break;
140 } else
141 return SEXP_rawval_lblk_copy (lblkp, 0);
142 }
143
144 return (lblkp);
145 }
146
147 int SEXP_rawval_lblk_decref (uintptr_t lblkp)
148 {
149 return (SEXP_atomic_dec_u16 (&SEXP_VALP_LBLK(lblkp)->refs) == 0);
150 }
151
152 uintptr_t SEXP_rawval_lblk_fill (uintptr_t lblkp, SEXP_t *s_exp[], uint16_t s_exp_count)
153 {
154 struct SEXP_val_lblk *lblk;
155
156 lblk = SEXP_VALP_LBLK(lblkp);
157
158 if (s_exp_count > (1 << (lblk->nxsz & 0xf)) - lblk->real)
159 return ((uintptr_t) NULL);
160
161 lblk->real = s_exp_count;
162
163 for (; s_exp_count > 0; --s_exp_count) {
164 lblk->memb[s_exp_count - 1].s_valp = SEXP_rawval_incref (s_exp[s_exp_count - 1]->s_valp);
165 lblk->memb[s_exp_count - 1].s_type = s_exp[s_exp_count - 1]->s_type;
166 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
167 lblk->memb[s_exp_count - 1].__magic0 = s_exp[s_exp_count - 1]->__magic0;
168 lblk->memb[s_exp_count - 1].__magic1 = s_exp[s_exp_count - 1]->__magic1;
169 #endif
170 }
171
172 return (lblkp);
173 }
174
175 uintptr_t SEXP_rawval_lblk_add (uintptr_t lblkp, const SEXP_t *s_exp)
176 {
177 uintptr_t lb_prev;
178 uintptr_t lb_head;
179 register struct SEXP_val_lblk *lblk;
180
181 lblk = SEXP_VALP_LBLK(lblkp);
182
183 if (lblk == NULL) {
184 lb_head = SEXP_rawval_lblk_new (1);
185 lb_prev = lb_head;
186 } else {
187 lb_head = lblkp;
188 lb_prev = 0;
189
190 do {
191 if (lblk->refs < 2) {
192 lb_prev = (uintptr_t)lblk;
193 lblk = SEXP_VALP_LBLK(lblk->nxsz);
194 } else {
195 uintptr_t lb_ptr;
196
197 /*
198 * We've encountered a block which belongs to more
199 * than one list so we have to create a copy of the
200 * rest of the list.
201 */
202 lb_ptr = SEXP_rawval_lblk_copy (lblkp, 0);
203
204 if (lb_prev == 0)
205 lb_head = lb_ptr;
206
207 /*
208 * Update pointer in the previous block (if there is one)
209 * to point at the copy. Also decrement the reference
210 * counter in current block.
211 */
212 if (lb_prev != 0)
213 SEXP_VALP_LBLK(lb_prev)->nxsz = (lb_ptr & SEXP_LBLKP_MASK) | (SEXP_VALP_LBLK(lb_prev)->nxsz & SEXP_LBLKS_MASK);
214
215 SEXP_rawval_lblk_decref (lblkp);
216
217 /*
218 * Get the last block without checking refs
219 * (we don't need to because it's our copy)
220 */
221 lb_prev = SEXP_rawval_lblk_last (lb_ptr);
222 break;
223 }
224 } while (lblk != NULL);
225 }
226
227 _A(lb_prev != 0);
228 _A(lb_head != 0);
229
230 (void)SEXP_rawval_lblk_add1 (lb_prev, s_exp);
231
232 return (lb_head);
233 }
234
235 uintptr_t SEXP_rawval_lblk_add1 (uintptr_t lblkp, const SEXP_t *s_exp)
236 {
237 struct SEXP_val_lblk *lblk = SEXP_VALP_LBLK(lblkp);
238
239 if (lblk->real < (1 << (lblk->nxsz & 0xf))) {
240
241 lblk->memb[lblk->real].s_valp = SEXP_rawval_incref (s_exp->s_valp);
242 lblk->memb[lblk->real].s_type = s_exp->s_type;
243 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
244 lblk->memb[lblk->real].__magic0 = s_exp->__magic0;
245 lblk->memb[lblk->real].__magic1 = s_exp->__magic1;
246 #endif
247 ++lblk->real;
248
249 return (lblkp);
250 } else {
251 uint8_t new_sz;
252 uintptr_t new_lb;
253
254 new_sz = lblk->nxsz & SEXP_LBLKS_MASK;
255 new_sz = new_sz == 15 ? 6 : new_sz + 1;
256
257 new_lb = SEXP_rawval_lblk_new (new_sz);
258 lblk->nxsz = (new_lb & SEXP_LBLKP_MASK) | (lblk->nxsz & SEXP_LBLKS_MASK);
259
260 /*
261 * We don't need to check the return value
262 * here because we are adding the S-exp to
263 * a newly allocated block and there is at
264 * least one free slot.
265 */
266 SEXP_rawval_lblk_add1 (new_lb, s_exp);
267
268 return (lblkp);
269 }
270
271 /* NOTREACHED */
272 return ((uintptr_t) NULL);
273 }
274
275 uintptr_t SEXP_rawval_lblk_last (uintptr_t lblkp)
276 {
277 register struct SEXP_val_lblk *lblk;
278 register uintptr_t last;
279
280 last = lblkp;
281 lblk = SEXP_VALP_LBLK(last);
282
283 while ((lblk->nxsz & SEXP_LBLKP_MASK) != ((uintptr_t)NULL)) {
284 last = lblk->nxsz;
285 lblk = SEXP_VALP_LBLK(last);
286 }
287
288 return (last);
289 }
290
291 SEXP_t *SEXP_rawval_lblk_nth (uintptr_t lblkp, uint32_t n)
292 {
293 register struct SEXP_val_lblk *lblk;
294
295 lblk = SEXP_VALP_LBLK(lblkp);
296
297 while (lblk != NULL) {
298 if (lblk->real >= n) {
299 return (lblk->memb + (n - 1));
300 } else {
301 n -= lblk->real;
302 lblk = SEXP_VALP_LBLK(lblk->nxsz);
303 }
304 }
305
306 return (NULL);
307 }
308
309 uintptr_t SEXP_rawval_lblk_replace (uintptr_t lblkp, uint32_t n, const SEXP_t *n_val, SEXP_t **o_val)
310 {
311 uintptr_t lb_prev;
312 uintptr_t lb_head;
313 struct SEXP_val_lblk *lblk;
314 SEXP_t *memb;
315
316 lblk = SEXP_VALP_LBLK(lblkp);
317
318 _A(lblk != NULL);
319
320 lb_head = lblkp;
321 lb_prev = 0;
322
323 while (n > lblk->real) {
324 if (lblk->refs < 2) {
325 n -= lblk->real;
326 lb_prev = (uintptr_t)lblk;
327 lblk = SEXP_VALP_LBLK(lblk->nxsz);
328
329 if (lblk == NULL) {
330 (*o_val) = NULL;
331 return (lb_head);
332 }
333 } else {
334 uintptr_t lb_ptr;
335
336 /*
337 * We've encountered a block which belongs to more
338 * than one list so we have to create a copy of the
339 * rest of the list.
340 */
341 lb_ptr = SEXP_rawval_lblk_copy ((uintptr_t)lblk, 0);
342
343 if (lb_prev == 0)
344 lb_head = lb_ptr;
345
346 /*
347 * Update pointer in the previous block (if there is one)
348 * to point at the copy. Also decrement the reference
349 * counter in current block.
350 */
351 if (lb_prev != 0)
352 SEXP_VALP_LBLK(lb_prev)->nxsz = (lb_ptr & SEXP_LBLKP_MASK) | (SEXP_VALP_LBLK(lb_prev)->nxsz & SEXP_LBLKS_MASK);
353
354 SEXP_rawval_lblk_decref ((uintptr_t)lblk);
355 memb = SEXP_rawval_lblk_nth (lb_ptr, n);
356
357 if (memb == NULL)
358 abort();
359
360 goto replace;
361 }
362 }
363
364 _A(n > 0);
365
366 memb = lblk->memb + (n - 1);
367 replace:
368 _A(lb_head != 0);
369 _A(memb != NULL);
370
371 (*o_val) = SEXP_new ();
372 (*o_val)->s_valp = memb->s_valp;
373 (*o_val)->s_type = memb->s_type;
374 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
375 (*o_val)->__magic0 = memb->__magic0;
376 (*o_val)->__magic1 = memb->__magic1;
377 #endif
378
379 memb->s_valp = SEXP_rawval_incref (n_val->s_valp);
380 memb->s_type = n_val->s_type;
381 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
382 memb->__magic0 = n_val->__magic0;
383 memb->__magic1 = n_val->__magic1;
384 #endif
385 return (lb_head);
386 }
387
388 int SEXP_rawval_lblk_cb (uintptr_t lblkp, int (*func) (SEXP_t *, void *), void *arg, uint32_t n)
389 {
390 register struct SEXP_val_lblk *lblk;
391 register uint16_t bi;
392
393 lblk = SEXP_VALP_LBLK(lblkp);
394
395 while (lblk != NULL) {
396 if (lblk->real >= n) {
397 int ret;
398
399 bi = (uint16_t)n - 1;
400
401 do {
402 while (bi < lblk->real) {
403 ret = func (lblk->memb + bi, arg);
404
405 if (ret != 0)
406 return (ret);
407 else
408 ++bi;
409 }
410
411 lblk = SEXP_VALP_LBLK(lblk->nxsz);
412 bi = 0;
413 } while (lblk != NULL);
414
415 break;
416 } else {
417 n -= lblk->real;
418 lblk = SEXP_VALP_LBLK(lblk->nxsz);
419 }
420 }
421
422 return (0);
423 }
424
425 uintptr_t SEXP_rawval_list_copy (uintptr_t s_valp)
426 {
427 SEXP_val_t v_dsc_o, v_dsc_c;
428
429 if (SEXP_val_new (&v_dsc_c, sizeof (void *) + sizeof (uint16_t),
430 SEXP_VALTYPE_LIST) != 0)
431 {
432 /* TODO: handle this */
433 return ((uintptr_t)NULL);
434 }
435
436 SEXP_val_dsc (&v_dsc_o, s_valp);
437
438 SEXP_LCASTP(v_dsc_c.mem)->b_addr = (void *) SEXP_rawval_lblk_copy ((uintptr_t)SEXP_LCASTP(v_dsc_o.mem)->b_addr,
439 (uintptr_t)SEXP_LCASTP(v_dsc_o.mem)->offset);
440 SEXP_LCASTP(v_dsc_c.mem)->offset = 0;
441
442 return (SEXP_val_ptr (&v_dsc_c));
443 }
444
445 uintptr_t SEXP_rawval_lblk_copy (uintptr_t lblkp, uint16_t n_skip)
446 {
447 struct SEXP_val_lblk *lb_new, *lb_old;
448 uintptr_t lb_next;
449 uintptr_t lb_head;
450 uint16_t off_n; /* offset in the new block */
451 uint16_t off_o; /* offset in the old block */
452 uint8_t cur_sz; /* size of the new block */
453
454 lb_head = 0;
455 off_n = 0;
456 off_o = n_skip;
457 lb_old = SEXP_VALP_LBLK(lblkp);
458
459 if (lb_old == NULL)
460 return ((uintptr_t) NULL);
461
462 cur_sz = 0;
463 lb_new = (struct SEXP_val_lblk *)SEXP_rawval_lblk_new (cur_sz);
464 lb_head = (uintptr_t)lb_new;
465
466 while (lb_old != NULL) {
467 if ((lb_old->real - off_o) == 0) {
468 /*
469 * move to the next old block
470 */
471 lb_old = SEXP_VALP_LBLK(lb_old->nxsz);
472
473 if (lb_old == NULL)
474 break;
475
476 off_o = 0;
477 }
478
479 /*
480 * allocate new block
481 */
482 if (lb_new->real >= (1 << (cur_sz))) {
483 lb_next = SEXP_rawval_lblk_new (++cur_sz);
484 lb_new->nxsz = (lb_next & SEXP_LBLKP_MASK) | (lb_new->nxsz & SEXP_LBLKS_MASK);
485 lb_new = SEXP_VALP_LBLK(lb_next);
486 off_n = 0;
487 }
488
489 /*
490 * copy list items
491 */
492 while (off_n < ((uint16_t)1 << cur_sz) && off_o < lb_old->real) {
493 lb_new->memb[off_n].s_valp = SEXP_rawval_incref (lb_old->memb[off_o].s_valp);
494 lb_new->memb[off_n].s_type = lb_old->memb[off_o].s_type;
495 #if !defined(NDEBUG) || defined(VALIDATE_SEXP)
496 lb_new->memb[off_n].__magic0 = lb_old->memb[off_o].__magic0;
497 lb_new->memb[off_n].__magic1 = lb_old->memb[off_o].__magic1;
498 #endif
499
500 ++off_n;
501 ++off_o;
502 ++lb_new->real;
503 }
504 }
505
506 return (lb_head);
507 }
508
509 void SEXP_rawval_lblk_free (uintptr_t lblkp, void (*func) (SEXP_t *))
510 {
511 if (SEXP_rawval_lblk_decref (lblkp)) {
512 struct SEXP_val_lblk *lblk, *next;
513
514 lblk = SEXP_VALP_LBLK(lblkp);
515 next = SEXP_VALP_LBLK(lblk->nxsz);
516
517 while (lblk->real > 0) {
518 --lblk->real;
519 func (lblk->memb + lblk->real);
520 }
521
522 free(lblk->memb);
523 oscap_aligned_free(lblk);
524
525 if (next != NULL)
526 SEXP_rawval_lblk_free ((uintptr_t)next, func);
527 }
528
529 return;
530 }
531
532 void SEXP_rawval_lblk_free1 (uintptr_t lblkp, void (*func) (SEXP_t *))
533 {
534 if (SEXP_rawval_lblk_decref (lblkp)) {
535 struct SEXP_val_lblk *lblk;
536
537 lblk = SEXP_VALP_LBLK(lblkp);
538
539 while (lblk->real > 0) {
540 --lblk->real;
541 func (lblk->memb + lblk->real);
542 }
543
544 free(lblk->memb);
545 oscap_aligned_free(lblk);
546 }
547
548 return;
549 }
550
551 uintptr_t SEXP_rawval_copy(uintptr_t s_valp)
552 {
553 uintptr_t uptr;
554 SEXP_val_t v_dsc;
555 SEXP_val_dsc(&v_dsc, s_valp);
556
557 if (v_dsc.type != SEXP_VALTYPE_LIST) {
558 SEXP_val_t v_dsc_copy;
559 if (SEXP_val_new(&v_dsc_copy, v_dsc.hdr->size, v_dsc.type) != 0) {
560 return ((uintptr_t)NULL);
561 }
562 memcpy(v_dsc_copy.mem, v_dsc.mem, v_dsc.hdr->size);
563 uptr = SEXP_val_ptr(&v_dsc_copy);
564 } else {
565 uptr = SEXP_rawval_list_copy(s_valp);
566 }
567 return uptr;
568 }