forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.c
More file actions
2594 lines (2300 loc) · 96 KB
/
Copy pathmagic.c
File metadata and controls
2594 lines (2300 loc) · 96 KB
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <magic.h>
#include <magic_mem.h>
#include <magic_asr.h>
#include <magic_eval.h>
#include <magic_analysis.h>
#include <magic_splay_tree.h>
#include <stdarg.h>
#if MAGIC_MEM_USAGE_OUTPUT_CTL
#include <common/util/time.h>
#endif
/* Workaround for extern-only structs. */
#ifndef __MINIX
#include <stdio.h>
EXTERN FILE *stdout;
PUBLIC FILE **UNUSED(_____magic_instr_FILE_unused) = &stdout;
#include <ncurses.h>
PUBLIC WINDOW *UNUSED(_____magic_instr_WINDOW_unused);
#endif
#include <netinet/in.h>
PUBLIC struct in6_addr *UNUSED(_____magic_instr_in6_addr_unused);
/* Magic printf. */
MAGIC_VAR printf_ptr_t _magic_printf = MAGIC_PRINTF_DEFAULT;
/* Magic lock primitives. */
PUBLIC magic_lock_t magic_dsentry_lock = NULL;
PUBLIC magic_unlock_t magic_dsentry_unlock = NULL;
PUBLIC void *magic_dsentry_lock_args = NULL;
PUBLIC void *magic_dsentry_unlock_args = NULL;
PUBLIC magic_lock_t magic_dfunction_lock = NULL;
PUBLIC magic_unlock_t magic_dfunction_unlock = NULL;
PUBLIC void *magic_dfunction_lock_args = NULL;
PUBLIC void *magic_dfunction_unlock_args = NULL;
PUBLIC magic_lock_t magic_dsodesc_lock = NULL;
PUBLIC magic_unlock_t magic_dsodesc_unlock = NULL;
PUBLIC void *magic_dsodesc_lock_args = NULL;
PUBLIC void *magic_dsodesc_unlock_args = NULL;
PUBLIC magic_lock_t magic_mpdesc_lock = NULL;
PUBLIC magic_unlock_t magic_mpdesc_unlock = NULL;
PUBLIC void *magic_mpdesc_lock_args = NULL;
PUBLIC void *magic_mpdesc_unlock_args = NULL;
/* Magic vars references. */
MAGIC_VAR struct _magic_vars_t _magic_vars_buff = {
/* Address Space Randomization (ASRPass) */
0, /* asr_seed */
0, /* asr_heap_map_do_permutate */
0, /* asr_heap_max_offset */
0, /* asr_heap_max_padding */
0, /* asr_map_max_offset_pages */
0, /* asr_map_max_padding_pages */
/* Runtime flags. */
0, /* no_mem_inst */
/* Magic type array. */
NULL, /* types */
0, /* types_num */
0, /* types_next_id */
/* Magic function array. */
NULL, /* functions */
0, /* functions_num */
0, /* functions_next_id */
/* Magic state entry array. */
NULL, /* sentries */
0, /* sentries_num */
0, /* sentries_str_num */
0, /* sentries_next_id */
/* Magic dynamic state index array. */
NULL, /* dsindexes */
0, /* dsindexes_num */
/* Magic dynamic state entry list. */
NULL, /* first_dsentry */
0, /* num_dead_sentries */
0, /* size_dead_dsentries */
/* Magic memory pool dynamic state entry list. */
NULL, /* first_mempool_dsentry */
/* Magic dynamic function list. */
NULL, /* first_dfunction */
NULL, /* last_dfunction */
0, /* dfunctions_num */
/* Magic SO library descriptor list. */
NULL, /* first_sodesc */
NULL, /* last_sodesc */
0, /* sodescs_num */
/* Magic DSO library descriptor list. */
NULL, /* first_dsodesc */
NULL, /* last_dsodesc */
0, /* dsodescs_num */
/* Magic stack-related variables. */
NULL, /* first_stack_dsentry */
NULL, /* last_stack_dsentry */
/* Magic memory ranges */
{ (void*) ULONG_MAX, (void*) 0 }, /* *null_range[2] */
{0,0}, /* *data_range[2] */
{0,0}, /* *heap_range[2] */
{0,0}, /* *map_range[2] */
{0,0}, /* *shm_range[2] */
{0,0}, /* *stack_range[2] */
{0,0}, /* *text_range[2] */
{0,0}, /* *sentry_range[2] */
{0,0}, /* *function_range[2] */
{0,0}, /* *dfunction_range[2] */
NULL, /* *heap_start */
NULL, /* *heap_end */
1, /* update_dsentry_ranges */
1, /* update_dfunction_ranges */
#ifdef __MINIX
{ { NULL, 0 } }, /* unmap_mem */
#endif
NULL, /* sentry_rl_buff */
0, /* sentry_rl_buff_offset */
0, /* sentry_rl_buff_size */
NULL, /* sentry_rl_index */
NULL, /* sentry_hash_buff */
0, /* sentry_hash_buff_offset */
0, /* sentry_hash_buff_size */
NULL, /* sentry_hash_head */
NULL, /* function_hash_buff */
0, /* function_hash_buff_offset */
0, /* function_hash_buff_size */
NULL, /* function_hash_head */
0 /* fake_malloc */
};
PUBLIC struct _magic_vars_t *_magic_vars = &_magic_vars_buff;
/* Magic void ptr and array (force at the least 1 void* and 1 void array in the list of globals). */
PUBLIC void* MAGIC_VOID_PTR = NULL;
PUBLIC char MAGIC_VOID_ARRAY[1];
/* Magic special types. */
MAGIC_VAR struct _magic_type *MAGIC_VOID_PTR_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_VOID_PTR_INT_CAST_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_VOID_ARRAY_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_PTRINT_TYPE = NULL;
MAGIC_VAR struct _magic_type *MAGIC_PTRINT_ARRAY_TYPE = NULL;
/* Magic annotations. */
MAGIC_VAR VOLATILE int MAGIC_CALL_ANNOTATION_VAR;
/* Magic status variables. */
PUBLIC int magic_init_done = 0;
PUBLIC int magic_libcommon_active = 0;
PUBLIC int magic_lookup_nested_dsentries = 1;
PUBLIC int magic_allow_dead_dsentries = MAGIC_ALLOW_DEAD_DSENTRIES_DEFAULT;
PUBLIC int magic_ignore_dead_dsentries = 0;
PUBLIC int magic_mmap_dsentry_header_prot = PROT_READ | PROT_WRITE;
MAGIC_VAR int _magic_enabled = 0;
MAGIC_VAR int _magic_checkpoint_enabled = 0;
MAGIC_VAR int _magic_lazy_checkpoint_enabled = 0;
/* Magic out-of-band dsentries. */
PUBLIC struct _magic_obdsentry _magic_obdsentries[MAGIC_MAX_OBDSENTRIES];
/* Pool management data. */
PUBLIC struct _magic_mpdesc _magic_mpdescs[MAGIC_MAX_MEMPOOLS];
/* Magic page size. */
PUBLIC unsigned long magic_sys_pagesize = 0;
/* Private variables. */
PUBLIC int magic_type_str_print_style = MAGIC_TYPE_STR_PRINT_STYLE_DEFAULT;
PRIVATE THREAD_LOCAL const struct _magic_type* magic_nested_types[MAGIC_MAX_RECURSIVE_TYPES] = {0};
PRIVATE THREAD_LOCAL const struct _magic_type* magic_nested_types2[MAGIC_MAX_RECURSIVE_TYPES] = {0};
PRIVATE THREAD_LOCAL unsigned magic_level = 0;
PRIVATE THREAD_LOCAL unsigned magic_counter;
PRIVATE THREAD_LOCAL struct _magic_dsentry magic_dsentry_buff;
PRIVATE THREAD_LOCAL struct _magic_dfunction magic_dfunction_buff;
/* Magic default stubs. */
PUBLIC struct _magic_type magic_default_type = {
0, "", NULL, 0, "", 0, 0, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_OPAQUE, 0, 0, NULL
};
PUBLIC struct _magic_dsentry magic_default_dsentry = {
MAGIC_DSENTRY_MNUM, /* magic_number */
"", /* parent_name */
{ 0 }, /* name_ext_buff */
{ 0, "", NULL, MAGIC_STATE_DYNAMIC, NULL, NULL }, /* sentry */
{ 0, "", NULL, 0, "", 0, 0, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_ARRAY, MAGIC_TYPE_IS_ROOT|MAGIC_TYPE_DYNAMIC, 0, NULL }, /* type */
{ NULL }, /* type_array */
#if MAGIC_DSENTRY_ALLOW_PREV
NULL, /* prev */
#endif
NULL, /* next */
NULL, /* next_mpool */
NULL, /* next_mblock */
#ifndef __MINIX
NULL, /* next_sobject */
NULL, /* sobject_base_addr */
#endif
NULL, /* ext */
MAGIC_DSENTRY_MSTATE_ALIVE, /* magic_state */
{ { 0, 0 } }, /* alloc_flags */
0 /* site_id */
};
PUBLIC struct _magic_dfunction magic_default_dfunction = {
MAGIC_DFUNCTION_MNUM,
"",
{ 0, "", NULL, MAGIC_STATE_DYNAMIC|MAGIC_STATE_TEXT|MAGIC_STATE_CONSTANT, NULL },
NULL,
NULL
};
PUBLIC struct _magic_type magic_default_ret_addr_type = {
0, "", NULL, 0, "", sizeof(void*), 1, NULL, NULL, NULL, NULL, NULL, MAGIC_TYPE_POINTER, MAGIC_TYPE_IS_ROOT|MAGIC_TYPE_DYNAMIC|MAGIC_TYPE_INT_CAST|MAGIC_TYPE_STRICT_VALUE_SET, 0, NULL
};
/* Magic code reentrant flag. */
PRIVATE int magic_reentrant = 1;
/*===========================================================================*
* _magic_vars_addr *
*===========================================================================*/
void *_magic_vars_addr()
{
return _magic_vars;
}
/*===========================================================================*
* _magic_vars_size *
*===========================================================================*/
size_t _magic_vars_size()
{
return sizeof(struct _magic_vars_t);
}
/*===========================================================================*
* magic_null_printf *
*===========================================================================*/
PUBLIC int magic_null_printf(const char *format, ...)
{
return 0;
}
#ifndef __MINIX
/*===========================================================================*
* magic_err_printf *
*===========================================================================*/
PUBLIC int magic_err_printf(const char *format, ...)
{
va_list va;
int ret;
va_start(va, format);
ret = vfprintf(stderr, format, va);
va_end(va);
return ret;
}
#endif
/*===========================================================================*
* magic_set_printf *
*===========================================================================*/
PUBLIC void magic_set_printf(printf_ptr_t func_ptr)
{
assert(func_ptr);
_magic_printf = func_ptr;
}
/*===========================================================================*
* magic_get_printf *
*===========================================================================*/
PUBLIC printf_ptr_t magic_get_printf(void)
{
return _magic_printf;
}
/*===========================================================================*
* magic_reentrant_enable *
*===========================================================================*/
PUBLIC void magic_reentrant_enable(void)
{
magic_reentrant = 1;
}
/*===========================================================================*
* magic_reentrant_disable *
*===========================================================================*/
PUBLIC void magic_reentrant_disable(void)
{
magic_reentrant = 0;
}
/*===========================================================================*
* magic_assert_failed *
*===========================================================================*/
PUBLIC void __dead magic_assert_failed(const char *assertion, const char *file,
const char *function, const int line)
{
_magic_printf("Assertion '%s' failed in file %s, function %s(), line %d, pid %d\n",
assertion, file, function, line, getpid());
abort();
}
/*===========================================================================*
* magic_get_sys_pagesize *
*===========================================================================*/
PUBLIC unsigned long magic_get_sys_pagesize()
{
if(!magic_sys_pagesize) {
magic_sys_pagesize = SYS_PAGESIZE;
}
return magic_sys_pagesize;
}
/*===========================================================================*
* magic_dsentry_set_lock_primitives *
*===========================================================================*/
PUBLIC void magic_dsentry_set_lock_primitives(magic_lock_t lock,
magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
assert(lock && unlock);
magic_dsentry_lock = lock;
magic_dsentry_unlock = unlock;
magic_dsentry_lock_args = lock_args;
magic_dsentry_unlock_args = unlock_args;
}
/*===========================================================================*
* magic_dfunction_set_lock_primitives *
*===========================================================================*/
PUBLIC void magic_dfunction_set_lock_primitives(magic_lock_t lock,
magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
assert(lock && unlock);
magic_dfunction_lock = lock;
magic_dfunction_unlock = unlock;
magic_dfunction_lock_args = lock_args;
magic_dfunction_unlock_args = unlock_args;
}
/*===========================================================================*
* magic_dsodesc_set_lock_primitives *
*===========================================================================*/
PUBLIC void magic_dsodesc_set_lock_primitives(magic_lock_t lock,
magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
assert(lock && unlock);
magic_dsodesc_lock = lock;
magic_dsodesc_unlock = unlock;
magic_dsodesc_lock_args = lock_args;
magic_dsodesc_unlock_args = unlock_args;
}
/*===========================================================================*
* magic_mpdesc_set_lock_primitives *
*===========================================================================*/
PUBLIC void magic_mpdesc_set_lock_primitives(magic_lock_t lock,
magic_unlock_t unlock, void *lock_args, void *unlock_args)
{
assert(lock && unlock);
magic_mpdesc_lock = lock;
magic_mpdesc_unlock = unlock;
magic_mpdesc_lock_args = lock_args;
magic_mpdesc_unlock_args = unlock_args;
}
/*===========================================================================*
* magic_types_init *
*===========================================================================*/
PRIVATE void magic_types_init()
{
static struct _magic_type _magic_void_ptr_type_buff;
static struct _magic_type _magic_void_array_type_buff;
static struct _magic_type *_magic_void_array_type_contained_types[1];
static struct _magic_type _magic_ptrint_type_buff;
static const char* _magic_ptrint_type_name = "ptrint";
static char _magic_ptrint_type_str_buff[8];
static struct _magic_type _magic_ptrint_array_type_buff;
static struct _magic_type *_magic_ptrint_array_type_contained_types[1];
assert(MAGIC_VOID_PTR_TYPE);
assert(MAGIC_VOID_PTR_TYPE->size == sizeof(void*));
assert(MAGIC_VOID_TYPE->size == sizeof(char));
MAGIC_VOID_PTR_INT_CAST_TYPE = &_magic_void_ptr_type_buff;
*MAGIC_VOID_PTR_INT_CAST_TYPE = *MAGIC_VOID_PTR_TYPE;
MAGIC_VOID_PTR_INT_CAST_TYPE->flags |= MAGIC_TYPE_INT_CAST;
MAGIC_VOID_ARRAY_TYPE = &_magic_void_array_type_buff;
*MAGIC_VOID_ARRAY_TYPE = magic_default_type;
MAGIC_TYPE_ARRAY_CREATE_FROM_N(MAGIC_VOID_ARRAY_TYPE, MAGIC_VOID_TYPE,
_magic_void_array_type_contained_types, 1);
MAGIC_PTRINT_TYPE = &_magic_ptrint_type_buff;
*MAGIC_PTRINT_TYPE = magic_default_type;
MAGIC_TYPE_INT_CREATE(MAGIC_PTRINT_TYPE, MAGIC_VOID_PTR_TYPE->size,
_magic_ptrint_type_name, _magic_ptrint_type_str_buff);
MAGIC_PTRINT_ARRAY_TYPE = &_magic_ptrint_array_type_buff;
*MAGIC_PTRINT_ARRAY_TYPE = magic_default_type;
MAGIC_TYPE_ARRAY_CREATE_FROM_N(MAGIC_PTRINT_ARRAY_TYPE, MAGIC_PTRINT_TYPE,
_magic_ptrint_array_type_contained_types, 1);
}
/*===========================================================================*
* magic_data_init *
*===========================================================================*/
MAGIC_FUNC void magic_data_init(void)
{
MAGIC_FUNC_BODY();
}
/*===========================================================================*
* magic_init *
*===========================================================================*/
PUBLIC void magic_init(void)
{
unsigned i;
if(magic_init_done || !_magic_enabled) {
return;
}
/* Initialize magic data structures first. */
magic_data_init();
/* Initialize asr support. */
magic_asr_init();
/* Initialize eval support. */
magic_eval_init();
/* Initialize magic obdsentries. */
memset(_magic_obdsentries, 0, MAGIC_MAX_OBDSENTRIES * sizeof(struct _magic_obdsentry));
/* Initialize memory pool descriptors. */
for (i = 0; i < MAGIC_MAX_MEMPOOLS; i++) {
snprintf(_magic_mpdescs[i].name, sizeof(_magic_mpdescs[i].name), "%s%d%s", MAGIC_MEMPOOL_NAME_PREFIX, i, MAGIC_ALLOC_NAME_SUFFIX);
}
/* Initialize special types. */
magic_types_init();
/* Initialize magic ranges. */
magic_ranges_init();
/* Perform stack-related initialization. */
magic_stack_init();
#if MAGIC_MEM_USAGE_OUTPUT_CTL
/* Initialize CPU frequency - used for timestamp logging. */
magic_cycles_per_ns = util_time_get_cycles_per_ns(1);
#endif
/* Checks. */
assert(magic_check_sentries() && "Bad sentries!");
assert(magic_check_dsentries_safe() && "Bad dsentries!");
/* All done. */
magic_init_done = 1;
}
/*===========================================================================*
* magic_do_check_dfunction *
*===========================================================================*/
PRIVATE INLINE int magic_do_check_dfunction(struct _magic_dfunction *dfunction, int flags)
{
struct _magic_function *function;
int is_mnum_ok, is_flags_ok, is_prev_ok, is_next_ok;
assert(dfunction && "NULL dfunction found!");
function = MAGIC_DFUNCTION_TO_FUNCTION(dfunction);
assert(function && "NULL function found!");
is_mnum_ok = MAGIC_DFUNCTION_MNUM_OK(dfunction);
if(!is_mnum_ok) {
return FALSE;
}
is_flags_ok = ((function->flags & flags) == flags) && (function->flags & MAGIC_STATE_DYNAMIC) && (MAGIC_STATE_REGION(function) & MAGIC_STATE_TEXT);
is_prev_ok = (dfunction->prev ? dfunction->prev->next && dfunction->prev->next == dfunction : dfunction == _magic_first_dfunction);
is_next_ok = (dfunction->next ? dfunction->next->prev && dfunction->next->prev == dfunction : dfunction == _magic_last_dfunction);
if(!is_flags_ok || !is_prev_ok || !is_next_ok) {
_magic_printf("magic_do_check_dfunction: bad dfunction, checks: %d %d %d\n", is_flags_ok, is_prev_ok, is_next_ok);
MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
_magic_printf("\n");
return FALSE;
}
return TRUE;
}
/*===========================================================================*
* magic_check_dfunction *
*===========================================================================*/
PUBLIC int magic_check_dfunction(struct _magic_dfunction *dfunction, int flags)
{
int check;
check = magic_do_check_dfunction(dfunction, flags);
if(!check) {
return FALSE;
}
#if MAGIC_CHECK_LEVEL == 2
check = magic_check_dfunctions();
if(!check) {
_magic_printf("magic_check_dfunction: bad other dfunction\n");
return FALSE;
}
#endif
return TRUE;
}
/*===========================================================================*
* magic_check_dfunctions *
*===========================================================================*/
PUBLIC int magic_check_dfunctions()
{
int magic_dfunctions_found = 0;
struct _magic_dfunction* dfunction = NULL;
struct _magic_function* function = NULL;
int ret, check = TRUE;
MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
ret = magic_do_check_dfunction(dfunction, 0);
if(ret == FALSE) {
check = FALSE;
}
magic_dfunctions_found++;
);
if(magic_dfunctions_found != _magic_dfunctions_num) {
_magic_printf("magic_check_dfunctions: magic_dfunctions_found=%d != _magic_dfunctions_num%d\n", magic_dfunctions_found, _magic_dfunctions_num);
check = FALSE;
}
if(dfunction != _magic_last_dfunction) {
_magic_printf("magic_check_dfunctions: dfunction=0x%08x != _magic_last_dfunction=0x%08x\n", dfunction, _magic_last_dfunction);
check = FALSE;
}
return check;
}
/*===========================================================================*
* magic_check_dfunctions_safe *
*===========================================================================*/
PUBLIC int magic_check_dfunctions_safe()
{
int ret;
MAGIC_DFUNCTION_LOCK();
ret = magic_check_dfunctions();
MAGIC_DFUNCTION_UNLOCK();
return ret;
}
/*===========================================================================*
* magic_print_dfunction *
*===========================================================================*/
PUBLIC void magic_print_dfunction(struct _magic_dfunction *dfunction)
{
MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
}
/*===========================================================================*
* magic_print_dfunctions *
*===========================================================================*/
PUBLIC void magic_print_dfunctions()
{
int magic_dfunctions_found = 0;
struct _magic_dfunction* dfunction;
struct _magic_function* function;
_magic_printf("magic_print_dfunctions: Printing %d functions\n", _magic_dfunctions_num);
MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
MAGIC_DFUNCTION_PRINT(dfunction, MAGIC_EXPAND_TYPE_STR);
_magic_printf("\n");
magic_dfunctions_found++;
);
if(magic_dfunctions_found != _magic_dfunctions_num) {
_magic_printf("magic_print_dfunctions: magic_dfunctions_found=%d != _magic_dfunctions_num%d\n", magic_dfunctions_found, _magic_dfunctions_num);
}
}
/*===========================================================================*
* magic_print_dfunctions_safe *
*===========================================================================*/
PUBLIC void magic_print_dfunctions_safe()
{
MAGIC_DFUNCTION_LOCK();
magic_print_dfunctions();
MAGIC_DFUNCTION_UNLOCK();
}
/*===========================================================================*
* magic_copy_dfunction *
*===========================================================================*/
PUBLIC void magic_copy_dfunction(struct _magic_dfunction *dfunction,
struct _magic_dfunction *dst_dfunction)
{
/* Raw copy. */
memcpy(dst_dfunction, dfunction, sizeof(struct _magic_dfunction));
}
/*===========================================================================*
* magic_print_dsindex *
*===========================================================================*/
PUBLIC void magic_print_dsindex(struct _magic_dsindex *dsindex)
{
MAGIC_DSINDEX_PRINT(dsindex, MAGIC_EXPAND_TYPE_STR);
}
/*===========================================================================*
* magic_print_dsindexes *
*===========================================================================*/
PUBLIC void magic_print_dsindexes()
{
int i;
struct _magic_dsindex* dsindex;
_magic_printf("magic_print_dsindexes: Printing %d indexes\n", _magic_dsindexes_num);
for(i=0;i<_magic_dsindexes_num;i++) {
dsindex = &_magic_dsindexes[i];
MAGIC_DSINDEX_PRINT(dsindex, MAGIC_EXPAND_TYPE_STR);
_magic_printf("\n");
}
}
/*===========================================================================*
* magic_do_check_dsentry *
*===========================================================================*/
PRIVATE INLINE int magic_do_check_dsentry(struct _magic_dsentry *dsentry, int flags)
{
struct _magic_sentry *sentry;
int is_mnum_ok, is_mstate_ok, is_flags_ok;
assert(dsentry && "NULL dsentry found!");
sentry = MAGIC_DSENTRY_TO_SENTRY(dsentry);
assert(sentry && "NULL sentry found!");
is_mnum_ok = MAGIC_DSENTRY_MNUM_OK(dsentry);
if(!is_mnum_ok) {
_magic_printf("magic_do_check_dsentry: bad ~mnum %08x\n", ~(dsentry->magic_number));
return FALSE;
}
is_mstate_ok = MAGIC_DSENTRY_MSTATE_OK(dsentry);
is_flags_ok = ((sentry->flags & flags) == flags) && (sentry->flags & MAGIC_STATE_DYNAMIC) && MAGIC_STATE_REGION(sentry);
if(!is_mstate_ok || !is_flags_ok) {
_magic_printf("magic_do_check_dsentry: bad dsentry, checks: %d %d\n", is_mstate_ok, is_flags_ok);
MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
_magic_printf("\n");
return FALSE;
}
return TRUE;
}
/*===========================================================================*
* magic_check_dsentry *
*===========================================================================*/
PUBLIC int magic_check_dsentry(struct _magic_dsentry *dsentry, int flags)
{
#if MAGIC_CHECK_LEVEL >= 1
int check;
check = magic_do_check_dsentry(dsentry, flags);
if(!check) {
return FALSE;
}
#endif
#if MAGIC_CHECK_LEVEL == 2
check = magic_check_dsentries();
if(!check) {
_magic_printf("magic_check_dsentry: bad other dsentry\n");
return FALSE;
}
#endif
return TRUE;
}
/*===========================================================================*
* magic_check_dsentries *
*===========================================================================*/
PUBLIC int magic_check_dsentries()
{
struct _magic_dsentry *prev_dsentry, *dsentry;
struct _magic_sentry* sentry;
int ret, check = TRUE;
MAGIC_DSENTRY_NESTED_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
ret = magic_do_check_dsentry(dsentry, 0);
if(ret == FALSE) {
check = FALSE;
}
);
return check;
}
/*===========================================================================*
* magic_check_dsentries_safe *
*===========================================================================*/
PUBLIC int magic_check_dsentries_safe()
{
int ret;
MAGIC_DSENTRY_LOCK();
ret = magic_check_dsentries();
MAGIC_DSENTRY_UNLOCK();
return ret;
}
/*===========================================================================*
* magic_print_dsentry *
*===========================================================================*/
PUBLIC void magic_print_dsentry(struct _magic_dsentry *dsentry)
{
MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
}
/*===========================================================================*
* magic_print_dsentries *
*===========================================================================*/
PUBLIC void magic_print_dsentries()
{
struct _magic_dsentry *prev_dsentry, *dsentry;
struct _magic_sentry* sentry;
int magic_dsentries_num = 0;
_magic_printf("magic_print_dsentries: Printing entries\n");
MAGIC_DSENTRY_NESTED_ITER(_magic_first_dsentry, prev_dsentry, dsentry, sentry,
MAGIC_DSENTRY_PRINT(dsentry, MAGIC_EXPAND_TYPE_STR);
_magic_printf("\n");
magic_dsentries_num++;
);
_magic_printf("magic_print_dsentries: %d entries found\n", magic_dsentries_num);
}
/*===========================================================================*
* magic_print_dsentries_safe *
*===========================================================================*/
PUBLIC void magic_print_dsentries_safe()
{
MAGIC_DSENTRY_LOCK();
magic_print_dsentries();
MAGIC_DSENTRY_UNLOCK();
}
/*===========================================================================*
* magic_copy_dsentry *
*===========================================================================*/
PUBLIC void magic_copy_dsentry(struct _magic_dsentry *dsentry,
struct _magic_dsentry *dst_dsentry)
{
struct _magic_sentry *sentry;
/* Raw copy. */
memcpy(dst_dsentry, dsentry, sizeof(struct _magic_dsentry));
/* Adjust pointers. */
sentry = MAGIC_DSENTRY_TO_SENTRY(dsentry);
if(sentry->type == &dsentry->type) {
MAGIC_DSENTRY_TO_SENTRY(dst_dsentry)->type = &dst_dsentry->type;
if(sentry->type->contained_types == dsentry->type_array) {
MAGIC_DSENTRY_TO_SENTRY(dst_dsentry)->type->contained_types = dst_dsentry->type_array;
}
}
}
/*===========================================================================*
* magic_print_sodesc *
*===========================================================================*/
PUBLIC void magic_print_sodesc(struct _magic_sodesc *sodesc)
{
MAGIC_SODESC_PRINT(sodesc);
}
/*===========================================================================*
* magic_print_sodescs *
*===========================================================================*/
PUBLIC void magic_print_sodescs()
{
int magic_sodescs_found = 0;
struct _magic_sodesc* sodesc;
_magic_printf("magic_print_sodescs: Printing %d sodescs\n", _magic_sodescs_num);
MAGIC_SODESC_ITER(_magic_first_sodesc, sodesc,
MAGIC_SODESC_PRINT(sodesc);
_magic_printf("\n");
magic_sodescs_found++;
);
if(magic_sodescs_found != _magic_sodescs_num) {
_magic_printf("magic_print_sodescs: magic_sodescs_found=%d != _magic_sodescs_num%d\n", magic_sodescs_found, _magic_sodescs_num);
}
}
/*===========================================================================*
* magic_print_dsodesc *
*===========================================================================*/
PUBLIC void magic_print_dsodesc(struct _magic_dsodesc *dsodesc)
{
MAGIC_DSODESC_PRINT(dsodesc);
}
/*===========================================================================*
* magic_print_dsodescs *
*===========================================================================*/
PUBLIC void magic_print_dsodescs()
{
int magic_dsodescs_found = 0;
struct _magic_dsodesc* dsodesc;
_magic_printf("magic_print_dsodescs: Printing %d dsodescs\n", _magic_dsodescs_num);
MAGIC_DSODESC_ITER(_magic_first_dsodesc, dsodesc,
MAGIC_DSODESC_PRINT(dsodesc);
_magic_printf("\n");
magic_dsodescs_found++;
);
if(magic_dsodescs_found != _magic_dsodescs_num) {
_magic_printf("magic_print_dsodescs: magic_dsodescs_found=%d != _magic_dsodescs_num%d\n", magic_dsodescs_found, _magic_dsodescs_num);
}
}
/*===========================================================================*
* magic_print_dsodescs_safe *
*===========================================================================*/
PUBLIC void magic_print_dsodescs_safe()
{
MAGIC_DSODESC_LOCK();
magic_print_dsodescs();
MAGIC_DSODESC_UNLOCK();
}
/*===========================================================================*
* magic_print_sections *
*===========================================================================*/
PUBLIC void magic_print_sections(void)
{
_magic_printf("magic_print_sections: data=[0x%08x;0x%08x], ro=[0x%08x;0x%08x], text=[0x%08x;0x%08x], st_data=[0x%08x;0x%08x], st_ro=[0x%08x;0x%08x], st_text=[0x%08x;0x%08x]",
(unsigned long) MAGIC_DATA_SECTION_START, (unsigned long) MAGIC_DATA_SECTION_END,
(unsigned long) MAGIC_DATA_RO_SECTION_START, (unsigned long) MAGIC_DATA_RO_SECTION_END,
(unsigned long) MAGIC_TEXT_SECTION_START, (unsigned long) MAGIC_TEXT_SECTION_END,
(unsigned long) MAGIC_ST_DATA_SECTION_START, (unsigned long) MAGIC_ST_DATA_SECTION_END,
(unsigned long) MAGIC_ST_DATA_RO_SECTION_START, (unsigned long) MAGIC_ST_DATA_RO_SECTION_END,
(unsigned long) MAGIC_ST_TEXT_SECTION_START, (unsigned long) MAGIC_ST_TEXT_SECTION_END);
}
/*===========================================================================*
* magic_mempool_sentry_lookup_by_addr *
*===========================================================================*/
PUBLIC struct _magic_sentry* magic_mempool_sentry_lookup_by_range(void *addr, struct _magic_dsentry *dsentry_buff)
{
struct _magic_dsentry *prev_dsentry, *dsentry;
struct _magic_sentry* sentry;
struct _magic_sentry* entry = NULL;
void *start_address, *end_address;
MAGIC_DSENTRY_LOCK();
MAGIC_DSENTRY_MEMPOOL_ALIVE_ITER(_magic_first_mempool_dsentry, prev_dsentry, dsentry, sentry,
start_address = sentry->address;
end_address = (void*) (((char*)sentry->address) + sentry->type->size - 1);
if(MAGIC_ADDR_IS_WITHIN(addr, start_address, end_address)) {
if(dsentry_buff) {
magic_copy_dsentry(dsentry, dsentry_buff);
entry = MAGIC_DSENTRY_TO_SENTRY(dsentry_buff);
}
else {
entry = sentry;
}
break;
}
);
MAGIC_DSENTRY_UNLOCK();
return entry;
}
/*===========================================================================*
* magic_dsindex_lookup_by_name *
*===========================================================================*/
PUBLIC struct _magic_dsindex*
magic_dsindex_lookup_by_name(const char *parent_name, const char *name)
{
int i;
struct _magic_dsindex* index = NULL;
assert(parent_name && name);
/* Scan all the indexes and return the one matching the provided names. */
for(i=0;i<_magic_dsindexes_num;i++) {
if(!strcmp(_magic_dsindexes[i].parent_name, parent_name)
&& !strcmp(_magic_dsindexes[i].name, name)) {
index = &_magic_dsindexes[i];
break;
}
}
return index;
}
/*===========================================================================*
* magic_dsentry_prev_lookup *
*===========================================================================*/
PUBLIC struct _magic_dsentry* magic_dsentry_prev_lookup(struct _magic_dsentry* dsentry)
{
struct _magic_dsentry *prev_dsentry, *it_dsentry;
struct _magic_sentry *sentry;
int found = 0;
#if MAGIC_DSENTRY_ALLOW_PREV
return dsentry->prev;
#else
MAGIC_DSENTRY_ITER(_magic_first_dsentry, prev_dsentry, it_dsentry, sentry,
if(dsentry == it_dsentry) {
found = 1;
break;
}
);
if(!found) {
return (struct _magic_dsentry*) MAGIC_ENOPTR;
}
return prev_dsentry;
#endif
}
/*===========================================================================*
* magic_mempool_dsentry_prev_lookup *
*===========================================================================*/
PUBLIC struct _magic_dsentry* magic_mempool_dsentry_prev_lookup(struct _magic_dsentry* dsentry)
{
struct _magic_dsentry *prev_dsentry, *it_dsentry;
struct _magic_sentry *sentry;
int found = 0;
MAGIC_DSENTRY_MEMPOOL_ITER(_magic_first_mempool_dsentry, prev_dsentry, it_dsentry, sentry,
if(dsentry == it_dsentry) {
found = 1;
break;
}
);
if(!found) {
return (struct _magic_dsentry*) MAGIC_ENOPTR;
}
return prev_dsentry;
}
/*===========================================================================*
* magic_function_lookup_by_id *
*===========================================================================*/
PUBLIC struct _magic_function* magic_function_lookup_by_id(_magic_id_t id,
struct _magic_dfunction *dfunction_buff)
{
struct _magic_function* entry = NULL;
struct _magic_function* function;
struct _magic_dfunction* dfunction;
if(id <= 0) {
return NULL;
}
/* O(1) ID lookup for functions. */
#if MAGIC_LOOKUP_FUNCTION
if((int)id <= _magic_functions_num) {
return &_magic_functions[id - 1];
}
#endif
/* O(N) ID lookup for dfunctions. */
#if MAGIC_LOOKUP_DFUNCTION
MAGIC_DFUNCTION_LOCK();
MAGIC_DFUNCTION_FUNC_ITER(_magic_first_dfunction, dfunction, function,
if(function->id == id) {
if(dfunction_buff) {
magic_copy_dfunction(dfunction, dfunction_buff);
entry = MAGIC_DFUNCTION_TO_FUNCTION(dfunction_buff);
}
else {
entry = function;
}
break;
}
);
MAGIC_DFUNCTION_UNLOCK();
#endif
return entry;
}
/*===========================================================================*
* magic_function_lookup_by_addr *
*===========================================================================*/
PUBLIC struct _magic_function* magic_function_lookup_by_addr(void *addr,
struct _magic_dfunction *dfunction_buff)