xref: /aosp_15_r20/art/test/454-get-vreg/get_vreg_jni.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "arch/context.h"
18*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
19*795d594fSAndroid Build Coastguard Worker #include "jni.h"
20*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
21*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "stack.h"
23*795d594fSAndroid Build Coastguard Worker #include "thread.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker namespace {
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker class TestVisitor : public StackVisitor {
30*795d594fSAndroid Build Coastguard Worker  public:
TestVisitor(Thread * thread,Context * context,mirror::Object * this_value)31*795d594fSAndroid Build Coastguard Worker   TestVisitor(Thread* thread, Context* context, mirror::Object* this_value)
32*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_)
33*795d594fSAndroid Build Coastguard Worker       : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
34*795d594fSAndroid Build Coastguard Worker         this_value_(this_value),
35*795d594fSAndroid Build Coastguard Worker         found_method_index_(0) {}
36*795d594fSAndroid Build Coastguard Worker 
VisitFrame()37*795d594fSAndroid Build Coastguard Worker   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
38*795d594fSAndroid Build Coastguard Worker     ArtMethod* m = GetMethod();
39*795d594fSAndroid Build Coastguard Worker     std::string m_name(m->GetName());
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker     if (m_name.compare("testSimpleVReg") == 0) {
42*795d594fSAndroid Build Coastguard Worker       found_method_index_ = 1;
43*795d594fSAndroid Build Coastguard Worker       uint32_t value = 0;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 0, kIntVReg, &value));
46*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 42u);
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker       bool success = GetVReg(m, 1, kIntVReg, &value);
49*795d594fSAndroid Build Coastguard Worker       if (!IsShadowFrame() && GetCurrentOatQuickMethodHeader()->IsOptimized()) {
50*795d594fSAndroid Build Coastguard Worker         CHECK(!success);
51*795d594fSAndroid Build Coastguard Worker       }
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker       success = GetVReg(m, 2, kIntVReg, &value);
54*795d594fSAndroid Build Coastguard Worker       if (!IsShadowFrame() && GetCurrentOatQuickMethodHeader()->IsOptimized()) {
55*795d594fSAndroid Build Coastguard Worker         CHECK(!success);
56*795d594fSAndroid Build Coastguard Worker       }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 3, kReferenceVReg, &value));
59*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(reinterpret_cast<mirror::Object*>(value), this_value_);
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 4, kIntVReg, &value));
62*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 1u);
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 5, kFloatVReg, &value));
65*795d594fSAndroid Build Coastguard Worker       uint32_t cast = bit_cast<uint32_t, float>(1.0f);
66*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, cast);
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 6, kIntVReg, &value));
69*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 2u);
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 7, kIntVReg, &value));
72*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, true);
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 8, kIntVReg, &value));
75*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 3u);
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 9, kIntVReg, &value));
78*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, static_cast<uint32_t>('c'));
79*795d594fSAndroid Build Coastguard Worker     } else if (m_name.compare("testPairVReg") == 0) {
80*795d594fSAndroid Build Coastguard Worker       found_method_index_ = 2;
81*795d594fSAndroid Build Coastguard Worker       uint64_t value = 0;
82*795d594fSAndroid Build Coastguard Worker       CHECK(GetVRegPair(m, 0, kLongLoVReg, kLongHiVReg, &value));
83*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 42u);
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker       bool success = GetVRegPair(m, 2, kLongLoVReg, kLongHiVReg, &value);
86*795d594fSAndroid Build Coastguard Worker       if (!IsShadowFrame() && GetCurrentOatQuickMethodHeader()->IsOptimized()) {
87*795d594fSAndroid Build Coastguard Worker         CHECK(!success);
88*795d594fSAndroid Build Coastguard Worker       }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker       success = GetVRegPair(m, 4, kLongLoVReg, kLongHiVReg, &value);
91*795d594fSAndroid Build Coastguard Worker       if (!IsShadowFrame() && GetCurrentOatQuickMethodHeader()->IsOptimized()) {
92*795d594fSAndroid Build Coastguard Worker         CHECK(!success);
93*795d594fSAndroid Build Coastguard Worker       }
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker       uint32_t value32 = 0;
96*795d594fSAndroid Build Coastguard Worker       CHECK(GetVReg(m, 6, kReferenceVReg, &value32));
97*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(reinterpret_cast<mirror::Object*>(value32), this_value_);
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker       CHECK(GetVRegPair(m, 7, kLongLoVReg, kLongHiVReg, &value));
100*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(static_cast<int64_t>(value), std::numeric_limits<int64_t>::min());
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker       CHECK(GetVRegPair(m, 9, kLongLoVReg, kLongHiVReg, &value));
103*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(static_cast<int64_t>(value), std::numeric_limits<int64_t>::max());
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker       CHECK(GetVRegPair(m, 11, kLongLoVReg, kLongHiVReg, &value));
106*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, 0u);
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker       CHECK(GetVRegPair(m, 13, kDoubleLoVReg, kDoubleHiVReg, &value));
109*795d594fSAndroid Build Coastguard Worker       uint64_t cast = bit_cast<uint64_t, double>(2.0);
110*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(value, cast);
111*795d594fSAndroid Build Coastguard Worker     }
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker     return true;
114*795d594fSAndroid Build Coastguard Worker   }
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker   mirror::Object* this_value_;
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   // Value returned to Java to ensure the methods testSimpleVReg and testPairVReg
119*795d594fSAndroid Build Coastguard Worker   // have been found and tested.
120*795d594fSAndroid Build Coastguard Worker   jint found_method_index_;
121*795d594fSAndroid Build Coastguard Worker };
122*795d594fSAndroid Build Coastguard Worker 
Java_Main_doNativeCall(JNIEnv *,jobject value)123*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jint JNICALL Java_Main_doNativeCall(JNIEnv*, jobject value) {
124*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
125*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<Context> context(Context::Create());
126*795d594fSAndroid Build Coastguard Worker   TestVisitor visitor(soa.Self(), context.get(), soa.Decode<mirror::Object>(value).Ptr());
127*795d594fSAndroid Build Coastguard Worker   visitor.WalkStack();
128*795d594fSAndroid Build Coastguard Worker   return visitor.found_method_index_;
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker }  // namespace
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker }  // namespace art
134