Debian Codegraph @

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

raw
1 /* plt-findfre-1.c -- Test for sframe_find_fre for SFRAME_FDE_TYPE_PCMASK.
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_plt_fde1 (sframe_encoder_ctx *ectx, uint32_t plt_vaddr,
32 uint32_t sframe_vaddr, int idx)
33 {
34 /* A contiguous block containing 3 FREs. The start_ip_offset must remain
35 less than 16 bytes. */
36 #define PLT1_NUM_FRES 3
37 sframe_frame_row_entry fres[PLT1_NUM_FRES]
38 = { {0x0, {0x1, 0, 0}, 0x3},
39 {0x6, {0x2, 0xf0, 0}, 0x5},
40 {0xc, {0x3, 0xf0, 0}, 0x4}
41 };
42
43 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1,
44 SFRAME_FDE_TYPE_PCMASK);
45 uint32_t offsetof_fde_in_sec
46 = sframe_encoder_get_offsetof_fde_start_addr (ectx, idx, NULL);
47 int32_t func_start_addr = (plt_vaddr
48 - (sframe_vaddr + offsetof_fde_in_sec));
49
50 /* 5 pltN entries of 16 bytes each. */
51 int err = sframe_encoder_add_funcdesc_v2 (ectx, func_start_addr,
52 16 * 5 /* func size in bytes. */,
53 finfo,
54 16 /* rep block size in bytes. */,
55 PLT1_NUM_FRES);
56 if (err == -1)
57 return err;
58
59 for (unsigned i = 0; i < PLT1_NUM_FRES; i++)
60 if (sframe_encoder_add_fre (ectx, idx, fres + i) == SFRAME_ERR)
61 return -1;
62
63 return 0;
64 }
65
66 static
67 void test_plt_findfre (uint32_t plt_vaddr, uint32_t sframe_vaddr)
68 {
69 sframe_encoder_ctx *ectx;
70 sframe_decoder_ctx *dctx;
71 sframe_frame_row_entry frep;
72 char *sframe_buf;
73 size_t sf_size;
74 int err = 0;
75 uint32_t fde_cnt = 0;
76
77 #define TEST(name, cond) \
78 do \
79 { \
80 if (cond) \
81 pass (name); \
82 else \
83 fail (name); \
84 } \
85 while (0)
86
87 ectx = sframe_encode (SFRAME_VERSION, SFRAME_F_FDE_FUNC_START_PCREL,
88 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
89 SFRAME_CFA_FIXED_FP_INVALID,
90 -8, /* Fixed RA offset for AMD64. */
91 &err);
92
93 err = add_plt_fde1 (ectx, plt_vaddr, sframe_vaddr, 0);
94 TEST ("plt-findfre-1: Adding FDE1 for plt", err == 0);
95
96 fde_cnt = sframe_encoder_get_num_fidx (ectx);
97 TEST ("plt-findfre-1: Test FDE count", fde_cnt == 1);
98
99 sframe_buf = sframe_encoder_write (ectx, &sf_size, &err);
100 TEST ("plt-findfre-1: Encoder write", err == 0);
101
102 dctx = sframe_decode (sframe_buf, sf_size, &err);
103 TEST ("plt-findfre-1: Decoder setup", dctx != NULL);
104
105 /* Find the first FRE in PLT1. */
106 err = sframe_find_fre (dctx, (plt_vaddr + 0x0 - sframe_vaddr), &frep);
107 TEST ("plt-findfre-1: Find first FRE in PLT1",
108 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x1));
109
110 /* Find the second FRE. */
111 err = sframe_find_fre (dctx, (plt_vaddr + 0x6 - sframe_vaddr), &frep);
112 TEST ("plt-findfre-1: Find second FRE in PLT1",
113 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x2));
114
115 /* Find the last FRE. */
116 err = sframe_find_fre (dctx, (plt_vaddr + 0xc - sframe_vaddr), &frep);
117 TEST ("plt-findfre-1: Find last FRE in PLT1",
118 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3));
119
120 /* Find the first FRE in PLT4. */
121 err = sframe_find_fre (dctx, (plt_vaddr + 16*3 + 0x0 - sframe_vaddr), &frep);
122 TEST ("plt-findfre-1: Find first FRE in PLT4",
123 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x1));
124
125 /* Find the second FRE in PLT4. */
126 err = sframe_find_fre (dctx, (plt_vaddr + 16*3 + 0x6 - sframe_vaddr), &frep);
127 TEST ("plt-findfre-1: Find second FRE in PLT4",
128 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x2));
129
130 /* Find the last FRE in PLT4. */
131 err = sframe_find_fre (dctx, (plt_vaddr + 16*3 + 0xc - sframe_vaddr), &frep);
132 TEST ("plt-findfre-1: Find last FRE in PLT4",
133 (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3));
134
135 /* Find no FRE for out of range PLT6. */
136 err = sframe_find_fre (dctx, (plt_vaddr + 16*5 + 0x0 - sframe_vaddr), &frep);
137 TEST ("plt-findfre-1: Find no FRE for out of range PLT6", err != 0);
138
139 sframe_encoder_free (&ectx);
140 sframe_decoder_free (&dctx);
141 }
142
143 int main (void)
144 {
145 uint32_t sframe_vaddr = 0x402220;
146 uint32_t plt_vaddr = 0x401020;
147 printf ("Testing with plt_vaddr = %#x; sframe_vaddr = %#x\n", plt_vaddr,
148 sframe_vaddr);
149 test_plt_findfre (plt_vaddr, sframe_vaddr);
150
151 sframe_vaddr = 0x401020;
152 plt_vaddr = 0x402220;
153 printf ("Testing with plt_vaddr = %#x; sframe_vaddr = %#x\n", plt_vaddr,
154 sframe_vaddr);
155 test_plt_findfre (plt_vaddr, sframe_vaddr);
156 }