jdk

Форк
0
/
test_safefetch.cpp 
156 строк · 4.5 Кб
1
/*
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.
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/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"
34

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
37

38
static const intptr_t patternN = LP64_ONLY(0xABCDABCDABCDABCDULL) NOT_LP64(0xABCDABCD);
39
static const int pattern32 = 0xABCDABCD;
40

41
static intptr_t* const  bad_addressN = (intptr_t*) VMError::segfault_address;
42
static int* const       bad_address32 = (int*) VMError::segfault_address;
43

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;
48

49

50
static void test_safefetchN_positive() {
51
  intptr_t a = SafeFetchN(good_addressN, 1);
52
  ASSERT_EQ(patternN, a);
53
}
54

55
static void test_safefetch32_positive() {
56
  uint64_t a = SafeFetch32(good_address32, 1);
57
  ASSERT_EQ((uint64_t)pattern32, a);
58
}
59

60
static void test_safefetchN_negative() {
61
  intptr_t a = SafeFetchN(bad_addressN, 0);
62
  ASSERT_EQ(0, a);
63
  a = SafeFetchN(bad_addressN, -1);
64
  ASSERT_EQ(-1, a);
65
  a = SafeFetchN(bad_addressN, ~patternN);
66
  ASSERT_EQ(~patternN, a);
67
  // Also test nullptr, but not on AIX, where nullptr is readable
68
#ifndef AIX
69
  a = SafeFetchN(nullptr, 0);
70
  ASSERT_EQ(0, a);
71
  a = SafeFetchN(nullptr, ~patternN);
72
  ASSERT_EQ(~patternN, a);
73
#endif
74
}
75

76
static void test_safefetch32_negative() {
77
  int a = SafeFetch32(bad_address32, 0);
78
  ASSERT_EQ(0, a);
79
  a = SafeFetch32(bad_address32, -1);
80
  ASSERT_EQ(-1, a);
81
  a = SafeFetch32(bad_address32, ~pattern32);
82
  ASSERT_EQ(~pattern32, a);
83
  // Also test nullptr, but not on AIX, where nullptr is readable
84
#ifndef AIX
85
  a = SafeFetch32(nullptr, 0);
86
  ASSERT_EQ(0, a);
87
  a = SafeFetch32(nullptr, ~pattern32);
88
  ASSERT_EQ(~pattern32, a);
89
#endif
90
}
91

92
TEST_VM(os, safefetchN_positive) {
93
  test_safefetchN_positive();
94
}
95

96
TEST_VM(os, safefetch32_positive) {
97
  test_safefetch32_positive();
98
}
99

100
TEST_VM(os, safefetchN_negative) {
101
  test_safefetchN_negative();
102
}
103

104
TEST_VM(os, safefetch32_negative) {
105
  test_safefetch32_negative();
106
}
107

108
// Try with Thread::current being nullptr. SafeFetch should work then too.
109
// See JDK-8282475
110

111
class ThreadCurrentNullMark : public StackObj {
112
  Thread* _saved;
113
public:
114
  ThreadCurrentNullMark() {
115
    _saved = Thread::current();
116
    Thread::clear_thread_current();
117
  }
118
  ~ThreadCurrentNullMark() {
119
    _saved->initialize_thread_current();
120
  }
121
};
122

123
TEST_VM(os, safefetchN_positive_current_null) {
124
  ThreadCurrentNullMark tcnmark;
125
  test_safefetchN_positive();
126
}
127

128
TEST_VM(os, safefetch32_positive_current_null) {
129
  ThreadCurrentNullMark tcnmark;
130
  test_safefetch32_positive();
131
}
132

133
TEST_VM(os, safefetchN_negative_current_null) {
134
  ThreadCurrentNullMark tcnmark;
135
  test_safefetchN_negative();
136
}
137

138
TEST_VM(os, safefetch32_negative_current_null) {
139
  ThreadCurrentNullMark tcnmark;
140
  test_safefetch32_negative();
141
}
142

143
class VM_TestSafeFetchAtSafePoint : public VM_GTestExecuteAtSafepoint {
144
public:
145
  void doit() {
146
    // Regression test for JDK-8257828
147
    // Should not crash.
148
    test_safefetchN_negative();
149
  }
150
};
151

152
TEST_VM(os, safefetch_negative_at_safepoint) {
153
  VM_TestSafeFetchAtSafePoint op;
154
  ThreadInVMfromNative invm(JavaThread::current());
155
  VMThread::execute(&op);
156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.