Commit 8c3f0c1d authored by swift_gan's avatar swift_gan

fix code

parent c976181d
...@@ -41,6 +41,7 @@ public: ...@@ -41,6 +41,7 @@ public:
class ArtMethod { class ArtMethod {
public: public:
uint16_t palceholder[32];
}; };
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define SANDHOOK_CAST_ART_METHOD_H #define SANDHOOK_CAST_ART_METHOD_H
#include "../includes/cast.h" #include "../includes/cast.h"
#include "../trampoline/trampoline_manager.h"
namespace SandHook { namespace SandHook {
...@@ -17,7 +18,7 @@ namespace SandHook { ...@@ -17,7 +18,7 @@ namespace SandHook {
int offset = 0; int offset = 0;
Size addr = getAddressFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver", "resolvedMethodsAddress"); Size addr = getAddressFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver", "resolvedMethodsAddress");
if (addr != 0) { if (addr != 0) {
offset = findOffset(&p, getParentSize(), 2, reinterpret_cast<void *>(addr)); offset = findOffset(&p, getParentSize(), 2, addr);
if (offset >= 0) { if (offset >= 0) {
return static_cast<Size>(offset); return static_cast<Size>(offset);
} }
...@@ -68,9 +69,9 @@ namespace SandHook { ...@@ -68,9 +69,9 @@ namespace SandHook {
class CastAccessFlag : public IMember<art::mirror::ArtMethod, uint32_t> { class CastAccessFlag : 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 {
int offset = findOffset(&p, getParentSize(), sizeof(uint32_t), 524313); int offset = findOffset(&p, getParentSize(), 2, (uint32_t)524313);
if (offset < 0) if (offset < 0)
throw getParentSize() + 1; return getParentSize() + 1;
else else
return static_cast<size_t>(offset); return static_cast<size_t>(offset);
} }
...@@ -105,8 +106,11 @@ namespace SandHook { ...@@ -105,8 +106,11 @@ namespace SandHook {
static ArrayMember<art::mirror::ArtMethod>* dexCacheResolvedMethods; static ArrayMember<art::mirror::ArtMethod>* dexCacheResolvedMethods;
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 Code quickToInterpreterBridge;
static void init(JNIEnv *env) { static void init(JNIEnv *env, int sdk) {
SDK_INT = sdk;
//init ArtMethodSize //init ArtMethodSize
jclass sizeTestClass = env->FindClass("com/swift/sandhook/ArtMethodSizeTest"); jclass sizeTestClass = env->FindClass("com/swift/sandhook/ArtMethodSizeTest");
Size artMethod1 = (Size) env->GetStaticMethodID(sizeTestClass, "method1", "()V"); Size artMethod1 = (Size) env->GetStaticMethodID(sizeTestClass, "method1", "()V");
...@@ -132,6 +136,10 @@ namespace SandHook { ...@@ -132,6 +136,10 @@ namespace SandHook {
dexMethodIndex = new CastDexMethodIndex(); dexMethodIndex = new CastDexMethodIndex();
dexMethodIndex->init(env, m1, size); dexMethodIndex->init(env, m1, size);
jclass neverCallTestClass = env->FindClass("com/swift/sandhook/ClassNeverCall");
art::mirror::ArtMethod* neverCall = reinterpret_cast<art::mirror::ArtMethod *>(env->GetMethodID(neverCallTestClass, "neverCall", "()V"));
quickToInterpreterBridge = reinterpret_cast<Code>(entryPointQuickCompiled->get(*neverCall));
// //test // //test
// art::mirror::ArtMethod** mArray = reinterpret_cast<art::mirror::ArtMethod**>(m1.dex_cache_resolved_methods_); // art::mirror::ArtMethod** mArray = reinterpret_cast<art::mirror::ArtMethod**>(m1.dex_cache_resolved_methods_);
// //
...@@ -152,6 +160,7 @@ namespace SandHook { ...@@ -152,6 +160,7 @@ namespace SandHook {
ArrayMember<art::mirror::ArtMethod>* CastArtMethod::dexCacheResolvedMethods = nullptr; ArrayMember<art::mirror::ArtMethod>* CastArtMethod::dexCacheResolvedMethods = nullptr;
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;
Code CastArtMethod::quickToInterpreterBridge = nullptr;
} }
......
#include <jni.h> #include <jni.h>
#include <string>
#include "casts/cast_art_method.h" #include "casts/cast_art_method.h"
#include "./trampoline/trampoline_manager.h" #include "./trampoline/trampoline_manager.h"
extern "C" JNIEXPORT jstring
JNICALL
Java_com_swift_sandhook_MainActivity_stringFromJNI(
JNIEnv *env,
jobject instance,
jobject method/* this */) {
std::string hello = "Hello from C++";
// art::mirror::ArtMethod* artMethod = (art::mirror::ArtMethod*) env->FromReflectedMethod(method);
return env->NewStringUTF(hello.c_str());
}extern "C"
JNIEXPORT jint JNICALL
Java_com_swift_sandhook_MainActivity_calArtSize(JNIEnv *env, jobject instance, jobject method1,
jobject method2) {
// Size artMethod1 = (Size) env->FromReflectedMethod(method1);
// Size artMethod2 = (Size) env->FromReflectedMethod(method2);
//
// Size size = artMethod2 - artMethod1;
//
// art::mirror::ArtMethod* methodTest = reinterpret_cast<art::mirror::ArtMethod*>(artMethod1);
//
// Size addr1 = reinterpret_cast<size_t>(&methodTest->ptr_sized_fields_.entry_point_from_quick_compiled_code_);
// Size addr2 = size + artMethod1 - BYTE_POINT;
return 0;
}
extern "C" void INLINE_HOOK_TRAMPOLINE();
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_MainActivity_initHook(JNIEnv *env, jobject instance) { Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk) {
INLINE_HOOK_TRAMPOLINE(); // TODO
SandHook::CastArtMethod::init(env, sdk);
SandHook::CastArtMethod::init(env); return JNI_TRUE;
} }
\ No newline at end of file
...@@ -20,9 +20,7 @@ import java.lang.reflect.Method; ...@@ -20,9 +20,7 @@ import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup. // Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -40,6 +38,8 @@ public class MainActivity extends AppCompatActivity { ...@@ -40,6 +38,8 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
SandHook.init();
try { try {
HookWrapper.addHookClass(ActivityHooker.class); HookWrapper.addHookClass(ActivityHooker.class);
} catch (HookErrorException e) { } catch (HookErrorException e) {
...@@ -48,47 +48,6 @@ public class MainActivity extends AppCompatActivity { ...@@ -48,47 +48,6 @@ public class MainActivity extends AppCompatActivity {
// Example of a call to a native method // Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text); TextView tv = (TextView) findViewById(R.id.sample_text);
try {
Method method1 = ArtMethodSizeTest.class.getDeclaredMethod("method1");
Method method2 = ArtMethodSizeTest.class.getDeclaredMethod("method2");
Field field = Class.class.getDeclaredField("dexCache");
field.setAccessible(true);
Object dexCache = field.get(ArtMethodSizeTest.class);
Field resolvedMethodsField = dexCache.getClass().getDeclaredField("resolvedMethods");
resolvedMethodsField.setAccessible(true);
// long[] methods = (long[]) resolvedMethodsField.get(dexCache);
//
//
// Field dexMethodIndexField = getField(Method.class, "dexMethodIndex");
// dexMethodIndexField.setAccessible(true);
// int dexMethodIndex = (int) dexMethodIndexField.get(method1);
//
// Field artMethodField = getField(Method.class, "artMethod");
// artMethodField.setAccessible(true);
// long artMethod = (long) artMethodField.get(method1);
//
// methods[dexMethodIndex] = artMethod;
//SandHook.init();
//initHook();
tv.setText("" + calArtSize(method1, method2));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int i= 0;i < 10;i++) {
new Thread(new Runnable() {
@Override
public void run() {
Log.e("Thread_Local", "thread_local_i = " + calArtSize(null, null));
}
}).start();
}
} }
...@@ -124,14 +83,5 @@ public class MainActivity extends AppCompatActivity { ...@@ -124,14 +83,5 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI(Method method);
public native int calArtSize(Method method1, Method method2);
public native void initHook();
} }
package com.swift.sandhook; package com.swift.sandhook;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
...@@ -17,11 +18,15 @@ public class SandHook { ...@@ -17,11 +18,15 @@ public class SandHook {
public static int testAccessFlag; public static int testAccessFlag;
static {
System.loadLibrary("native-lib");
}
public static boolean init() { public static boolean init() {
initTestOffset(); initTestOffset();
SandHookMethodResolver.init(); SandHookMethodResolver.init();
return true; return initNative(Build.VERSION.SDK_INT);
} }
public static void hook(@NonNull Method target, @NonNull Method hook, @Nullable Method backup) { public static void hook(@NonNull Method target, @NonNull Method hook, @Nullable Method backup) {
...@@ -97,5 +102,6 @@ public class SandHook { ...@@ -97,5 +102,6 @@ public class SandHook {
throw new NoSuchFieldException(fieldName); throw new NoSuchFieldException(fieldName);
} }
private static native boolean initNative(int sdk);
} }
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