2
* Copyright (c) 2020, 2024, 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.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "runtime/interfaceSupport.inline.hpp"
27
#include "runtime/safefetch.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
#include "testutils.hpp"
35
// Note: beyond these tests, there exist additional tests testing that safefetch in error handling
36
// (in the context of signal handling) works, see runtime/ErrorHandling
38
static const intptr_t patternN = LP64_ONLY(0xABCDABCDABCDABCDULL) NOT_LP64(0xABCDABCD);
39
static const int pattern32 = 0xABCDABCD;
41
static intptr_t* const bad_addressN = (intptr_t*) VMError::segfault_address;
42
static int* const bad_address32 = (int*) VMError::segfault_address;
44
static intptr_t dataN[3] = { 0, patternN, 0 };
45
static int data32[3] = { 0, pattern32, 0 };
46
static intptr_t* const good_addressN = dataN + 1;
47
static int* const good_address32 = data32 + 1;
50
static void test_safefetchN_positive() {
51
intptr_t a = SafeFetchN(good_addressN, 1);
52
ASSERT_EQ(patternN, a);
55
static void test_safefetch32_positive() {
56
uint64_t a = SafeFetch32(good_address32, 1);
57
ASSERT_EQ((uint64_t)pattern32, a);
60
static void test_safefetchN_negative() {
61
intptr_t a = SafeFetchN(bad_addressN, 0);
63
a = SafeFetchN(bad_addressN, -1);
65
a = SafeFetchN(bad_addressN, ~patternN);
66
ASSERT_EQ(~patternN, a);
67
// Also test nullptr, but not on AIX, where nullptr is readable
69
a = SafeFetchN(nullptr, 0);
71
a = SafeFetchN(nullptr, ~patternN);
72
ASSERT_EQ(~patternN, a);
76
static void test_safefetch32_negative() {
77
int a = SafeFetch32(bad_address32, 0);
79
a = SafeFetch32(bad_address32, -1);
81
a = SafeFetch32(bad_address32, ~pattern32);
82
ASSERT_EQ(~pattern32, a);
83
// Also test nullptr, but not on AIX, where nullptr is readable
85
a = SafeFetch32(nullptr, 0);
87
a = SafeFetch32(nullptr, ~pattern32);
88
ASSERT_EQ(~pattern32, a);
92
TEST_VM(os, safefetchN_positive) {
93
test_safefetchN_positive();
96
TEST_VM(os, safefetch32_positive) {
97
test_safefetch32_positive();
100
TEST_VM(os, safefetchN_negative) {
101
test_safefetchN_negative();
104
TEST_VM(os, safefetch32_negative) {
105
test_safefetch32_negative();
108
// Try with Thread::current being nullptr. SafeFetch should work then too.
111
class ThreadCurrentNullMark : public StackObj {
114
ThreadCurrentNullMark() {
115
_saved = Thread::current();
116
Thread::clear_thread_current();
118
~ThreadCurrentNullMark() {
119
_saved->initialize_thread_current();
123
TEST_VM(os, safefetchN_positive_current_null) {
124
ThreadCurrentNullMark tcnmark;
125
test_safefetchN_positive();
128
TEST_VM(os, safefetch32_positive_current_null) {
129
ThreadCurrentNullMark tcnmark;
130
test_safefetch32_positive();
133
TEST_VM(os, safefetchN_negative_current_null) {
134
ThreadCurrentNullMark tcnmark;
135
test_safefetchN_negative();
138
TEST_VM(os, safefetch32_negative_current_null) {
139
ThreadCurrentNullMark tcnmark;
140
test_safefetch32_negative();
143
class VM_TestSafeFetchAtSafePoint : public VM_GTestExecuteAtSafepoint {
146
// Regression test for JDK-8257828
148
test_safefetchN_negative();
152
TEST_VM(os, safefetch_negative_at_safepoint) {
153
VM_TestSafeFetchAtSafePoint op;
154
ThreadInVMfromNative invm(JavaThread::current());
155
VMThread::execute(&op);