1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jni_internal.h"
#include <pthread.h>
#include "common_runtime_test.h"
#include "java_vm_ext.h"
#include "runtime.h"
namespace art {
class JavaVmExtTest : public CommonRuntimeTest {
protected:
virtual void SetUp() {
CommonRuntimeTest::SetUp();
vm_ = Runtime::Current()->GetJavaVM();
}
virtual void TearDown() OVERRIDE {
CommonRuntimeTest::TearDown();
}
JavaVMExt* vm_;
};
TEST_F(JavaVmExtTest, JNI_GetDefaultJavaVMInitArgs) {
jint err = JNI_GetDefaultJavaVMInitArgs(nullptr);
EXPECT_EQ(JNI_ERR, err);
}
TEST_F(JavaVmExtTest, JNI_GetCreatedJavaVMs) {
JavaVM* vms_buf[1];
jsize num_vms;
jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
EXPECT_EQ(JNI_OK, ok);
EXPECT_EQ(1, num_vms);
EXPECT_EQ(vms_buf[0], vm_);
}
static bool gSmallStack = false;
static bool gAsDaemon = false;
static void* attach_current_thread_callback(void* arg ATTRIBUTE_UNUSED) {
JavaVM* vms_buf[1];
jsize num_vms;
JNIEnv* env;
jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
EXPECT_EQ(JNI_OK, ok);
if (ok == JNI_OK) {
if (!gAsDaemon) {
ok = vms_buf[0]->AttachCurrentThread(&env, nullptr);
} else {
ok = vms_buf[0]->AttachCurrentThreadAsDaemon(&env, nullptr);
}
// TODO: Find a way to test with exact SMALL_STACK value, for which we would bail. The pthreads
// spec says that the stack size argument is a lower bound, and bionic currently gives us
// a chunk more on arm64.
if (!gSmallStack) {
EXPECT_EQ(JNI_OK, ok);
}
if (ok == JNI_OK) {
ok = vms_buf[0]->DetachCurrentThread();
EXPECT_EQ(JNI_OK, ok);
}
}
return nullptr;
}
TEST_F(JavaVmExtTest, AttachCurrentThread) {
pthread_t pthread;
const char* reason = __PRETTY_FUNCTION__;
gSmallStack = false;
gAsDaemon = false;
CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
nullptr), reason);
void* ret_val;
CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
EXPECT_EQ(ret_val, nullptr);
}
TEST_F(JavaVmExtTest, AttachCurrentThreadAsDaemon) {
pthread_t pthread;
const char* reason = __PRETTY_FUNCTION__;
gSmallStack = false;
gAsDaemon = true;
CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
nullptr), reason);
void* ret_val;
CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
EXPECT_EQ(ret_val, nullptr);
}
TEST_F(JavaVmExtTest, AttachCurrentThread_SmallStack) {
pthread_t pthread;
pthread_attr_t attr;
const char* reason = __PRETTY_FUNCTION__;
gSmallStack = true;
gAsDaemon = false;
CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, PTHREAD_STACK_MIN), reason);
CHECK_PTHREAD_CALL(pthread_create, (&pthread, &attr, attach_current_thread_callback,
nullptr), reason);
CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason);
void* ret_val;
CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
EXPECT_EQ(ret_val, nullptr);
}
TEST_F(JavaVmExtTest, DetachCurrentThread) {
JNIEnv* env;
jint ok = vm_->AttachCurrentThread(&env, nullptr);
ASSERT_EQ(JNI_OK, ok);
ok = vm_->DetachCurrentThread();
EXPECT_EQ(JNI_OK, ok);
jint err = vm_->DetachCurrentThread();
EXPECT_EQ(JNI_ERR, err);
}
} // namespace art
|