Blame elf/tst-_dl_addr_inside_object.c

Packit 6c4009
/* Unit test for _dl_addr_inside_object.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <elf.h>
Packit 6c4009
#include <libc-symbols.h>
Packit 6c4009
Packit 6c4009
extern int _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr);
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int ret, err = 0;
Packit 6c4009
  ElfW(Addr) addr;
Packit 6c4009
  struct link_map map;
Packit 6c4009
  ElfW(Phdr) header;
Packit 6c4009
  map.l_phdr = &header;
Packit 6c4009
  map.l_phnum = 1;
Packit 6c4009
  map.l_addr = 0x0;
Packit 6c4009
  /* Segment spans 0x2000 -> 0x4000.  */
Packit 6c4009
  header.p_vaddr = 0x2000;
Packit 6c4009
  header.p_memsz = 0x2000;
Packit 6c4009
  header.p_type = PT_LOAD;
Packit 6c4009
  /* Address is above the segment e.g. > 0x4000.  */
Packit 6c4009
  addr = 0x5000;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: Above: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("FAIL: Above: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: Above: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is inside the segment e.g. 0x2000 < addr < 0x4000.  */
Packit 6c4009
  addr = 0x3000;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("FAIL: Inside: Address is detected as outside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("PASS: Inside: Address is detected as inside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: Inside: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is below the segment e.g. < 0x2000.  */
Packit 6c4009
  addr = 0x1000;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: Below: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("FAIL: Below: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: Below: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is in the segment and addr == p_vaddr.  */
Packit 6c4009
  addr = 0x2000;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("FAIL: At p_vaddr: Address is detected as outside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("PASS: At p_vaddr: Address is detected as inside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At p_vaddr: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is in the segment and addr == p_vaddr + p_memsz - 1.  */
Packit 6c4009
  addr = 0x2000 + 0x2000 - 0x1;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("FAIL: At p_memsz-1: Address is detected as outside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("PASS: At p_memsz-1: Address is detected as inside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At p_memsz-1: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is outside the segment and addr == p_vaddr + p_memsz.  */
Packit 6c4009
  addr = 0x2000 + 0x2000;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: At p_memsz: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("FAIL: At p_memsz: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At p_memsz: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is outside the segment and p_vaddr at maximum address.  */
Packit 6c4009
  addr = 0x0 - 0x2;
Packit 6c4009
  header.p_vaddr = 0x0 - 0x1;
Packit 6c4009
  header.p_memsz = 0x1;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: At max: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("FAIL: At max: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At max: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is outside the segment and p_vaddr at minimum address.  */
Packit 6c4009
  addr = 0x1;
Packit 6c4009
  header.p_vaddr = 0x0;
Packit 6c4009
  header.p_memsz = 0x1;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: At min: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("FAIL: At min: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At min: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Address is always inside the segment with p_memsz at max.  */
Packit 6c4009
  addr = 0x0;
Packit 6c4009
  header.p_vaddr = 0x0;
Packit 6c4009
  header.p_memsz = 0x0 - 0x1;
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("FAIL: At maxmem: Address is detected as outside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
        printf ("PASS: At maxmem: Address is detected as inside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: At maxmem: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
  /* Attempt to wrap addr into the segment.
Packit 6c4009
     Pick a load address in the middle of the address space.
Packit 6c4009
     Place the test address at 0x0 so it wraps to the middle again.  */
Packit 6c4009
  map.l_addr = 0x0 - 0x1;
Packit 6c4009
  map.l_addr = map.l_addr / 2;
Packit 6c4009
  addr = 0;
Packit 6c4009
  /* Setup a segment covering 1/2 the address space.  */
Packit 6c4009
  header.p_vaddr = 0x0;
Packit 6c4009
  header.p_memsz = 0x0 - 0x1 - map.l_addr;
Packit 6c4009
  /* No matter where you place addr everything is shifted modulo l_addr
Packit 6c4009
     and even with this underflow you're always 1 byte away from being
Packit 6c4009
     in the range.  */
Packit 6c4009
  ret = _dl_addr_inside_object (&map, addr);
Packit 6c4009
  switch (ret)
Packit 6c4009
    {
Packit 6c4009
      case 0:
Packit 6c4009
	printf ("PASS: Underflow: Address is detected as outside the segment.\n");
Packit 6c4009
	break;
Packit 6c4009
      case 1:
Packit 6c4009
	printf ("FAIL: Underflow: Address is detected as inside the segment.\n");
Packit 6c4009
	err++;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
	printf ("FAIL: Underflow: Invalid return value.\n");
Packit 6c4009
	exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>