1 /* 2 * Copyright (C) 2012 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 "entrypoints/entrypoint_utils.h" 18 19 #include "art_field-inl.h" 20 #include "art_method-inl.h" 21 #include "base/enums.h" 22 #include "base/mutex.h" 23 #include "class_linker-inl.h" 24 #include "dex_file-inl.h" 25 #include "entrypoints/entrypoint_utils-inl.h" 26 #include "entrypoints/quick/callee_save_frame.h" 27 #include "entrypoints/runtime_asm_entrypoints.h" 28 #include "gc/accounting/card_table-inl.h" 29 #include "java_vm_ext.h" 30 #include "mirror/class-inl.h" 31 #include "mirror/method.h" 32 #include "mirror/object-inl.h" 33 #include "mirror/object_array-inl.h" 34 #include "nth_caller_visitor.h" 35 #include "oat_quick_method_header.h" 36 #include "reflection.h" 37 #include "scoped_thread_state_change-inl.h" 38 #include "well_known_classes.h" 39 40 namespace art { 41 42 void CheckReferenceResult(Handle<mirror::Object> o, Thread* self) { 43 if (o == nullptr) { 44 return; 45 } 46 // Make sure that the result is an instance of the type this method was expected to return. 47 ArtMethod* method = self->GetCurrentMethod(nullptr); 48 mirror::Class* return_type = method->GetReturnType(true /* resolve */); 49 50 if (!o->InstanceOf(return_type)) { 51 Runtime::Current()->GetJavaVM()->JniAbortF(nullptr, 52 "attempt to return an instance of %s from %s", 53 o->PrettyTypeOf().c_str(), 54 method->PrettyMethod().c_str()); 55 } 56 } 57 58 JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty, 59 jobject rcvr_jobj, jobject interface_method_jobj, 60 std::vector<jvalue>& args) { 61 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); 62 63 // Build argument array possibly triggering GC. 64 soa.Self()->AssertThreadSuspensionIsAllowable(); 65 jobjectArray args_jobj = nullptr; 66 const JValue zero; 67 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion(); 68 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility. 69 if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) { 70 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr); 71 if (args_jobj == nullptr) { 72 CHECK(soa.Self()->IsExceptionPending()); 73 return zero; 74 } 75 for (size_t i = 0; i < args.size(); ++i) { 76 if (shorty[i + 1] == 'L') { 77 jobject val = args.at(i).l; 78 soa.Env()->SetObjectArrayElement(args_jobj, i, val); 79 } else { 80 JValue jv; 81 jv.SetJ(args.at(i).j); 82 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv).Ptr(); 83 if (val == nullptr) { 84 CHECK(soa.Self()->IsExceptionPending()); 85 return zero; 86 } 87 soa.Decode<mirror::ObjectArray<mirror::Object>>(args_jobj)->Set<false>(i, val); 88 } 89 } 90 } 91 92 // Call Proxy.invoke(Proxy proxy, Method method, Object[] args). 93 jvalue invocation_args[3]; 94 invocation_args[0].l = rcvr_jobj; 95 invocation_args[1].l = interface_method_jobj; 96 invocation_args[2].l = args_jobj; 97 jobject result = 98 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy, 99 WellKnownClasses::java_lang_reflect_Proxy_invoke, 100 invocation_args); 101 102 // Unbox result and handle error conditions. 103 if (LIKELY(!soa.Self()->IsExceptionPending())) { 104 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) { 105 // Do nothing. 106 return zero; 107 } else { 108 ArtMethod* interface_method = 109 soa.Decode<mirror::Method>(interface_method_jobj)->GetArtMethod(); 110 // This can cause thread suspension. 111 mirror::Class* result_type = interface_method->GetReturnType(true /* resolve */); 112 ObjPtr<mirror::Object> result_ref = soa.Decode<mirror::Object>(result); 113 JValue result_unboxed; 114 if (!UnboxPrimitiveForResult(result_ref.Ptr(), result_type, &result_unboxed)) { 115 DCHECK(soa.Self()->IsExceptionPending()); 116 return zero; 117 } 118 return result_unboxed; 119 } 120 } else { 121 // In the case of checked exceptions that aren't declared, the exception must be wrapped by 122 // a UndeclaredThrowableException. 123 mirror::Throwable* exception = soa.Self()->GetException(); 124 if (exception->IsCheckedException()) { 125 bool declares_exception = false; 126 { 127 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 128 ObjPtr<mirror::Object> rcvr = soa.Decode<mirror::Object>(rcvr_jobj); 129 mirror::Class* proxy_class = rcvr->GetClass(); 130 ObjPtr<mirror::Method> interface_method = soa.Decode<mirror::Method>(interface_method_jobj); 131 ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface( 132 interface_method->GetArtMethod(), kRuntimePointerSize); 133 auto virtual_methods = proxy_class->GetVirtualMethodsSlice(kRuntimePointerSize); 134 size_t num_virtuals = proxy_class->NumVirtualMethods(); 135 size_t method_size = ArtMethod::Size(kRuntimePointerSize); 136 // Rely on the fact that the methods are contiguous to determine the index of the method in 137 // the slice. 138 int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) - 139 reinterpret_cast<uintptr_t>(&virtual_methods.At(0))) / method_size; 140 CHECK_LT(throws_index, static_cast<int>(num_virtuals)); 141 mirror::ObjectArray<mirror::Class>* declared_exceptions = 142 proxy_class->GetProxyThrows()->Get(throws_index); 143 mirror::Class* exception_class = exception->GetClass(); 144 for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { 145 mirror::Class* declared_exception = declared_exceptions->Get(i); 146 declares_exception = declared_exception->IsAssignableFrom(exception_class); 147 } 148 } 149 if (!declares_exception) { 150 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", 151 nullptr); 152 } 153 } 154 return zero; 155 } 156 } 157 158 bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload) { 159 DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature)); 160 if (UNLIKELY(obj == nullptr)) { 161 ThrowNullPointerException("null array in FILL_ARRAY_DATA"); 162 return false; 163 } 164 mirror::Array* array = obj->AsArray(); 165 DCHECK(!array->IsObjectArray()); 166 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) { 167 Thread* self = Thread::Current(); 168 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", 169 "failed FILL_ARRAY_DATA; length=%d, index=%d", 170 array->GetLength(), payload->element_count); 171 return false; 172 } 173 // Copy data from dex file to memory assuming both are little endian. 174 uint32_t size_in_bytes = payload->element_count * payload->element_width; 175 memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes); 176 return true; 177 } 178 179 static inline std::pair<ArtMethod*, uintptr_t> DoGetCalleeSaveMethodOuterCallerAndPc( 180 ArtMethod** sp, Runtime::CalleeSaveType type) REQUIRES_SHARED(Locks::mutator_lock_) { 181 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type)); 182 183 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type); 184 auto** caller_sp = reinterpret_cast<ArtMethod**>( 185 reinterpret_cast<uintptr_t>(sp) + callee_frame_size); 186 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type); 187 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>( 188 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset)); 189 ArtMethod* outer_method = *caller_sp; 190 return std::make_pair(outer_method, caller_pc); 191 } 192 193 static inline ArtMethod* DoGetCalleeSaveMethodCaller(ArtMethod* outer_method, 194 uintptr_t caller_pc, 195 bool do_caller_check) 196 REQUIRES_SHARED(Locks::mutator_lock_) { 197 ArtMethod* caller = outer_method; 198 if (LIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) { 199 if (outer_method != nullptr) { 200 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc); 201 DCHECK(current_code != nullptr); 202 DCHECK(current_code->IsOptimized()); 203 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc); 204 CodeInfo code_info = current_code->GetOptimizedCodeInfo(); 205 MethodInfo method_info = current_code->GetOptimizedMethodInfo(); 206 CodeInfoEncoding encoding = code_info.ExtractEncoding(); 207 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); 208 DCHECK(stack_map.IsValid()); 209 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) { 210 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding); 211 caller = GetResolvedMethod(outer_method, 212 method_info, 213 inline_info, 214 encoding.inline_info.encoding, 215 inline_info.GetDepth(encoding.inline_info.encoding) - 1); 216 } 217 } 218 if (kIsDebugBuild && do_caller_check) { 219 // Note that do_caller_check is optional, as this method can be called by 220 // stubs, and tests without a proper call stack. 221 NthCallerVisitor visitor(Thread::Current(), 1, true); 222 visitor.WalkStack(); 223 CHECK_EQ(caller, visitor.caller); 224 } 225 } else { 226 // We're instrumenting, just use the StackVisitor which knows how to 227 // handle instrumented frames. 228 NthCallerVisitor visitor(Thread::Current(), 1, true); 229 visitor.WalkStack(); 230 caller = visitor.caller; 231 } 232 return caller; 233 } 234 235 ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp, 236 Runtime::CalleeSaveType type, 237 bool do_caller_check) 238 REQUIRES_SHARED(Locks::mutator_lock_) { 239 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 240 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type); 241 ArtMethod* outer_method = outer_caller_and_pc.first; 242 uintptr_t caller_pc = outer_caller_and_pc.second; 243 ArtMethod* caller = DoGetCalleeSaveMethodCaller(outer_method, caller_pc, do_caller_check); 244 return caller; 245 } 246 247 CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self, 248 Runtime::CalleeSaveType type) { 249 CallerAndOuterMethod result; 250 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 251 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame(); 252 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type); 253 result.outer_method = outer_caller_and_pc.first; 254 uintptr_t caller_pc = outer_caller_and_pc.second; 255 result.caller = 256 DoGetCalleeSaveMethodCaller(result.outer_method, caller_pc, /* do_caller_check */ true); 257 return result; 258 } 259 260 ArtMethod* GetCalleeSaveOuterMethod(Thread* self, Runtime::CalleeSaveType type) { 261 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 262 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame(); 263 return DoGetCalleeSaveMethodOuterCallerAndPc(sp, type).first; 264 } 265 266 } // namespace art 267