1/*
2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "precompiled.hpp"
25#include "gc/shared/collectedHeap.hpp"
26#include "memory/universe.hpp"
27#include "unittest.hpp"
28
29TEST_VM(CollectedHeap, is_in) {
30 CollectedHeap* heap = Universe::heap();
31
32 uintptr_t epsilon = (uintptr_t) MinObjAlignment;
33 uintptr_t heap_start = (uintptr_t) heap->reserved_region().start();
34 uintptr_t heap_end = (uintptr_t) heap->reserved_region().end();
35
36 // Test that NULL is not in the heap.
37 ASSERT_FALSE(heap->is_in(NULL)) << "NULL is unexpectedly in the heap";
38
39 // Test that a pointer to before the heap start is reported as outside the heap.
40 ASSERT_GE(heap_start, ((uintptr_t) NULL + epsilon))
41 << "Sanity check - heap should not start at 0";
42
43 void* before_heap = (void*) (heap_start - epsilon);
44 ASSERT_FALSE(heap->is_in(before_heap)) << "before_heap: " << p2i(before_heap)
45 << " is unexpectedly in the heap";
46
47 // Test that a pointer to after the heap end is reported as outside the heap.
48 ASSERT_LE(heap_end, ((uintptr_t)-1 - epsilon))
49 << "Sanity check - heap should not end at the end of address space";
50
51 void* after_heap = (void*) (heap_end + epsilon);
52 ASSERT_FALSE(heap->is_in(after_heap)) << "after_heap: " << p2i(after_heap)
53 << " is unexpectedly in the heap";
54}
55