|
1 /* |
|
2 * Copyright (c) 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 "memory/allocation.hpp" |
|
26 #include "memory/allocation.inline.hpp" |
|
27 #include "memory/guardedMemory.hpp" |
|
28 #include "runtime/os.hpp" |
|
29 #include "unittest.hpp" |
|
30 |
|
31 static void guarded_memory_test_check(void* p, size_t sz, void* tag) { |
|
32 ASSERT_TRUE(p != NULL) << "NULL pointer given to check"; |
|
33 u_char* c = (u_char*) p; |
|
34 GuardedMemory guarded(c); |
|
35 EXPECT_EQ(guarded.get_tag(), tag) << "Tag is not the same as supplied"; |
|
36 EXPECT_EQ(guarded.get_user_ptr(), c) << "User pointer is not the same as supplied"; |
|
37 EXPECT_EQ(guarded.get_user_size(), sz) << "User size is not the same as supplied"; |
|
38 EXPECT_TRUE(guarded.verify_guards()) << "Guard broken"; |
|
39 } |
|
40 |
|
41 class GuardedMemoryTest { |
|
42 public: |
|
43 static size_t get_guard_header_size() { |
|
44 return sizeof (GuardedMemory::GuardHeader); |
|
45 } |
|
46 static size_t get_guard_size() { |
|
47 return sizeof (GuardedMemory::Guard); |
|
48 } |
|
49 }; |
|
50 |
|
51 // Test GuardedMemory size |
|
52 TEST(GuardedMemory, size) { |
|
53 size_t total_sz = GuardedMemory::get_total_size(1); |
|
54 ASSERT_GT(total_sz, (size_t) 1) << "Unexpected size"; |
|
55 ASSERT_GE(total_sz, GuardedMemoryTest::get_guard_header_size() + 1 |
|
56 + GuardedMemoryTest::get_guard_size()) << "Unexpected size"; |
|
57 } |
|
58 |
|
59 // Test the basic characteristics |
|
60 TEST(GuardedMemory, basic) { |
|
61 u_char* basep = |
|
62 (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); |
|
63 GuardedMemory guarded(basep, 1, (void*) 0xf000f000); |
|
64 |
|
65 EXPECT_EQ(badResourceValue, *basep) |
|
66 << "Expected guard in the form of badResourceValue"; |
|
67 |
|
68 u_char* userp = guarded.get_user_ptr(); |
|
69 EXPECT_EQ(uninitBlockPad, *userp) |
|
70 << "Expected uninitialized data in the form of uninitBlockPad"; |
|
71 guarded_memory_test_check(userp, 1, (void*) 0xf000f000); |
|
72 |
|
73 void* freep = guarded.release_for_freeing(); |
|
74 EXPECT_EQ((u_char*) freep, basep) << "Expected the same pointer guard was "; |
|
75 EXPECT_EQ(freeBlockPad, *userp) << "Expected user data to be free block padded"; |
|
76 EXPECT_FALSE(guarded.verify_guards()); |
|
77 os::free(freep); |
|
78 } |
|
79 |
|
80 // Test a number of odd sizes |
|
81 TEST(GuardedMemory, odd_sizes) { |
|
82 u_char* basep = |
|
83 (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); |
|
84 GuardedMemory guarded(basep, 1, (void*) 0xf000f000); |
|
85 |
|
86 size_t sz = 0; |
|
87 do { |
|
88 void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); |
|
89 void* up = guarded.wrap_with_guards(p, sz, (void*) 1); |
|
90 memset(up, 0, sz); |
|
91 guarded_memory_test_check(up, sz, (void*) 1); |
|
92 if (HasFatalFailure()) { |
|
93 return; |
|
94 } |
|
95 |
|
96 os::free(guarded.release_for_freeing()); |
|
97 sz = (sz << 4) + 1; |
|
98 } while (sz < (256 * 1024)); |
|
99 } |
|
100 |
|
101 // Test buffer overrun into head... |
|
102 TEST(GuardedMemory, buffer_overrun_head) { |
|
103 u_char* basep = |
|
104 (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); |
|
105 GuardedMemory guarded(basep, 1, (void*) 0xf000f000); |
|
106 |
|
107 guarded.wrap_with_guards(basep, 1); |
|
108 *basep = 0; |
|
109 EXPECT_FALSE(guarded.verify_guards()); |
|
110 os::free(basep); |
|
111 } |
|
112 |
|
113 // Test buffer overrun into tail with a number of odd sizes |
|
114 TEST(GuardedMemory, buffer_overrun_tail) { |
|
115 u_char* basep = |
|
116 (u_char*) os::malloc(GuardedMemory::get_total_size(1), mtInternal); |
|
117 GuardedMemory guarded(basep, 1, (void*) 0xf000f000); |
|
118 |
|
119 size_t sz = 1; |
|
120 do { |
|
121 void* p = os::malloc(GuardedMemory::get_total_size(sz), mtInternal); |
|
122 void* up = guarded.wrap_with_guards(p, sz, (void*) 1); |
|
123 memset(up, 0, sz + 1); // Buffer-overwrite (within guard) |
|
124 EXPECT_FALSE(guarded.verify_guards()) << "Guard was not broken as expected"; |
|
125 os::free(guarded.release_for_freeing()); |
|
126 sz = (sz << 4) + 1; |
|
127 } while (sz < (256 * 1024)); |
|
128 } |
|
129 |
|
130 // Test wrap_copy/wrap_free |
|
131 TEST(GuardedMemory, wrap) { |
|
132 EXPECT_TRUE(GuardedMemory::free_copy(NULL)) << "Expected free NULL to be OK"; |
|
133 |
|
134 const char* str = "Check my bounds out"; |
|
135 size_t str_sz = strlen(str) + 1; |
|
136 char* str_copy = (char*) GuardedMemory::wrap_copy(str, str_sz); |
|
137 guarded_memory_test_check(str_copy, str_sz, NULL); |
|
138 if (HasFatalFailure()) { |
|
139 return; |
|
140 } |
|
141 EXPECT_EQ(0, strcmp(str, str_copy)) << "Not identical copy"; |
|
142 EXPECT_TRUE(GuardedMemory::free_copy(str_copy)) << "Free copy failed to verify"; |
|
143 |
|
144 void* no_data = NULL; |
|
145 void* no_data_copy = GuardedMemory::wrap_copy(no_data, 0); |
|
146 EXPECT_TRUE(GuardedMemory::free_copy(no_data_copy)) |
|
147 << "Expected valid guards even for no data copy"; |
|
148 } |