1 /*
2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2020 SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 #include "precompiled.hpp"
26 #include "runtime/interfaceSupport.inline.hpp"
27 #include "runtime/stubRoutines.hpp"
28 #include "runtime/vmOperations.hpp"
29 #include "runtime/vmThread.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/vmError.hpp"
32 #include "unittest.hpp"
33 
34 static const intptr_t pattern = LP64_ONLY(0xABCDABCDABCDABCDULL) NOT_LP64(0xABCDABCD);
35 static intptr_t* invalid_address = (intptr_t*)(intptr_t) NOT_AIX(os::min_page_size()) AIX_ONLY(-1);
36 
TEST_VM(os,safefetch_can_use)37 TEST_VM(os, safefetch_can_use) {
38   // Once VM initialization is through,
39   // safefetch should work on every platform.
40   ASSERT_TRUE(CanUseSafeFetch32());
41 }
42 
TEST_VM(os,safefetch_positive)43 TEST_VM(os, safefetch_positive) {
44   intptr_t v = pattern;
45   intptr_t a = SafeFetchN(&v, 1);
46   ASSERT_EQ(v, a);
47 }
48 
TEST_VM(os,safefetch_negative)49 TEST_VM(os, safefetch_negative) {
50   intptr_t a = SafeFetchN(invalid_address, pattern);
51   ASSERT_EQ(pattern, a);
52   a = SafeFetchN(invalid_address, ~pattern);
53   ASSERT_EQ(~pattern, a);
54 }
55 
56 class VM_TestSafeFetchAtSafePoint : public VM_GTestExecuteAtSafepoint {
57 public:
doit()58   void doit() {
59     // Regression test for JDK-8257828
60     // Should not crash.
61     intptr_t a = SafeFetchN(invalid_address, pattern);
62     ASSERT_EQ(pattern, a);
63     a = SafeFetchN(invalid_address, ~pattern);
64     ASSERT_EQ(~pattern, a);
65   }
66 };
67 
TEST_VM(os,safefetch_negative_at_safepoint)68 TEST_VM(os, safefetch_negative_at_safepoint) {
69   VM_TestSafeFetchAtSafePoint op;
70   ThreadInVMfromNative invm(JavaThread::current());
71   VMThread::execute(&op);
72 }
73