1  /* plt-findfre-1.c -- Test for sframe_find_fre for SFRAME_FDE_TYPE_PCMASK.
       2  
       3     Copyright (C) 2023 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, int idx)
      32  {
      33    int i, err;
      34    /* A contiguous block containing 3 FREs.  The start_ip_offset must remain
      35       less than 16 bytes.  */
      36    sframe_frame_row_entry fres[]
      37      = { {0x0, {0x1, 0, 0}, 0x3},
      38  	{0x6, {0x2, 0xf0, 0}, 0x5},
      39  	{0xc, {0x3, 0xf0, 0}, 0x4}
      40        };
      41  
      42    unsigned char finfo = sframe_fde_create_func_info (SFRAME_FRE_TYPE_ADDR1,
      43  						     SFRAME_FDE_TYPE_PCMASK);
      44    /* 5 pltN entries of 16 bytes each.  */
      45    err = sframe_encoder_add_funcdesc_v2 (ectx, 0x1000, 16*5, finfo, 16, 3);
      46    if (err == -1)
      47      return err;
      48  
      49    for (i = 0; i < 3; i++)
      50      if (sframe_encoder_add_fre (ectx, idx, fres+i) == SFRAME_ERR)
      51        return -1;
      52  
      53    return 0;
      54  }
      55  
      56  int main (void)
      57  {
      58    sframe_encoder_ctx *ectx;
      59    sframe_decoder_ctx *dctx;
      60    sframe_frame_row_entry frep;
      61    char *sframe_buf;
      62    size_t sf_size;
      63    int err = 0;
      64    unsigned int fde_cnt = 0;
      65  
      66  #define TEST(name, cond)                                                      \
      67    do                                                                          \
      68      {                                                                         \
      69        if (cond)                                                               \
      70  	pass (name);                                                          \
      71        else                                                                    \
      72  	fail (name);                                                          \
      73      }                                                                         \
      74      while (0)
      75  
      76    ectx = sframe_encode (SFRAME_VERSION, 0, SFRAME_ABI_AMD64_ENDIAN_LITTLE,
      77  			SFRAME_CFA_FIXED_FP_INVALID,
      78  			-8, /* Fixed RA offset for AMD64.  */
      79  			&err);
      80  
      81    err = add_plt_fde1 (ectx, 0);
      82    TEST ("plt-findfre-1: Adding FDE1 for plt", err == 0);
      83  
      84    fde_cnt = sframe_encoder_get_num_fidx (ectx);
      85    TEST ("plt-findfre-1: Test FDE count", fde_cnt == 1);
      86  
      87    sframe_buf = sframe_encoder_write (ectx, &sf_size, &err);
      88    TEST ("plt-findfre-1: Encoder write", err == 0);
      89  
      90    dctx = sframe_decode (sframe_buf, sf_size, &err);
      91    TEST("plt-findfre-1: Decoder setup", dctx != NULL);
      92  
      93    /* Find the first FRE in PLT1.  */
      94    err = sframe_find_fre (dctx, (0x1000 + 0x0), &frep);
      95    TEST("plt-findfre-1: Find first FRE in PLT1",
      96         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x1)));
      97  
      98    /* Find the second FRE.  */
      99    err = sframe_find_fre (dctx, (0x1000 + 0x6), &frep);
     100    TEST("plt-findfre-1: Find second FRE in PLT1",
     101         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x2)));
     102  
     103    /* Find the last FRE.  */
     104    err = sframe_find_fre (dctx, (0x1000 + 0xc), &frep);
     105    TEST("plt-findfre-1: Find last FRE in PLT1",
     106         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3)));
     107  
     108    /* Find the first FRE in PLT4.  */
     109    err = sframe_find_fre (dctx, (0x1000 + 16*3 + 0x0), &frep);
     110    TEST("plt-findfre-1: Find first FRE in PLT4",
     111         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x1)));
     112  
     113    /* Find the second FRE in PLT4.  */
     114    err = sframe_find_fre (dctx, (0x1000 + 16*3 + 0x6), &frep);
     115    TEST("plt-findfre-1: Find second FRE in PLT4",
     116         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x2)));
     117  
     118    /* Find the last FRE in PLT4.  */
     119    err = sframe_find_fre (dctx, (0x1000 + 16*3 + 0xc), &frep);
     120    TEST("plt-findfre-1: Find last FRE in PLT4",
     121         ((err == 0) && (sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3)));
     122  
     123    sframe_encoder_free (&ectx);
     124    sframe_decoder_free (&dctx);
     125  
     126    return 0;
     127  }