1 /* Unit test for _dl_addr_inside_object.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
23 #include <libc-symbols.h>
25 extern int internal_function
_dl_addr_inside_object (struct link_map
*l
,
26 const ElfW(Addr
) addr
);
38 /* Segment spans 0x2000 -> 0x4000. */
39 header
.p_vaddr
= 0x2000;
40 header
.p_memsz
= 0x2000;
41 header
.p_type
= PT_LOAD
;
42 /* Address is above the segment e.g. > 0x4000. */
44 ret
= _dl_addr_inside_object (&map
, addr
);
48 printf ("PASS: Above: Address is detected as outside the segment.\n");
51 printf ("FAIL: Above: Address is detected as inside the segment.\n");
55 printf ("FAIL: Above: Invalid return value.\n");
58 /* Address is inside the segment e.g. 0x2000 < addr < 0x4000. */
60 ret
= _dl_addr_inside_object (&map
, addr
);
64 printf ("FAIL: Inside: Address is detected as outside the segment.\n");
68 printf ("PASS: Inside: Address is detected as inside the segment.\n");
71 printf ("FAIL: Inside: Invalid return value.\n");
74 /* Address is below the segment e.g. < 0x2000. */
76 ret
= _dl_addr_inside_object (&map
, addr
);
80 printf ("PASS: Below: Address is detected as outside the segment.\n");
83 printf ("FAIL: Below: Address is detected as inside the segment.\n");
87 printf ("FAIL: Below: Invalid return value.\n");
90 /* Address is in the segment and addr == p_vaddr. */
92 ret
= _dl_addr_inside_object (&map
, addr
);
96 printf ("FAIL: At p_vaddr: Address is detected as outside the segment.\n");
100 printf ("PASS: At p_vaddr: Address is detected as inside the segment.\n");
103 printf ("FAIL: At p_vaddr: Invalid return value.\n");
106 /* Address is in the segment and addr == p_vaddr + p_memsz - 1. */
107 addr
= 0x2000 + 0x2000 - 0x1;
108 ret
= _dl_addr_inside_object (&map
, addr
);
112 printf ("FAIL: At p_memsz-1: Address is detected as outside the segment.\n");
116 printf ("PASS: At p_memsz-1: Address is detected as inside the segment.\n");
119 printf ("FAIL: At p_memsz-1: Invalid return value.\n");
122 /* Address is outside the segment and addr == p_vaddr + p_memsz. */
123 addr
= 0x2000 + 0x2000;
124 ret
= _dl_addr_inside_object (&map
, addr
);
128 printf ("PASS: At p_memsz: Address is detected as outside the segment.\n");
131 printf ("FAIL: At p_memsz: Address is detected as inside the segment.\n");
135 printf ("FAIL: At p_memsz: Invalid return value.\n");
138 /* Address is outside the segment and p_vaddr at maximum address. */
140 header
.p_vaddr
= 0x0 - 0x1;
141 header
.p_memsz
= 0x1;
142 ret
= _dl_addr_inside_object (&map
, addr
);
146 printf ("PASS: At max: Address is detected as outside the segment.\n");
149 printf ("FAIL: At max: Address is detected as inside the segment.\n");
153 printf ("FAIL: At max: Invalid return value.\n");
156 /* Address is outside the segment and p_vaddr at minimum address. */
158 header
.p_vaddr
= 0x0;
159 header
.p_memsz
= 0x1;
160 ret
= _dl_addr_inside_object (&map
, addr
);
164 printf ("PASS: At min: Address is detected as outside the segment.\n");
167 printf ("FAIL: At min: Address is detected as inside the segment.\n");
171 printf ("FAIL: At min: Invalid return value.\n");
174 /* Address is always inside the segment with p_memsz at max. */
176 header
.p_vaddr
= 0x0;
177 header
.p_memsz
= 0x0 - 0x1;
178 ret
= _dl_addr_inside_object (&map
, addr
);
182 printf ("FAIL: At maxmem: Address is detected as outside the segment.\n");
186 printf ("PASS: At maxmem: Address is detected as inside the segment.\n");
189 printf ("FAIL: At maxmem: Invalid return value.\n");
192 /* Attempt to wrap addr into the segment.
193 Pick a load address in the middle of the address space.
194 Place the test address at 0x0 so it wraps to the middle again. */
195 map
.l_addr
= 0x0 - 0x1;
196 map
.l_addr
= map
.l_addr
/ 2;
198 /* Setup a segment covering 1/2 the address space. */
199 header
.p_vaddr
= 0x0;
200 header
.p_memsz
= 0x0 - 0x1 - map
.l_addr
;
201 /* No matter where you place addr everything is shifted modulo l_addr
202 and even with this underflow you're always 1 byte away from being
204 ret
= _dl_addr_inside_object (&map
, addr
);
208 printf ("PASS: Underflow: Address is detected as outside the segment.\n");
211 printf ("FAIL: Underflow: Address is detected as inside the segment.\n");
215 printf ("FAIL: Underflow: Invalid return value.\n");
222 #define TEST_FUNCTION do_test ()
223 #include "../test-skeleton.c"