Commit 9179864a authored by swift_gan's avatar swift_gan Committed by swift_gan

ForceProcessProfiles & clear hotness_count

parent a4947cef
...@@ -113,6 +113,10 @@ GCRoot ArtMethod::getDeclaringClass() { ...@@ -113,6 +113,10 @@ GCRoot ArtMethod::getDeclaringClass() {
return CastArtMethod::declaringClass->get(this); return CastArtMethod::declaringClass->get(this);
} }
uint16_t ArtMethod::getHotnessCount() {
return CastArtMethod::hotnessCount->get(this);
}
void ArtMethod::setQuickCodeEntry(void *entry) { void ArtMethod::setQuickCodeEntry(void *entry) {
CastArtMethod::entryPointQuickCompiled->set(this, entry); CastArtMethod::entryPointQuickCompiled->set(this, entry);
} }
...@@ -136,6 +140,10 @@ void ArtMethod::setDeclaringClass(GCRoot classPtr) { ...@@ -136,6 +140,10 @@ void ArtMethod::setDeclaringClass(GCRoot classPtr) {
CastArtMethod::declaringClass->set(this, classPtr); CastArtMethod::declaringClass->set(this, classPtr);
} }
void ArtMethod::setHotnessCount(uint16_t count) {
CastArtMethod::hotnessCount->set(this, count);
}
bool ArtMethod::compile(JNIEnv* env) { bool ArtMethod::compile(JNIEnv* env) {
if (isCompiled()) if (isCompiled())
return true; return true;
......
...@@ -141,8 +141,11 @@ namespace SandHook { ...@@ -141,8 +141,11 @@ namespace SandHook {
class CastDexMethodIndex : public IMember<art::mirror::ArtMethod, uint32_t> { class CastDexMethodIndex : public IMember<art::mirror::ArtMethod, uint32_t> {
protected: protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod *p) override { Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod *p) override {
if (SDK_INT >= ANDROID_P) if (SDK_INT >= ANDROID_P) {
return getParentSize() + 1; return CastArtMethod::accessFlag->getOffset()
+ CastArtMethod::accessFlag->size()
+ sizeof(uint32_t);
}
int offset = 0; int offset = 0;
jint index = getIntFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver", jint index = getIntFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver",
"dexMethodIndex"); "dexMethodIndex");
...@@ -156,6 +159,17 @@ namespace SandHook { ...@@ -156,6 +159,17 @@ namespace SandHook {
} }
}; };
class CastHotnessCount : public IMember<art::mirror::ArtMethod, uint16_t> {
protected:
Size calOffset(JNIEnv *jniEnv, mirror::ArtMethod *p) override {
if (SDK_INT <= ANDROID_N)
return getParentSize() + 1;
return CastArtMethod::dexMethodIndex->getOffset()
+ CastArtMethod::dexMethodIndex->size()
+ sizeof(uint16_t);
}
};
void CastArtMethod::init(JNIEnv *env) { void CastArtMethod::init(JNIEnv *env) {
//init ArtMethodSize //init ArtMethodSize
...@@ -187,6 +201,9 @@ namespace SandHook { ...@@ -187,6 +201,9 @@ namespace SandHook {
declaringClass = new CastShadowClass(); declaringClass = new CastShadowClass();
declaringClass->init(env, m1, size); declaringClass->init(env, m1, size);
hotnessCount = new CastHotnessCount();
hotnessCount->init(env, m1, size);
jclass neverCallTestClass = env->FindClass("com/swift/sandhook/ClassNeverCall"); jclass neverCallTestClass = env->FindClass("com/swift/sandhook/ClassNeverCall");
...@@ -240,6 +257,7 @@ namespace SandHook { ...@@ -240,6 +257,7 @@ namespace SandHook {
IMember<art::mirror::ArtMethod, uint32_t> *CastArtMethod::dexMethodIndex = nullptr; IMember<art::mirror::ArtMethod, uint32_t> *CastArtMethod::dexMethodIndex = nullptr;
IMember<art::mirror::ArtMethod, uint32_t> *CastArtMethod::accessFlag = nullptr; IMember<art::mirror::ArtMethod, uint32_t> *CastArtMethod::accessFlag = nullptr;
IMember<art::mirror::ArtMethod, GCRoot> *CastArtMethod::declaringClass = nullptr; IMember<art::mirror::ArtMethod, GCRoot> *CastArtMethod::declaringClass = nullptr;
IMember<art::mirror::ArtMethod, uint16_t> *CastArtMethod::hotnessCount = nullptr;
void *CastArtMethod::quickToInterpreterBridge = nullptr; void *CastArtMethod::quickToInterpreterBridge = nullptr;
void *CastArtMethod::genericJniStub = nullptr; void *CastArtMethod::genericJniStub = nullptr;
void *CastArtMethod::staticResolveStub = nullptr; void *CastArtMethod::staticResolveStub = nullptr;
......
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
#define BYTE_POINT sizeof(void*) #define BYTE_POINT sizeof(void*)
typedef size_t Size;
//32bit //32bit
#if defined(__i386__) || defined(__arm__) #if defined(__i386__) || defined(__arm__)
#define Size uint32_t
//64bit //64bit
#elif defined(__aarch64__) || defined(__x86_64__) #elif defined(__aarch64__) || defined(__x86_64__)
#define Size uint64_t
#else #else
#endif #endif
......
...@@ -61,12 +61,14 @@ public: ...@@ -61,12 +61,14 @@ public:
void setDexCacheResolveList(void* list); void setDexCacheResolveList(void* list);
void setDexCacheResolveItem(uint32_t index, void* item); void setDexCacheResolveItem(uint32_t index, void* item);
void setDeclaringClass(GCRoot classPtr); void setDeclaringClass(GCRoot classPtr);
void setHotnessCount(uint16_t count);
void* getQuickCodeEntry(); void* getQuickCodeEntry();
void* getInterpreterCodeEntry(); void* getInterpreterCodeEntry();
uint32_t getAccessFlags(); uint32_t getAccessFlags();
uint32_t getDexMethodIndex(); uint32_t getDexMethodIndex();
GCRoot getDeclaringClass(); GCRoot getDeclaringClass();
uint16_t getHotnessCount();
bool compile(JNIEnv* env); bool compile(JNIEnv* env);
bool deCompile(); bool deCompile();
......
...@@ -37,6 +37,10 @@ namespace SandHook { ...@@ -37,6 +37,10 @@ namespace SandHook {
offset = calOffset(jniEnv, p); offset = calOffset(jniEnv, p);
} }
Size size() {
return sizeof(MType);
}
virtual Size getOffset() { virtual Size getOffset() {
return offset; return offset;
} }
...@@ -54,7 +58,7 @@ namespace SandHook { ...@@ -54,7 +58,7 @@ namespace SandHook {
virtual void set(PType* p, MType t) { virtual void set(PType* p, MType t) {
if (offset > parentSize) if (offset > parentSize)
return; return;
memcpy(reinterpret_cast<void *>((Size)p + getOffset()), &t, sizeof(MType)); memcpy(reinterpret_cast<void *>((Size)p + getOffset()), &t, size());
}; };
template<typename T> template<typename T>
......
...@@ -19,6 +19,7 @@ namespace SandHook { ...@@ -19,6 +19,7 @@ namespace SandHook {
static IMember<art::mirror::ArtMethod, uint32_t>* dexMethodIndex; static IMember<art::mirror::ArtMethod, uint32_t>* dexMethodIndex;
static IMember<art::mirror::ArtMethod, uint32_t>* accessFlag; static IMember<art::mirror::ArtMethod, uint32_t>* accessFlag;
static IMember<art::mirror::ArtMethod, GCRoot>* declaringClass; static IMember<art::mirror::ArtMethod, GCRoot>* declaringClass;
static IMember<art::mirror::ArtMethod, uint16_t>* hotnessCount;
static void* quickToInterpreterBridge; static void* quickToInterpreterBridge;
static void* genericJniStub; static void* genericJniStub;
static void* staticResolveStub; static void* staticResolveStub;
......
...@@ -35,6 +35,8 @@ extern "C" { ...@@ -35,6 +35,8 @@ extern "C" {
bool replaceUpdateCompilerOptionsQ(); bool replaceUpdateCompilerOptionsQ();
bool forceProcessProfiles();
} }
#endif //SANDHOOK_HIDE_API_H #endif //SANDHOOK_HIDE_API_H
...@@ -10,4 +10,7 @@ ...@@ -10,4 +10,7 @@
extern "C" extern "C"
JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook); JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook);
extern "C"
JNIEXPORT void* findSym(const char *elf, const char *sym_name);
#endif //SANDHOOK_SANDHOOK_H #endif //SANDHOOK_SANDHOOK_H
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "includes/cast_compiler_options.h" #include "includes/cast_compiler_options.h"
#include "includes/log.h" #include "includes/log.h"
#include "includes/native_hook.h" #include "includes/native_hook.h"
#include "includes/elf_util.h"
#include <jni.h> #include <jni.h>
...@@ -69,6 +70,14 @@ bool doHookWithReplacement(JNIEnv* env, ...@@ -69,6 +70,14 @@ bool doHookWithReplacement(JNIEnv* env,
hookMethod->disableCompilable(); hookMethod->disableCompilable();
} }
if (SDK_INT > ANDROID_N) {
forceProcessProfiles();
}
if ((SDK_INT >= ANDROID_N && SDK_INT <= ANDROID_P)
|| (SDK_INT >= ANDROID_Q && !originMethod->isAbstract())) {
originMethod->setHotnessCount(0);
}
if (backupMethod != nullptr) { if (backupMethod != nullptr) {
originMethod->backup(backupMethod); originMethod->backup(backupMethod);
backupMethod->disableCompilable(); backupMethod->disableCompilable();
...@@ -113,6 +122,13 @@ bool doHookWithInline(JNIEnv* env, ...@@ -113,6 +122,13 @@ bool doHookWithInline(JNIEnv* env,
} }
originMethod->disableCompilable(); originMethod->disableCompilable();
if (SDK_INT > ANDROID_N) {
forceProcessProfiles();
}
if ((SDK_INT >= ANDROID_N && SDK_INT <= ANDROID_P)
|| (SDK_INT >= ANDROID_Q && !originMethod->isAbstract())) {
originMethod->setHotnessCount(0);
}
originMethod->flushCache(); originMethod->flushCache();
SandHook::HookTrampoline* hookTrampoline = trampolineManager.installInlineTrampoline(originMethod, hookMethod, backupMethod); SandHook::HookTrampoline* hookTrampoline = trampolineManager.installInlineTrampoline(originMethod, hookMethod, backupMethod);
...@@ -352,7 +368,6 @@ Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance) ...@@ -352,7 +368,6 @@ Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance)
} }
//native hook //native hook
extern "C" extern "C"
JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook) { JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook) {
...@@ -365,6 +380,12 @@ JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook) { ...@@ -365,6 +380,12 @@ JNIEXPORT bool nativeHookNoBackup(void* origin, void* hook) {
} }
extern "C"
JNIEXPORT void* findSym(const char *elf, const char *sym_name) {
SandHook::ElfImg elfImg(elf);
return reinterpret_cast<void *>(elfImg.getSymbAddress(sym_name));
}
static JNINativeMethod jniSandHook[] = { static JNINativeMethod jniSandHook[] = {
{ {
"initNative", "initNative",
......
...@@ -27,6 +27,8 @@ extern "C" { ...@@ -27,6 +27,8 @@ extern "C" {
//for Android Q //for Android Q
void (**origin_jit_update_options)(void *) = nullptr; void (**origin_jit_update_options)(void *) = nullptr;
void (*profileSaver_ForceProcessProfiles)() = nullptr;
const char* art_lib_path; const char* art_lib_path;
const char* jit_lib_path; const char* jit_lib_path;
...@@ -97,6 +99,10 @@ extern "C" { ...@@ -97,6 +99,10 @@ extern "C" {
origin_jit_update_options = reinterpret_cast<void (**)(void *)>(getSymCompat(art_lib_path, "_ZN3art3jit3Jit20jit_update_options_E")); origin_jit_update_options = reinterpret_cast<void (**)(void *)>(getSymCompat(art_lib_path, "_ZN3art3jit3Jit20jit_update_options_E"));
} }
if (SDK_INT > ANDROID_N) {
profileSaver_ForceProcessProfiles = reinterpret_cast<void (*)()>(getSymCompat(art_lib_path, "_ZN3art12ProfileSaver20ForceProcessProfilesEv"));
}
} }
bool canCompile() { bool canCompile() {
...@@ -214,5 +220,11 @@ extern "C" { ...@@ -214,5 +220,11 @@ extern "C" {
*origin_jit_update_options = fake_jit_update_options; *origin_jit_update_options = fake_jit_update_options;
} }
bool forceProcessProfiles() {
if (profileSaver_ForceProcessProfiles == nullptr)
return false;
profileSaver_ForceProcessProfiles();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment