1/* encode-1.c -- Test for encoder in libsframe. 2 3 Copyright (C) 2022 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 30static int 31add_fde1 (sframe_encoder_ctx *encode, int idx) 32{ 33 int i, err; 34 /* A contiguous block containing 4 FREs. */ 35 sframe_frame_row_entry fres[] 36 = { {0x0, {0x8, 0, 0}, 0x3}, 37 {0x1, {0x10, 0xf0, 0}, 0x5}, 38 {0x4, {0x10, 0xf0, 0}, 0x4}, 39 {0x1a, {0x8, 0xf0, 0}, 0x5} 40 }; 41 42 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, 43 SFRAME_FDE_TYPE_PCINC); 44 err = sframe_encoder_add_funcdesc (encode, 0xfffff03e, 0x1b, finfo, 4); 45 if (err == -1) 46 return err; 47 48 for (i = 0; i < 4; i++) 49 if (sframe_encoder_add_fre (encode, idx,fres+i) == SFRAME_ERR) 50 return -1; 51 52 return 0; 53} 54 55static int 56add_fde2 (sframe_encoder_ctx *encode, int idx) 57{ 58 int i, err; 59 /* A contiguous block containing 4 FREs. */ 60 sframe_frame_row_entry fres[] 61 = { {0x0, {0x8, 0, 0}, 0x3}, 62 {0x1, {0x10, 0xf0, 0}, 0x5}, 63 {0x4, {0x10, 0xf0, 0}, 0x4}, 64 {0xf, {0x8, 0xf0, 0}, 0x5} 65 }; 66 67 unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1, 68 SFRAME_FDE_TYPE_PCINC); 69 err = sframe_encoder_add_funcdesc (encode, 0xfffff059, 0x10, finfo, 4); 70 if (err == -1) 71 return err; 72 73 for (i = 0; i < 4; i++) 74 if (sframe_encoder_add_fre (encode, idx, fres+i) == SFRAME_ERR) 75 return -1; 76 77 return 0; 78} 79 80/* 81 * SFrame info from the following source (2 fdes, 4 fres in each fde): 82 * static int cnt; 83 * int foo() { return ++cnt; } 84 * int main() { return foo(); } 85 */ 86#define DATA "DATA2" 87 88static int 89data_match (char *sframe_buf, size_t sz) 90{ 91 FILE *fp; 92 struct stat st; 93 char *sf_buf; 94 size_t sf_size; 95 int diffs; 96 97 fp = fopen (DATA, "r"); 98 if (fp == NULL) 99 return 0; 100 if (fstat (fileno (fp), &st) < 0) 101 { 102 perror ("fstat"); 103 fclose (fp); 104 return 0; 105 } 106 sf_buf = malloc (st.st_size); 107 if (sf_buf == NULL) 108 { 109 perror ("malloc"); 110 return 0; 111 } 112 sf_size = fread (sf_buf, 1, st.st_size, fp); 113 fclose (fp); 114 if (sf_size == 0 || sf_buf == NULL) 115 { 116 fprintf (stderr, "Encode: Read section failed\n"); 117 return 0; 118 } 119 if (sf_size != sz) 120 return 0; 121 122 diffs = memcmp (sf_buf, sframe_buf, sz); 123 124 free (sf_buf); 125 return diffs == 0; 126} 127 128int main (void) 129{ 130 sframe_encoder_ctx *encode; 131 sframe_frame_row_entry frep; 132 char *sframe_buf; 133 size_t sf_size; 134 int err = 0; 135 unsigned int fde_cnt = 0; 136 int match_p = 0; 137 138#define TEST(name, cond) \ 139 do \ 140 { \ 141 if (cond) \ 142 pass (name); \ 143 else \ 144 fail (name); \ 145 } \ 146 while (0) 147 148 encode = sframe_encode (SFRAME_VERSION, 0, 149 SFRAME_ABI_AMD64_ENDIAN_LITTLE, 150 SFRAME_CFA_FIXED_FP_INVALID, 151 -8, /* Fixed RA offset for AMD64. */ 152 &err); 153 154 fde_cnt = sframe_encoder_get_num_fidx (encode); 155 TEST ("encode-1: Encoder FDE count", fde_cnt == 0); 156 157 err = sframe_encoder_add_fre (encode, 1, &frep); 158 TEST ("encode-1: Encoder update workflow", err == SFRAME_ERR); 159 160 err = add_fde1 (encode, 0); 161 TEST ("encode-1: Encoder adding FDE1", err == 0); 162 163 err = add_fde2 (encode, 1); 164 TEST ("encode-1: Encoder adding FDE2", err == 0); 165 166 fde_cnt = sframe_encoder_get_num_fidx (encode); 167 TEST ("encode-1: Encoder FDE count", fde_cnt == 2); 168 169 sframe_buf = sframe_encoder_write (encode, &sf_size, &err); 170 TEST ("encode-1: Encoder write", err == 0); 171 172 match_p = data_match (sframe_buf, sf_size); 173 TEST ("encode-1: Encode buffer match", match_p == 1); 174 175 sframe_encoder_free (&encode); 176 return 0; 177} 178