blob: fed734ef5eebb6b1e8b7318bf021d9534da2176b [file] [log] [blame]
/*
* 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 <limits.h>
#include <cfloat>
#include <cmath>
#include "common_test.h"
#include "invoke_arg_array_builder.h"
#include "mirror/art_method-inl.h"
#include "mirror/class-inl.h"
#include "mirror/object_array-inl.h"
#include "mirror/object-inl.h"
#include "ScopedLocalRef.h"
#include "sirt_ref.h"
namespace art {
class JniInternalTest : public CommonTest {
protected:
virtual void SetUp() {
CommonTest::SetUp();
vm_ = Runtime::Current()->GetJavaVM();
// Turn on -verbose:jni for the JNI tests.
// gLogVerbosity.jni = true;
vm_->AttachCurrentThread(&env_, NULL);
ScopedLocalRef<jclass> aioobe(env_,
env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"));
CHECK(aioobe.get() != NULL);
aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get()));
ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException"));
CHECK(ase.get() != NULL);
ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get()));
ScopedLocalRef<jclass> sioobe(env_,
env_->FindClass("java/lang/StringIndexOutOfBoundsException"));
CHECK(sioobe.get() != NULL);
sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get()));
}
void CleanUpJniEnv() {
if (aioobe_ != NULL) {
env_->DeleteGlobalRef(aioobe_);
aioobe_ = NULL;
}
if (ase_ != NULL) {
env_->DeleteGlobalRef(ase_);
ase_ = NULL;
}
if (sioobe_ != NULL) {
env_->DeleteGlobalRef(sioobe_);
sioobe_ = NULL;
}
}
virtual void TearDown() {
CleanUpJniEnv();
CommonTest::TearDown();
}
jclass GetPrimitiveClass(char descriptor) {
ScopedObjectAccess soa(env_);
mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor);
CHECK(c != nullptr);
return soa.AddLocalReference<jclass>(c);
}
void JniInternalTestMakeExecutable(mirror::ArtMethod** method,
mirror::Object** receiver,
bool is_static, const char* method_name,
const char* method_signature)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
const char* class_name = is_static ? "StaticLeafMethods" : "NonStaticLeafMethods";
jobject jclass_loader(LoadDex(class_name));
Thread* self = Thread::Current();
SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
SirtRef<mirror::ClassLoader>
class_loader(self,
ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader));
if (is_static) {
MakeExecutable(ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader),
class_name);
} else {
MakeExecutable(nullptr, "java.lang.Class");
MakeExecutable(nullptr, "java.lang.Object");
MakeExecutable(ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader),
class_name);
}
mirror::Class* c = class_linker_->FindClass(DotToDescriptor(class_name).c_str(), class_loader);
CHECK(c != NULL);
*method = is_static ? c->FindDirectMethod(method_name, method_signature)
: c->FindVirtualMethod(method_name, method_signature);
CHECK(method != nullptr);
*receiver = (is_static ? nullptr : c->AllocObject(self));
// Start runtime.
bool started = runtime_->Start();
CHECK(started);
self->TransitionFromSuspendedToRunnable();
}
void InvokeNopMethod(bool is_static) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "nop", "()V");
ArgArray arg_array(NULL, 0);
JValue result;
if (!is_static) {
arg_array.Append(receiver);
}
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'V');
}
void InvokeIdentityByteMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "identity", "(I)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
result.SetB(-1);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'B');
EXPECT_EQ(0, result.GetB());
args[0] = -1;
result.SetB(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'B');
EXPECT_EQ(-1, result.GetB());
args[0] = SCHAR_MAX;
result.SetB(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'B');
EXPECT_EQ(SCHAR_MAX, result.GetB());
args[0] = (SCHAR_MIN << 24) >> 24;
result.SetB(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'B');
EXPECT_EQ(SCHAR_MIN, result.GetB());
}
void InvokeIdentityIntMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "identity", "(I)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
result.SetI(-1);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(0, result.GetI());
args[0] = -1;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-1, result.GetI());
args[0] = INT_MAX;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(INT_MAX, result.GetI());
args[0] = INT_MIN;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(INT_MIN, result.GetI());
}
void InvokeIdentityDoubleMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "identity", "(D)D");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue value;
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
value.SetD(0.0);
arg_array.AppendWide(value.GetJ());
result.SetD(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(0.0, result.GetD());
value.SetD(-1.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(-1.0, result.GetD());
value.SetD(DBL_MAX);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(DBL_MAX, result.GetD());
value.SetD(DBL_MIN);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(DBL_MIN, result.GetD());
}
void InvokeSumIntIntMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(II)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
arg_array.Append(0U);
result.SetI(-1);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(0, result.GetI());
args[0] = 1;
args[1] = 2;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(3, result.GetI());
args[0] = -2;
args[1] = 5;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(3, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MIN;
result.SetI(1234);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-1, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MAX;
result.SetI(INT_MIN);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-2, result.GetI());
}
void InvokeSumIntIntIntMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(III)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
result.SetI(-1);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(0, result.GetI());
args[0] = 1;
args[1] = 2;
args[2] = 3;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(6, result.GetI());
args[0] = -1;
args[1] = 2;
args[2] = -3;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-2, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MIN;
args[2] = INT_MAX;
result.SetI(1234);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(2147483646, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MAX;
args[2] = INT_MAX;
result.SetI(INT_MIN);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(2147483645, result.GetI());
}
void InvokeSumIntIntIntIntMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(IIII)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
result.SetI(-1);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(0, result.GetI());
args[0] = 1;
args[1] = 2;
args[2] = 3;
args[3] = 4;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(10, result.GetI());
args[0] = -1;
args[1] = 2;
args[2] = -3;
args[3] = 4;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(2, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MIN;
args[2] = INT_MAX;
args[3] = INT_MIN;
result.SetI(1234);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-2, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MAX;
args[2] = INT_MAX;
args[3] = INT_MAX;
result.SetI(INT_MIN);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-4, result.GetI());
}
void InvokeSumIntIntIntIntIntMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(IIIII)I");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
arg_array.Append(0U);
result.SetI(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(0, result.GetI());
args[0] = 1;
args[1] = 2;
args[2] = 3;
args[3] = 4;
args[4] = 5;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(15, result.GetI());
args[0] = -1;
args[1] = 2;
args[2] = -3;
args[3] = 4;
args[4] = -5;
result.SetI(0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(-3, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MIN;
args[2] = INT_MAX;
args[3] = INT_MIN;
args[4] = INT_MAX;
result.SetI(1234);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(2147483645, result.GetI());
args[0] = INT_MAX;
args[1] = INT_MAX;
args[2] = INT_MAX;
args[3] = INT_MAX;
args[4] = INT_MAX;
result.SetI(INT_MIN);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'I');
EXPECT_EQ(2147483643, result.GetI());
}
void InvokeSumDoubleDoubleMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(DD)D");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue value;
JValue value2;
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
value.SetD(0.0);
value2.SetD(0.0);
arg_array.AppendWide(value.GetJ());
arg_array.AppendWide(value2.GetJ());
result.SetD(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(0.0, result.GetD());
value.SetD(1.0);
value2.SetD(2.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(3.0, result.GetD());
value.SetD(1.0);
value2.SetD(-2.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(-1.0, result.GetD());
value.SetD(DBL_MAX);
value2.SetD(DBL_MIN);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(1.7976931348623157e308, result.GetD());
value.SetD(DBL_MAX);
value2.SetD(DBL_MAX);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(INFINITY, result.GetD());
}
void InvokeSumDoubleDoubleDoubleMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDD)D");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue value;
JValue value2;
JValue value3;
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
value.SetD(0.0);
value2.SetD(0.0);
value3.SetD(0.0);
arg_array.AppendWide(value.GetJ());
arg_array.AppendWide(value2.GetJ());
arg_array.AppendWide(value3.GetJ());
result.SetD(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(0.0, result.GetD());
value.SetD(1.0);
value2.SetD(2.0);
value3.SetD(3.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(6.0, result.GetD());
value.SetD(1.0);
value2.SetD(-2.0);
value3.SetD(3.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(2.0, result.GetD());
}
void InvokeSumDoubleDoubleDoubleDoubleMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDDD)D");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue value;
JValue value2;
JValue value3;
JValue value4;
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
value.SetD(0.0);
value2.SetD(0.0);
value3.SetD(0.0);
value4.SetD(0.0);
arg_array.AppendWide(value.GetJ());
arg_array.AppendWide(value2.GetJ());
arg_array.AppendWide(value3.GetJ());
arg_array.AppendWide(value4.GetJ());
result.SetD(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(0.0, result.GetD());
value.SetD(1.0);
value2.SetD(2.0);
value3.SetD(3.0);
value4.SetD(4.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
args[6] = value4.GetJ();
args[7] = value4.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(10.0, result.GetD());
value.SetD(1.0);
value2.SetD(-2.0);
value3.SetD(3.0);
value4.SetD(-4.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
args[6] = value4.GetJ();
args[7] = value4.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(-2.0, result.GetD());
}
void InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(bool is_static)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
mirror::ArtMethod* method;
mirror::Object* receiver;
JniInternalTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDDDD)D");
ArgArray arg_array(NULL, 0);
uint32_t* args = arg_array.GetArray();
JValue value;
JValue value2;
JValue value3;
JValue value4;
JValue value5;
JValue result;
if (!is_static) {
arg_array.Append(receiver);
args++;
}
value.SetD(0.0);
value2.SetD(0.0);
value3.SetD(0.0);
value4.SetD(0.0);
value5.SetD(0.0);
arg_array.AppendWide(value.GetJ());
arg_array.AppendWide(value2.GetJ());
arg_array.AppendWide(value3.GetJ());
arg_array.AppendWide(value4.GetJ());
arg_array.AppendWide(value5.GetJ());
result.SetD(-1.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(0.0, result.GetD());
value.SetD(1.0);
value2.SetD(2.0);
value3.SetD(3.0);
value4.SetD(4.0);
value5.SetD(5.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
args[6] = value4.GetJ();
args[7] = value4.GetJ() >> 32;
args[8] = value5.GetJ();
args[9] = value5.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(15.0, result.GetD());
value.SetD(1.0);
value2.SetD(-2.0);
value3.SetD(3.0);
value4.SetD(-4.0);
value5.SetD(5.0);
args[0] = value.GetJ();
args[1] = value.GetJ() >> 32;
args[2] = value2.GetJ();
args[3] = value2.GetJ() >> 32;
args[4] = value3.GetJ();
args[5] = value3.GetJ() >> 32;
args[6] = value4.GetJ();
args[7] = value4.GetJ() >> 32;
args[8] = value5.GetJ();
args[9] = value5.GetJ() >> 32;
result.SetD(0.0);
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'D');
EXPECT_EQ(3.0, result.GetD());
}
JavaVMExt* vm_;
JNIEnv* env_;
jclass aioobe_;
jclass ase_;
jclass sioobe_;
};
TEST_F(JniInternalTest, AllocObject) {
jclass c = env_->FindClass("java/lang/String");
ASSERT_TRUE(c != NULL);
jobject o = env_->AllocObject(c);
ASSERT_TRUE(o != NULL);
// We have an instance of the class we asked for...
ASSERT_TRUE(env_->IsInstanceOf(o, c));
// ...whose fields haven't been initialized because
// we didn't call a constructor.
ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I")));
ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I")));
ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL);
}
TEST_F(JniInternalTest, GetVersion) {
ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
}
#define EXPECT_CLASS_FOUND(NAME) \
EXPECT_TRUE(env_->FindClass(NAME) != NULL); \
EXPECT_FALSE(env_->ExceptionCheck())
#define EXPECT_CLASS_NOT_FOUND(NAME) \
EXPECT_TRUE(env_->FindClass(NAME) == NULL); \
EXPECT_TRUE(env_->ExceptionCheck()); \
env_->ExceptionClear()
TEST_F(JniInternalTest, FindClass) {
// Reference types...
EXPECT_CLASS_FOUND("java/lang/String");
// ...for arrays too, where you must include "L;".
EXPECT_CLASS_FOUND("[Ljava/lang/String;");
// Primitive arrays are okay too, if the primitive type is valid.
EXPECT_CLASS_FOUND("[C");
{
// We support . as well as / for compatibility, if -Xcheck:jni is off.
CheckJniAbortCatcher check_jni_abort_catcher;
EXPECT_CLASS_FOUND("java.lang.String");
check_jni_abort_catcher.Check("illegal class name 'java.lang.String'");
EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;");
check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'");
EXPECT_CLASS_FOUND("[Ljava.lang.String;");
check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'");
EXPECT_CLASS_NOT_FOUND("[java.lang.String");
check_jni_abort_catcher.Check("illegal class name '[java.lang.String'");
// You can't include the "L;" in a JNI class descriptor.
EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;");
check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'");
// But you must include it for an array of any reference type.
EXPECT_CLASS_NOT_FOUND("[java/lang/String");
check_jni_abort_catcher.Check("illegal class name '[java/lang/String'");
EXPECT_CLASS_NOT_FOUND("[K");
check_jni_abort_catcher.Check("illegal class name '[K'");
}
// But primitive types aren't allowed...
EXPECT_CLASS_NOT_FOUND("C");
EXPECT_CLASS_NOT_FOUND("K");
}
#define EXPECT_EXCEPTION(exception_class) \
do { \
EXPECT_TRUE(env_->ExceptionCheck()); \
jthrowable exception = env_->ExceptionOccurred(); \
EXPECT_NE(static_cast<jthrowable>(NULL), exception); \
env_->ExceptionClear(); \
EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \
} while (false)
TEST_F(JniInternalTest, GetFieldID) {
jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
ASSERT_TRUE(jlnsfe != NULL);
jclass c = env_->FindClass("java/lang/String");
ASSERT_TRUE(c != NULL);
// Wrong type.
jfieldID fid = env_->GetFieldID(c, "count", "J");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Wrong type where type doesn't exist.
fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Wrong name.
fid = env_->GetFieldID(c, "Count", "I");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Good declared field lookup.
fid = env_->GetFieldID(c, "count", "I");
EXPECT_NE(static_cast<jfieldID>(NULL), fid);
EXPECT_TRUE(fid != NULL);
EXPECT_FALSE(env_->ExceptionCheck());
// Good superclass field lookup.
c = env_->FindClass("java/lang/StringBuilder");
fid = env_->GetFieldID(c, "count", "I");
EXPECT_NE(static_cast<jfieldID>(NULL), fid);
EXPECT_TRUE(fid != NULL);
EXPECT_FALSE(env_->ExceptionCheck());
// Not instance.
fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
}
TEST_F(JniInternalTest, GetStaticFieldID) {
jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
ASSERT_TRUE(jlnsfe != NULL);
jclass c = env_->FindClass("java/lang/String");
ASSERT_TRUE(c != NULL);
// Wrong type.
jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Wrong type where type doesn't exist.
fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Wrong name.
fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
// Good declared field lookup.
fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
EXPECT_NE(static_cast<jfieldID>(NULL), fid);
EXPECT_TRUE(fid != NULL);
EXPECT_FALSE(env_->ExceptionCheck());
// Not static.
fid = env_->GetStaticFieldID(c, "count", "I");
EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
EXPECT_EXCEPTION(jlnsfe);
}
TEST_F(JniInternalTest, GetMethodID) {
jclass jlobject = env_->FindClass("java/lang/Object");
jclass jlstring = env_->FindClass("java/lang/String");
jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
// Sanity check that no exceptions are pending
ASSERT_FALSE(env_->ExceptionCheck());
// Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
// a pending exception
jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
EXPECT_EQ(static_cast<jmethodID>(NULL), method);
EXPECT_EXCEPTION(jlnsme);
// Check that java.lang.Object.equals() does exist
method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
EXPECT_NE(static_cast<jmethodID>(NULL), method);
EXPECT_FALSE(env_->ExceptionCheck());
// Check that GetMethodID for java.lang.String.valueOf(int) fails as the
// method is static
method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
EXPECT_EQ(static_cast<jmethodID>(NULL), method);
EXPECT_EXCEPTION(jlnsme);
// Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor
method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V");
EXPECT_NE(static_cast<jmethodID>(NULL), method);
EXPECT_FALSE(env_->ExceptionCheck());
}
TEST_F(JniInternalTest, GetStaticMethodID) {
jclass jlobject = env_->FindClass("java/lang/Object");
jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
// Sanity check that no exceptions are pending
ASSERT_FALSE(env_->ExceptionCheck());
// Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
// a pending exception
jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
EXPECT_EQ(static_cast<jmethodID>(NULL), method);
EXPECT_EXCEPTION(jlnsme);
// Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
// the method is not static
method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
EXPECT_EQ(static_cast<jmethodID>(NULL), method);
EXPECT_EXCEPTION(jlnsme);
// Check that java.lang.String.valueOf(int) does exist
jclass jlstring = env_->FindClass("java/lang/String");
method = env_->GetStaticMethodID(jlstring, "valueOf",
"(I)Ljava/lang/String;");
EXPECT_NE(static_cast<jmethodID>(NULL), method);
EXPECT_FALSE(env_->ExceptionCheck());
}
TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
jclass jlrField = env_->FindClass("java/lang/reflect/Field");
jclass c = env_->FindClass("java/lang/String");
ASSERT_TRUE(c != NULL);
jfieldID fid = env_->GetFieldID(c, "count", "I");
ASSERT_TRUE(fid != NULL);
// Turn the fid into a java.lang.reflect.Field...
jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
ASSERT_TRUE(c != NULL);
ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
// ...and back again.
jfieldID fid2 = env_->FromReflectedField(field);
ASSERT_TRUE(fid2 != NULL);
// Make sure we can actually use it.
jstring s = env_->NewStringUTF("poop");
ASSERT_EQ(4, env_->GetIntField(s, fid2));
}
TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
jclass c = env_->FindClass("java/lang/String");
ASSERT_TRUE(c != NULL);
jmethodID mid = env_->GetMethodID(c, "length", "()I");
ASSERT_TRUE(mid != NULL);
// Turn the mid into a java.lang.reflect.Method...
jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
ASSERT_TRUE(c != NULL);
ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
// ...and back again.
jmethodID mid2 = env_->FromReflectedMethod(method);
ASSERT_TRUE(mid2 != NULL);
// Make sure we can actually use it.
jstring s = env_->NewStringUTF("poop");
// TODO: this should return 4, but the runtime skips the method
// invoke because the runtime isn't started. In the future it would
// be nice to use interpretter for things like this. This still does
// validate that we have a sane jmethodID value.
ASSERT_EQ(0, env_->CallIntMethod(s, mid2));
}
void BogusMethod() {
// You can't pass NULL function pointers to RegisterNatives.
}
TEST_F(JniInternalTest, RegisterNatives) {
jclass jlobject = env_->FindClass("java/lang/Object");
jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
// Sanity check that no exceptions are pending
ASSERT_FALSE(env_->ExceptionCheck());
// Check that registering to a non-existent java.lang.Object.foo() causes a
// NoSuchMethodError
{
JNINativeMethod methods[] = { { "foo", "()V", NULL } };
env_->RegisterNatives(jlobject, methods, 1);
}
EXPECT_EXCEPTION(jlnsme);
// Check that registering non-native methods causes a NoSuchMethodError
{
JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } };
env_->RegisterNatives(jlobject, methods, 1);
}
EXPECT_EXCEPTION(jlnsme);
// Check that registering native methods is successful
{
JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } };
env_->RegisterNatives(jlobject, methods, 1);
}
EXPECT_FALSE(env_->ExceptionCheck());
env_->UnregisterNatives(jlobject);
}
#define EXPECT_PRIMITIVE_ARRAY(new_fn, \
get_region_fn, \
set_region_fn, \
get_elements_fn, \
release_elements_fn, \
scalar_type, \
expected_class_descriptor) \
{ \
CheckJniAbortCatcher jni_abort_catcher; \
/* Allocate an negative sized array and check it has the right failure type. */ \
env_->new_fn(-1); \
jni_abort_catcher.Check("negative array length: -1"); \
env_->new_fn(std::numeric_limits<jint>::min()); \
jni_abort_catcher.Check("negative array length: -2147483648"); \
} \
jsize size = 4; \
\
/* Allocate an array and check it has the right type and length. */ \
scalar_type ## Array a = env_->new_fn(size); \
EXPECT_TRUE(a != NULL); \
EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \
EXPECT_EQ(size, env_->GetArrayLength(a)); \
\
/* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \
/* AIOOBE for negative start offset. */ \
env_->get_region_fn(a, -1, 1, NULL); \
EXPECT_EXCEPTION(aioobe_); \
env_->set_region_fn(a, -1, 1, NULL); \
EXPECT_EXCEPTION(aioobe_); \
\
/* AIOOBE for negative length. */ \
env_->get_region_fn(a, 0, -1, NULL); \
EXPECT_EXCEPTION(aioobe_); \
env_->set_region_fn(a, 0, -1, NULL); \
EXPECT_EXCEPTION(aioobe_); \
\
/* AIOOBE for buffer overrun. */ \
env_->get_region_fn(a, size - 1, size, NULL); \
EXPECT_EXCEPTION(aioobe_); \
env_->set_region_fn(a, size - 1, size, NULL); \
EXPECT_EXCEPTION(aioobe_); \
\
/* It's okay for the buffer to be NULL as long as the length is 0. */ \
env_->get_region_fn(a, 2, 0, NULL); \
/* Even if the offset is invalid... */ \
env_->get_region_fn(a, 123, 0, NULL); \
EXPECT_EXCEPTION(aioobe_); \
\
/* It's okay for the buffer to be NULL as long as the length is 0. */ \
env_->set_region_fn(a, 2, 0, NULL); \
/* Even if the offset is invalid... */ \
env_->set_region_fn(a, 123, 0, NULL); \
EXPECT_EXCEPTION(aioobe_); \
\
/* Prepare a couple of buffers. */ \
UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \
UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \
for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \
for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \
\
/* Copy all of src_buf onto the heap. */ \
env_->set_region_fn(a, 0, size, &src_buf[0]); \
/* Copy back only part. */ \
env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
<< "short copy equal"; \
/* Copy the missing pieces. */ \
env_->get_region_fn(a, 0, 1, &dst_buf[0]); \
env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
<< "fixed copy not equal"; \
/* Copy back the whole array. */ \
env_->get_region_fn(a, 0, size, &dst_buf[0]); \
EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
<< "full copy not equal"; \
/* GetPrimitiveArrayCritical */ \
void* v = env_->GetPrimitiveArrayCritical(a, NULL); \
EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \
<< "GetPrimitiveArrayCritical not equal"; \
env_->ReleasePrimitiveArrayCritical(a, v, 0); \
/* GetXArrayElements */ \
scalar_type* xs = env_->get_elements_fn(a, NULL); \
EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \
<< # get_elements_fn " not equal"; \
env_->release_elements_fn(a, xs, 0); \
TEST_F(JniInternalTest, BooleanArrays) {
EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion,
GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
}
TEST_F(JniInternalTest, ByteArrays) {
EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion,
GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
}
TEST_F(JniInternalTest, CharArrays) {
EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion,
GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
}
TEST_F(JniInternalTest, DoubleArrays) {
EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion,
GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
}
TEST_F(JniInternalTest, FloatArrays) {
EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion,
GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
}
TEST_F(JniInternalTest, IntArrays) {
EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion,
GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
}
TEST_F(JniInternalTest, LongArrays) {
EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion,
GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
}
TEST_F(JniInternalTest, ShortArrays) {
EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion,
GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
}
TEST_F(JniInternalTest, NewObjectArray) {
jclass element_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(element_class != nullptr);
jclass array_class = env_->FindClass("[Ljava/lang/String;");
ASSERT_TRUE(array_class != nullptr);
jobjectArray a = env_->NewObjectArray(0, element_class, nullptr);
EXPECT_TRUE(a != nullptr);
EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
EXPECT_EQ(0, env_->GetArrayLength(a));
a = env_->NewObjectArray(1, element_class, nullptr);
EXPECT_TRUE(a != nullptr);
EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
EXPECT_EQ(1, env_->GetArrayLength(a));
EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr));
}
TEST_F(JniInternalTest, NewObjectArrayWithNegativeLength) {
jclass element_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(element_class != nullptr);
jclass array_class = env_->FindClass("[Ljava/lang/String;");
ASSERT_TRUE(array_class != nullptr);
CheckJniAbortCatcher jni_abort_catcher;
env_->NewObjectArray(-1, element_class, nullptr);
jni_abort_catcher.Check("negative array length: -1");
env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr);
jni_abort_catcher.Check("negative array length: -2147483648");
}
TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) {
const char* primitive_descriptors = "VZBSCIJFD";
const char* primitive_names[] = {
"void", "boolean", "byte", "short", "char", "int", "long", "float", "double"
};
ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names));
CheckJniAbortCatcher jni_abort_catcher;
for (size_t i = 0; i < strlen(primitive_descriptors); ++i) {
jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]);
env_->NewObjectArray(1, primitive_class, nullptr);
std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i]));
jni_abort_catcher.Check(error_msg.c_str());
}
}
TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) {
jclass element_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(element_class != nullptr);
jclass array_class = env_->FindClass("[Ljava/lang/String;");
ASSERT_TRUE(array_class != nullptr);
jstring s = env_->NewStringUTF("poop");
jobjectArray a = env_->NewObjectArray(2, element_class, s);
EXPECT_TRUE(a != nullptr);
EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
EXPECT_EQ(2, env_->GetArrayLength(a));
EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s));
EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s));
// Attempt to incorrect create an array of strings with initial value of string arrays.
CheckJniAbortCatcher jni_abort_catcher;
env_->NewObjectArray(2, element_class, a);
jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element "
"type of 'java.lang.String'");
}
TEST_F(JniInternalTest, GetArrayLength) {
// Already tested in NewObjectArray/NewPrimitiveArray.
}
TEST_F(JniInternalTest, GetObjectClass) {
jclass string_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(string_class != NULL);
jclass class_class = env_->FindClass("java/lang/Class");
ASSERT_TRUE(class_class != NULL);
jstring s = env_->NewStringUTF("poop");
jclass c = env_->GetObjectClass(s);
ASSERT_TRUE(env_->IsSameObject(string_class, c));
jclass c2 = env_->GetObjectClass(c);
ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2)));
}
TEST_F(JniInternalTest, GetSuperclass) {
jclass object_class = env_->FindClass("java/lang/Object");
ASSERT_TRUE(object_class != NULL);
jclass string_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(string_class != NULL);
jclass runnable_interface = env_->FindClass("java/lang/Runnable");
ASSERT_TRUE(runnable_interface != NULL);
ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL);
ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface)));
}
TEST_F(JniInternalTest, IsAssignableFrom) {
jclass object_class = env_->FindClass("java/lang/Object");
ASSERT_TRUE(object_class != NULL);
jclass string_class = env_->FindClass("java/lang/String");
ASSERT_TRUE(string_class != NULL);
ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class));
ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class));
}
TEST_F(JniInternalTest, GetObjectRefType) {
jclass local = env_->FindClass("java/lang/Object");
ASSERT_TRUE(local != NULL);
EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local));
jobject global = env_->NewGlobalRef(local);
EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global));
jweak weak_global = env_->NewWeakGlobalRef(local);
EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global));
jobject invalid = reinterpret_cast<jobject>(this);
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid));
// TODO: invoke a native method and test that its arguments are considered local references.
}
TEST_F(JniInternalTest, StaleWeakGlobal) {
jclass java_lang_Class = env_->FindClass("java/lang/Class");
ASSERT_TRUE(java_lang_Class != NULL);
jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, NULL);
ASSERT_TRUE(local_ref != NULL);
jweak weak_global = env_->NewWeakGlobalRef(local_ref);
ASSERT_TRUE(weak_global != NULL);
env_->DeleteLocalRef(local_ref);
Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global.
jobject new_global_ref = env_->NewGlobalRef(weak_global);
EXPECT_TRUE(new_global_ref == NULL);
jobject new_local_ref = env_->NewLocalRef(weak_global);
EXPECT_TRUE(new_local_ref == NULL);
}
TEST_F(JniInternalTest, NewStringUTF) {
EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL);
jstring s;
s = env_->NewStringUTF("");
EXPECT_TRUE(s != NULL);
EXPECT_EQ(0, env_->GetStringLength(s));
EXPECT_EQ(0, env_->GetStringUTFLength(s));
s = env_->NewStringUTF("hello");
EXPECT_TRUE(s != NULL);
EXPECT_EQ(5, env_->GetStringLength(s));
EXPECT_EQ(5, env_->GetStringUTFLength(s));
// TODO: check some non-ASCII strings.
}
TEST_F(JniInternalTest, NewString) {
jchar chars[] = { 'h', 'i' };
jstring s;
s = env_->NewString(chars, 0);
EXPECT_TRUE(s != NULL);
EXPECT_EQ(0, env_->GetStringLength(s));
EXPECT_EQ(0, env_->GetStringUTFLength(s));
s = env_->NewString(chars, 2);
EXPECT_TRUE(s != NULL);
EXPECT_EQ(2, env_->GetStringLength(s));
EXPECT_EQ(2, env_->GetStringUTFLength(s));
// TODO: check some non-ASCII strings.
}
TEST_F(JniInternalTest, NewStringNullCharsZeroLength) {
jstring s = env_->NewString(nullptr, 0);
EXPECT_TRUE(s != nullptr);
EXPECT_EQ(0, env_->GetStringLength(s));
}
TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) {
CheckJniAbortCatcher jni_abort_catcher;
env_->NewString(nullptr, 1);
jni_abort_catcher.Check("chars == null && char_count > 0");
}
TEST_F(JniInternalTest, NewStringNegativeLength) {
CheckJniAbortCatcher jni_abort_catcher;
env_->NewString(nullptr, -1);
jni_abort_catcher.Check("char_count < 0: -1");
env_->NewString(nullptr, std::numeric_limits<jint>::min());
jni_abort_catcher.Check("char_count < 0: -2147483648");
}
TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
// Already tested in the NewString/NewStringUTF tests.
}
TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) {
jstring s = env_->NewStringUTF("hello");
ASSERT_TRUE(s != NULL);
env_->GetStringRegion(s, -1, 0, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringRegion(s, 0, -1, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringRegion(s, 0, 10, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringRegion(s, 10, 1, NULL);
EXPECT_EXCEPTION(sioobe_);
jchar chars[4] = { 'x', 'x', 'x', 'x' };
env_->GetStringRegion(s, 1, 2, &chars[1]);
EXPECT_EQ('x', chars[0]);
EXPECT_EQ('e', chars[1]);
EXPECT_EQ('l', chars[2]);
EXPECT_EQ('x', chars[3]);
// It's okay for the buffer to be NULL as long as the length is 0.
env_->GetStringRegion(s, 2, 0, NULL);
// Even if the offset is invalid...
env_->GetStringRegion(s, 123, 0, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringUTFRegion(s, -1, 0, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringUTFRegion(s, 0, -1, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringUTFRegion(s, 0, 10, NULL);
EXPECT_EXCEPTION(sioobe_);
env_->GetStringUTFRegion(s, 10, 1, NULL);
EXPECT_EXCEPTION(sioobe_);
char bytes[4] = { 'x', 'x', 'x', 'x' };
env_->GetStringUTFRegion(s, 1, 2, &bytes[1]);
EXPECT_EQ('x', bytes[0]);
EXPECT_EQ('e', bytes[1]);
EXPECT_EQ('l', bytes[2]);
EXPECT_EQ('x', bytes[3]);
// It's okay for the buffer to be NULL as long as the length is 0.
env_->GetStringUTFRegion(s, 2, 0, NULL);
// Even if the offset is invalid...
env_->GetStringUTFRegion(s, 123, 0, NULL);
EXPECT_EXCEPTION(sioobe_);
}
TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
// Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni.
{
CheckJniAbortCatcher check_jni_abort_catcher;
EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL);
check_jni_abort_catcher.Check("GetStringUTFChars received null jstring");
}
jstring s = env_->NewStringUTF("hello");
ASSERT_TRUE(s != NULL);
const char* utf = env_->GetStringUTFChars(s, NULL);
EXPECT_STREQ("hello", utf);
env_->ReleaseStringUTFChars(s, utf);
jboolean is_copy = JNI_FALSE;
utf = env_->GetStringUTFChars(s, &is_copy);
EXPECT_EQ(JNI_TRUE, is_copy);
EXPECT_STREQ("hello", utf);
env_->ReleaseStringUTFChars(s, utf);
}
TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) {
jstring s = env_->NewStringUTF("hello");
ASSERT_TRUE(s != NULL);
jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
const jchar* chars = env_->GetStringChars(s, NULL);
EXPECT_EQ(expected[0], chars[0]);
EXPECT_EQ(expected[1], chars[1]);
EXPECT_EQ(expected[2], chars[2]);
EXPECT_EQ(expected[3], chars[3]);
EXPECT_EQ(expected[4], chars[4]);
env_->ReleaseStringChars(s, chars);
jboolean is_copy = JNI_FALSE;
chars = env_->GetStringChars(s, &is_copy);
EXPECT_EQ(JNI_TRUE, is_copy);
EXPECT_EQ(expected[0], chars[0]);
EXPECT_EQ(expected[1], chars[1]);
EXPECT_EQ(expected[2], chars[2]);
EXPECT_EQ(expected[3], chars[3]);
EXPECT_EQ(expected[4], chars[4]);
env_->ReleaseStringChars(s, chars);
}
TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) {
jstring s = env_->NewStringUTF("hello");
ASSERT_TRUE(s != NULL);
jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
const jchar* chars = env_->GetStringCritical(s, NULL);
EXPECT_EQ(expected[0], chars[0]);
EXPECT_EQ(expected[1], chars[1]);
EXPECT_EQ(expected[2], chars[2]);
EXPECT_EQ(expected[3], chars[3]);
EXPECT_EQ(expected[4], chars[4]);
env_->ReleaseStringCritical(s, chars);
jboolean is_copy = JNI_FALSE;
chars = env_->GetStringCritical(s, &is_copy);
// TODO: Fix GetStringCritical to use the same mechanism as GetPrimitiveArrayElementsCritical.
EXPECT_EQ(JNI_TRUE, is_copy);
EXPECT_EQ(expected[0], chars[0]);
EXPECT_EQ(expected[1], chars[1]);
EXPECT_EQ(expected[2], chars[2]);
EXPECT_EQ(expected[3], chars[3]);
EXPECT_EQ(expected[4], chars[4]);
env_->ReleaseStringCritical(s, chars);
}
TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
jclass java_lang_Class = env_->FindClass("java/lang/Class");
ASSERT_TRUE(java_lang_Class != NULL);
jobjectArray array = env_->NewObjectArray(1, java_lang_Class, NULL);
EXPECT_TRUE(array != NULL);
EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL);
env_->SetObjectArrayElement(array, 0, java_lang_Class);
EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class));
// ArrayIndexOutOfBounds for negative index.
env_->SetObjectArrayElement(array, -1, java_lang_Class);
EXPECT_EXCEPTION(aioobe_);
// ArrayIndexOutOfBounds for too-large index.
env_->SetObjectArrayElement(array, 1, java_lang_Class);
EXPECT_EXCEPTION(aioobe_);
// ArrayStoreException thrown for bad types.
env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!"));
EXPECT_EXCEPTION(ase_);
}
#define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \
do { \
jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \
EXPECT_TRUE(fid != NULL); \
env_->SetStatic ## type ## Field(c, fid, value1); \
EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \
env_->SetStatic ## type ## Field(c, fid, value2); \
EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \
} while (false)
#define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \
do { \
jfieldID fid = env_->GetFieldID(c, field_name, sig); \
EXPECT_TRUE(fid != NULL); \
env_->Set ## type ## Field(instance, fid, value1); \
EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \
env_->Set ## type ## Field(instance, fid, value2); \
EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \
} while (false)
TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
TEST_DISABLED_FOR_PORTABLE();
Thread::Current()->TransitionFromSuspendedToRunnable();
LoadDex("AllFields");
bool started = runtime_->Start();
CHECK(started);
jclass c = env_->FindClass("AllFields");
ASSERT_TRUE(c != NULL);
jobject o = env_->AllocObject(c);
ASSERT_TRUE(o != NULL);
EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false);
EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2);
EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b');
EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0);
EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0);
EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2);
EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2);
EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2);
EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false);
EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2);
EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b');
EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0);
EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0);
EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2);
EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2);
EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2);
}
TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
TEST_DISABLED_FOR_PORTABLE();
Thread::Current()->TransitionFromSuspendedToRunnable();
LoadDex("AllFields");
runtime_->Start();
jclass c = env_->FindClass("AllFields");
ASSERT_TRUE(c != NULL);
jobject o = env_->AllocObject(c);
ASSERT_TRUE(o != NULL);
jstring s1 = env_->NewStringUTF("hello");
ASSERT_TRUE(s1 != NULL);
jstring s2 = env_->NewStringUTF("world");
ASSERT_TRUE(s2 != NULL);
jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;");
ASSERT_TRUE(s_fid != NULL);
jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;");
ASSERT_TRUE(i_fid != NULL);
env_->SetStaticObjectField(c, s_fid, s1);
ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid)));
env_->SetStaticObjectField(c, s_fid, s2);
ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid)));
env_->SetObjectField(o, i_fid, s1);
ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid)));
env_->SetObjectField(o, i_fid, s2);
ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid)));
}
TEST_F(JniInternalTest, NewLocalRef_NULL) {
EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL);
}
TEST_F(JniInternalTest, NewLocalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewLocalRef(s);
EXPECT_TRUE(o != NULL);
EXPECT_TRUE(o != s);
EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o));
}
TEST_F(JniInternalTest, DeleteLocalRef_NULL) {
env_->DeleteLocalRef(NULL);
}
TEST_F(JniInternalTest, DeleteLocalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
env_->DeleteLocalRef(s);
// Currently, deleting an already-deleted reference is just a CheckJNI warning.
{
CheckJniAbortCatcher check_jni_abort_catcher;
env_->DeleteLocalRef(s);
std::string expected(StringPrintf("native code passing in reference to "
"invalid local reference: %p", s));
check_jni_abort_catcher.Check(expected.c_str());
}
s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewLocalRef(s);
ASSERT_TRUE(o != NULL);
env_->DeleteLocalRef(s);
env_->DeleteLocalRef(o);
}
TEST_F(JniInternalTest, PushLocalFrame_10395422) {
// The JNI specification is ambiguous about whether the given capacity is to be interpreted as a
// maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how
// Android historically treated it, and it's how the RI treats it. It's also the more useful
// interpretation!
ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0));
env_->PopLocalFrame(NULL);
// Negative capacities are not allowed.
ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1));
// And it's okay to have an upper limit. Ours is currently 512.
ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192));
}
TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) {
jobject original = env_->NewStringUTF("");
ASSERT_TRUE(original != NULL);
jobject outer;
jobject inner1, inner2;
ScopedObjectAccess soa(env_);
mirror::Object* inner2_direct_pointer;
{
ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
outer = env_->NewLocalRef(original);
{
ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
inner1 = env_->NewLocalRef(outer);
inner2 = env_->NewStringUTF("survivor");
inner2_direct_pointer = soa.Decode<mirror::Object*>(inner2);
env_->PopLocalFrame(inner2);
}
EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer));
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
// Our local reference for the survivor is invalid because the survivor
// gets a new local reference...
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
// ...but the survivor should be in the local reference table.
JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(env_);
EXPECT_TRUE(env->locals.ContainsDirectPointer(inner2_direct_pointer));
env_->PopLocalFrame(NULL);
}
EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer));
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
}
TEST_F(JniInternalTest, NewGlobalRef_NULL) {
EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL);
}
TEST_F(JniInternalTest, NewGlobalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewGlobalRef(s);
EXPECT_TRUE(o != NULL);
EXPECT_TRUE(o != s);
// TODO: check that o is a global reference.
}
TEST_F(JniInternalTest, DeleteGlobalRef_NULL) {
env_->DeleteGlobalRef(NULL);
}
TEST_F(JniInternalTest, DeleteGlobalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewGlobalRef(s);
ASSERT_TRUE(o != NULL);
env_->DeleteGlobalRef(o);
// Currently, deleting an already-deleted reference is just a CheckJNI warning.
{
CheckJniAbortCatcher check_jni_abort_catcher;
env_->DeleteGlobalRef(o);
std::string expected(StringPrintf("native code passing in reference to "
"invalid global reference: %p", o));
check_jni_abort_catcher.Check(expected.c_str());
}
jobject o1 = env_->NewGlobalRef(s);
ASSERT_TRUE(o1 != NULL);
jobject o2 = env_->NewGlobalRef(s);
ASSERT_TRUE(o2 != NULL);
env_->DeleteGlobalRef(o1);
env_->DeleteGlobalRef(o2);
}
TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) {
EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL);
}
TEST_F(JniInternalTest, NewWeakGlobalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewWeakGlobalRef(s);
EXPECT_TRUE(o != NULL);
EXPECT_TRUE(o != s);
// TODO: check that o is a weak global reference.
}
TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) {
env_->DeleteWeakGlobalRef(NULL);
}
TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
jstring s = env_->NewStringUTF("");
ASSERT_TRUE(s != NULL);
jobject o = env_->NewWeakGlobalRef(s);
ASSERT_TRUE(o != NULL);
env_->DeleteWeakGlobalRef(o);
// Currently, deleting an already-deleted reference is just a CheckJNI warning.
{
CheckJniAbortCatcher check_jni_abort_catcher;
env_->DeleteWeakGlobalRef(o);
std::string expected(StringPrintf("native code passing in reference to "
"invalid weak global reference: %p", o));
check_jni_abort_catcher.Check(expected.c_str());
}
jobject o1 = env_->NewWeakGlobalRef(s);
ASSERT_TRUE(o1 != NULL);
jobject o2 = env_->NewWeakGlobalRef(s);
ASSERT_TRUE(o2 != NULL);
env_->DeleteWeakGlobalRef(o1);
env_->DeleteWeakGlobalRef(o2);
}
TEST_F(JniInternalTest, StaticMainMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
jobject jclass_loader = LoadDex("Main");
SirtRef<mirror::ClassLoader>
class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader));
CompileDirectMethod(class_loader, "Main", "main", "([Ljava/lang/String;)V");
mirror::Class* klass = class_linker_->FindClass("LMain;", class_loader);
ASSERT_TRUE(klass != NULL);
mirror::ArtMethod* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V");
ASSERT_TRUE(method != NULL);
ArgArray arg_array(NULL, 0);
arg_array.Append(0U);
JValue result;
// Start runtime.
bool started = runtime_->Start();
CHECK(started);
Thread::Current()->TransitionFromSuspendedToRunnable();
method->Invoke(Thread::Current(), arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'V');
}
TEST_F(JniInternalTest, StaticNopMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeNopMethod(true);
}
TEST_F(JniInternalTest, NonStaticNopMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeNopMethod(false);
}
TEST_F(JniInternalTest, StaticIdentityByteMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityByteMethod(true);
}
TEST_F(JniInternalTest, NonStaticIdentityByteMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityByteMethod(false);
}
TEST_F(JniInternalTest, StaticIdentityIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityIntMethod(true);
}
TEST_F(JniInternalTest, NonStaticIdentityIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityIntMethod(false);
}
TEST_F(JniInternalTest, StaticIdentityDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityDoubleMethod(true);
}
TEST_F(JniInternalTest, NonStaticIdentityDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeIdentityDoubleMethod(false);
}
TEST_F(JniInternalTest, StaticSumIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntMethod(false);
}
TEST_F(JniInternalTest, StaticSumIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntMethod(false);
}
TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntIntMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumIntIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntIntMethod(false);
}
TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntIntIntMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumIntIntIntIntIntMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumIntIntIntIntIntMethod(false);
}
TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleMethod(false);
}
TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleMethod(false);
}
TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleDoubleMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleDoubleMethod(false);
}
TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true);
}
TEST_F(JniInternalTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) {
TEST_DISABLED_FOR_PORTABLE();
ScopedObjectAccess soa(Thread::Current());
InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false);
}
TEST_F(JniInternalTest, Throw) {
EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
jclass exception_class = env_->FindClass("java/lang/RuntimeException");
ASSERT_TRUE(exception_class != NULL);
jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class));
ASSERT_TRUE(exception != NULL);
EXPECT_EQ(JNI_OK, env_->Throw(exception));
EXPECT_TRUE(env_->ExceptionCheck());
jthrowable thrown_exception = env_->ExceptionOccurred();
env_->ExceptionClear();
EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
}
TEST_F(JniInternalTest, ThrowNew) {
EXPECT_EQ(JNI_ERR, env_->Throw(NULL));
jclass exception_class = env_->FindClass("java/lang/RuntimeException");
ASSERT_TRUE(exception_class != NULL);
jthrowable thrown_exception;
EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
EXPECT_TRUE(env_->ExceptionCheck());
thrown_exception = env_->ExceptionOccurred();
env_->ExceptionClear();
EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL));
EXPECT_TRUE(env_->ExceptionCheck());
thrown_exception = env_->ExceptionOccurred();
env_->ExceptionClear();
EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
}
TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) {
// Start runtime.
Thread* self = Thread::Current();
self->TransitionFromSuspendedToRunnable();
MakeExecutable(nullptr, "java.lang.Class");
MakeExecutable(nullptr, "java.lang.Object");
MakeExecutable(nullptr, "java.nio.DirectByteBuffer");
MakeExecutable(nullptr, "java.nio.MemoryBlock");
MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock");
MakeExecutable(nullptr, "java.nio.MappedByteBuffer");
MakeExecutable(nullptr, "java.nio.ByteBuffer");
MakeExecutable(nullptr, "java.nio.Buffer");
// TODO: we only load a dex file here as starting the runtime relies upon it.
const char* class_name = "StaticLeafMethods";
LoadDex(class_name);
bool started = runtime_->Start();
ASSERT_TRUE(started);
jclass buffer_class = env_->FindClass("java/nio/Buffer");
ASSERT_TRUE(buffer_class != NULL);
char bytes[1024];
jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes));
ASSERT_TRUE(buffer != NULL);
ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes);
ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes));
}
TEST_F(JniInternalTest, MonitorEnterExit) {
// Create an object to torture
jclass object_class = env_->FindClass("java/lang/Object");
ASSERT_TRUE(object_class != NULL);
jobject object = env_->AllocObject(object_class);
ASSERT_TRUE(object != NULL);
// Expected class of exceptions
jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException");
ASSERT_TRUE(imse_class != NULL);
jthrowable thrown_exception;
// Unlock of unowned monitor
env_->MonitorExit(object);
EXPECT_TRUE(env_->ExceptionCheck());
thrown_exception = env_->ExceptionOccurred();
env_->ExceptionClear();
EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
// Lock of unowned monitor
env_->MonitorEnter(object);
EXPECT_FALSE(env_->ExceptionCheck());
// Regular unlock
env_->MonitorExit(object);
EXPECT_FALSE(env_->ExceptionCheck());
// Recursively lock a lot
size_t max_recursive_lock = 1024;
for (size_t i = 0; i < max_recursive_lock; i++) {
env_->MonitorEnter(object);
EXPECT_FALSE(env_->ExceptionCheck());
}
// Recursively unlock a lot
for (size_t i = 0; i < max_recursive_lock; i++) {
env_->MonitorExit(object);
EXPECT_FALSE(env_->ExceptionCheck());
}
// Unlock of unowned monitor
env_->MonitorExit(object);
EXPECT_TRUE(env_->ExceptionCheck());
thrown_exception = env_->ExceptionOccurred();
env_->ExceptionClear();
EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
// It's an error to call MonitorEnter or MonitorExit on NULL.
{
CheckJniAbortCatcher check_jni_abort_catcher;
env_->MonitorEnter(NULL);
check_jni_abort_catcher.Check("in call to MonitorEnter");
env_->MonitorExit(NULL);
check_jni_abort_catcher.Check("in call to MonitorExit");
}
}
TEST_F(JniInternalTest, DetachCurrentThread) {
CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv
jint ok = vm_->DetachCurrentThread();
EXPECT_EQ(JNI_OK, ok);
jint err = vm_->DetachCurrentThread();
EXPECT_EQ(JNI_ERR, err);
vm_->AttachCurrentThread(&env_, NULL); // need attached thread for CommonTest::TearDown
}
} // namespace art