1 /*
2  * Copyright (C) 2015 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 "arch/context.h"
18 #include "art_method-inl.h"
19 #include "jni.h"
20 #include "oat_quick_method_header.h"
21 #include "scoped_thread_state_change-inl.h"
22 #include "stack.h"
23 #include "thread.h"
24 
25 namespace art {
26 
27 namespace {
28 
29 class TestVisitor : public StackVisitor {
30  public:
TestVisitor(Thread * thread,Context * context)31   TestVisitor(Thread* thread, Context* context)
32       REQUIRES_SHARED(Locks::mutator_lock_)
33       : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
34 
VisitFrame()35   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
36     ArtMethod* m = GetMethod();
37     std::string m_name(m->GetName());
38 
39     if (m_name.compare("mergeOk") == 0) {
40       uint32_t value = 0;
41 
42       CHECK(GetVReg(m, 0, kIntVReg, &value));
43       CHECK_EQ(value, 0u);
44 
45       CHECK(GetVReg(m, 1, kIntVReg, &value));
46       CHECK_EQ(value, 1u);
47 
48       CHECK(GetVReg(m, 2, kIntVReg, &value));
49       CHECK_EQ(value, 2u);
50 
51       CHECK(GetVReg(m, 3, kIntVReg, &value));
52       CHECK_EQ(value, 1u);
53 
54       CHECK(GetVReg(m, 4, kIntVReg, &value));
55       CHECK_EQ(value, 2u);
56       did_check_ = true;
57     } else if (m_name.compare("mergeNotOk") == 0) {
58       uint32_t value = 0;
59 
60       CHECK(GetVReg(m, 0, kIntVReg, &value));
61       CHECK_EQ(value, 0u);
62 
63       CHECK(GetVReg(m, 1, kIntVReg, &value));
64       CHECK_EQ(value, 1u);
65 
66       bool success = GetVReg(m, 2, kIntVReg, &value);
67       if (!IsShadowFrame() && GetCurrentOatQuickMethodHeader()->IsOptimized()) {
68         CHECK(!success);
69       }
70 
71       CHECK(GetVReg(m, 3, kIntVReg, &value));
72       CHECK_EQ(value, 1u);
73 
74       CHECK(GetVReg(m, 4, kFloatVReg, &value));
75       uint32_t cast = bit_cast<uint32_t, float>(4.0f);
76       CHECK_EQ(value, cast);
77       did_check_ = true;
78     } else if (m_name.compare("phiEquivalent") == 0) {
79       uint32_t value = 0;
80 
81       CHECK(GetVReg(m, 0, kIntVReg, &value));
82       // Quick doesn't like this one on x64.
83       CHECK_EQ(value, 0u);
84 
85       CHECK(GetVReg(m, 1, kIntVReg, &value));
86       CHECK_EQ(value, 1u);
87 
88       CHECK(GetVReg(m, 2, kFloatVReg, &value));
89       CHECK_EQ(value, 1u);
90 
91       did_check_ = true;
92     } else if (m_name.compare("mergeReferences") == 0) {
93       uint32_t value = 0;
94 
95       CHECK(GetVReg(m, 0, kIntVReg, &value));
96       CHECK_EQ(value, 0u);
97 
98       CHECK(GetVReg(m, 1, kIntVReg, &value));
99       CHECK_EQ(value, 1u);
100 
101       CHECK(GetVReg(m, 2, kReferenceVReg, &value));
102       CHECK_EQ(value, 0u);
103 
104       CHECK(GetVReg(m, 3, kReferenceVReg, &value));
105       CHECK_NE(value, 0u);
106 
107       did_check_ = true;
108     } else if (m_name.compare("phiAllEquivalents") == 0) {
109       uint32_t value = 0;
110 
111       CHECK(GetVReg(m, 0, kIntVReg, &value));
112       CHECK_EQ(value, 0u);
113 
114       CHECK(GetVReg(m, 1, kIntVReg, &value));
115       CHECK_EQ(value, 1u);
116 
117       CHECK(GetVReg(m, 2, kReferenceVReg, &value));
118       CHECK_EQ(value, 0u);
119 
120       did_check_ = true;
121     }
122 
123     return true;
124   }
125 
126   bool did_check_ = false;
127 };
128 
Java_PhiLiveness_regsNativeCall(JNIEnv *,jclass value ATTRIBUTE_UNUSED)129 extern "C" JNIEXPORT void JNICALL Java_PhiLiveness_regsNativeCall(
130     JNIEnv*, jclass value ATTRIBUTE_UNUSED) {
131   ScopedObjectAccess soa(Thread::Current());
132   std::unique_ptr<Context> context(Context::Create());
133   TestVisitor visitor(soa.Self(), context.get());
134   visitor.WalkStack();
135   CHECK(visitor.did_check_);
136 }
137 
Java_PhiLiveness_regsNativeCallWithParameters(JNIEnv *,jclass value ATTRIBUTE_UNUSED,jobject main,jint int_value,jfloat float_value)138 extern "C" JNIEXPORT void JNICALL Java_PhiLiveness_regsNativeCallWithParameters(
139     JNIEnv*, jclass value ATTRIBUTE_UNUSED, jobject main, jint int_value, jfloat float_value) {
140   ScopedObjectAccess soa(Thread::Current());
141   std::unique_ptr<Context> context(Context::Create());
142   CHECK(soa.Decode<mirror::Object>(main) == nullptr);
143   CHECK_EQ(int_value, 0);
144   int32_t cast = bit_cast<int32_t, float>(float_value);
145   CHECK_EQ(cast, 0);
146   TestVisitor visitor(soa.Self(), context.get());
147   visitor.WalkStack();
148   CHECK(visitor.did_check_);
149 }
150 
151 }  // namespace
152 
153 }  // namespace art
154