Repository navigation
Expand file tree
/
Copy pathsearch.cpp
More file actions
2079 lines (1930 loc) · 55.5 KB
/
Copy pathsearch.cpp
File metadata and controls
2079 lines (1930 loc) · 55.5 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
/*
* search.c
*
* Created on: Dec 13, 2011
* Author: myan
*/
#include <algorithm>
#include <list>
#include <set>
#include "search.h"
#include "segment.h"
#include "heap.h"
/////////////////////////////////////////////////////
// Data Structures used for implementation
/////////////////////////////////////////////////////
struct shared_object
{
address_t start;
address_t end;
std::list<struct object_reference*> thread_owners; // list of thread references
std::list<struct shared_object*> parent_shrobjs; // list of shared_objects that point to me
};
/////////////////////////////////////////////////////
// global variables
/////////////////////////////////////////////////////
bool g_skip_free = true;
bool g_skip_unknown = false;
unsigned int g_max_indirection_level = 16;
#define MAX_INDIRECTION_LEVEL 64
static unsigned int g_shrobj_level = 1;
static const unsigned int MAX_SHROBJ_LEVEL = 16;
static void* gp_mem_buf = NULL;
static size_t g_mem_buf_sz = 0;
static int g_nregs = 0;
static struct reg_value* g_regs_buf = NULL;
static const int min_chars = 4;
static unsigned int g_output_count = 0;
static bool shared_object_comp_func(void *lhs, void *rhs);
typedef std::set<struct shared_object*, decltype(&shared_object_comp_func)> SharedObjectSet;
static SharedObjectSet g_shared_objects;
/////////////////////////////////////////////////////
// forward declarations.
/////////////////////////////////////////////////////
static size_t is_string(address_t, int, bool*);
static void print_string(address_t str);
static void print_wstring(address_t str);
static void print_ref_chain (const std::list<struct object_reference*>&);
static void init_shared_objects(void);
static void empty_shared_objects(void);
static void print_shared_objects_by_threads(void);
static struct shared_object* add_one_shared_object(address_t, bool, unsigned int);
static bool has_multiple_thread_owners(struct shared_object* shrobj);
/***************************************************************************
* Search functions
***************************************************************************/
/*
* Params:
* next_bit_index represents the i_th pointers in this segment
* searched-for value is in the range of [target_low, target_high)
* Return:
* true if the 1st match is found, false otherwise
* next_bit_index is updated
*/
static bool
search_value_by_range(struct ca_segment* segment,
size_t* next_bit_index,
struct object_range** targets,
int target_is_ptr,
address_t* found_val,
address_t* found_vaddr)
{
size_t ptr_sz = g_ptr_bit >> 3;
size_t max_bit_index = segment->m_fsize / ptr_sz;
if (!segment->m_bitvec_ready)
set_addressable_bit_vec(segment);
// find next addressable pointer
while (*next_bit_index < max_bit_index)
{
unsigned int target_index;
// The bit vector of addressable can speed up search significantly
if (target_is_ptr)
{
unsigned int i;
unsigned int uint_bit;
size_t uint_index = *next_bit_index >> 5;
unsigned int bits = segment->m_ptr_bitvec[uint_index];
if (bits == 0)
{
// There are no addressable ptrs in the next chunk of memory (32 ptrs)
// Also account for unaligned next_bit_index
*next_bit_index = (*next_bit_index & ~(size_t)0x1F) + 32;
continue;
}
// consider the bit inside the uint
uint_bit = *next_bit_index & (size_t)0x1F;
bits &= ~( (1 << uint_bit) - 1);
if (bits == 0)
{
*next_bit_index = (*next_bit_index & ~(size_t)0x1F) + 32;
continue;
}
// find the non-zero bit
bits = bits >> uint_bit;
for (i=uint_bit; i<32; i++)
{
if (bits & 0x1)
{
// this is a valid ptr, check if it points to target object
size_t offset = *next_bit_index * ptr_sz;
const char* next_ref = segment->m_faddr + offset;
address_t val;
if (ptr_sz == 8)
{
#ifdef sun
// sparcv9 core aligns on 4-byte only. sigh..
if ((address_t)next_ref & 0x7ul)
memcpy(&val, next_ref, 8);
else
#endif
val = *(address_t*)next_ref;
}
else
val = *(unsigned int*)next_ref;
for (target_index=0; targets[target_index]; target_index++)
{
if (val >= targets[target_index]->low && val < targets[target_index]->high)
{
*found_val = val;
*found_vaddr = segment->m_vaddr + offset;
return true;
}
}
}
bits = bits >> 1;
(*next_bit_index)++;
}
}
else // we have to scan raw data for arbitrary target
{
const char* start = segment->m_faddr;
const char* next = start + (*next_bit_index * ptr_sz);
const char* end = start + segment->m_fsize;
while (next + ptr_sz <= end)
{
address_t val;
if (ptr_sz == 8)
{
#ifdef sun
// sparcv9 core aligns on 4-byte only. sigh..
if ((address_t)next & 0x7ul)
memcpy(&val, next, 8);
else
#endif
val = *(address_t*)next;
}
else
val = *(unsigned int*)next;
for (target_index=0; targets[target_index]; target_index++)
{
if (val >= targets[target_index]->low && val < targets[target_index]->high)
{
*found_val = val;
*found_vaddr = segment->m_vaddr + (next - start);
return true;
}
}
(*next_bit_index)++;
next += ptr_sz;
}
}
}
return false;
}
/////////////////////////////////////////////////////////////////////////
// The work horse of value search
// Found references are inserted into output list.
// Return true if at least one is found
/////////////////////////////////////////////////////////////////////////
static bool
search_value_internal(const std::list<struct object_range*>& targets,
bool target_is_ptr,
enum storage_type stype,
std::list<struct object_reference*>& refs)
{
bool lbFound = false;
unsigned int i;
unsigned int num_targets = targets.size();
struct object_range** target_array = NULL;
if (num_targets == 0)
return false;
else
{
// use an array terminated with NULL, for performance sake
target_array = (struct object_range**) malloc (sizeof(struct object_range*) * (num_targets+1));
i = 0;
for (auto target : targets)
target_array[i++] = target;
target_array[i++] = NULL;
if (i != num_targets + 1)
{
CA_PRINT("Internal error: corrupted target list\n");
free(target_array);
return false;
}
}
// search all threads' registers/stacks
for (i=0; i<g_segment_count; i++)
{
struct ca_segment* segment = &g_segments[i];
// registers are read if this is a thread stack
if (segment->m_type == ENUM_STACK && (stype & ENUM_REGISTER))
{
if (search_registers(segment, targets, refs))
lbFound = true;
}
// skip undesired setment
if ( (segment->m_type & stype) == 0)
continue;
// This search may take long, bail out if user is impatient
if (user_request_break())
{
CA_PRINT("Abort searching\n");
break;
}
// search segment memory
if (segment->m_fsize > 0)
{
size_t next_bit_index = 0;
// if we are debugging core file, read memory from mmap-ed file
// for live process, use a buffer to read in the whole segment
if (!g_debug_core)
{
if (segment->m_fsize > g_mem_buf_sz)
{
if (gp_mem_buf)
free(gp_mem_buf);
gp_mem_buf = malloc(segment->m_fsize);
g_mem_buf_sz = segment->m_fsize;
}
if (!read_memory_wrapper(segment, segment->m_vaddr, gp_mem_buf, segment->m_fsize) )
{
// can't read the segment's data, something is broken
continue;
}
else
segment->m_faddr = (char*) gp_mem_buf;
}
// begin to scan memory, pointed by segment->m_faddr
while (1)
{
address_t val = 0xdeadbeef;
address_t vaddr = 0xdeadbeef;
if(search_value_by_range(segment, &next_bit_index, target_array, target_is_ptr, &val, &vaddr))
{
// find a match in this segment
bool valid_ref = false;
struct object_reference* ref = (struct object_reference*) malloc(sizeof(struct object_reference));
ref->storage_type = segment->m_type;
ref->vaddr = vaddr;
ref->value = val;
// detail for various storage class
if (segment->m_type == ENUM_STACK)
{
ref->where.stack.tid = segment->m_thread.tid;
ref->where.stack.frame = get_frame_number(segment, vaddr, &ref->where.stack.offset);
if (ref->where.stack.frame >= 0 || !g_skip_free)
valid_ref = true;
}
else if (segment->m_type == ENUM_MODULE_TEXT || segment->m_type == ENUM_MODULE_DATA)
{
// it belongs to a module's .text or .data
valid_ref = true;
ref->where.module.name = segment->m_module_name;
ref->where.module.base = segment->m_vaddr;
ref->where.module.size = segment->m_vsize;
}
else if (segment->m_type == ENUM_HEAP)
{
if (CA_HEAP->is_heap_block(vaddr))
{
// otherwise, it is on heap
struct heap_block blk;
CA_HEAP->get_heap_block_info(vaddr, &blk);
// we generally don't care about free heap memory
if (blk.inuse || !g_skip_free)
{
valid_ref = true;
ref->where.heap.addr = blk.addr;
ref->where.heap.size = blk.size;
ref->where.heap.inuse = blk.inuse;
}
}
else // it is in heap segment, but not recognized by allocator
ref->storage_type = ENUM_UNKNOWN;
}
// keep meaningful ref, and throw away undesired one
if (valid_ref || (!g_skip_unknown && ref->storage_type == ENUM_UNKNOWN))
{
refs.push_front(ref);
lbFound = true;
#if 0
// avoid exceedingly too many refs for any human being to read
if (refs.size() > 16 * 1024)
break;
#endif
}
else
free(ref);
next_bit_index++;
}
else
break;
}
// remove reference to the global buffer, for the sake of peace mind
if (!g_debug_core)
segment->m_faddr = NULL;
}
}
// cleanup
if (target_array)
free(target_array);
return lbFound;
}
// Given an address (ref->vaddr), figure out its proper storage type
void
fill_ref_location(struct object_reference* ref)
{
struct heap_block blockinfo;
address_t addr = ref->vaddr;
// the target address must be in process address space
struct ca_segment* segment = get_segment(addr, 1);
if (!segment)
{
ref->storage_type = ENUM_UNKNOWN;
return;
}
// analyze target's storage type
if (segment->m_type == ENUM_STACK)
{
// stack address, that's it.
ref->storage_type = ENUM_STACK;
ref->where.stack.tid = segment->m_thread.tid;
ref->where.stack.frame = get_frame_number(segment, addr, &ref->where.stack.offset);
}
else if (segment->m_type == ENUM_MODULE_DATA || segment->m_type == ENUM_MODULE_TEXT)
{
// A module's .text/.data address, that's it.
ref->storage_type = segment->m_type;
ref->where.module.base = segment->m_vaddr;
ref->where.module.size = segment->m_vsize;
ref->where.module.name = segment->m_module_name;
}
else if (CA_HEAP->is_heap_block(addr)
&& CA_HEAP->get_heap_block_info(addr, &blockinfo) )
{
// heap address
ref->vaddr = addr;
ref->storage_type = ENUM_HEAP;
ref->where.heap.addr = blockinfo.addr;
ref->where.heap.size = blockinfo.size;
ref->where.heap.inuse = blockinfo.inuse;
}
else
{
ref->storage_type = ENUM_UNKNOWN;
//an unknown memory object
//ref->where.target.size = 1;
}
}
/////////////////////////////////////////////////////////////////////////
// Given an object, check whether its data member references the target
/////////////////////////////////////////////////////////////////////////
static bool search_object_tree (std::list<struct object_reference*>& refs, address_t obj_vaddr, size_t obj_sz, size_t iLevel)
{
size_t ptr_sz = g_ptr_bit >> 3;
bool rc = false;
struct object_reference* ref;
struct ca_segment* segment;
address_t sym_addr;
size_t sym_sz;
address_t cursor = 0;
address_t end = 0;
struct object_reference new_ref;
ref = refs.front();
segment = get_segment (ref->value, ptr_sz);
if (!segment)
return false;
// go deeper if the referenced value points to a global variable or an in-use heap block
if (segment->m_type == ENUM_MODULE_DATA && known_global_sym(ref, &sym_addr, &sym_sz))
{
cursor = sym_addr;
end = sym_addr + sym_sz;
}
else if (segment->m_type == ENUM_HEAP && CA_HEAP->is_heap_block(ref->value))
{
struct heap_block blk;
CA_HEAP->get_heap_block_info(ref->value, &blk);
// we generally don't care about free heap memory
if (blk.inuse || !g_skip_free)
{
cursor = blk.addr;
end = blk.addr + blk.size;
new_ref.where.heap.addr = blk.addr;
new_ref.where.heap.inuse = blk.inuse;
new_ref.where.heap.size = blk.size;
}
}
if (cursor && end)
{
new_ref.level = ref->level + 1;
new_ref.storage_type = segment->m_type;
// check each pointer/ref of the memory block
while (cursor + ptr_sz <= end)
{
address_t val = 0;
if (!read_memory_wrapper(segment, cursor, (void*)&val, ptr_sz))
break;
if (val >= obj_vaddr && val < obj_vaddr + obj_sz)
{
// find one match
rc = true;
new_ref.vaddr = cursor;
new_ref.value = val;
refs.push_front(&new_ref);
print_ref_chain (refs);
refs.pop_front();
}
else if (iLevel > 1 && val)
{
// user wants to dig deeper
new_ref.vaddr = cursor;
new_ref.value = val;
refs.push_front(&new_ref);
if (search_object_tree (refs, obj_vaddr, obj_sz, iLevel - 1))
rc = true;
refs.pop_front();
}
cursor += ptr_sz;
}
}
return rc;
}
/////////////////////////////////////////////////////////////////////////
// Same as find_object_refs, except only thread stack memory is searched
// including registers of thread context
/////////////////////////////////////////////////////////////////////////
bool find_object_refs_on_threads(address_t obj_vaddr, size_t obj_sz, unsigned int iLevel)
{
size_t ptr_sz = g_ptr_bit >> 3;
unsigned int i;
bool rc = false;
if (iLevel <= 0 || iLevel > g_max_indirection_level)
{
CA_PRINT("The indirecton level %d is too high\n", iLevel);
return false;
}
// static buffer for all register values
if (g_nregs == 0)
{
g_nregs = read_registers (NULL, NULL, 0);
g_regs_buf = (struct reg_value*) malloc(g_nregs * sizeof(struct reg_value));
}
g_output_count = 0;
// search all threads' registers/stacks
for (i=0; i<g_segment_count; i++)
{
struct ca_segment* segment = &g_segments[i];
if (user_request_break())
{
CA_PRINT("Abort searching references\n");
break;
}
if (segment->m_type == ENUM_STACK)
{
int k;
struct object_reference ref;
// read register values of this thread context
int nread = read_registers (segment, g_regs_buf, g_nregs);
memset(&ref, 0, sizeof(ref));
for (k=0; k<nread; k++)
{
if (g_regs_buf[k].reg_width == ptr_sz
&& g_regs_buf[k].value >= obj_vaddr && g_regs_buf[k].value < obj_vaddr + obj_sz)
{
rc = true;
ref.storage_type = ENUM_REGISTER;
ref.where.reg.tid = get_thread_id (segment);
ref.where.reg.reg_num = k;
ref.value = g_regs_buf[k].value;
CA_PRINT("------------------------ %d ------------------------\n", ++g_output_count);
print_ref(&ref, 0, false, true);
CA_PRINT("\n");
}
}
// stack memory
{
address_t cursor = segment->m_vaddr;
address_t end = segment->m_vaddr + segment->m_fsize;
if (g_skip_free)
{
address_t rsp = get_rsp(segment);
if (rsp > segment->m_vaddr)
cursor = rsp;
}
ref.storage_type = ENUM_STACK;
ref.where.stack.tid = get_thread_id (segment);
ref.level = 1;
while (cursor + ptr_sz <= end)
{
address_t val = 0;
if (!read_memory_wrapper(segment, cursor, (void*)&val, ptr_sz))
break;
ref.vaddr = cursor;
ref.value = val;
if (val >= obj_vaddr && val < obj_vaddr + obj_sz)
{
rc = true;
ref.where.stack.frame = get_frame_number(segment, cursor, &ref.where.stack.offset);
CA_PRINT("------------------------ %d ------------------------\n", ++g_output_count);
print_ref (&ref, 0, false, true);
CA_PRINT("\n");
}
else if (iLevel > 1 && val)
{
std::list<struct object_reference *> refs;
refs.push_front(&ref);
if (search_object_tree (refs, obj_vaddr, obj_sz, iLevel - 1))
rc = true;
refs.clear();
}
cursor += ptr_sz;
}
}
}
}
return rc;
}
/////////////////////////////////////////////////////////////////////////
// Return a list of references to the input object
// parameter iLevel is ignored for now
/////////////////////////////////////////////////////////////////////////
std::list<struct object_reference*>
search_object_refs(address_t obj_vaddr, size_t obj_sz, unsigned int iLevel, enum storage_type stype)
{
std::list<struct object_reference*> ref_list;
std::list<struct object_range*> targets;
std::list<struct object_reference*> result_list;
struct object_range target;
// set up search target
target.low = obj_vaddr;
target.high = target.low + obj_sz;
targets.push_front(&target);
// invoke full-core memory search
if (search_value_internal(targets, false, stype, ref_list) )
{
// References are found, eliminate unwanted and save the rest into result
for (auto ref : ref_list)
{
// remove self/circular-reference heap block
bool dup_heap_block = false;
if (ref->storage_type == ENUM_HEAP)
{
for (auto cursor : result_list)
{
if (cursor->storage_type == ENUM_HEAP && cursor->where.heap.addr == ref->where.heap.addr)
{
dup_heap_block = true;
break;
}
}
}
if (!dup_heap_block)
result_list.push_front(ref);
else
free (ref);
}
}
return result_list;
}
/////////////////////////////////////////////////////////////////////////
// Horizontal search.
// direct and indirect references to an object up to iLevel
/////////////////////////////////////////////////////////////////////////
bool find_object_refs(address_t obj_vaddr, size_t obj_sz, unsigned int iLevel)
{
unsigned int i, n;
struct object_reference* ref;
std::list<struct object_reference*> ref_list;
// references are placed in an array
unsigned int ref_cnt = 0;
unsigned int ref_buf_sz = 64;
struct object_reference** refs = (struct object_reference**) malloc(sizeof(struct object_reference*)*ref_buf_sz);
// the 1st element is the searched target
refs[0] = (struct object_reference*) malloc(sizeof(struct object_reference));
ref = refs[0];
ref->level = 0;
ref->target_index = -1;
ref->storage_type = ENUM_UNKNOWN;
ref->vaddr = obj_vaddr;
ref->value = 0;
ref->where.target.size = obj_sz;
ref_cnt++;
//fill_ref_location(ref);
// search references and put in the vector flat out
for (n=0; n<iLevel; n++)
{
unsigned int vec_sz = ref_cnt;
for (i=0; i<vec_sz; i++)
{
ref = refs[i];
if (ref->level == n && ref->storage_type != ENUM_REGISTER)
{
bool target_is_ptr = true;
std::list<struct object_range*> targets;
struct object_range target;
target.low = ref->vaddr;
target.high = target.low + 1;
// default search scope is the exact address
if (ref->target_index < 0)
{
target_is_ptr = false;
target.high = target.low + obj_sz;
}
else if (ref->storage_type == ENUM_HEAP)
{
target.low = ref->where.heap.addr;
target.high = target.low + ref->where.heap.size;
}
targets.push_front(&target);
// invoke full-core memory search
// ref_list shall be empty at this point
if (search_value_internal(targets, target_is_ptr, ENUM_ALL, ref_list) )
{
for (auto aref : ref_list)
{
// remove self/circular-reference heap block
int dup_heap_block = false;
if (aref->storage_type == ENUM_HEAP)
{
int k;
for (k=ref_cnt-1; k>=0; k--)
{
const struct object_reference* cursor = refs[k];
if (cursor->storage_type == ENUM_HEAP && cursor->where.heap.addr == aref->where.heap.addr)
{
dup_heap_block = true;
break;
}
}
}
if (!dup_heap_block)
{
// append the newly found reference
aref->level = n + 1;
aref->target_index = i;
if (ref_cnt >= ref_buf_sz)
{
refs = (struct object_reference**) realloc(refs, ref_buf_sz*2*sizeof(struct object_reference*));
ref_buf_sz *= 2;
}
refs[ref_cnt] = aref;
ref_cnt++;
}
else
free(aref);
}
ref_list.clear();
}
}
}
}
ref_list.clear();
// display the result
if (ref_cnt > 1)
{
int ref_cursor = ref_cnt - 1;
int cur_level = iLevel;
int indent = 0;
int prev_target = refs[ref_cursor]->target_index;
CA_PRINT("------------------------- Level %d -------------------------\n", cur_level);
clear_addr_type_map();
while (ref_cursor>0 && cur_level>0)
{
ref = refs[ref_cursor];
if (ref->level == cur_level)
{
int next_referenced;
// no arrow, verbose
print_ref(ref, indent, false, true);
ref_cursor--;
next_referenced = (refs[ref_cursor])->target_index;
if (next_referenced != prev_target)
{
if (prev_target == 0)
{
int k;
for (k=0; k<indent; k++)
CA_PRINT(" ");
CA_PRINT("|--> searched target [" PRINT_FORMAT_POINTER ", " PRINT_FORMAT_POINTER ")\n", obj_vaddr, obj_vaddr+obj_sz);
}
else
print_ref(refs[prev_target], indent, true, false); // arrow/no verbose
prev_target = next_referenced;
CA_PRINT("\n");
//clear_addr_type_map();
}
}
else
{
cur_level--;
if (cur_level > 0)
{
indent++;
CA_PRINT("------------------------- Level %d -------------------------\n", cur_level);
//clear_addr_type_map();
}
}
}
clear_addr_type_map();
}
// clean up
for (i=0; i<ref_cnt; i++)
free(refs[i]);
free(refs);
if (ref_cnt > 1)
return true;
return false;
}
/////////////////////////////////////////////////////////////////////////
// Vertical search.
// Find a recognizable object to identify the type associated with
// the memory.
/////////////////////////////////////////////////////////////////////////
bool find_object_type(address_t obj_vaddr)
{
bool lbFound = false;
int i;
unsigned int n;
struct object_reference* ref;
std::list<struct object_reference*> ref_list;
// references are placed in an array
unsigned int k;
unsigned int ref_cnt = 0;
unsigned int ref_buf_sz = 64;
struct object_reference** refs;
// sanity check
if (!get_segment(obj_vaddr, 1))
{
CA_PRINT("[Error] Address " PRINT_FORMAT_POINTER " is not in target's address space\n", obj_vaddr);
return false;
}
refs = (struct object_reference**) malloc(sizeof(struct object_reference*)*ref_buf_sz);
// the 1st element is the searched target
refs[0] = (struct object_reference*) malloc(sizeof(struct object_reference));
ref = refs[0];
ref->level = 0;
ref->target_index = -1;
ref->storage_type = ENUM_UNKNOWN;
ref->vaddr = obj_vaddr;
ref->value = 0;
ref->where.target.size = 1;
ref_cnt++;
fill_ref_location(ref);
if (ref->storage_type == ENUM_HEAP)
{
if (ref->where.heap.inuse)
{
CA_PRINT("Address " PRINT_FORMAT_POINTER " belongs to heap block [" PRINT_FORMAT_POINTER ", " PRINT_FORMAT_POINTER "] size=" PRINT_FORMAT_SIZE "\n",
ref->vaddr, ref->where.heap.addr, ref->where.heap.addr+ref->where.heap.size, ref->where.heap.size);
}
else
{
CA_PRINT("Address " PRINT_FORMAT_POINTER " belongs to a FREE memory block [" PRINT_FORMAT_POINTER ", " PRINT_FORMAT_POINTER ") size=" PRINT_FORMAT_SIZE "\n",
obj_vaddr, ref->where.heap.addr, ref->where.heap.addr + ref->where.heap.size, ref->where.heap.size);
}
}
// There is no need to dig deeper for free heap block, or
// if the target is a heap object w/ _vptr
if (ref->storage_type == ENUM_REGISTER
|| ref->storage_type == ENUM_STACK
|| ref->storage_type == ENUM_MODULE_TEXT
|| ref->storage_type == ENUM_MODULE_DATA
|| (ref->storage_type == ENUM_HEAP && ref->where.heap.inuse && is_heap_object_with_vptr(ref, NULL, 0)))
lbFound = true;
else if (ref->storage_type == ENUM_HEAP && !ref->where.heap.inuse)
lbFound = false;
else
{
// Deep search of heap objects
for (n=0; !lbFound && n<g_max_indirection_level; n++)
{
int vec_sz = ref_cnt;
if (refs[vec_sz-1]->level != n) // no more candidate to search
break;
for (i=vec_sz-1; !lbFound && i>=0 && refs[i]->level==n; i--)
{
bool target_is_ptr = true;
std::list<struct object_range*> targets;
struct object_range target;
target.low = ref->vaddr;
target.high = target.low + 1;
// searched target can only be heap block or unknown
ref = refs[i];
if (ref->storage_type == ENUM_HEAP)
{
target.low = ref->where.heap.addr;
target.high = target.low + ref->where.heap.size;
}
else if (ref->target_index < 0)
{
target_is_ptr = false;
target.high = target.low + ref->where.target.size;
}
targets.push_front(&target);
// invoke full-core memory search
if (search_value_internal(targets, target_is_ptr, ENUM_ALL, ref_list) )
{
// first scan for success, global/stack/heap w/o _vptr
for (auto aref : ref_list)
{
int remove_heap_block = false;
if ( (aref->storage_type == ENUM_STACK && aref->where.stack.frame >= 0)
|| aref->storage_type == ENUM_REGISTER
|| aref->storage_type == ENUM_MODULE_DATA
|| (aref->storage_type==ENUM_HEAP && aref->where.heap.inuse && is_heap_object_with_vptr(aref, NULL, 0)) )
{
lbFound = true;
}
// remove self-reference or free heap block
if (!lbFound && aref->storage_type == ENUM_HEAP)
{
if (aref->where.heap.inuse)
{
int refidx;
for (refidx=ref_cnt-1; refidx>=0; refidx--)
{
const struct object_reference* cursor = refs[refidx];
if (cursor->storage_type == ENUM_HEAP && cursor->where.heap.addr == aref->where.heap.addr)
{
remove_heap_block = true;
break;
}
}
}
else
remove_heap_block = true;
}
if (remove_heap_block)
free(aref);
else
{
// append the newly found reference
aref->level = n + 1;
aref->target_index = i;
if (ref_cnt >= ref_buf_sz)
{
refs = (struct object_reference**) realloc(refs, ref_buf_sz*2*sizeof(struct object_reference*));
ref_buf_sz *= 2;
}
refs[ref_cnt] = aref;
ref_cnt++;
}
}
}
ref_list.clear();
}
}
ref_list.clear();
if (n == g_max_indirection_level)
CA_PRINT("Warning: max levels of indirection %d has been reached\n", g_max_indirection_level);
}
// display the result
clear_addr_type_map();
if (lbFound)
{
int count;
n = refs[ref_cnt-1]->level;
for (i=ref_cnt-1, count=1; i>=0 && refs[i]->level==n; i--)
{
ref = refs[i];
if ( (ref->storage_type == ENUM_STACK && ref->where.stack.frame >= 0)
|| ref->storage_type == ENUM_REGISTER
|| ref->storage_type == ENUM_MODULE_DATA
|| ref->storage_type == ENUM_MODULE_TEXT
|| (ref->storage_type==ENUM_HEAP && is_heap_object_with_vptr(ref, NULL, 0)) )
{
int indent = 0;
int next_ref_index = i;
CA_PRINT("------------------------- %d -------------------------\n", count);
count++;
clear_addr_type_map();
while (next_ref_index >= 0)
{
ref = refs[next_ref_index];
print_ref(ref, indent, indent>0, true); // verbose
next_ref_index = ref->target_index;
indent++;
}
CA_PRINT("\n");
}
}
}
clear_addr_type_map();
// clean up
for (k=0; k<ref_cnt; k++)
free(refs[k]);
free(refs);
return lbFound;
}
/////////////////////////////////////////////////////////////////////////
// Return a list of C++ objects with _vptr to the type of the input expression
// the caller is responsible to release the list and its elements
/////////////////////////////////////////////////////////////////////////
std::list<struct object_reference*> search_cplusplus_objects_with_vptr(const char* exp)
{
std::list<struct object_range*> vtables;
char type_name[NAME_BUF_SZ];
size_t type_sz;
std::list<struct object_reference*> result_list;
if (get_vtable_from_exp(exp, vtables, type_name, NAME_BUF_SZ, &type_sz))
{
std::list<struct object_reference*> ref_list;
if (search_value_internal(vtables, true, ENUM_ALL, ref_list) )
{
std::set<address_t> unique_refs;
// go through all found objects
for (auto ref : ref_list)
{
address_t obj_addr;
bool delete_ref = true;
// avoid slicing of heap object
if (ref->storage_type == ENUM_HEAP)
obj_addr = ref->where.heap.addr;