blob: 4a7b1ca6ef04db6fe145a058963ba699c31cf227 [file] [log] [blame]
Ian Rogersf4d4da12014-11-11 16:10:33 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010017#include "jni/jni_internal.h"
Ian Rogersf4d4da12014-11-11 16:10:33 -080018
19#include <pthread.h>
20
21#include "common_runtime_test.h"
Andreas Gampe1b35b462017-09-29 18:52:15 -070022#include "gc/heap.h"
Ian Rogersf4d4da12014-11-11 16:10:33 -080023#include "java_vm_ext.h"
24#include "runtime.h"
25
26namespace art {
27
28class JavaVmExtTest : public CommonRuntimeTest {
29 protected:
Andreas Gampefa6a1b02018-09-07 08:11:55 -070030 void SetUp() override {
Ian Rogersf4d4da12014-11-11 16:10:33 -080031 CommonRuntimeTest::SetUp();
32
33 vm_ = Runtime::Current()->GetJavaVM();
34 }
35
36
Roland Levillainf73caca2018-08-24 17:19:07 +010037 void TearDown() override {
Ian Rogersf4d4da12014-11-11 16:10:33 -080038 CommonRuntimeTest::TearDown();
39 }
40
41 JavaVMExt* vm_;
42};
43
44TEST_F(JavaVmExtTest, JNI_GetDefaultJavaVMInitArgs) {
45 jint err = JNI_GetDefaultJavaVMInitArgs(nullptr);
46 EXPECT_EQ(JNI_ERR, err);
47}
48
49TEST_F(JavaVmExtTest, JNI_GetCreatedJavaVMs) {
50 JavaVM* vms_buf[1];
51 jsize num_vms;
52 jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
53 EXPECT_EQ(JNI_OK, ok);
54 EXPECT_EQ(1, num_vms);
55 EXPECT_EQ(vms_buf[0], vm_);
56}
57
58static bool gSmallStack = false;
59static bool gAsDaemon = false;
60
61static void* attach_current_thread_callback(void* arg ATTRIBUTE_UNUSED) {
62 JavaVM* vms_buf[1];
63 jsize num_vms;
64 JNIEnv* env;
65 jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
66 EXPECT_EQ(JNI_OK, ok);
67 if (ok == JNI_OK) {
68 if (!gAsDaemon) {
69 ok = vms_buf[0]->AttachCurrentThread(&env, nullptr);
70 } else {
71 ok = vms_buf[0]->AttachCurrentThreadAsDaemon(&env, nullptr);
72 }
Andreas Gampe12865492015-01-08 16:43:13 -080073 // TODO: Find a way to test with exact SMALL_STACK value, for which we would bail. The pthreads
74 // spec says that the stack size argument is a lower bound, and bionic currently gives us
75 // a chunk more on arm64.
76 if (!gSmallStack) {
77 EXPECT_EQ(JNI_OK, ok);
78 }
Ian Rogersf4d4da12014-11-11 16:10:33 -080079 if (ok == JNI_OK) {
80 ok = vms_buf[0]->DetachCurrentThread();
81 EXPECT_EQ(JNI_OK, ok);
82 }
83 }
84 return nullptr;
85}
86
87TEST_F(JavaVmExtTest, AttachCurrentThread) {
88 pthread_t pthread;
89 const char* reason = __PRETTY_FUNCTION__;
90 gSmallStack = false;
91 gAsDaemon = false;
92 CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
93 nullptr), reason);
94 void* ret_val;
95 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
96 EXPECT_EQ(ret_val, nullptr);
97}
98
99TEST_F(JavaVmExtTest, AttachCurrentThreadAsDaemon) {
100 pthread_t pthread;
101 const char* reason = __PRETTY_FUNCTION__;
102 gSmallStack = false;
103 gAsDaemon = true;
104 CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
105 nullptr), reason);
106 void* ret_val;
107 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
108 EXPECT_EQ(ret_val, nullptr);
109}
110
111TEST_F(JavaVmExtTest, AttachCurrentThread_SmallStack) {
Andreas Gampe9fdcedd2019-01-28 09:21:38 -0800112 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/123500163
Ian Rogersf4d4da12014-11-11 16:10:33 -0800113 pthread_t pthread;
114 pthread_attr_t attr;
115 const char* reason = __PRETTY_FUNCTION__;
116 gSmallStack = true;
117 gAsDaemon = false;
118 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
119 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, PTHREAD_STACK_MIN), reason);
120 CHECK_PTHREAD_CALL(pthread_create, (&pthread, &attr, attach_current_thread_callback,
121 nullptr), reason);
122 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason);
123 void* ret_val;
124 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
125 EXPECT_EQ(ret_val, nullptr);
126}
127
128TEST_F(JavaVmExtTest, DetachCurrentThread) {
129 JNIEnv* env;
130 jint ok = vm_->AttachCurrentThread(&env, nullptr);
131 ASSERT_EQ(JNI_OK, ok);
132 ok = vm_->DetachCurrentThread();
133 EXPECT_EQ(JNI_OK, ok);
134
135 jint err = vm_->DetachCurrentThread();
136 EXPECT_EQ(JNI_ERR, err);
137}
138
Andreas Gampe1b35b462017-09-29 18:52:15 -0700139class JavaVmExtStackTraceTest : public JavaVmExtTest {
140 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100141 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Andreas Gampe1b35b462017-09-29 18:52:15 -0700142 options->emplace_back("-XX:GlobalRefAllocStackTraceLimit=50000", nullptr);
143 }
144};
145
146TEST_F(JavaVmExtStackTraceTest, TestEnableDisable) {
147 ASSERT_FALSE(Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
148
149 JNIEnv* env;
150 jint ok = vm_->AttachCurrentThread(&env, nullptr);
151 ASSERT_EQ(JNI_OK, ok);
152
153 std::vector<jobject> global_refs_;
154 jobject local_ref = env->NewStringUTF("Dummy");
155 for (size_t i = 0; i < 2000; ++i) {
156 global_refs_.push_back(env->NewGlobalRef(local_ref));
157 }
158
159 EXPECT_TRUE(Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
160
161 for (jobject global_ref : global_refs_) {
162 env->DeleteGlobalRef(global_ref);
163 }
164
165 EXPECT_FALSE(Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
166
Andreas Gampe45485342017-10-11 12:04:35 -0700167 global_refs_.clear();
168 for (size_t i = 0; i < 2000; ++i) {
169 global_refs_.push_back(env->NewGlobalRef(local_ref));
170 }
171
172 EXPECT_TRUE(Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
173
174 for (jobject global_ref : global_refs_) {
175 env->DeleteGlobalRef(global_ref);
176 }
177
178 EXPECT_FALSE(Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
179
Andreas Gampe1b35b462017-09-29 18:52:15 -0700180 ok = vm_->DetachCurrentThread();
181 EXPECT_EQ(JNI_OK, ok);
182}
183
Ian Rogersf4d4da12014-11-11 16:10:33 -0800184} // namespace art