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 "prims/jvm.h"
26 #include "runtime/arguments.hpp"
27 #include "unittest.hpp"
28 #include "utilities/globalDefinitions.hpp"
29
30 TEST(arguments, atojulong) {
31 char ullong_max[32];
32 int ret = jio_snprintf(ullong_max, sizeof(ullong_max), JULONG_FORMAT, ULLONG_MAX);
33 ASSERT_NE(-1, ret);
34
35 julong value;
36 const char* invalid_strings[] = {
37 "", "-1", "-100", " 1", "2 ", "3 2", "1.0",
38 "0x4.5", "0x", "0x0x1" "0.001", "4e10", "e"
39 "K", "M", "G", "1MB", "1KM", "AA", "0B",
40 "18446744073709551615K", "17179869184G",
41 "999999999999999999999999999999"
42 };
43 for (uint i = 0; i < ARRAY_SIZE(invalid_strings); i++) {
44 ASSERT_FALSE(Arguments::atojulong(invalid_strings[i], &value))
45 << "Invalid string '" << invalid_strings[i] << "' parsed without error.";
46 }
47
48 struct {
49 const char* str;
50 julong expected_value;
51 } valid_strings[] = {
52 { "0", 0 },
53 { "4711", 4711 },
54 { "1K", 1ULL * K },
55 { "1k", 1ULL * K },
56 { "2M", 2ULL * M },
57 { "2m", 2ULL * M },
58 { "4G", 4ULL * G },
59 { "4g", 4ULL * G },
60 { "0K", 0 },
61 { ullong_max, ULLONG_MAX },
62 { "0xcafebabe", 0xcafebabe },
63 { "0XCAFEBABE", 0xcafebabe },
64 { "0XCAFEbabe", 0xcafebabe },
65 { "0x10K", 0x10 * K }
66 };
67 for (uint i = 0; i < ARRAY_SIZE(valid_strings); i++) {
68 ASSERT_TRUE(Arguments::atojulong(valid_strings[i].str, &value))
69 << "Valid string '" << valid_strings[i].str << "' did not parse.";
70 ASSERT_EQ(valid_strings[i].expected_value, value);
71 }
72 }
|
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 "prims/jvm.h"
26 #include "runtime/arguments.hpp"
27 #include "unittest.hpp"
28 #include "utilities/globalDefinitions.hpp"
29
30 class ArgumentsTest : public ::testing::Test {
31 static intx parse_xss_inner_annotated(const char* str, jint expected_err, const char* file, int line_number);
32
33 public:
34 static void check_memory_size_min();
35 static void check_memory_size_max();
36 static void parse_xss();
37 };
38
39 TEST_F(ArgumentsTest, atojulong) {
40 char ullong_max[32];
41 int ret = jio_snprintf(ullong_max, sizeof(ullong_max), JULONG_FORMAT, ULLONG_MAX);
42 ASSERT_NE(-1, ret);
43
44 julong value;
45 const char* invalid_strings[] = {
46 "", "-1", "-100", " 1", "2 ", "3 2", "1.0",
47 "0x4.5", "0x", "0x0x1" "0.001", "4e10", "e"
48 "K", "M", "G", "1MB", "1KM", "AA", "0B",
49 "18446744073709551615K", "17179869184G",
50 "999999999999999999999999999999"
51 };
52 for (uint i = 0; i < ARRAY_SIZE(invalid_strings); i++) {
53 ASSERT_FALSE(Arguments::atojulong(invalid_strings[i], &value))
54 << "Invalid string '" << invalid_strings[i] << "' parsed without error.";
55 }
56
57 struct {
58 const char* str;
59 julong expected_value;
60 } valid_strings[] = {
61 { "0", 0 },
62 { "4711", 4711 },
63 { "1K", 1ULL * K },
64 { "1k", 1ULL * K },
65 { "2M", 2ULL * M },
66 { "2m", 2ULL * M },
67 { "4G", 4ULL * G },
68 { "4g", 4ULL * G },
69 { "0K", 0 },
70 { ullong_max, ULLONG_MAX },
71 { "0xcafebabe", 0xcafebabe },
72 { "0XCAFEBABE", 0xcafebabe },
73 { "0XCAFEbabe", 0xcafebabe },
74 { "0x10K", 0x10 * K }
75 };
76 for (uint i = 0; i < ARRAY_SIZE(valid_strings); i++) {
77 ASSERT_TRUE(Arguments::atojulong(valid_strings[i].str, &value))
78 << "Valid string '" << valid_strings[i].str << "' did not parse.";
79 ASSERT_EQ(valid_strings[i].expected_value, value);
80 }
81 }
82
83 // Proxy through the TestClass test fixture, which is a friend class of the implementation.
84 #define FRIEND_PROXIED(TestClass, function, mode) \
85 TEST ## mode (TestClass, function) { \
86 TestClass::function(); \
87 } \
88 void TestClass::function()
89
90 #define FRIEND_PROXIED_TEST_F( TestClass, function) FRIEND_PROXIED(TestClass, function, _F)
91 #define FRIEND_PROXIED_TEST_VM_F(TestClass, function) FRIEND_PROXIED(TestClass, function, _VM_F)
92
93 FRIEND_PROXIED_TEST_F(ArgumentsTest, check_memory_size_min) {
94 EXPECT_EQ(Arguments::check_memory_size(999, 1000, max_uintx), Arguments::arg_too_small);
95 EXPECT_EQ(Arguments::check_memory_size(1000, 1000, max_uintx), Arguments::arg_in_range);
96 EXPECT_EQ(Arguments::check_memory_size(1001, 1000, max_uintx), Arguments::arg_in_range);
97
98 EXPECT_EQ(Arguments::check_memory_size(max_intx - 2, max_intx - 1, max_uintx), Arguments::arg_too_small);
99 EXPECT_EQ(Arguments::check_memory_size(max_intx - 1, max_intx - 1, max_uintx), Arguments::arg_in_range);
100 EXPECT_EQ(Arguments::check_memory_size(max_intx - 0, max_intx - 1, max_uintx), Arguments::arg_in_range);
101
102 EXPECT_EQ(Arguments::check_memory_size(max_intx - 1, max_intx, max_uintx), Arguments::arg_too_small);
103 EXPECT_EQ(Arguments::check_memory_size(max_intx , max_intx, max_uintx), Arguments::arg_in_range);
104
105 NOT_LP64(
106 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 1, max_intx, max_uintx), Arguments::arg_in_range);
107
108 EXPECT_EQ(Arguments::check_memory_size( max_intx - 1, (julong)max_intx + 1, max_uintx), Arguments::arg_too_small);
109 EXPECT_EQ(Arguments::check_memory_size( max_intx , (julong)max_intx + 1, max_uintx), Arguments::arg_too_small);
110 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 1, (julong)max_intx + 1, max_uintx), Arguments::arg_in_range);
111 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 2, (julong)max_intx + 1, max_uintx), Arguments::arg_in_range);
112 );
113
114 EXPECT_EQ(Arguments::check_memory_size(max_uintx - 2, max_uintx - 1, max_uintx), Arguments::arg_too_small);
115 EXPECT_EQ(Arguments::check_memory_size(max_uintx - 1, max_uintx - 1, max_uintx), Arguments::arg_in_range);
116 EXPECT_EQ(Arguments::check_memory_size(max_uintx , max_uintx - 1, max_uintx), Arguments::arg_in_range);
117
118 EXPECT_EQ(Arguments::check_memory_size(max_uintx - 1, max_uintx, max_uintx), Arguments::arg_too_small);
119 EXPECT_EQ(Arguments::check_memory_size(max_uintx , max_uintx, max_uintx), Arguments::arg_in_range);
120 }
121
122 FRIEND_PROXIED_TEST_F(ArgumentsTest, check_memory_size_max) {
123 EXPECT_EQ(Arguments::check_memory_size(max_uintx - 1, 1000, max_uintx), Arguments::arg_in_range);
124 EXPECT_EQ(Arguments::check_memory_size(max_uintx , 1000, max_uintx), Arguments::arg_in_range);
125
126 EXPECT_EQ(Arguments::check_memory_size(max_intx - 2 , 1000, max_intx - 1), Arguments::arg_in_range);
127 EXPECT_EQ(Arguments::check_memory_size(max_intx - 1 , 1000, max_intx - 1), Arguments::arg_in_range);
128 EXPECT_EQ(Arguments::check_memory_size(max_intx , 1000, max_intx - 1), Arguments::arg_too_big);
129
130 EXPECT_EQ(Arguments::check_memory_size(max_intx - 1 , 1000, max_intx), Arguments::arg_in_range);
131 EXPECT_EQ(Arguments::check_memory_size(max_intx , 1000, max_intx), Arguments::arg_in_range);
132
133 NOT_LP64(
134 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 1 , 1000, max_intx), Arguments::arg_too_big);
135
136 EXPECT_EQ(Arguments::check_memory_size( max_intx , 1000, (julong)max_intx + 1), Arguments::arg_in_range);
137 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 1 , 1000, (julong)max_intx + 1), Arguments::arg_in_range);
138 EXPECT_EQ(Arguments::check_memory_size((julong)max_intx + 2 , 1000, (julong)max_intx + 1), Arguments::arg_too_big);
139 );
140 }
141
142 // A random value - used to verify the output when parsing is expected to fail.
143 static const intx no_value = 4711;
144
145 inline intx ArgumentsTest::parse_xss_inner_annotated(const char* str, jint expected_err, const char* file, int line_number) {
146 intx value = no_value;
147 jint err = Arguments::parse_xss(NULL /* Silence error messages */, str, &value);
148 EXPECT_EQ(err, expected_err) << "Failure from: " << file << ":" << line_number;
149 return value;
150 }
151
152 // Wrapper around the help function - gives file and line number when a test failure occurs.
153 #define parse_xss_inner(str, expected_err) ArgumentsTest::parse_xss_inner_annotated(str, expected_err, __FILE__, __LINE__)
154
155 static intx calc_expected(julong small_xss_input) {
156 assert(small_xss_input <= max_julong / 2, "Sanity");
157
158 // Match code in arguments.cpp
159 julong julong_ret = align_size_up_(small_xss_input, K) / K;
160 assert(julong_ret <= (julong)max_intx, "Overflow: " JULONG_FORMAT, julong_ret);
161 return (intx)julong_ret;
162 }
163
164 static char buff[100];
165 static char* to_string(julong value) {
166 jio_snprintf(buff, sizeof(buff), JULONG_FORMAT, value);
167 return buff;
168 }
169
170 FRIEND_PROXIED_TEST_VM_F(ArgumentsTest, parse_xss) {
171 // Test the maximum input value - should fail.
172 {
173 EXPECT_EQ(parse_xss_inner(to_string(max_julong), JNI_EINVAL), no_value);
174 NOT_LP64(EXPECT_EQ(parse_xss_inner(to_string(max_uintx), JNI_EINVAL), no_value));
175 }
176
177 // Test values "far" away from the uintx boundary.
178 {
179 LP64_ONLY(EXPECT_EQ(parse_xss_inner(to_string(max_julong / 2), JNI_OK), calc_expected(max_julong / 2)));
180 EXPECT_EQ(parse_xss_inner(to_string(INT_MAX), JNI_OK), calc_expected(INT_MAX));
181 EXPECT_EQ(parse_xss_inner(to_string(INT_MAX / 2), JNI_OK), calc_expected(INT_MAX / 2));
182 }
183
184 // Test value aligned both to K and vm_page_size.
185 {
186 EXPECT_TRUE(is_size_aligned(32 * M, K));
187 EXPECT_TRUE(is_size_aligned(32 * M, (size_t)os::vm_page_size()));
188 EXPECT_EQ(parse_xss_inner(to_string(32 * M), JNI_OK), (intx)(32 * M / K));
189 }
190
191 // Test values around the uintx boundary.
192 {
193 // Reverse calculation of ThreadStackSize in os_windows.cpp
194 julong page_aligned_max_uintx = align_size_down_((julong)max_uintx, (size_t)os::vm_page_size());
195 julong expected_max = align_size_down_(page_aligned_max_uintx, K) / K;
196 ASSERT_LE(expected_max, (julong)max_intx) << "expected_max will overflow intx";
197
198 // Calculation used in os_windows.cpp
199 julong max_expanded = align_size_up_(expected_max * K, (size_t)os::vm_page_size());
200 ASSERT_LE(max_expanded, (julong)max_uintx) << "max_expanded will overflow uintx";
201
202 // Test exact max and values around max
203 EXPECT_EQ(parse_xss_inner(to_string(max_expanded), JNI_OK), (intx)expected_max);
204 EXPECT_EQ(parse_xss_inner(to_string(max_expanded - 1), JNI_OK), (intx)expected_max);
205 EXPECT_EQ(parse_xss_inner(to_string(max_expanded + 1), JNI_EINVAL), no_value);
206 }
207 }
|