1 /* Unit test for _dl_addr_inside_object.
2 Copyright (C) 2016-2018 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 _dl_addr_inside_object (struct link_map
*l
, const ElfW(Addr
) addr
);
37 /* Segment spans 0x2000 -> 0x4000. */
38 header
.p_vaddr
= 0x2000;
39 header
.p_memsz
= 0x2000;
40 header
.p_type
= PT_LOAD
;
41 /* Address is above the segment e.g. > 0x4000. */
43 ret
= _dl_addr_inside_object (&map
, addr
);
47 printf ("PASS: Above: Address is detected as outside the segment.\n");
50 printf ("FAIL: Above: Address is detected as inside the segment.\n");
54 printf ("FAIL: Above: Invalid return value.\n");
57 /* Address is inside the segment e.g. 0x2000 < addr < 0x4000. */
59 ret
= _dl_addr_inside_object (&map
, addr
);
63 printf ("FAIL: Inside: Address is detected as outside the segment.\n");
67 printf ("PASS: Inside: Address is detected as inside the segment.\n");
70 printf ("FAIL: Inside: Invalid return value.\n");
73 /* Address is below the segment e.g. < 0x2000. */
75 ret
= _dl_addr_inside_object (&map
, addr
);
79 printf ("PASS: Below: Address is detected as outside the segment.\n");
82 printf ("FAIL: Below: Address is detected as inside the segment.\n");
86 printf ("FAIL: Below: Invalid return value.\n");
89 /* Address is in the segment and addr == p_vaddr. */
91 ret
= _dl_addr_inside_object (&map
, addr
);
95 printf ("FAIL: At p_vaddr: Address is detected as outside the segment.\n");
99 printf ("PASS: At p_vaddr: Address is detected as inside the segment.\n");
102 printf ("FAIL: At p_vaddr: Invalid return value.\n");
105 /* Address is in the segment and addr == p_vaddr + p_memsz - 1. */
106 addr
= 0x2000 + 0x2000 - 0x1;
107 ret
= _dl_addr_inside_object (&map
, addr
);
111 printf ("FAIL: At p_memsz-1: Address is detected as outside the segment.\n");
115 printf ("PASS: At p_memsz-1: Address is detected as inside the segment.\n");
118 printf ("FAIL: At p_memsz-1: Invalid return value.\n");
121 /* Address is outside the segment and addr == p_vaddr + p_memsz. */
122 addr
= 0x2000 + 0x2000;
123 ret
= _dl_addr_inside_object (&map
, addr
);
127 printf ("PASS: At p_memsz: Address is detected as outside the segment.\n");
130 printf ("FAIL: At p_memsz: Address is detected as inside the segment.\n");
134 printf ("FAIL: At p_memsz: Invalid return value.\n");
137 /* Address is outside the segment and p_vaddr at maximum address. */
139 header
.p_vaddr
= 0x0 - 0x1;
140 header
.p_memsz
= 0x1;
141 ret
= _dl_addr_inside_object (&map
, addr
);
145 printf ("PASS: At max: Address is detected as outside the segment.\n");
148 printf ("FAIL: At max: Address is detected as inside the segment.\n");
152 printf ("FAIL: At max: Invalid return value.\n");
155 /* Address is outside the segment and p_vaddr at minimum address. */
157 header
.p_vaddr
= 0x0;
158 header
.p_memsz
= 0x1;
159 ret
= _dl_addr_inside_object (&map
, addr
);
163 printf ("PASS: At min: Address is detected as outside the segment.\n");
166 printf ("FAIL: At min: Address is detected as inside the segment.\n");
170 printf ("FAIL: At min: Invalid return value.\n");
173 /* Address is always inside the segment with p_memsz at max. */
175 header
.p_vaddr
= 0x0;
176 header
.p_memsz
= 0x0 - 0x1;
177 ret
= _dl_addr_inside_object (&map
, addr
);
181 printf ("FAIL: At maxmem: Address is detected as outside the segment.\n");
185 printf ("PASS: At maxmem: Address is detected as inside the segment.\n");
188 printf ("FAIL: At maxmem: Invalid return value.\n");
191 /* Attempt to wrap addr into the segment.
192 Pick a load address in the middle of the address space.
193 Place the test address at 0x0 so it wraps to the middle again. */
194 map
.l_addr
= 0x0 - 0x1;
195 map
.l_addr
= map
.l_addr
/ 2;
197 /* Setup a segment covering 1/2 the address space. */
198 header
.p_vaddr
= 0x0;
199 header
.p_memsz
= 0x0 - 0x1 - map
.l_addr
;
200 /* No matter where you place addr everything is shifted modulo l_addr
201 and even with this underflow you're always 1 byte away from being
203 ret
= _dl_addr_inside_object (&map
, addr
);
207 printf ("PASS: Underflow: Address is detected as outside the segment.\n");
210 printf ("FAIL: Underflow: Address is detected as inside the segment.\n");
214 printf ("FAIL: Underflow: Invalid return value.\n");
221 #include <support/test-driver.c>