Debian Codegraph @

libsframe/testsuite/libsframe.encode/encode-1.c

raw
1 /* encode-1.c -- Test for encoder in libsframe.
2
3 Copyright (C) 2022-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 #define FDE1_NUM_FRES 4
35 /* A contiguous block containing 4 FREs. */
36 sframe_frame_row_entry fres[]
37 = { {0x0, {0x8, 0, 0}, 0x3},
38 {0x1, {0x10, 0xf0, 0}, 0x5},
39 {0x4, {0x10, 0xf0, 0}, 0x4},
40 {0x1a, {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 (0x1a). */
44 *func_size = 0x1b;
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 {
70 /* A contiguous block containing 4 FREs. */
71 #define FDE2_NUM_FRES 4
72 sframe_frame_row_entry fres[]
73 = { {0x0, {0x8, 0, 0}, 0x3},
74 {0x1, {0x10, 0xf0, 0}, 0x5},
75 {0x4, {0x10, 0xf0, 0}, 0x4},
76 {0xf, {0x8, 0xf0, 0}, 0x5}
77 };
78 /* Function size in bytes. P.S. Must be a value greater than the
79 fre_start_addr of the last FRE above (0xf). */
80 *func_size = 0x10;
81
82 uint32_t offsetof_fde_in_sec
83 = sframe_encoder_get_offsetof_fde_start_addr (encode, idx, NULL);
84 int32_t func1_start_addr = (start_pc_vaddr
85 - (sframe_vaddr + offsetof_fde_in_sec));
86 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1,
87 SFRAME_FDE_TYPE_PCINC);
88 int err = sframe_encoder_add_funcdesc_v2 (encode, func1_start_addr,
89 *func_size, finfo, 0,
90 FDE1_NUM_FRES);
91 if (err == -1)
92 return err;
93
94 for (unsigned int i = 0; i < FDE1_NUM_FRES; i++)
95 if (sframe_encoder_add_fre (encode, idx, fres + i) == SFRAME_ERR)
96 return -1;
97
98 return 0;
99 }
100
101 /*
102 * SFrame info from the following source (2 fdes, 4 fres in each fde):
103 * static int cnt;
104 * int foo() { return ++cnt; }
105 * int main() { return foo(); }
106 */
107 #define DATA "DATA2"
108
109 static int
110 data_match (char *sframe_buf, size_t sz)
111 {
112 FILE *fp;
113 struct stat st;
114 char *sf_buf;
115 size_t sf_size;
116 int diffs;
117
118 fp = fopen (DATA, "r");
119 if (fp == NULL)
120 return 0;
121 if (fstat (fileno (fp), &st) < 0)
122 {
123 perror ("fstat");
124 fclose (fp);
125 return 0;
126 }
127 sf_buf = malloc (st.st_size);
128 if (sf_buf == NULL)
129 {
130 perror ("malloc");
131 return 0;
132 }
133 sf_size = fread (sf_buf, 1, st.st_size, fp);
134 fclose (fp);
135 if (sf_size == 0 || sf_buf == NULL)
136 {
137 fprintf (stderr, "Encode: Read section failed\n");
138 return 0;
139 }
140 if (sf_size != sz)
141 return 0;
142
143 diffs = memcmp (sf_buf, sframe_buf, sz);
144
145 free (sf_buf);
146 return diffs == 0;
147 }
148
149 int main (void)
150 {
151 sframe_encoder_ctx *encode;
152 sframe_frame_row_entry frep;
153 uint32_t sframe_vaddr;
154 uint32_t func1_start_vaddr;
155 uint32_t func2_start_vaddr;
156 uint32_t func1_size = 0;
157 uint32_t func2_size = 0;
158 char *sframe_buf;
159 size_t sf_size;
160 int err = 0;
161 unsigned int fde_cnt = 0;
162 int match_p = 0;
163
164 #define TEST(name, cond) \
165 do \
166 { \
167 if (cond) \
168 pass (name); \
169 else \
170 fail (name); \
171 } \
172 while (0)
173
174 sframe_vaddr = 0x4020c8;
175 encode = sframe_encode (SFRAME_VERSION,
176 SFRAME_F_FDE_FUNC_START_PCREL,
177 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
178 SFRAME_CFA_FIXED_FP_INVALID,
179 -8, /* Fixed RA offset for AMD64. */
180 &err);
181
182 fde_cnt = sframe_encoder_get_num_fidx (encode);
183 TEST ("encode-1: Encoder FDE count", fde_cnt == 0);
184
185 err = sframe_encoder_add_fre (encode, 1, &frep);
186 TEST ("encode-1: Encoder update workflow", err == SFRAME_ERR);
187
188 func1_start_vaddr = 0x401106;
189 err = add_fde1 (encode, func1_start_vaddr, sframe_vaddr, 0, &func1_size);
190 TEST ("encode-1: Encoder adding FDE1", err == 0);
191
192 /* Function 2 is placed after 0x0 bytes from the end of Function 1. */
193 func2_start_vaddr = func1_start_vaddr + func1_size + 0x0;
194 err = add_fde2 (encode, func2_start_vaddr, sframe_vaddr, 1, &func2_size);
195 TEST ("encode-1: Encoder adding FDE2", err == 0);
196
197 fde_cnt = sframe_encoder_get_num_fidx (encode);
198 TEST ("encode-1: Encoder FDE count", fde_cnt == 2);
199
200 sframe_buf = sframe_encoder_write (encode, &sf_size, &err);
201 TEST ("encode-1: Encoder write", err == 0);
202
203 match_p = data_match (sframe_buf, sf_size);
204 TEST ("encode-1: Encode buffer match", match_p == 1);
205
206 sframe_encoder_free (&encode);
207 return 0;
208 }