1 /*
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 
17 #include "jni/jni_internal.h"
18 
19 #include <pthread.h>
20 
21 #include "common_runtime_test.h"
22 #include "gc/heap.h"
23 #include "java_vm_ext.h"
24 #include "runtime.h"
25 
26 namespace art {
27 
28 class JavaVmExtTest : public CommonRuntimeTest {
29  protected:
SetUp()30   void SetUp() override {
31     CommonRuntimeTest::SetUp();
32 
33     vm_ = Runtime::Current()->GetJavaVM();
34   }
35 
36 
TearDown()37   void TearDown() override {
38     CommonRuntimeTest::TearDown();
39   }
40 
41   JavaVMExt* vm_;
42 };
43 
TEST_F(JavaVmExtTest,JNI_GetDefaultJavaVMInitArgs)44 TEST_F(JavaVmExtTest, JNI_GetDefaultJavaVMInitArgs) {
45   jint err = JNI_GetDefaultJavaVMInitArgs(nullptr);
46   EXPECT_EQ(JNI_ERR, err);
47 }
48 
TEST_F(JavaVmExtTest,JNI_GetCreatedJavaVMs)49 TEST_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 
58 static bool gSmallStack = false;
59 static bool gAsDaemon = false;
60 
attach_current_thread_callback(void * arg ATTRIBUTE_UNUSED)61 static 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     }
73     // 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     }
79     if (ok == JNI_OK) {
80       ok = vms_buf[0]->DetachCurrentThread();
81       EXPECT_EQ(JNI_OK, ok);
82     }
83   }
84   return nullptr;
85 }
86 
TEST_F(JavaVmExtTest,AttachCurrentThread)87 TEST_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 
TEST_F(JavaVmExtTest,AttachCurrentThreadAsDaemon)99 TEST_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 
TEST_F(JavaVmExtTest,AttachCurrentThread_SmallStack)111 TEST_F(JavaVmExtTest, AttachCurrentThread_SmallStack) {
112   TEST_DISABLED_FOR_MEMORY_TOOL();  // b/123500163
113   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 
TEST_F(JavaVmExtTest,DetachCurrentThread)128 TEST_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 
139 class JavaVmExtStackTraceTest : public JavaVmExtTest {
140  protected:
SetUpRuntimeOptions(RuntimeOptions * options)141   void SetUpRuntimeOptions(RuntimeOptions* options) override {
142     options->emplace_back("-XX:GlobalRefAllocStackTraceLimit=50000", nullptr);
143   }
144 };
145 
TEST_F(JavaVmExtStackTraceTest,TestEnableDisable)146 TEST_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("Hello");
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 
167   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 
180   ok = vm_->DetachCurrentThread();
181   EXPECT_EQ(JNI_OK, ok);
182 }
183 
184 }  // namespace art
185