-
Notifications
You must be signed in to change notification settings - Fork 10
/
fp.c
3199 lines (2817 loc) · 111 KB
/
fp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
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
/* fp.c - Parser for obtaining functions' parameters from stack frames.
*
* Copyright (C) 2013 Alexandr Terekhov
* Copyright (C) 2013 EPAM Systems. All rights reserved.
*
* 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.
*/
#include "defs.h"
#define DECLARE_REG_UNION_X(R) \
union { \
uint64_t r ## R ## x; \
struct { \
union { \
uint32_t e ## R ## x; \
struct { \
union { \
uint16_t R ## x; \
struct { \
uint8_t R ## l; \
uint8_t R ## h; \
}; \
}; \
unsigned char R ## x ## _res[2]; \
}; \
}; \
unsigned char e ## R ## _res[4]; \
}; \
}
#define DECLARE_REG_UNION_IP(R) \
union { \
uint64_t r ## R; \
struct { \
union { \
uint32_t e ## R; \
struct { \
union { \
uint16_t R; \
struct { \
uint8_t R ## l; \
uint8_t R ## l ## _res; \
}; \
}; \
uint8_t R ## _res[2]; \
}; \
}; \
uint8_t e ## R ## _res[4]; \
}; \
}
#define DECLARE_REG_UNION_R(R) \
union { \
uint64_t r ## R; \
struct { \
union { \
uint32_t r ## R ## d; \
struct { \
union { \
uint16_t r ## R ## w; \
struct { \
uint8_t r ## R ## b; \
uint8_t r ## R ## b ## _res; \
}; \
}; \
uint8_t r ## R ## w ## _res[2]; \
}; \
}; \
uint8_t r ## R ## d ## _res[4]; \
}; \
}
#define DECLARE_REG_UNION_RIP \
union { \
uint64_t rip; \
struct { \
uint32_t eip; \
uint32_t rip_res; \
}; \
}
#define OFF_X(R) offsetof(__pr, r ## R ## x), offsetof(__pr, e ## R ## x), \
offsetof(__pr, R ## x), offsetof(__pr, R ## h), offsetof(__pr, R ## l)
#define OFF_IP(R) offsetof(__pr, r ## R), offsetof(__pr, e ## R), \
offsetof(__pr, R), 0, offsetof(__pr, R ## l)
#define OFF_R(R) offsetof(__pr, r ## R), offsetof(__pr, r ## R ## d), \
offsetof(__pr, r ## R ## w), 0, offsetof(__pr, r ## R ## b)
#define OFF_RIP offsetof(__pr, rip), offsetof(__pr, eip), 0, 0, 0
#define E_X(RR) R ## RR ## X, E ## RR ## X, RR ## X, RR ## H, RR ## L
#define E_IP(RR) R ## RR, E ## RR, RR, FOO ## RR, RR ## L
#define E_R(RR) R ## RR, R ## RR ## D, R ## RR ## W, FOO ## RR, R ## RR ## L
#define E_RIP RIP, EIP, IP_FOO, IP_BAR, IP_BAZ
#define S_X(RR) "%r" RR "x", "%e" RR "x", "%" RR "x", "%" RR "h", "%" RR "l"
#define S_IP(RR) "%" "r" RR, "%" "e" RR, "%" RR, "foo" RR, "%" RR "l"
#define S_R(RR) "%r" RR, "%r" RR "d", "%r" RR "w", "foo" RR, "%r" RR "l"
#define S_RIP "%rip", "%eip", "ip_foo", "ip_bar", "ip_baz"
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
enum e_registers {
// 0 1 2 3
E_X(A), E_X(B), E_X(C), E_X(D),
// 4 5 6 7
E_IP(DI), E_IP(SI), E_IP(BP), E_IP(SP),
// 8 9 10 11
E_R(8), E_R(9), E_R(10), E_R(11),
// 12 13 14 15
E_R(12), E_R(13), E_R(14), E_R(15),
// 16 17 Per-CPU
E_RIP, RCOUNT, GS_REG, INVALID = 255,
};
enum e_instructions {
LEAVE, UD2, NOP,
MOVZBL, MOVZWL, MOVSLQ, MOVSBL, MOVABS, MOV,
CMOVcc,
PUSHF, PUSH, POPF, POP,
SUB, CALL, LEA, RET, XOR,
BTS, BTR, BT, CMP, TEST, INC, DEC, JMP, Jcc,
// JAE, JNE, JNZ, JBE, JNA, JNS, JMP,
// JA, JB, JC, JE, JZ, JL, JS, OTHER_JUMP,
SHL, SHR, SAR, IMUL,
SBB, XADD, ADD, NOT, AND, OR,
ICOUNT
};
enum e_condition {
COND_A = 0, COND_AE, COND_B, COND_BE,
COND_C, COND_E, COND_L, COND_S, COND_Z,
COND_COUNT, COND_INVALID = 255
};
enum e_reliability {
RELIABLE_NO = 0,
RELIABLE_ABS = 1,
RELIABLE_REG = 2,
};
typedef struct parameter_registers {
DECLARE_REG_UNION_X(a);
DECLARE_REG_UNION_X(b);
DECLARE_REG_UNION_X(d);
DECLARE_REG_UNION_X(c);
DECLARE_REG_UNION_IP(si);
DECLARE_REG_UNION_IP(di);
DECLARE_REG_UNION_IP(bp);
DECLARE_REG_UNION_IP(sp);
DECLARE_REG_UNION_R(8);
DECLARE_REG_UNION_R(9);
DECLARE_REG_UNION_R(10);
DECLARE_REG_UNION_R(11);
DECLARE_REG_UNION_R(12);
DECLARE_REG_UNION_R(13);
DECLARE_REG_UNION_R(14);
DECLARE_REG_UNION_R(15);
DECLARE_REG_UNION_RIP;
uint64_t params_mask;
uint8_t params_regs[RCOUNT];
enum e_reliability reliable[RCOUNT];
uint64_t was_touched;
uint8_t zf; // Zero Flag
uint8_t cf; // Carry Flag
uint8_t sf; // Sign Flag
uint8_t of; // Overflow Flag
} __pr;
enum line_status {
LINE_STATUS_UNKNOWN = -1,
LINE_STATUS_RESERVED = 0,
LINE_STATUS_WILL_RET = 1,
LINE_STATUS_DESTINATION = 2,
};
struct code_line {
uint64_t rip;
uint8_t hit;
char cmd[256];
char first[256];
char second[256];
char third[256];
char raw[256];
enum line_status will_ret;
enum e_instructions instr;
enum e_condition cond;
uint8_t cond_negate;
int width;
};
struct stack_frame_t {
char symbol[64];
uint8_t is_exception;
uint64_t nearest;
uint64_t rip;
uint64_t rsp;
uint32_t len;
uint32_t allocated;
struct code_line *code;
struct parameter_registers regs;
};
struct stack_parser_context {
uint8_t frames_count;
uint8_t to_be_processed;
uint8_t should_get_stack_value;
int64_t irq_count;
uint64_t irq_count_offset;
uint64_t gs_base;
struct stack_frame_t *frames;
struct task_context *tc;
struct stack_parser_context *parent;
};
struct list { uint64_t addr; struct list *next; };
#define REGEXP_RANGE(s, i) \
s + matchptr[i].rm_so, matchptr[i].rm_eo - matchptr[i].rm_so
#define p_regs(R) ((char *)regs + offsets[R])
#define p_prev_regs(R) ((char *)prev_regs + offsets[R])
#define REGISTER_64BIT(r) (((r) / 5) * 5)
#define GET_MINIMAL(a,b) ((a) < (b) ? (a) : (b))
#define GET_MAXIMAL(a,b) ((a) > (b) ? (a) : (b))
/*
* Operation Suffixes
* b = byte (8 bit)
* s = short (16 bit integer) or single (32-bit floating point)
* w = word (16 bit)
* l = long (32 bit integer or 64-bit floating point)
* q = quad (64 bit)
* t = ten bytes (80-bit floating point)
*/
uint64_t registers_mask[] = {
0xffffffffffffffff,0x00000000ffffffff, 0x000000000000ffff,
0x00000000000000ff, 0x00000000000000ff,
};
uint64_t registers_msb[] = {
1ULL << 63, 1ULL << 31, 1ULL << 15, 1ULL << 7, 1ULL << 7,
};
const char *op_suffixes[] = {"b", "s", "w", "l", "q"};
const unsigned char op_width[] = { 8, 8, 16, 32, 64,};
/* TODO
* Add conditional suffixes like `z`, `e`, `s` etc
* for instructions `J`, `CMOV`, `SET`
*/
const char *conditions[] = { "a", "ae", "b", "be", "c", "e", "l", "s", "z", 0 };
const char *s_instructions[] = {
"leave", "ud2", "nop",
"movzbl", "movzwl", "movslq", "movsbl", "movabs", "mov",
"cmov",
"pushf", "push", "popf", "pop",
"sub", "call", "lea", "ret", "xor",
"bts", "btr", "bt", "cmp", "test", "inc", "dec", "jmp", "j",
// "jae", "jne", "jnz", "jbe", "jna", "jns", "jmp",
// "ja", "jb", "jc", "je", "jz", "jl", "js", "j",
"shl", "shr", "sar", "imul",
"sbb", "xadd", "add", "not", "and", "or",
};
char *s_registers[] = {
S_X("a"), S_X("b"), S_X("c"), S_X("d"),
S_IP("di"), S_IP("si"), S_IP("bp"), S_IP("sp"),
S_R("8"), S_R("9"), S_R("10"), S_R("11"),
S_R("12"), S_R("13"), S_R("14"), S_R("15"),
S_RIP,
};
int16_t offsets[] = {
OFF_X(a), OFF_X(b), OFF_X(c), OFF_X(d),
OFF_IP(di), OFF_IP(si), OFF_IP(bp), OFF_IP(sp),
OFF_R(8), OFF_R(9), OFF_R(10), OFF_R(11),
OFF_R(12), OFF_R(13), OFF_R(14), OFF_R(15),
OFF_RIP,
};
char *traps_symbols[] = {
"divide_error", // 0
"debug", // 1
"nmi", // 2
"int3", // 3
"overflow", // 4
"bounds", // 5
"invalid_op", // 6
"device_not_available", // 7
"double_fault", // 8
"coprocessor_segment_overrun", // 9
"invalid_TSS", // 10
"segment_not_present", // 11
"stack_segment", // 12
"general_protection", // 13
"page_fault", // 14
"spurious_interrupt_bug", // 15
"coprocessor_error", // 16
"alignment_check", // 17
"machine_check", // 18
"simd_coprocessor_error", // 19
0
};
enum e_registers x86_64_abi_parameters[] = { RDI, RSI, RDX, RCX, R8, R9, R10 };
// struct per_cpu_variable *per_cpu_variables;
void parse_stack(struct bt_info *bt);
static uint8_t try_disassemble(const char *, uint64_t);
static void fill_mapped_register(struct stack_parser_context *, enum e_registers);
static void disassemble_frame(struct stack_frame_t *, unsigned char, uint8_t);
static uint8_t parse_frame(
struct stack_parser_context *ctx, uint8_t, uint8_t, uint8_t,
uint8_t (*)(enum e_instructions, char*, char*)
);
static uint8_t parse_argument(
struct stack_parser_context *, struct parameter_registers *,
char *, enum e_registers *, uint64_t *, enum e_reliability *
);
static void update_flags(
uint64_t, enum e_registers, uint64_t, enum e_registers,
uint64_t, unsigned char, struct parameter_registers *
);
static enum e_registers find_register(char *);
static enum e_instructions find_instr(const char *, int *);
static uint64_t str2dec(const char *, const char *);
static int8_t get_exception_no(const char *);
static int8_t get_exception_no_by_postprocess(const char *);
static uint64_t get_exception_displacement(int8_t);
static uint8_t is_apic_interrupt(const char *);
static uint8_t get_register_width(enum e_registers);
static uint8_t is_stack_register(enum e_registers);
static uint8_t is_param_register(enum e_registers);
static uint8_t is_callee_save_register(enum e_registers);
static uint8_t is_compare_instruction(enum e_instructions);
static uint8_t is_jump_instruction(enum e_instructions);
// static uint8_t save_args_callback(enum e_instructions, char *, char *);
static enum e_registers get_mapped(struct parameter_registers *, enum e_registers);
static uint8_t is_mapped(struct parameter_registers *, enum e_registers);
static void set_mapping(struct parameter_registers *, enum e_registers, enum e_registers);
static void clean_mapping(struct parameter_registers *, enum e_registers);
static void set_reliable(struct parameter_registers *, enum e_registers, enum e_reliability);
static void clean_reliable(struct parameter_registers *, enum e_registers);
static enum e_reliability get_reliable_state(struct parameter_registers *, enum e_registers);
static uint8_t wasnt_touched(struct parameter_registers *, enum e_registers);
static uint64_t get_reg(struct parameter_registers *, enum e_registers);
static void set_reg(struct parameter_registers *, enum e_registers, uint64_t);
static void add_reg(struct parameter_registers *, enum e_registers, int64_t);
static uint64_t get_stack_value(struct stack_parser_context *, uint64_t, unsigned char);
static void split_command(const char *, char *, char *, char *, char *, char *);
static uint8_t get_memory_operand(char *, struct parameter_registers *, uint64_t *, enum e_reliability *);
static uint8_t fill_frames(struct bt_info *, struct stack_parser_context *);
static uint8_t function_returns_value(const char *);
static void print_proto(struct stack_frame_t *, uint8_t, struct parameter_registers *);
static uint64_t get_frame_size(struct stack_parser_context *,char *, uint64_t, uint8_t);
static uint64_t pop_list (struct list **);
static void push_list (struct list **, uint64_t);
static uint8_t get_gdb_line(char *, char *);
static uint8_t error_occured_while_reading;
static enum e_condition find_cond(const char *);
static void print_mark(uint8_t, char *, struct code_line *);
static uint8_t try_disassemble(const char *c, uint64_t cip) {
char b[BUFSIZE];
if(!c || !*c)
return 0;
open_tmpfile();
if(!strcmp(c, "system_call_fastpath"))
c = "system_call";
if(!strcmp(c, "ret_from_intr"))
c = "common_interrupt";
sprintf(b, "disassemble %s", c);
if (gdb_pass_through(b, fp, GNU_RETURN_ON_ERROR))
return 1;
if(cip && strcmp(c, "error_entry"))
sprintf(b, "disassemble %s, 0x%lx", c, cip);
else
sprintf(b, "x/150i %s", c);
if (!gdb_pass_through(b, fp, GNU_RETURN_ON_ERROR)) {
if (CRASHDEBUG(1)) fprintf(fp, "Error while disassembling '%s'\n", b);
close_tmpfile();
return 0;
}
return 1;
}
static void push_list (struct list **l, uint64_t v) {
struct list *n = malloc(sizeof(struct list));
n->next = *l;
n->addr = v;
*l = n;
}
static uint64_t pop_list (struct list **l) {
struct list *o = *l;
uint64_t v = o ? o->addr : 0;
if(*l) {
*l = (*l)->next;
free(o);
}
return v;
}
static uint64_t str2dec(const char *s, const char *e) {
long long int r = 0, hex = 0, neg = 0;
if(0 == (s && *s))
return 0;
neg = (*s == '-');
s += neg;
s += *s == '$' ? 1 : 0;
hex = (*s == '0' && *(s + 1) == 'x');
s += hex << 1;
while(e ? (s < e) : *s) {
r *= (hex ? 16 : 10);
if('0' <= (*s | 0x20) && (*s | 0x20) <= '9')
r += (*s | 0x20) - '0';
else if('a' <= (*s | 0x20) && (*s | 0x20) <= 'f')
r += (*s | 0x20) - 'a' + 10;
s++;
}
return (neg ? -1 : 1) * r;
}
static uint8_t get_gdb_line(char *i, char *o) {
open_tmpfile2();
if (!gdb_pass_through(i, pc->tmpfile2, GNU_RETURN_ON_ERROR)) {
close_tmpfile2();
fprintf(fp, "gdb request failed: %s\n", i);
return 0;
}
rewind(pc->tmpfile2);
fgets(o, BUFSIZE, pc->tmpfile2);
if(*(o + strlen(o) - 1) == '\n')
*(o + strlen(o) - 1) = 0; // Chop trailing '\n'
close_tmpfile2();
return 1;
}
static int8_t get_exception_no_by_postprocess(const char *s) {
uint8_t i;
if(0 == (s && *s))
return -1;
if(strncmp(s, "do_", 3))
return -1;
for(i = 0; i < 19; i++)
if(!strcmp(traps_symbols[i], s + 3))
return i;
return -1;
}
static int8_t get_exception_no(const char *s) {
uint8_t i;
if(0 == (s && *s))
return -1;
for(i = 0; i < 19; i++)
if(!strcmp(traps_symbols[i], s))
return i;
if(STREQ(s, "system_call_fastpath") ||
STREQ(s, "tracesys"))
return 125;
if(STREQ(s, "common_interrupt"))
return 126;
if(is_apic_interrupt(s))
return 127;
return -1;
}
static uint8_t is_apic_interrupt(const char *s) {
const char *ai[] = {
"thermal_interrupt", "threshold_interrupt",
"reschedule_interrupt", "invalidate_interrupt",
"call_function_interrupt", "apic_timer_interrupt",
"error_interrupt", "spurious_interrupt", 0
};
uint8_t i = 0;
if(0 == (s && *s))
return 0;
while(ai[i]) {
if(STREQ(s, ai[i]))
return 1;
i++;
}
return 0;
}
static uint64_t get_exception_displacement(int8_t e) {
if(0 <= e && e <= 7)
return 5 * 0x8;
switch(e) {
case 9:
case 15:
case 16:
case 18:
case 19:
return 5 * 0x8;
case 126: /* common_interrupt */
/* 0x8 - is for earlier pushed IRQ number (ENTRY irq_entries_start) */
return 0x28 + 0x8;
case 127: /* apic interrupt */
return 0x28;
default:
return 6 * 0x8;
}
}
static uint8_t is_param_register(enum e_registers reg) {
uint8_t i;
if(reg == INVALID)
return 0;
for(i = 0; i < 7; i++)
if(REGISTER_64BIT(reg) == x86_64_abi_parameters[i])
return 1;
return 0;
}
static uint8_t is_callee_save_register(enum e_registers reg) {
if(reg == INVALID)
return 0;
switch(REGISTER_64BIT(reg)) {
case RBX:
case RBP:
case R12:
case R13:
case R14:
case R15:
return 1;
default:
return 0;
}
}
static uint8_t is_stack_register(enum e_registers r) {
switch(REGISTER_64BIT(r)) {
case RBP:
case RSP:
return 1;
default:
return 0;
}
}
static uint8_t is_compare_instruction(enum e_instructions i) {
switch(i) {
case BT:
case BTS:
case BTR:
case CMP:
case TEST:
return 1;
default:
return 0;
}
}
static uint8_t is_jump_instruction(enum e_instructions i) {
switch(i) {
/* case JAE:
case JNE:
case JNZ:
case JBE:
case JNA:
case JNS:
case JMP:
case JA:
case JB:
case JC:
case JE:
case JZ:
case JL:
case OTHER_JUMP:*/
case Jcc:
return 1;
default:
return 0;
}
}
static enum e_registers find_register(char *r) {
int i;
if(r && *r)
for (i = 0; i < RCOUNT; i++)
if(!strcmp(r, s_registers[i]))
return i;
return INVALID;
}
static enum e_condition find_cond(const char *c) {
uint8_t i;
for(i = 0; i < COND_COUNT; i++)
if(STREQ(c, conditions[i]))
return i;
return COND_INVALID;
}
static uint8_t check_condition(struct parameter_registers *r, enum e_condition c, uint8_t cond_negate) {
int res = 0;
if(NULL == r)
return 0;
switch(c) {
case COND_A:
res = (r->zf == 1);
case COND_AE:
res &= (r->cf == 1);
return cond_negate ^ res;
case COND_C:
case COND_B:
return cond_negate ^ (r->cf == 1);
case COND_BE:
return cond_negate ^ (r->cf == 1 || r->zf == 1);
case COND_L:
return cond_negate ^ (r->of != r->sf);
case COND_S:
return cond_negate ^ (r->sf == 1);
case COND_E:
case COND_Z:
return cond_negate ^ (r->zf == 1);
default:
return 0;
}
}
enum e_instructions find_instr(const char *s, int *width) {
int i, j;
if(NULL == s || '\0' == *s)
return -1;
if(s && *s)
for(i = 0; i < ICOUNT; i++)
if(!strncmp(s, s_instructions[i], strlen(s_instructions[i])))
break;
if(i == ICOUNT)
return -1;
if(i == Jcc || i == CMOVcc)
return i;
if(strlen(s_instructions[i]) == strlen(s)) {
if(width) *width = 64;
return i;
}
for(j = 0; j <= 4; j++)
if(0 == strcmp(s + strlen(s_instructions[i]), op_suffixes[j])) {
if(width) *width = op_width[j];
return i;
}
return -1;
}
static void fill_instruction(const char *s, int *width, struct code_line *cl) {
if(NULL == cl)
return;
cl->instr = find_instr(s, width);
if(cl->instr == Jcc || cl->instr == CMOVcc) {
if(*(s + 1) == 'n') {
cl->cond_negate = 1;
cl->cond = find_cond(s + 2);
} else {
cl->cond_negate = 0;
cl->cond = find_cond(s + 1);
}
} else
cl->cond = COND_INVALID;
}
/* XXX Should be deleted XXX
static uint8_t save_args_callback(enum e_instructions instr_i, char *src, char *dst) {
if(instr_i == PUSH && RBP == find_register(src))
return 1;
return 0;
}
*/
static uint8_t last_frame_to_process(char *s) {
if(NULL == s)
return 0;
if(
STREQ(s, "__schedule") ||
STREQ(s, "kthread") ||
STREQ(s, "cpu_idle") ||
STREQ(s, "child_rip")
)
return 1;
return 0;
}
static void set_reliable(struct parameter_registers *regs, enum e_registers r, enum e_reliability rel) {
enum e_registers tr;
if(r >= RCOUNT || NULL == regs) return;
for(tr = REGISTER_64BIT(r); tr < r; tr++)
regs->reliable[tr] = RELIABLE_NO;
for(tr = r; tr < REGISTER_64BIT(r + 5); tr++)
regs->reliable[tr] = rel;
if(r == RIP)
return;
if (CRASHDEBUG(3))
fprintf(fp, "\n\t\t\t\tSET RELIABLE (frame: %s) status '%d' for register: %s",
(container_of(regs, struct stack_frame_t, regs))->symbol, rel, s_registers[r]);
}
static void clean_reliable(struct parameter_registers *regs, enum e_registers r) {
set_reliable(regs, r, RELIABLE_NO);
}
enum e_reliability get_reliable_state(struct parameter_registers *regs, enum e_registers r) {
if(r == INVALID)
return RELIABLE_NO;
return regs->reliable[r];
}
static uint64_t get_reg(struct parameter_registers *regs, enum e_registers r) {
if(r >= RCOUNT) return 0;
return (*((uint64_t*)p_regs(r)) & registers_mask[r % 5]);
}
static uint8_t get_register_width(enum e_registers r) {
if(r >= RCOUNT) return 0;
switch(r % 5) {
case 0: return 64;
case 1: return 32;
case 2: return 16;
case 3: return 8;
case 4: return 8;
default: return 0;
}
}
static void set_reg(struct parameter_registers *regs, enum e_registers r, uint64_t value) {
if(r >= RCOUNT) return;
value = value & registers_mask[r % 5];
switch(r % 5) {
case 0:
*((uint64_t*)p_regs(r)) = value; break;
case 1:
*((uint32_t*)p_regs(r)) = value; break;
case 2:
*((uint16_t*)p_regs(r)) = value; break;
case 3:
*((uint8_t*)p_regs(r)) = value; break;
case 4:
*((uint8_t*)p_regs(r)) = value; break;
}
}
static void add_reg(struct parameter_registers *regs, enum e_registers r, int64_t delta) {
set_reg(regs, r, get_reg(regs, r) + delta);
}
static uint8_t is_mapped(struct parameter_registers *regs, enum e_registers r) {
if(r == INVALID || r >= RCOUNT)
return 0;
return !!(regs->params_mask & (1 << (r / 5)));
}
static void set_mapping(struct parameter_registers *regs, enum e_registers cs, enum e_registers p) {
if(cs == INVALID || p == INVALID || cs >= RCOUNT || p >= RCOUNT)
return;
clean_mapping(regs, cs);
clean_mapping(regs, p);
// If we have instruction
// mov %rbx,%rdi
// let's make our life easier with mutual mapping, that is
// - rbx => rdi
// - rdi => rbx
regs->params_mask |= (1 << (cs / 5)) | (1 << (p / 5));
regs->params_regs[cs] = p;
regs->params_regs[p] = cs;
if (CRASHDEBUG(3)) fprintf(fp, "\n\t\t\t\tSET MAPPING (frame '%s'): Value of %s <=> %s",
(container_of(regs, struct stack_frame_t, regs))->symbol, s_registers[cs], s_registers[p]);
}
static void clean_mapping(struct parameter_registers *regs, enum e_registers r) {
enum e_registers i, rm = INVALID;
if(INVALID == r || !is_mapped(regs, r))
return;
rm = get_mapped(regs, r);
if(INVALID == (rm = get_mapped(regs, r))) {
if (CRASHDEBUG(1)) fprintf(fp, "\t\tsomething wrong while cleaning mapping for register '%s'", s_registers[r]);
return;
}
regs->params_mask &= ~( (1 << (r / 5)) | (1 << (rm / 5)));
for(i = REGISTER_64BIT(r); i < REGISTER_64BIT(r + 5); i++)
regs->params_regs[i] = INVALID;
for(i = REGISTER_64BIT(rm); i < REGISTER_64BIT(rm + 5); i++)
regs->params_regs[i] = INVALID;
if (CRASHDEBUG(3))
fprintf(fp, "\n\t\t\t\tREMOVE MAPPING (frame '%s') for register: %s (%s) <=> %s (%s)",
(container_of(regs, struct stack_frame_t, regs))->symbol,
s_registers[REGISTER_64BIT(r)], s_registers[r],
s_registers[REGISTER_64BIT(rm)], s_registers[rm]
);
}
static enum e_registers get_mapped(struct parameter_registers *regs, enum e_registers r) {
enum e_registers i;
if(0 == is_mapped(regs, r))
return INVALID;
for(i = REGISTER_64BIT(r); i < REGISTER_64BIT(r + 5); i++)
if(regs->params_regs[i] != INVALID)
return regs->params_regs[i];
return INVALID;
}
static uint8_t wasnt_touched(struct parameter_registers *regs, enum e_registers r) {
return !(regs->was_touched & (1 << (r / 5)));
}
// XXX TODO:
// Frame 1:
// mov %r15,%rsi
// Frame 2:
// mov %rsi,%r12
// mov %r12,%rsi
// Frame 3:
// mov %r15,-0x8(%rbp)
// So, we definitely know, the value of R15 and consequently
// value of RSI at the beginning of frame 2
//
// XXX TODO:
//
// Frame 1:
// mov %r15,%rsi
// Frame 2:
// mov %rbx,%rsi <-- smash RSI
// mov %r15,-0x8(%rbp) <--- wasnt_touched(regs, R15) == 0
// So, we CAN NOT restore RSI for current frame,
// but since R15 wasn't touch, we CAN restore
// RSI for the previous frame.
//
// For instance:
// <worker_thread at 0xffffffff80049aaf <+232>: mov %rbx,%rdi SET MAPPING (frame 'worker_thread'): Value of %rbx <=> %rdi
// <worker_thread at 0xffffffff80049ab2 <+235>: callq 0xffffffff8004d0b2 <run_workqueue>
// ...
// <run_workqueue at 0xffffffff8004d0ba <+8>: mov %rdi,%r12 REMOVE MAPPING (frame 'run_workqueue') for register: %rdi (%rdi) <=> %rbx (%rbx)
// <run_workqueue at 0xffffffff8004d0be <+12>: push %rbx
// >>>> OR <<<<
// <cache_reap at 0xffffffff800d802a <+157>: mov %r12,%rdi
// <cache_reap at 0xffffffff800d802d <+160>: callq 0xffffffff800d74b5 <drain_array>
// ...
// <drain_array at 0xffffffff800d74b7 <+2>: mov %rdi,%r15 REMOVE MAPPING (frame 'drain_array') for register: %rdi (%rdi) <=> %r12 (%r12)
// <drain_array at 0xffffffff800d74c4 <+15>: push %r12
//
// XXX TODO:
static void fill_mapped_register(
struct stack_parser_context *ctx,
enum e_registers r
) {
struct parameter_registers *regs, *prev_regs;
enum e_registers untouched = INVALID, mr = INVALID /*Mapped register */;
uint8_t mapping = 1; /* At first let's deal with mappings */
uint32_t f;
if(0 == ctx->to_be_processed || INVALID == r)
return;
for(f = ctx->to_be_processed; f < ctx->frames_count - 1;) {
regs = &(ctx->frames + f)->regs;
prev_regs = &(ctx->frames + f + 1)->regs;
if(NULL == prev_regs || NULL == regs)
return;
if(mapping) {
// Start with mapped registers
if(is_mapped(regs, r) && wasnt_touched(regs, r)) {
// It was mapped but for some reason we can't
// determine which register it was mapped to.
if(INVALID == (mr = get_mapped(prev_regs, r)))
break;
if(mr != get_mapped(regs, r))
break;
set_reg(regs, mr, get_reg(regs, r));
set_reg(prev_regs, mr, get_reg(regs, r));
set_reg(prev_regs, r, get_reg(regs, r));
set_reliable(regs, mr, get_reliable_state(regs, r));
set_reliable(prev_regs, mr, get_reliable_state(regs, mr));
set_reliable(prev_regs, r, get_reliable_state(regs, r));
clean_mapping(regs, r);
if (CRASHDEBUG(1)) fprintf(fp, "\n\t\t\t\tMAPPING for frame '%s': Values: %s is 0x%lx and %s is 0x%lx now",
(ctx->frames + f + 1)->symbol,
s_registers[mr],
get_reg(prev_regs, mr),
s_registers[r],
get_reg(prev_regs, r)
);
}
if(0 == (r != INVALID && mr != INVALID))
return;
if(is_param_register(r) && wasnt_touched(regs, r))
untouched = r;
else if(is_param_register(mr) && wasnt_touched(regs, mr))
untouched = mr;
else
return;
mapping = 0;
} else {
// Afterwards try to fill registers which
// were not touched while frame executing
if(untouched == INVALID)
break;
/* TODO Check, whether it's necessary. Apparently not.
if((ctx->frames + f)->is_exception)
break;
*/
// TODO XXX
// Track reliability of all stack memory,
// for instance:
// mov %rdi,0x78(%rsp)
// ...... and afterwards
// mov 0x12,0x78(%rsp)
// that means, that RDI can't be reliable.
// TODO XXX
if(wasnt_touched(regs, untouched)
&& RELIABLE_NO == get_reliable_state(prev_regs, untouched)) {
set_reg(prev_regs, untouched, get_reg(regs, untouched));
set_reliable(prev_regs, untouched, get_reliable_state(regs, untouched));
regs->was_touched |= (1 << (untouched / 5));
if (CRASHDEBUG(1)) fprintf(fp, "\n\t\t\t\tPOSTMAPPING for frame '%s': Values: %s is 0x%lx now",
(ctx->frames + f + 1)->symbol,
s_registers[untouched],
get_reg(prev_regs, untouched)
);
} else
break;
}
f++;
}
return;
}