Commit 746e516a authored by swift_gan's avatar swift_gan Committed by swift_gan

tweak code

parent 7617452e
...@@ -52,13 +52,13 @@ Off A64_ADR_ADRP::getImmPCOffset() { ...@@ -52,13 +52,13 @@ Off A64_ADR_ADRP::getImmPCOffset() {
U32 lo = get()->immlo; U32 lo = get()->immlo;
Off offset = signExtend64(IMM_LO_W + IMM_HI_W, COMBINE(hi, lo, IMM_LO_W)); Off offset = signExtend64(IMM_LO_W + IMM_HI_W, COMBINE(hi, lo, IMM_LO_W));
if (isADRP()) { if (isADRP()) {
offset *= PAGE_SIZE; offset *= P_SIZE;
} }
return offset; return offset;
} }
Addr A64_ADR_ADRP::getImmPCOffsetTarget() { Addr A64_ADR_ADRP::getImmPCOffsetTarget() {
void * base = AlignDown(getPC(), PAGE_SIZE); void * base = AlignDown(getPC(), P_SIZE);
return getImmPCOffset() + reinterpret_cast<Addr>(base); return getImmPCOffset() + reinterpret_cast<Addr>(base);
} }
...@@ -132,7 +132,7 @@ Off A64_B_BL::getImmPCOffset() { ...@@ -132,7 +132,7 @@ Off A64_B_BL::getImmPCOffset() {
void A64_B_BL::onOffsetApply(Off offset) { void A64_B_BL::onOffsetApply(Off offset) {
this->offset = offset; this->offset = offset;
get()->imm26 = TruncateToUint26(offset); get()->imm26 = TruncateToUint26(offset >> 2);
} }
void A64_B_BL::decode(STRUCT_A64(B_BL) *inst) { void A64_B_BL::decode(STRUCT_A64(B_BL) *inst) {
...@@ -143,7 +143,7 @@ void A64_B_BL::decode(STRUCT_A64(B_BL) *inst) { ...@@ -143,7 +143,7 @@ void A64_B_BL::decode(STRUCT_A64(B_BL) *inst) {
void A64_B_BL::assembler() { void A64_B_BL::assembler() {
SET_OPCODE(B_BL); SET_OPCODE(B_BL);
get()->op = op; get()->op = op;
get()->imm26 = TruncateToUint26(offset); get()->imm26 = TruncateToUint26(offset >> 4);
} }
...@@ -179,7 +179,7 @@ void A64_CBZ_CBNZ::assembler() { ...@@ -179,7 +179,7 @@ void A64_CBZ_CBNZ::assembler() {
get()->op = op; get()->op = op;
get()->rt = rt->getCode(); get()->rt = rt->getCode();
get()->sf = rt->is64Bit() ? 1 : 0; get()->sf = rt->is64Bit() ? 1 : 0;
get()->imm19 = TruncateToUint19(offset); get()->imm19 = TruncateToUint19(offset >> 2);
} }
...@@ -205,7 +205,7 @@ void A64_B_COND::decode(STRUCT_A64(B_COND) *inst) { ...@@ -205,7 +205,7 @@ void A64_B_COND::decode(STRUCT_A64(B_COND) *inst) {
void A64_B_COND::assembler() { void A64_B_COND::assembler() {
SET_OPCODE(B_COND); SET_OPCODE(B_COND);
get()->cond = condition; get()->cond = condition;
get()->imm19 = TruncateToUint19(offset); get()->imm19 = TruncateToUint19(offset >> 2);
} }
...@@ -243,7 +243,7 @@ void A64_TBZ_TBNZ::assembler() { ...@@ -243,7 +243,7 @@ void A64_TBZ_TBNZ::assembler() {
get()->b5 = rt->is64Bit() ? 1 : 0; get()->b5 = rt->is64Bit() ? 1 : 0;
get()->rt = rt->getCode(); get()->rt = rt->getCode();
get()->b40 = static_cast<InstA64>(BITS(bit, sizeof(InstA64) - 5, sizeof(InstA64))); get()->b40 = static_cast<InstA64>(BITS(bit, sizeof(InstA64) - 5, sizeof(InstA64)));
get()->imm14 = TruncateToUint14(offset); get()->imm14 = TruncateToUint14(offset >> 2);
} }
...@@ -278,7 +278,7 @@ void A64_LDR_LIT::assembler() { ...@@ -278,7 +278,7 @@ void A64_LDR_LIT::assembler() {
SET_OPCODE(LDR_LIT); SET_OPCODE(LDR_LIT);
get()->rt = rt->getCode(); get()->rt = rt->getCode();
get()->op = op; get()->op = op;
get()->imm19 = TruncateToUint19(offset); get()->imm19 = TruncateToUint19(offset >> 2);
} }
......
...@@ -8,6 +8,7 @@ using namespace SandHook::Assembler; ...@@ -8,6 +8,7 @@ using namespace SandHook::Assembler;
using namespace SandHook::Asm; using namespace SandHook::Asm;
void CodeContainer::append(Unit<Base> *unit) { void CodeContainer::append(Unit<Base> *unit) {
Label* l = dynamic_cast<Label *>(unit);
units.push_back(unit); units.push_back(unit);
switch (unit->unitType()) { switch (unit->unitType()) {
case UnitLabel: case UnitLabel:
......
...@@ -28,7 +28,7 @@ const int PTR_BYTE = sizeof(void*); ...@@ -28,7 +28,7 @@ const int PTR_BYTE = sizeof(void*);
const int BITS_OF_BYTE = 8; const int BITS_OF_BYTE = 8;
const Addr PAGE_SIZE = 2 << PAGE_OFFSET; const Addr P_SIZE = 2 << PAGE_OFFSET;
enum Arch { enum Arch {
arm32, arm32,
......
...@@ -3,12 +3,23 @@ ...@@ -3,12 +3,23 @@
// //
#include <jni.h> #include <jni.h>
#include <sys/mman.h>
#include "sandhook_native.h" #include "sandhook_native.h"
#include "inst_arm64.h" #include "inst_arm64.h"
bool memUnprotect(Addr addr, Addr len) {
long pagesize = 4096;
unsigned alignment = (unsigned)((unsigned long long)addr % pagesize);
int i = mprotect((void *) (addr - alignment), (size_t) (alignment + len),
PROT_READ | PROT_WRITE | PROT_EXEC);
if (i == -1) {
return false;
}
return true;
}
void do2() { void do2() {
int a = 1 + 1;
} }
void do1() { void do1() {
...@@ -19,22 +30,28 @@ extern "C" ...@@ -19,22 +30,28 @@ extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_nativehook_NativeHook_test(JNIEnv *env, jclass jclass1) { Java_com_swift_sandhook_nativehook_NativeHook_test(JNIEnv *env, jclass jclass1) {
do1();
U8 s = sizeof(STRUCT_A64(B_BL));
union { union {
InstA64 raw = 0x36A01005; InstA64 raw = 0x36A01005;
STRUCT_A64(TBZ_TBNZ) bl; STRUCT_A64(TBZ_TBNZ) bl;
} test; } test;
if (IS_OPCODE(test.raw, TBZ_TBNZ)) { InstA64* codebl = reinterpret_cast<InstA64 *>((Addr)do1 + 8);
if (IS_OPCODE(*codebl, B_BL)) {
//decode
A64_B_BL a64bl(reinterpret_cast<STRUCT_A64(B_BL)*>(codebl));
Off off = a64bl.offset;
void (*dosth2)() =reinterpret_cast<void (*)()>(a64bl.getImmPCOffsetTarget());
dosth2();
STRUCT_A64(TBZ_TBNZ) bl = test.bl; //asm
memUnprotect(reinterpret_cast<Addr>(a64bl.get()), a64bl.size());
a64bl.assembler();
Off off1 = a64bl.getImmPCOffset();
A64_TBZ_TBNZ a64ldr(bl); do1();
Off off = a64ldr.offset;
} }
} }
\ No newline at end of file
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