1 /*
2  * Copyright (C) 2022 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 <type_traits>
18 
19 #include "art_method-alloc-inl.h"
20 
21 #include "base/casts.h"
22 #include "common_runtime_test.h"
23 #include "mirror/class-alloc-inl.h"
24 #include "well_known_classes.h"
25 
26 namespace art HIDDEN {
27 
28 namespace {
29 // Helper function to avoid `ASSERT_EQ` with floating point types.
ToIntegralType(float value)30 int32_t ToIntegralType(float value) { return bit_cast<int32_t>(value); }
ToIntegralType(double value)31 int64_t ToIntegralType(double value) { return bit_cast<int64_t>(value); }
ToIntegralType(T value)32 template <typename T> T ToIntegralType(T value) { return value; }
33 }
34 
35 class ArtMethodTest : public CommonRuntimeTest {
36  protected:
ArtMethodTest()37   ArtMethodTest() {
38     use_boot_image_ = true;  // Make the Runtime creation cheaper.
39   }
40 
41   // Test primitive type boxing and unboxing.
42   //
43   // This provides basic checks that the translation of the compile-time shorty
44   // to argument types and return type are correct and that values are passed
45   // correctly for these single-argument calls (`ArtMethod::InvokeStatic()` with
46   // primitive args and `ArtMethod::InvokeInstance()` with a reference arg).
47   template <typename Type, char kPrimitive>
TestBoxUnbox(ArtMethod * value_of,const char * unbox_name,Type value)48   void TestBoxUnbox(ArtMethod* value_of, const char* unbox_name, Type value) {
49     Thread* self = Thread::Current();
50     ScopedObjectAccess soa(self);
51     ASSERT_STREQ(value_of->GetName(), "valueOf");
52     std::string unbox_signature = std::string("()") + kPrimitive;
53     ArtMethod* unbox_method = value_of->GetDeclaringClass()->FindClassMethod(
54         unbox_name, unbox_signature, kRuntimePointerSize);
55     ASSERT_TRUE(unbox_method != nullptr);
56     ASSERT_FALSE(unbox_method->IsStatic());
57     ASSERT_TRUE(value_of->GetDeclaringClass()->IsFinal());
58 
59     static_assert(std::is_same_v<ObjPtr<mirror::Object> (ArtMethod::*)(Thread*, Type),
60                                  decltype(&ArtMethod::InvokeStatic<'L', kPrimitive>)>);
61     StackHandleScope<1u> hs(self);
62     Handle<mirror::Object> boxed =
63         hs.NewHandle(value_of->InvokeStatic<'L', kPrimitive>(self, value));
64     ASSERT_TRUE(boxed != nullptr);
65     ASSERT_OBJ_PTR_EQ(boxed->GetClass(), value_of->GetDeclaringClass());
66     static_assert(std::is_same_v<Type (ArtMethod::*)(Thread*, ObjPtr<mirror::Object>),
67                                  decltype(&ArtMethod::InvokeInstance<kPrimitive>)>);
68     // Exercise both `InvokeInstance()` and `InvokeFinal()` (boxing classes are final).
69     Type unboxed1 = unbox_method->InvokeInstance<kPrimitive>(self, boxed.Get());
70     ASSERT_EQ(ToIntegralType(value), ToIntegralType(unboxed1));
71     Type unboxed2 = unbox_method->InvokeFinal<kPrimitive>(self, boxed.Get());
72     ASSERT_EQ(ToIntegralType(value), ToIntegralType(unboxed2));
73   }
74 };
75 
TEST_F(ArtMethodTest,BoxUnboxBoolean)76 TEST_F(ArtMethodTest, BoxUnboxBoolean) {
77   TestBoxUnbox<bool, 'Z'>(WellKnownClasses::java_lang_Boolean_valueOf, "booleanValue", true);
78 }
79 
TEST_F(ArtMethodTest,BoxUnboxByte)80 TEST_F(ArtMethodTest, BoxUnboxByte) {
81   TestBoxUnbox<int8_t, 'B'>(WellKnownClasses::java_lang_Byte_valueOf, "byteValue", -12);
82 }
83 
TEST_F(ArtMethodTest,BoxUnboxChar)84 TEST_F(ArtMethodTest, BoxUnboxChar) {
85   TestBoxUnbox<uint16_t, 'C'>(WellKnownClasses::java_lang_Character_valueOf, "charValue", 0xffaa);
86 }
87 
TEST_F(ArtMethodTest,BoxUnboxShort)88 TEST_F(ArtMethodTest, BoxUnboxShort) {
89   TestBoxUnbox<int16_t, 'S'>(WellKnownClasses::java_lang_Short_valueOf, "shortValue", -0x1234);
90 }
91 
TEST_F(ArtMethodTest,BoxUnboxInt)92 TEST_F(ArtMethodTest, BoxUnboxInt) {
93   TestBoxUnbox<int32_t, 'I'>(WellKnownClasses::java_lang_Integer_valueOf, "intValue", -0x12345678);
94 }
95 
TEST_F(ArtMethodTest,BoxUnboxLong)96 TEST_F(ArtMethodTest, BoxUnboxLong) {
97   TestBoxUnbox<int64_t, 'J'>(
98       WellKnownClasses::java_lang_Long_valueOf, "longValue", UINT64_C(-0x1234567887654321));
99 }
100 
TEST_F(ArtMethodTest,BoxUnboxFloat)101 TEST_F(ArtMethodTest, BoxUnboxFloat) {
102   TestBoxUnbox<float, 'F'>(WellKnownClasses::java_lang_Float_valueOf, "floatValue", -2.0f);
103 }
104 
TEST_F(ArtMethodTest,BoxUnboxDouble)105 TEST_F(ArtMethodTest, BoxUnboxDouble) {
106   TestBoxUnbox<double, 'D'>(WellKnownClasses::java_lang_Double_valueOf, "doubleValue", 8.0);
107 }
108 
TEST_F(ArtMethodTest,ArrayList)109 TEST_F(ArtMethodTest, ArrayList) {
110   Thread* self = Thread::Current();
111   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
112   ScopedObjectAccess soa(self);
113   StackHandleScope<4u> hs(self);
114   Handle<mirror::Class> list_class =
115       hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/util/List;"));
116   ASSERT_TRUE(list_class != nullptr);
117   Handle<mirror::Class> abstract_list_class =
118       hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/util/AbstractList;"));
119   ASSERT_TRUE(abstract_list_class != nullptr);
120   Handle<mirror::Class> array_list_class =
121       hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/util/ArrayList;"));
122   ASSERT_TRUE(array_list_class != nullptr);
123   ASSERT_TRUE(abstract_list_class->Implements(list_class.Get()));
124   ASSERT_TRUE(array_list_class->IsSubClass(abstract_list_class.Get()));
125 
126   ArtMethod* init = array_list_class->FindClassMethod("<init>", "()V", kRuntimePointerSize);
127   ASSERT_TRUE(init != nullptr);
128   ArtMethod* array_list_size_method =
129       array_list_class->FindClassMethod("size", "()I", kRuntimePointerSize);
130   DCHECK(array_list_size_method != nullptr);
131   ArtMethod* abstract_list_size_method =
132       abstract_list_class->FindClassMethod("size", "()I", kRuntimePointerSize);
133   DCHECK(abstract_list_size_method != nullptr);
134   ArtMethod* list_size_method =
135       list_class->FindInterfaceMethod("size", "()I", kRuntimePointerSize);
136   DCHECK(list_size_method != nullptr);
137 
138   Handle<mirror::Object> array_list = init->NewObject<>(hs, self);
139   ASSERT_FALSE(self->IsExceptionPending());
140   ASSERT_TRUE(array_list != nullptr);
141 
142   // Invoke `ArrayList.size()` directly, with virtual dispatch from
143   // `AbstractList.size()` and with interface dispatch from `List.size()`.
144   int32_t size = array_list_size_method->InvokeInstance<'I'>(self, array_list.Get());
145   ASSERT_FALSE(self->IsExceptionPending());
146   ASSERT_EQ(0, size);
147   size = abstract_list_size_method->InvokeVirtual<'I'>(self, array_list.Get());
148   ASSERT_FALSE(self->IsExceptionPending());
149   ASSERT_EQ(0, size);
150   size = list_size_method->InvokeInterface<'I'>(self, array_list.Get());
151   ASSERT_FALSE(self->IsExceptionPending());
152   ASSERT_EQ(0, size);
153 
154   // Try to invoke abstract method `AbstractList.size()` directly.
155   abstract_list_size_method->InvokeInstance<'I'>(self, array_list.Get());
156   ASSERT_TRUE(self->IsExceptionPending());
157   self->ClearException();
158 }
159 
160 }  // namespace art
161