Debian Codegraph @

libsframe/testsuite/libsframe.find/findfre-1.c

raw
1 /* findfre-1.c -- Test for sframe_find_fre in libsframe.
2
3 Copyright (C) 2023-2025 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23
24 #include "sframe-api.h"
25
26 /* DejaGnu should not use gnulib's vsnprintf replacement here. */
27 #undef vsnprintf
28 #include <dejagnu.h>
29
30 static int
31 add_fde1 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr,
32 uint32_t sframe_vaddr, int idx, uint32_t *func_size)
33 {
34 /* A contiguous block containing 4 FREs. */
35 #define FDE1_NUM_FRES 4
36 sframe_frame_row_entry fres[FDE1_NUM_FRES]
37 = { {0x0, {0x1, 0, 0}, 0x3},
38 {0x1, {0x2, 0xf0, 0}, 0x5},
39 {0x10, {0x3, 0xf0, 0}, 0x4},
40 {0x38, {0x8, 0xf0, 0}, 0x5}
41 };
42 /* Function size in bytes. P.S. Must be a value greater than the
43 fre_start_addr of the last FRE above (0x38). */
44 *func_size = 0x40;
45
46 uint32_t offsetof_fde_in_sec
47 = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL);
48 int32_t func1_start_addr = (start_pc_vaddr
49 - (sframe_vaddr + offsetof_fde_in_sec));
50 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1,
51 SFRAME_FDE_TYPE_PCINC);
52 int err = sframe_encoder_add_funcdesc_v2 (encode, func1_start_addr,
53 *func_size, finfo, 0,
54 FDE1_NUM_FRES);
55 if (err == -1)
56 return err;
57
58 for (unsigned int i = 0; i < FDE1_NUM_FRES; i++)
59 if (sframe_encoder_add_fre (encode, idx, fres + i) == SFRAME_ERR)
60 return -1;
61
62 return 0;
63 }
64
65 static int
66 add_fde2 (sframe_encoder_ctx *encode, uint32_t start_pc_vaddr,
67 uint32_t sframe_vaddr, int idx, uint32_t *func_size)
68 {
69 /* A contiguous block containing 4 FREs. */
70 #define FDE2_NUM_FRES 4
71 sframe_frame_row_entry fres[FDE2_NUM_FRES]
72 = { {0x0, {0x10, 0, 0}, 0x3},
73 {0x10, {0x12, 0xf0, 0}, 0x5},
74 {0x14, {0x14, 0xf0, 0}, 0x4},
75 {0x20, {0x15, 0xf0, 0}, 0x5}
76 };
77 /* Function size in bytes. P.S. Must be a value greater than the
78 fre_start_addr of the last FRE above (0x20). */
79 *func_size = 0x60;
80
81 uint32_t offsetof_fde_in_sec
82 = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL);
83 int32_t func2_start_addr = (start_pc_vaddr
84 - (sframe_vaddr + offsetof_fde_in_sec));
85 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1,
86 SFRAME_FDE_TYPE_PCINC);
87 int err = sframe_encoder_add_funcdesc_v2 (encode, func2_start_addr,
88 *func_size, finfo, 0,
89 FDE2_NUM_FRES);
90 if (err == -1)
91 return err;
92
93 for (unsigned int i = 0; i < FDE2_NUM_FRES; i++)
94 if (sframe_encoder_add_fre (encode, idx, fres + i) == SFRAME_ERR)
95 return -1;
96
97 return 0;
98 }
99
100 static
101 void test_text_findfre (uint32_t text_vaddr, uint32_t sframe_vaddr)
102 {
103 sframe_encoder_ctx *encode;
104 sframe_decoder_ctx *dctx;
105 sframe_frame_row_entry frep;
106 uint32_t func1_start_vaddr;
107 uint32_t func2_start_vaddr;
108 uint32_t func1_size = 0;
109 uint32_t func2_size = 0;
110 uint32_t fde_cnt = 0;
111 int32_t lookup_pc;
112 char *sframe_buf;
113 size_t sf_size;
114 int err = 0;
115
116 #define TEST(name, cond) \
117 do \
118 { \
119 if (cond) \
120 pass (name); \
121 else \
122 fail (name); \
123 } \
124 while (0)
125
126 encode = sframe_encode (SFRAME_VERSION,
127 SFRAME_F_FDE_FUNC_START_PCREL,
128 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
129 SFRAME_CFA_FIXED_FP_INVALID,
130 -8, /* Fixed RA offset for AMD64. */
131 &err);
132
133 func1_start_vaddr = text_vaddr;
134 err = add_fde1 (encode, func1_start_vaddr, sframe_vaddr, 0, &func1_size);
135 TEST ("findfre-1: Adding FDE1", err == 0);
136
137 /* Function 2 is placed after 0x10 bytes from the end of Function 1. */
138 func2_start_vaddr = func1_start_vaddr + func1_size + 0x10;
139 err = add_fde2 (encode, func2_start_vaddr, sframe_vaddr, 1, &func2_size);
140 TEST ("findfre-1: Adding FDE2", err == 0);
141
142 fde_cnt = sframe_encoder_get_num_fidx (encode);
143 TEST ("findfre-1: Test FDE count", fde_cnt == 2);
144
145 sframe_buf = sframe_encoder_write (encode, &sf_size, &err);
146 TEST ("findfre-1: Encoder write", err == 0);
147
148 dctx = sframe_decode (sframe_buf, sf_size, &err);
149 TEST ("findfre-1: Decoder setup", dctx != NULL);
150
151 /* Find the third FRE in first FDE. */
152 lookup_pc = func1_start_vaddr + 0x15 - sframe_vaddr;
153 err = sframe_find_fre (dctx, lookup_pc, &frep);
154 TEST ("findfre-1: Find third FRE",
155 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3));
156
157 /* Find an FRE for PC at the end of range covered by FRE. */
158 lookup_pc = func1_start_vaddr + 0x9 - sframe_vaddr;
159 err = sframe_find_fre (dctx, lookup_pc, &frep);
160 TEST ("findfre-1: Find FRE for last PC covered by FRE",
161 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x2));
162
163 /* Find the last FRE in first FDE. */
164 lookup_pc = func1_start_vaddr + 0x39 - sframe_vaddr;
165 err = sframe_find_fre (dctx, lookup_pc, &frep);
166 TEST ("findfre-1: Find last FRE",
167 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x8));
168
169 /* Find the second FRE in second FDE. */
170 lookup_pc = func2_start_vaddr + 0x11 - sframe_vaddr;
171 err = sframe_find_fre (dctx, lookup_pc, &frep);
172 TEST ("findfre-1: Find second FRE",
173 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x12));
174
175 /* Find the first FRE in second FDE. */
176 lookup_pc = func2_start_vaddr + 0x0 - sframe_vaddr;
177 err = sframe_find_fre (dctx, lookup_pc, &frep);
178 TEST ("findfre-1: Find first FRE",
179 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x10));
180
181 /* Find FRE for PC out of range. Expect error code. */
182 lookup_pc = func1_start_vaddr + func1_size - sframe_vaddr;
183 err = sframe_find_fre (dctx, lookup_pc, &frep);
184 TEST ("findfre-1: Find FRE for out of range PC", err == SFRAME_ERR);
185
186 sframe_encoder_free (&encode);
187 sframe_decoder_free (&dctx);
188 }
189
190 int main (void)
191 {
192 uint32_t sframe_vaddr = 0x402220;
193 uint32_t text_vaddr = 0x401020;
194 printf ("Testing with text_vaddr = %#x; sframe_vaddr = %#x\n", text_vaddr,
195 sframe_vaddr);
196 test_text_findfre (text_vaddr, sframe_vaddr);
197
198 sframe_vaddr = 0x401020;
199 text_vaddr = 0x402220;
200 printf ("Testing with text_vaddr = %#x; sframe_vaddr = %#x\n", text_vaddr,
201 sframe_vaddr);
202 test_text_findfre (text_vaddr, sframe_vaddr);
203 }