We only need to deoptimize non-native methods

Fix #140, close #141
This commit is contained in:
LoveSy 2025-03-03 21:44:33 +08:00
parent 4a18848a86
commit 4a2293e222
No known key found for this signature in database
2 changed files with 12 additions and 28 deletions

View File

@ -33,8 +33,6 @@ private:
inline static Function<"art_quick_to_interpreter_bridge", void(void *)> inline static Function<"art_quick_to_interpreter_bridge", void(void *)>
art_quick_to_interpreter_bridge_; art_quick_to_interpreter_bridge_;
inline static Function<"art_quick_generic_jni_trampoline", void(void *)>
art_quick_generic_jni_trampoline_;
inline static Function<"_ZN3art15instrumentationL19GetOptimizedCodeForEPNS_9ArtMethodE", inline static Function<"_ZN3art15instrumentationL19GetOptimizedCodeForEPNS_9ArtMethodE",
void *(ArtMethod *)> GetOptimizedCodeFor_; void *(ArtMethod *)> GetOptimizedCodeFor_;
@ -117,8 +115,7 @@ private:
backup_method->SetEntryPoint(new_trampoline); backup_method->SetEntryPoint(new_trampoline);
} }
} else if (deoptimized) { } else if (deoptimized) {
if (new_trampoline != &art_quick_to_interpreter_bridge_ && if (new_trampoline != &art_quick_to_interpreter_bridge_ && !art_method->IsNative()) {
new_trampoline != &art_quick_generic_jni_trampoline_) {
LOGV("re-deoptimize for %p", art_method); LOGV("re-deoptimize for %p", art_method);
SetEntryPointsToInterpreter(art_method); SetEntryPointsToInterpreter(art_method);
} }
@ -194,7 +191,7 @@ public:
} }
} }
if (!handler.dlsym(SetEntryPointsToInterpreter_)) [[unlikely]] { if (!handler.dlsym(SetEntryPointsToInterpreter_)) [[likely]] {
if (handler.dlsym(GetOptimizedCodeFor_, true)) [[likely]] { if (handler.dlsym(GetOptimizedCodeFor_, true)) [[likely]] {
auto obj = JNI_FindClass(env, "java/lang/Object"); auto obj = JNI_FindClass(env, "java/lang/Object");
if (!obj) { if (!obj) {
@ -210,41 +207,28 @@ public:
// just in case // just in case
dummy->SetNonNative(); dummy->SetNonNative();
art_quick_to_interpreter_bridge_ = GetOptimizedCodeFor_(dummy.get()); art_quick_to_interpreter_bridge_ = GetOptimizedCodeFor_(dummy.get());
} } else if (!handler.dlsym(art_quick_to_interpreter_bridge_)) [[unlikely]] {
if (!art_quick_to_interpreter_bridge_ && !handler.dlsym(art_quick_to_interpreter_bridge_)) [[unlikely]] {
return false;
}
if (handler.dlsym(GetRuntimeQuickGenericJniStub_)) [[likely]] {
art_quick_generic_jni_trampoline_ = GetRuntimeQuickGenericJniStub_(nullptr);
}
if (!art_quick_generic_jni_trampoline_ && !handler.dlsym(art_quick_generic_jni_trampoline_)) [[unlikely]] {
return false; return false;
} }
} }
LOGD("art_quick_to_interpreter_bridge = %p", &art_quick_to_interpreter_bridge_); LOGD("art_quick_to_interpreter_bridge = %p", &art_quick_to_interpreter_bridge_);
LOGD("art_quick_generic_jni_trampoline = %p", &art_quick_generic_jni_trampoline_);
return true; return true;
} }
[[gnu::always_inline]] static bool SetEntryPointsToInterpreter(ArtMethod *art_method) { [[gnu::always_inline]] static bool SetEntryPointsToInterpreter(ArtMethod *art_method) {
if (art_method->IsNative()) {
return false;
}
if (SetEntryPointsToInterpreter_) [[likely]] { if (SetEntryPointsToInterpreter_) [[likely]] {
SetEntryPointsToInterpreter_(nullptr, art_method); SetEntryPointsToInterpreter_(nullptr, art_method);
return true; return true;
} }
// Android 13 // Android 13
if (art_quick_to_interpreter_bridge_ && art_quick_generic_jni_trampoline_) [[likely]] { if (art_quick_to_interpreter_bridge_) [[likely]] {
if (art_method->GetAccessFlags() & ArtMethod::kAccNative) [[unlikely]] { LOGV("deoptimize method %s from %p to %p", art_method->PrettyMethod(true).data(),
LOGV("deoptimize native method %s from %p to %p", art_method->GetEntryPoint(), &art_quick_to_interpreter_bridge_);
art_method->PrettyMethod(true).data(), art_method->GetEntryPoint(), art_method->SetEntryPoint(
&art_quick_generic_jni_trampoline_); reinterpret_cast<void *>(&art_quick_to_interpreter_bridge_));
art_method->SetEntryPoint(
reinterpret_cast<void *>(&art_quick_generic_jni_trampoline_));
} else {
LOGV("deoptimize method %s from %p to %p", art_method->PrettyMethod(true).data(),
art_method->GetEntryPoint(), &art_quick_to_interpreter_bridge_);
art_method->SetEntryPoint(
reinterpret_cast<void *>(&art_quick_to_interpreter_bridge_));
}
return true; return true;
} }
return false; return false;

View File

@ -793,7 +793,7 @@ using ::lsplant::IsHooked;
if (auto *backup = IsHooked(art_method); backup) { if (auto *backup = IsHooked(art_method); backup) {
art_method = backup; art_method = backup;
} }
if (!art_method) { if (!art_method || art_method->IsNative()) {
return false; return false;
} }
return ClassLinker::SetEntryPointsToInterpreter(art_method); return ClassLinker::SetEntryPointsToInterpreter(art_method);