-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsframe.c
1898 lines (1562 loc) · 50.8 KB
/
sframe.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
/* sframe.c - SFrame decoder/encoder.
Copyright (C) 2022-2025 Free Software Foundation, Inc.
This file is part of libsframe.
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "sframe-impl.h"
#include "swap.h"
struct sf_fde_tbl
{
unsigned int count;
unsigned int alloced;
sframe_func_desc_entry entry[1];
};
struct sf_fre_tbl
{
unsigned int count;
unsigned int alloced;
sframe_frame_row_entry entry[1];
};
#define _sf_printflike_(string_index,first_to_check) \
__attribute__ ((__format__ (__printf__, (string_index), (first_to_check))))
static void debug_printf (const char *, ...);
static int _sframe_debug; /* Control for printing out debug info. */
static int number_of_entries = 64;
static void
sframe_init_debug (void)
{
static int inited;
if (!inited)
{
_sframe_debug = getenv ("SFRAME_DEBUG") != NULL;
inited = 1;
}
}
_sf_printflike_ (1, 2)
static void debug_printf (const char *format, ...)
{
if (_sframe_debug)
{
va_list args;
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
}
}
/* Generate bitmask of given size in bytes. This is used for
some checks on the FRE start address.
SFRAME_FRE_TYPE_ADDR1 => 1 byte => [ bitmask = 0xff ]
SFRAME_FRE_TYPE_ADDR2 => 2 byte => [ bitmask = 0xffff ]
SFRAME_FRE_TYPE_ADDR4 => 4 byte => [ bitmask = 0xffffffff ]. */
#define SFRAME_BITMASK_OF_SIZE(size_in_bytes) \
(((uint64_t)1 << (size_in_bytes*8)) - 1)
/* Store the specified error code into errp if it is non-NULL.
Return SFRAME_ERR. */
static int
sframe_set_errno (int *errp, int error)
{
if (errp != NULL)
*errp = error;
return SFRAME_ERR;
}
/* Store the specified error code into errp if it is non-NULL.
Return NULL. */
static void *
sframe_ret_set_errno (int *errp, int error)
{
if (errp != NULL)
*errp = error;
return NULL;
}
/* Get the SFrame header size. */
static uint32_t
sframe_get_hdr_size (sframe_header *sfh)
{
return SFRAME_V1_HDR_SIZE (*sfh);
}
/* Access functions for frame row entry data. */
static uint8_t
sframe_fre_get_offset_count (uint8_t fre_info)
{
return SFRAME_V1_FRE_OFFSET_COUNT (fre_info);
}
static uint8_t
sframe_fre_get_offset_size (uint8_t fre_info)
{
return SFRAME_V1_FRE_OFFSET_SIZE (fre_info);
}
static bool
sframe_get_fre_ra_mangled_p (uint8_t fre_info)
{
return SFRAME_V1_FRE_MANGLED_RA_P (fre_info);
}
/* Access functions for info from function descriptor entry. */
static uint32_t
sframe_get_fre_type (sframe_func_desc_entry *fdep)
{
uint32_t fre_type = 0;
if (fdep)
fre_type = SFRAME_V1_FUNC_FRE_TYPE (fdep->sfde_func_info);
return fre_type;
}
static uint32_t
sframe_get_fde_type (sframe_func_desc_entry *fdep)
{
uint32_t fde_type = 0;
if (fdep)
fde_type = SFRAME_V1_FUNC_FDE_TYPE (fdep->sfde_func_info);
return fde_type;
}
/* Check if flipping is needed, based on ENDIAN. */
static int
need_swapping (int endian)
{
unsigned int ui = 1;
char *c = (char *)&ui;
int is_little = (int)*c;
switch (endian)
{
case SFRAME_ABI_AARCH64_ENDIAN_LITTLE:
case SFRAME_ABI_AMD64_ENDIAN_LITTLE:
return !is_little;
case SFRAME_ABI_AARCH64_ENDIAN_BIG:
return is_little;
default:
break;
}
return 0;
}
/* Flip the endianness of the SFrame header. */
static void
flip_header (sframe_header *sfheader)
{
swap_thing (sfheader->sfh_preamble.sfp_magic);
swap_thing (sfheader->sfh_preamble.sfp_version);
swap_thing (sfheader->sfh_preamble.sfp_flags);
swap_thing (sfheader->sfh_cfa_fixed_fp_offset);
swap_thing (sfheader->sfh_cfa_fixed_ra_offset);
swap_thing (sfheader->sfh_num_fdes);
swap_thing (sfheader->sfh_num_fres);
swap_thing (sfheader->sfh_fre_len);
swap_thing (sfheader->sfh_fdeoff);
swap_thing (sfheader->sfh_freoff);
}
static void
flip_fde (sframe_func_desc_entry *fdep)
{
swap_thing (fdep->sfde_func_start_address);
swap_thing (fdep->sfde_func_size);
swap_thing (fdep->sfde_func_start_fre_off);
swap_thing (fdep->sfde_func_num_fres);
}
/* Check if SFrame header has valid data. */
static bool
sframe_header_sanity_check_p (sframe_header *hp)
{
unsigned char all_flags = SFRAME_F_FDE_SORTED | SFRAME_F_FRAME_POINTER;
/* Check preamble is valid. */
if (hp->sfh_preamble.sfp_magic != SFRAME_MAGIC
|| (hp->sfh_preamble.sfp_version != SFRAME_VERSION_1
&& hp->sfh_preamble.sfp_version != SFRAME_VERSION_2)
|| (hp->sfh_preamble.sfp_flags | all_flags) != all_flags)
return false;
/* Check offsets are valid. */
if (hp->sfh_fdeoff > hp->sfh_freoff)
return false;
return true;
}
/* Flip the start address pointed to by FP. */
static void
flip_fre_start_address (char *addr, uint32_t fre_type)
{
if (fre_type == SFRAME_FRE_TYPE_ADDR2)
{
uint16_t *start_addr = (uint16_t *)addr;
swap_thing (*start_addr);
}
else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
{
uint32_t *start_addr = (uint32_t *)addr;
swap_thing (*start_addr);
}
}
static void
flip_fre_stack_offsets (char *offsets, uint8_t offset_size, uint8_t offset_cnt)
{
int j;
if (offset_size == SFRAME_FRE_OFFSET_2B)
{
uint16_t *ust = (uint16_t *)offsets;
for (j = offset_cnt; j > 0; ust++, j--)
swap_thing (*ust);
}
else if (offset_size == SFRAME_FRE_OFFSET_4B)
{
uint32_t *uit = (uint32_t *)offsets;
for (j = offset_cnt; j > 0; uit++, j--)
swap_thing (*uit);
}
}
/* Get the FRE start address size, given the FRE_TYPE. */
static size_t
sframe_fre_start_addr_size (uint32_t fre_type)
{
size_t addr_size = 0;
switch (fre_type)
{
case SFRAME_FRE_TYPE_ADDR1:
addr_size = 1;
break;
case SFRAME_FRE_TYPE_ADDR2:
addr_size = 2;
break;
case SFRAME_FRE_TYPE_ADDR4:
addr_size = 4;
break;
default:
/* No other value is expected. */
sframe_assert (0);
break;
}
return addr_size;
}
/* Check if the FREP has valid data. */
static bool
sframe_fre_sanity_check_p (sframe_frame_row_entry *frep)
{
uint8_t offset_size, offset_cnt;
uint8_t fre_info;
if (frep == NULL)
return false;
fre_info = frep->fre_info;
offset_size = sframe_fre_get_offset_size (fre_info);
if (offset_size != SFRAME_FRE_OFFSET_1B
&& offset_size != SFRAME_FRE_OFFSET_2B
&& offset_size != SFRAME_FRE_OFFSET_4B)
return false;
offset_cnt = sframe_fre_get_offset_count (fre_info);
if (offset_cnt > MAX_NUM_STACK_OFFSETS)
return false;
return true;
}
/* Get FRE_INFO's offset size in bytes. */
static size_t
sframe_fre_offset_bytes_size (uint8_t fre_info)
{
uint8_t offset_size, offset_cnt;
offset_size = sframe_fre_get_offset_size (fre_info);
debug_printf ("offset_size = %u\n", offset_size);
offset_cnt = sframe_fre_get_offset_count (fre_info);
if (offset_size == SFRAME_FRE_OFFSET_2B
|| offset_size == SFRAME_FRE_OFFSET_4B) /* 2 or 4 bytes. */
return (offset_cnt * (offset_size * 2));
return (offset_cnt);
}
/* Get total size in bytes to represent FREP in the binary format. This
includes the starting address, FRE info, and all the offsets. */
static size_t
sframe_fre_entry_size (sframe_frame_row_entry *frep, uint32_t fre_type)
{
if (frep == NULL)
return 0;
uint8_t fre_info = frep->fre_info;
size_t addr_size = sframe_fre_start_addr_size (fre_type);
return (addr_size + sizeof (frep->fre_info)
+ sframe_fre_offset_bytes_size (fre_info));
}
/* Get the function descriptor entry at index FUNC_IDX in the decoder
context CTX. */
static sframe_func_desc_entry *
sframe_decoder_get_funcdesc_at_index (sframe_decoder_ctx *ctx,
uint32_t func_idx)
{
sframe_func_desc_entry *fdep;
uint32_t num_fdes;
int err;
num_fdes = sframe_decoder_get_num_fidx (ctx);
if (num_fdes == 0
|| func_idx >= num_fdes
|| ctx->sfd_funcdesc == NULL)
return sframe_ret_set_errno (&err, SFRAME_ERR_DCTX_INVAL);
fdep = &ctx->sfd_funcdesc[func_idx];
return fdep;
}
/* Check whether for the given FDEP, the SFrame Frame Row Entry identified via
the START_IP_OFFSET and the END_IP_OFFSET, provides the stack trace
information for the PC. */
static bool
sframe_fre_check_range_p (sframe_func_desc_entry *fdep,
int32_t start_ip_offset, int32_t end_ip_offset,
int32_t pc)
{
int32_t start_ip, end_ip;
int32_t func_start_addr;
uint8_t rep_block_size;
uint32_t fde_type;
int32_t masked_pc;
bool mask_p;
bool ret;
ret = false;
if (!fdep)
return ret;
func_start_addr = fdep->sfde_func_start_address;
fde_type = sframe_get_fde_type (fdep);
mask_p = (fde_type == SFRAME_FDE_TYPE_PCMASK);
rep_block_size = fdep->sfde_func_rep_size;
if (!mask_p)
{
start_ip = start_ip_offset + func_start_addr;
end_ip = end_ip_offset + func_start_addr;
ret = ((start_ip <= pc) && (end_ip >= pc));
}
else
{
/* For FDEs for repetitive pattern of insns, we need to return the FRE
where pc % rep_block_size is between start_ip_offset and
end_ip_offset. */
masked_pc = pc % rep_block_size;
ret = ((start_ip_offset <= masked_pc) && (end_ip_offset >= masked_pc));
}
return ret;
}
static int
flip_fre (char *fp, uint32_t fre_type, size_t *fre_size)
{
uint8_t fre_info;
uint8_t offset_size, offset_cnt;
size_t addr_size, fre_info_size = 0;
int err = 0;
if (fre_size == NULL)
return sframe_set_errno (&err, SFRAME_ERR_INVAL);
flip_fre_start_address (fp, fre_type);
/* Advance the buffer pointer to where the FRE info is. */
addr_size = sframe_fre_start_addr_size (fre_type);
fp += addr_size;
/* FRE info is uint8_t. No need to flip. */
fre_info = *(uint8_t*)fp;
offset_size = sframe_fre_get_offset_size (fre_info);
offset_cnt = sframe_fre_get_offset_count (fre_info);
/* Advance the buffer pointer to where the stack offsets are. */
fre_info_size = sizeof (uint8_t);
fp += fre_info_size;
flip_fre_stack_offsets (fp, offset_size, offset_cnt);
*fre_size
= addr_size + fre_info_size + sframe_fre_offset_bytes_size (fre_info);
return 0;
}
/* Endian flip the contents of FRAME_BUF of size BUF_SIZE.
The SFrame header in the FRAME_BUF must be endian flipped prior to
calling flip_sframe.
Endian flipping at decode time vs encode time have different needs. At
encode time, the frame_buf is in host endianness, and hence, values should
be read up before the buffer is changed to foreign endianness. This change
of behaviour is specified via TO_FOREIGN arg.
If an error code is returned, the buffer should not be used. */
static int
flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
{
unsigned int i, j, prev_frep_index;
sframe_header *ihp;
char *fdes;
char *fp = NULL;
sframe_func_desc_entry *fdep;
unsigned int num_fdes = 0;
unsigned int num_fres = 0;
uint32_t fre_type = 0;
uint32_t fre_offset = 0;
size_t esz = 0;
size_t hdrsz = 0;
int err = 0;
/* For error checking. */
size_t bytes_flipped = 0;
/* Header must be in host endianness at this time. */
ihp = (sframe_header *)frame_buf;
if (!sframe_header_sanity_check_p (ihp))
return sframe_set_errno (&err, SFRAME_ERR_BUF_INVAL);
/* The contents of the SFrame header are safe to read. Get the number of
FDEs and the first FDE in the buffer. */
hdrsz = sframe_get_hdr_size (ihp);
num_fdes = ihp->sfh_num_fdes;
fdes = frame_buf + hdrsz + ihp->sfh_fdeoff;
fdep = (sframe_func_desc_entry *)fdes;
j = 0;
prev_frep_index = 0;
for (i = 0; i < num_fdes; fdep++, i++)
{
if ((char*)fdep >= (frame_buf + buf_size))
goto bad;
if (to_foreign)
{
num_fres = fdep->sfde_func_num_fres;
fre_type = sframe_get_fre_type (fdep);
fre_offset = fdep->sfde_func_start_fre_off;
}
flip_fde (fdep);
bytes_flipped += sizeof (sframe_func_desc_entry);
if (!to_foreign)
{
num_fres = fdep->sfde_func_num_fres;
fre_type = sframe_get_fre_type (fdep);
fre_offset = fdep->sfde_func_start_fre_off;
}
fp = frame_buf + sframe_get_hdr_size (ihp) + ihp->sfh_freoff;
fp += fre_offset;
for (; j < prev_frep_index + num_fres; j++)
{
if (flip_fre (fp, fre_type, &esz))
goto bad;
bytes_flipped += esz;
if (esz == 0 || esz > buf_size)
goto bad;
fp += esz;
}
prev_frep_index = j;
}
/* All FDEs and FREs must have been endian flipped by now. */
if ((j != ihp->sfh_num_fres) || (bytes_flipped != (buf_size - hdrsz)))
goto bad;
/* Success. */
return 0;
bad:
return SFRAME_ERR;
}
/* The SFrame Decoder. */
/* Get SFrame header from the given decoder context DCTX. */
static sframe_header *
sframe_decoder_get_header (sframe_decoder_ctx *dctx)
{
sframe_header *hp = NULL;
if (dctx != NULL)
hp = &dctx->sfd_header;
return hp;
}
/* Compare function for qsort'ing the FDE table. */
static int
fde_func (const void *p1, const void *p2)
{
const sframe_func_desc_entry *aa = p1;
const sframe_func_desc_entry *bb = p2;
if (aa->sfde_func_start_address < bb->sfde_func_start_address)
return -1;
else if (aa->sfde_func_start_address > bb->sfde_func_start_address)
return 1;
return 0;
}
/* Get IDX'th offset from FRE. Set errp as applicable. */
static int32_t
sframe_get_fre_offset (sframe_frame_row_entry *fre, int idx, int *errp)
{
uint8_t offset_cnt, offset_size;
if (fre == NULL || !sframe_fre_sanity_check_p (fre))
return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
offset_cnt = sframe_fre_get_offset_count (fre->fre_info);
offset_size = sframe_fre_get_offset_size (fre->fre_info);
if (offset_cnt < idx + 1)
return sframe_set_errno (errp, SFRAME_ERR_FREOFFSET_NOPRESENT);
if (errp)
*errp = 0; /* Offset Valid. */
if (offset_size == SFRAME_FRE_OFFSET_1B)
{
int8_t *sp = (int8_t *)fre->fre_offsets;
return sp[idx];
}
else if (offset_size == SFRAME_FRE_OFFSET_2B)
{
int16_t *sp = (int16_t *)fre->fre_offsets;
return sp[idx];
}
else
{
int32_t *ip = (int32_t *)fre->fre_offsets;
return ip[idx];
}
}
/* Free the decoder context. */
void
sframe_decoder_free (sframe_decoder_ctx **dctxp)
{
if (dctxp != NULL)
{
sframe_decoder_ctx *dctx = *dctxp;
if (dctx == NULL)
return;
if (dctx->sfd_funcdesc != NULL)
{
free (dctx->sfd_funcdesc);
dctx->sfd_funcdesc = NULL;
}
if (dctx->sfd_fres != NULL)
{
free (dctx->sfd_fres);
dctx->sfd_fres = NULL;
}
if (dctx->sfd_buf != NULL)
{
free (dctx->sfd_buf);
dctx->sfd_buf = NULL;
}
free (*dctxp);
*dctxp = NULL;
}
}
/* Create an FDE function info byte given an FRE_TYPE and an FDE_TYPE. */
/* FIXME API for linker. Revisit if its better placed somewhere else? */
unsigned char
sframe_fde_create_func_info (uint32_t fre_type,
uint32_t fde_type)
{
unsigned char func_info;
sframe_assert (fre_type == SFRAME_FRE_TYPE_ADDR1
|| fre_type == SFRAME_FRE_TYPE_ADDR2
|| fre_type == SFRAME_FRE_TYPE_ADDR4);
sframe_assert (fde_type == SFRAME_FDE_TYPE_PCINC
|| fde_type == SFRAME_FDE_TYPE_PCMASK);
func_info = SFRAME_V1_FUNC_INFO (fde_type, fre_type);
return func_info;
}
/* Get the FRE type given the function size. */
/* FIXME API for linker. Revisit if its better placed somewhere else? */
uint32_t
sframe_calc_fre_type (size_t func_size)
{
uint32_t fre_type = 0;
if (func_size < SFRAME_FRE_TYPE_ADDR1_LIMIT)
fre_type = SFRAME_FRE_TYPE_ADDR1;
else if (func_size < SFRAME_FRE_TYPE_ADDR2_LIMIT)
fre_type = SFRAME_FRE_TYPE_ADDR2;
/* Adjust the check a bit so that it remains warning-free but meaningful
on 32-bit systems. */
else if (func_size <= (size_t) (SFRAME_FRE_TYPE_ADDR4_LIMIT - 1))
fre_type = SFRAME_FRE_TYPE_ADDR4;
return fre_type;
}
/* Get the base reg id from the FRE info. Set errp if failure. */
uint8_t
sframe_fre_get_base_reg_id (sframe_frame_row_entry *fre, int *errp)
{
if (fre == NULL)
return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
uint8_t fre_info = fre->fre_info;
return SFRAME_V1_FRE_CFA_BASE_REG_ID (fre_info);
}
/* Get the CFA offset from the FRE. If the offset is invalid, sets errp. */
int32_t
sframe_fre_get_cfa_offset (sframe_decoder_ctx *dctx ATTRIBUTE_UNUSED,
sframe_frame_row_entry *fre, int *errp)
{
return sframe_get_fre_offset (fre, SFRAME_FRE_CFA_OFFSET_IDX, errp);
}
/* Get the FP offset from the FRE. If the offset is invalid, sets errp. */
int32_t
sframe_fre_get_fp_offset (sframe_decoder_ctx *dctx,
sframe_frame_row_entry *fre, int *errp)
{
uint32_t fp_offset_idx = 0;
int8_t fp_offset = sframe_decoder_get_fixed_fp_offset (dctx);
/* If the FP offset is not being tracked, return the fixed FP offset
from the SFrame header. */
if (fp_offset != SFRAME_CFA_FIXED_FP_INVALID)
{
if (errp)
*errp = 0;
return fp_offset;
}
/* In some ABIs, the stack offset to recover RA (using the CFA) from is
fixed (like AMD64). In such cases, the stack offset to recover FP will
appear at the second index. */
fp_offset_idx = ((sframe_decoder_get_fixed_ra_offset (dctx)
!= SFRAME_CFA_FIXED_RA_INVALID)
? SFRAME_FRE_RA_OFFSET_IDX
: SFRAME_FRE_FP_OFFSET_IDX);
return sframe_get_fre_offset (fre, fp_offset_idx, errp);
}
/* Get the RA offset from the FRE. If the offset is invalid, sets errp. */
int32_t
sframe_fre_get_ra_offset (sframe_decoder_ctx *dctx,
sframe_frame_row_entry *fre, int *errp)
{
int8_t ra_offset = sframe_decoder_get_fixed_ra_offset (dctx);
/* If the RA offset was not being tracked, return the fixed RA offset
from the SFrame header. */
if (ra_offset != SFRAME_CFA_FIXED_RA_INVALID)
{
if (errp)
*errp = 0;
return ra_offset;
}
/* Otherwise, get the RA offset from the FRE. */
return sframe_get_fre_offset (fre, SFRAME_FRE_RA_OFFSET_IDX, errp);
}
/* Get whether the RA is mangled. */
bool
sframe_fre_get_ra_mangled_p (sframe_decoder_ctx *dctx ATTRIBUTE_UNUSED,
sframe_frame_row_entry *fre, int *errp)
{
if (fre == NULL || !sframe_fre_sanity_check_p (fre))
return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
return sframe_get_fre_ra_mangled_p (fre->fre_info);
}
static int
sframe_frame_row_entry_copy (sframe_frame_row_entry *dst,
sframe_frame_row_entry *src)
{
int err = 0;
if (dst == NULL || src == NULL)
return sframe_set_errno (&err, SFRAME_ERR_INVAL);
memcpy (dst, src, sizeof (sframe_frame_row_entry));
return 0;
}
/* Decode the SFrame FRE start address offset value from FRE_BUF in on-disk
binary format, given the FRE_TYPE. Updates the FRE_START_ADDR.
Returns 0 on success, SFRAME_ERR otherwise. */
static int
sframe_decode_fre_start_address (const char *fre_buf,
uint32_t *fre_start_addr,
uint32_t fre_type)
{
uint32_t saddr = 0;
int err = 0;
size_t addr_size = 0;
addr_size = sframe_fre_start_addr_size (fre_type);
if (fre_type == SFRAME_FRE_TYPE_ADDR1)
{
uint8_t *uc = (uint8_t *)fre_buf;
saddr = (uint32_t)*uc;
}
else if (fre_type == SFRAME_FRE_TYPE_ADDR2)
{
uint16_t *ust = (uint16_t *)fre_buf;
/* SFrame is an unaligned on-disk format. Using memcpy helps avoid the
use of undesirable unaligned loads. See PR libsframe/29856. */
uint16_t tmp = 0;
memcpy (&tmp, ust, addr_size);
saddr = (uint32_t)tmp;
}
else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
{
uint32_t *uit = (uint32_t *)fre_buf;
uint32_t tmp = 0;
memcpy (&tmp, uit, addr_size);
saddr = (uint32_t)tmp;
}
else
return sframe_set_errno (&err, SFRAME_ERR_INVAL);
*fre_start_addr = saddr;
return 0;
}
/* Decode a frame row entry FRE which starts at location FRE_BUF. The function
updates ESZ to the size of the FRE as stored in the binary format.
This function works closely with the SFrame binary format.
Returns SFRAME_ERR if failure. */
static int
sframe_decode_fre (const char *fre_buf, sframe_frame_row_entry *fre,
uint32_t fre_type, size_t *esz)
{
int err = 0;
const char *stack_offsets = NULL;
size_t stack_offsets_sz;
size_t addr_size;
size_t fre_size;
if (fre_buf == NULL || fre == NULL || esz == NULL)
return sframe_set_errno (&err, SFRAME_ERR_INVAL);
/* Copy over the FRE start address. */
sframe_decode_fre_start_address (fre_buf, &fre->fre_start_addr, fre_type);
addr_size = sframe_fre_start_addr_size (fre_type);
fre->fre_info = *(uint8_t *)(fre_buf + addr_size);
/* Sanity check as the API works closely with the binary format. */
sframe_assert (sizeof (fre->fre_info) == sizeof (uint8_t));
/* Cleanup the space for fre_offsets first, then copy over the valid
bytes. */
memset (fre->fre_offsets, 0, MAX_OFFSET_BYTES);
/* Get offsets size. */
stack_offsets_sz = sframe_fre_offset_bytes_size (fre->fre_info);
stack_offsets = fre_buf + addr_size + sizeof (fre->fre_info);
memcpy (fre->fre_offsets, stack_offsets, stack_offsets_sz);
/* The FRE has been decoded. Use it to perform one last sanity check. */
fre_size = sframe_fre_entry_size (fre, fre_type);
sframe_assert (fre_size == (addr_size + sizeof (fre->fre_info)
+ stack_offsets_sz));
*esz = fre_size;
return 0;
}
/* Decode the specified SFrame buffer CF_BUF of size CF_SIZE and return the
new SFrame decoder context.
Sets ERRP for the caller if any error. Frees up the allocated memory in
case of error. */
sframe_decoder_ctx *
sframe_decode (const char *sf_buf, size_t sf_size, int *errp)
{
const sframe_preamble *sfp;
size_t hdrsz;
sframe_header *sfheaderp;
sframe_decoder_ctx *dctx;
char *frame_buf;
char *tempbuf = NULL;
int fidx_size;
uint32_t fre_bytes;
int foreign_endian = 0;
sframe_init_debug ();
if ((sf_buf == NULL) || (!sf_size))
return sframe_ret_set_errno (errp, SFRAME_ERR_INVAL);
else if (sf_size < sizeof (sframe_header))
return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
sfp = (const sframe_preamble *) sf_buf;
debug_printf ("sframe_decode: magic=0x%x version=%u flags=%u\n",
sfp->sfp_magic, sfp->sfp_version, sfp->sfp_flags);
/* Check for foreign endianness. */
if (sfp->sfp_magic != SFRAME_MAGIC)
{
if (sfp->sfp_magic == bswap_16 (SFRAME_MAGIC))
foreign_endian = 1;
else
return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
}
/* Initialize a new decoder context. */
if ((dctx = malloc (sizeof (sframe_decoder_ctx))) == NULL)
return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
memset (dctx, 0, sizeof (sframe_decoder_ctx));
if (foreign_endian)
{
/* Allocate a new buffer and initialize it. */
tempbuf = (char *) malloc (sf_size * sizeof (char));
if (tempbuf == NULL)
return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
memcpy (tempbuf, sf_buf, sf_size);
/* Flip the header. */
sframe_header *ihp = (sframe_header *) tempbuf;
flip_header (ihp);
/* Flip the rest of the SFrame section data buffer. */
if (flip_sframe (tempbuf, sf_size, 0))
{
free (tempbuf);
return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
}
frame_buf = tempbuf;
/* This buffer is malloc'd when endian flipping the contents of the input
buffer are needed. Keep a reference to it so it can be free'd up
later in sframe_decoder_free (). */
dctx->sfd_buf = tempbuf;
}
else
frame_buf = (char *)sf_buf;
/* Handle the SFrame header. */
dctx->sfd_header = *(sframe_header *) frame_buf;
/* Validate the contents of SFrame header. */
sfheaderp = &dctx->sfd_header;
if (!sframe_header_sanity_check_p (sfheaderp))
{
sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
goto decode_fail_free;
}
hdrsz = sframe_get_hdr_size (sfheaderp);
frame_buf += hdrsz;
/* Handle the SFrame Function Descriptor Entry section. */
fidx_size
= sfheaderp->sfh_num_fdes * sizeof (sframe_func_desc_entry);
dctx->sfd_funcdesc = malloc (fidx_size);
if (dctx->sfd_funcdesc == NULL)
{
sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
goto decode_fail_free;
}
memcpy (dctx->sfd_funcdesc, frame_buf, fidx_size);
debug_printf ("%u total fidx size\n", fidx_size);
frame_buf += (fidx_size);
/* Handle the SFrame Frame Row Entry section. */
dctx->sfd_fres = (char *) malloc (sfheaderp->sfh_fre_len);
if (dctx->sfd_fres == NULL)
{
sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
goto decode_fail_free;
}
memcpy (dctx->sfd_fres, frame_buf, sfheaderp->sfh_fre_len);
fre_bytes = sfheaderp->sfh_fre_len;
dctx->sfd_fre_nbytes = fre_bytes;
debug_printf ("%u total fre bytes\n", fre_bytes);
return dctx;
decode_fail_free:
if (foreign_endian && tempbuf != NULL)
free (tempbuf);
sframe_decoder_free (&dctx);
dctx = NULL;
return dctx;
}
/* Get the size of the SFrame header from the decoder context CTX. */
unsigned int
sframe_decoder_get_hdr_size (sframe_decoder_ctx *ctx)
{
sframe_header *dhp;
dhp = sframe_decoder_get_header (ctx);
return sframe_get_hdr_size (dhp);
}
/* Get the SFrame's abi/arch info given the decoder context DCTX. */
uint8_t
sframe_decoder_get_abi_arch (sframe_decoder_ctx *dctx)
{
sframe_header *sframe_header;
sframe_header = sframe_decoder_get_header (dctx);
return sframe_header->sfh_abi_arch;
}
/* Get the format version from the SFrame decoder context DCTX. */
uint8_t
sframe_decoder_get_version (sframe_decoder_ctx *dctx)
{
sframe_header *dhp;
dhp = sframe_decoder_get_header (dctx);
return dhp->sfh_preamble.sfp_version;
}
/* Get the SFrame's fixed FP offset given the decoder context CTX. */