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