Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
SandHook
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
SandHook
Commits
7617452e
Commit
7617452e
authored
May 10, 2019
by
swift_gan
Committed by
swift_gan
May 10, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tweak code
parent
1e554746
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
192 additions
and
62 deletions
+192
-62
inst_arm64.cpp
nativehook/src/main/cpp/archs/arm64/inst/inst_arm64.cpp
+27
-22
inst_arm64.h
nativehook/src/main/cpp/archs/arm64/inst/inst_arm64.h
+18
-0
inst_struct_aarch64.h
...ehook/src/main/cpp/archs/arm64/inst/inst_struct_aarch64.h
+14
-8
data.h
nativehook/src/main/cpp/asm/data.h
+25
-8
instruction.h
nativehook/src/main/cpp/asm/instruction.h
+22
-6
label.h
nativehook/src/main/cpp/asm/label.h
+7
-8
unit.h
nativehook/src/main/cpp/asm/unit.h
+20
-2
assembler.cpp
nativehook/src/main/cpp/assembler/assembler.cpp
+31
-0
assembler.h
nativehook/src/main/cpp/includes/assembler.h
+20
-0
base.h
nativehook/src/main/cpp/includes/base.h
+8
-8
No files found.
nativehook/src/main/cpp/archs/arm64/inst/inst_arm64.cpp
View file @
7617452e
...
@@ -14,6 +14,17 @@ U32 InstructionA64<InstStruct>::size() {
...
@@ -14,6 +14,17 @@ U32 InstructionA64<InstStruct>::size() {
return
sizeof
(
InstA64
);
return
sizeof
(
InstA64
);
}
}
//Unknow
A64_UNKNOW
::
A64_UNKNOW
(
A64_STRUCT_UNKNOW
*
inst
)
:
InstructionA64
(
inst
)
{
}
A64_UNKNOW
::
A64_UNKNOW
(
STRUCT_A64
(
UNKNOW
)
&
inst
)
{
inst_backup
=
inst
;
this
->
set
(
&
inst_backup
);
}
//PC Rel Inst
//PC Rel Inst
template
<
typename
Inst
>
template
<
typename
Inst
>
...
@@ -109,13 +120,21 @@ A64_B_BL::A64_B_BL(STRUCT_A64(B_BL) *inst) : A64_INST_PC_REL(inst) {
...
@@ -109,13 +120,21 @@ A64_B_BL::A64_B_BL(STRUCT_A64(B_BL) *inst) : A64_INST_PC_REL(inst) {
}
}
A64_B_BL
::
A64_B_BL
(
A64_B_BL
::
OP
op
,
Off
offset
)
:
op
(
op
),
offset
(
offset
)
{
A64_B_BL
::
A64_B_BL
(
A64_B_BL
::
OP
op
,
Off
offset
)
:
op
(
op
),
offset
(
offset
)
{
assembler
();
}
A64_B_BL
::
A64_B_BL
(
A64_B_BL
::
OP
op
,
Label
&
l
)
:
op
(
op
)
{
bindLabel
(
l
);
}
}
Off
A64_B_BL
::
getImmPCOffset
()
{
Off
A64_B_BL
::
getImmPCOffset
()
{
return
signExtend64
(
26
+
2
,
COMBINE
(
get
()
->
imm26
,
0
b00
,
2
));
return
signExtend64
(
26
+
2
,
COMBINE
(
get
()
->
imm26
,
0
b00
,
2
));
}
}
void
A64_B_BL
::
onOffsetApply
(
Off
offset
)
{
this
->
offset
=
offset
;
get
()
->
imm26
=
TruncateToUint26
(
offset
);
}
void
A64_B_BL
::
decode
(
STRUCT_A64
(
B_BL
)
*
inst
)
{
void
A64_B_BL
::
decode
(
STRUCT_A64
(
B_BL
)
*
inst
)
{
op
=
OP
(
inst
->
op
);
op
=
OP
(
inst
->
op
);
offset
=
getImmPCOffset
();
offset
=
getImmPCOffset
();
...
@@ -139,9 +158,7 @@ A64_CBZ_CBNZ::A64_CBZ_CBNZ(STRUCT_A64(CBZ_CBNZ) *inst) : A64_INST_PC_REL(inst) {
...
@@ -139,9 +158,7 @@ A64_CBZ_CBNZ::A64_CBZ_CBNZ(STRUCT_A64(CBZ_CBNZ) *inst) : A64_INST_PC_REL(inst) {
A64_CBZ_CBNZ
::
A64_CBZ_CBNZ
(
A64_CBZ_CBNZ
::
OP
op
,
Off
offset
,
RegisterA64
*
rt
)
:
op
(
op
),
A64_CBZ_CBNZ
::
A64_CBZ_CBNZ
(
A64_CBZ_CBNZ
::
OP
op
,
Off
offset
,
RegisterA64
*
rt
)
:
op
(
op
),
offset
(
offset
),
offset
(
offset
),
rt
(
rt
)
{
rt
(
rt
)
{}
assembler
();
}
Off
A64_CBZ_CBNZ
::
getImmPCOffset
()
{
Off
A64_CBZ_CBNZ
::
getImmPCOffset
()
{
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
...
@@ -174,9 +191,7 @@ A64_B_COND::A64_B_COND(STRUCT_A64(B_COND) *inst) : A64_INST_PC_REL(inst) {
...
@@ -174,9 +191,7 @@ A64_B_COND::A64_B_COND(STRUCT_A64(B_COND) *inst) : A64_INST_PC_REL(inst) {
decode
(
inst
);
decode
(
inst
);
}
}
A64_B_COND
::
A64_B_COND
(
Condition
condition
,
Off
offset
)
:
condition
(
condition
),
offset
(
offset
)
{
A64_B_COND
::
A64_B_COND
(
Condition
condition
,
Off
offset
)
:
condition
(
condition
),
offset
(
offset
)
{}
assembler
();
}
Off
A64_B_COND
::
getImmPCOffset
()
{
Off
A64_B_COND
::
getImmPCOffset
()
{
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
...
@@ -205,9 +220,7 @@ A64_TBZ_TBNZ::A64_TBZ_TBNZ(STRUCT_A64(TBZ_TBNZ) &inst) : A64_INST_PC_REL(&inst)
...
@@ -205,9 +220,7 @@ A64_TBZ_TBNZ::A64_TBZ_TBNZ(STRUCT_A64(TBZ_TBNZ) &inst) : A64_INST_PC_REL(&inst)
A64_TBZ_TBNZ
::
A64_TBZ_TBNZ
(
A64_TBZ_TBNZ
::
OP
op
,
RegisterA64
*
rt
,
U32
bit
,
Off
offset
)
:
op
(
op
),
A64_TBZ_TBNZ
::
A64_TBZ_TBNZ
(
A64_TBZ_TBNZ
::
OP
op
,
RegisterA64
*
rt
,
U32
bit
,
Off
offset
)
:
op
(
op
),
rt
(
rt
),
rt
(
rt
),
bit
(
bit
),
bit
(
bit
),
offset
(
offset
)
{
offset
(
offset
)
{}
assembler
();
}
Off
A64_TBZ_TBNZ
::
getImmPCOffset
()
{
Off
A64_TBZ_TBNZ
::
getImmPCOffset
()
{
return
signExtend64
(
14
+
2
,
COMBINE
(
get
()
->
imm14
,
0
b00
,
2
));
return
signExtend64
(
14
+
2
,
COMBINE
(
get
()
->
imm14
,
0
b00
,
2
));
...
@@ -245,9 +258,7 @@ A64_LDR_LIT::A64_LDR_LIT(STRUCT_A64(LDR_LIT) *inst) : A64_INST_PC_REL(inst) {
...
@@ -245,9 +258,7 @@ A64_LDR_LIT::A64_LDR_LIT(STRUCT_A64(LDR_LIT) *inst) : A64_INST_PC_REL(inst) {
A64_LDR_LIT
::
A64_LDR_LIT
(
A64_LDR_LIT
::
OP
op
,
RegisterA64
*
rt
,
Off
offset
)
:
op
(
op
),
rt
(
rt
),
A64_LDR_LIT
::
A64_LDR_LIT
(
A64_LDR_LIT
::
OP
op
,
RegisterA64
*
rt
,
Off
offset
)
:
op
(
op
),
rt
(
rt
),
offset
(
offset
)
{
offset
(
offset
)
{}
assembler
();
}
Off
A64_LDR_LIT
::
getImmPCOffset
()
{
Off
A64_LDR_LIT
::
getImmPCOffset
()
{
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
return
signExtend64
(
19
+
2
,
COMBINE
(
get
()
->
imm19
,
0
b00
,
2
));
...
@@ -279,14 +290,10 @@ A64_STR_IMM::A64_STR_IMM(STRUCT_A64(STR_IMM) *inst) : InstructionA64(inst) {
...
@@ -279,14 +290,10 @@ A64_STR_IMM::A64_STR_IMM(STRUCT_A64(STR_IMM) *inst) : InstructionA64(inst) {
decode
(
inst
);
decode
(
inst
);
}
}
A64_STR_IMM
::
A64_STR_IMM
(
RegisterA64
&
rt
,
const
MemOperand
&
operand
)
:
rt
(
&
rt
),
operand
(
operand
)
{
A64_STR_IMM
::
A64_STR_IMM
(
RegisterA64
&
rt
,
const
MemOperand
&
operand
)
:
rt
(
&
rt
),
operand
(
operand
)
{}
assembler
();
}
A64_STR_IMM
::
A64_STR_IMM
(
Condition
condition
,
RegisterA64
&
rt
,
const
MemOperand
&
operand
)
A64_STR_IMM
::
A64_STR_IMM
(
Condition
condition
,
RegisterA64
&
rt
,
const
MemOperand
&
operand
)
:
condition
(
condition
),
rt
(
&
rt
),
operand
(
operand
)
{
:
condition
(
condition
),
rt
(
&
rt
),
operand
(
operand
)
{}
assembler
();
}
AddrMode
A64_STR_IMM
::
decodeAddrMode
()
{
AddrMode
A64_STR_IMM
::
decodeAddrMode
()
{
if
(
get
()
->
P
==
1
&&
get
()
->
W
==
0
)
{
if
(
get
()
->
P
==
1
&&
get
()
->
W
==
0
)
{
...
@@ -325,9 +332,7 @@ A64_BR_BLR_RET::A64_BR_BLR_RET(STRUCT_A64(BR_BLR_RET) &inst) : InstructionA64(&i
...
@@ -325,9 +332,7 @@ A64_BR_BLR_RET::A64_BR_BLR_RET(STRUCT_A64(BR_BLR_RET) &inst) : InstructionA64(&i
decode
(
&
inst
);
decode
(
&
inst
);
}
}
A64_BR_BLR_RET
::
A64_BR_BLR_RET
(
A64_BR_BLR_RET
::
OP
op
,
XRegister
&
rn
)
:
op
(
op
),
rn
(
&
rn
)
{
A64_BR_BLR_RET
::
A64_BR_BLR_RET
(
A64_BR_BLR_RET
::
OP
op
,
XRegister
&
rn
)
:
op
(
op
),
rn
(
&
rn
)
{}
assembler
();
}
void
A64_BR_BLR_RET
::
decode
(
A64_STRUCT_BR_BLR_RET
*
inst
)
{
void
A64_BR_BLR_RET
::
decode
(
A64_STRUCT_BR_BLR_RET
*
inst
)
{
rn
=
XReg
(
static_cast
<
U8
>
(
inst
->
op
));
rn
=
XReg
(
static_cast
<
U8
>
(
inst
->
op
));
...
...
nativehook/src/main/cpp/archs/arm64/inst/inst_arm64.h
View file @
7617452e
...
@@ -152,6 +152,20 @@ namespace SandHook {
...
@@ -152,6 +152,20 @@ namespace SandHook {
};
};
class
INST_A64
(
UNKNOW
)
:
public
InstructionA64
<
STRUCT_A64
(
UNKNOW
)
>
{
public
:
A64_UNKNOW
(
STRUCT_A64
(
UNKNOW
)
*
inst
);
A64_UNKNOW
(
STRUCT_A64
(
UNKNOW
)
&
inst
);
inline
bool
unknow
()
override
{
return
true
;
}
private
:
STRUCT_A64
(
UNKNOW
)
inst_backup
;
};
template
<
typename
Inst
>
template
<
typename
Inst
>
class
A64_INST_PC_REL
:
public
InstructionA64
<
Inst
>
{
class
A64_INST_PC_REL
:
public
InstructionA64
<
Inst
>
{
public
:
public
:
...
@@ -287,6 +301,8 @@ namespace SandHook {
...
@@ -287,6 +301,8 @@ namespace SandHook {
A64_B_BL
(
OP
op
,
Off
offset
);
A64_B_BL
(
OP
op
,
Off
offset
);
A64_B_BL
(
OP
op
,
Label
&
l
);
DEFINE_IS
(
B_BL
)
DEFINE_IS
(
B_BL
)
inline
Off
getOffset
()
{
inline
Off
getOffset
()
{
...
@@ -303,6 +319,8 @@ namespace SandHook {
...
@@ -303,6 +319,8 @@ namespace SandHook {
Off
getImmPCOffset
()
override
;
Off
getImmPCOffset
()
override
;
void
onOffsetApply
(
Off
offset
)
override
;
void
decode
(
STRUCT_A64
(
B_BL
)
*
decode
)
override
;
void
decode
(
STRUCT_A64
(
B_BL
)
*
decode
)
override
;
void
assembler
()
override
;
void
assembler
()
override
;
...
...
nativehook/src/main/cpp/archs/arm64/inst/inst_struct_aarch64.h
View file @
7617452e
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
#define STRUCT_A64(X) A64_STRUCT_##X
#define STRUCT_A64(X) A64_STRUCT_##X
#define OPCODE_A64(X) A64_OPCODE_##X
#define OPCODE_A64(X) A64_OPCODE_##X
#define DEFINE_OPCODE(X, V) const U32 OPCODE_A64(X) = V;
#define DEFINE_OPCODE(X, V) const U32 OPCODE_A64(X) = V;
#define DEFINE_STRUCT_A64(X) struct STRUCT_A64(X) : public Base
enum
InstructionFields
{
enum
InstructionFields
{
...
@@ -86,6 +87,11 @@ enum Extend {
...
@@ -86,6 +87,11 @@ enum Extend {
SXTX
=
7
SXTX
=
7
};
};
//unknow inst
DEFINE_STRUCT_A64
(
UNKNOW
)
{
InstA64
raw
;
};
#define IMM_LO_W 2
#define IMM_LO_W 2
#define IMM_HI_W 19
#define IMM_HI_W 19
DEFINE_OPCODE
(
ADR_ADRP
,
0
b10000
)
DEFINE_OPCODE
(
ADR_ADRP
,
0
b10000
)
...
@@ -98,7 +104,7 @@ struct STRUCT_A64(ADR_ADRP) {
...
@@ -98,7 +104,7 @@ struct STRUCT_A64(ADR_ADRP) {
};
};
DEFINE_OPCODE
(
MOV_WIDE
,
0
b100101
)
DEFINE_OPCODE
(
MOV_WIDE
,
0
b100101
)
struct
STRUCT_A64
(
MOV_WIDE
)
{
DEFINE_
STRUCT_A64
(
MOV_WIDE
)
{
InstA64
rd
:
5
;
InstA64
rd
:
5
;
InstA64
imm16
:
16
;
InstA64
imm16
:
16
;
InstA64
hw
:
2
;
InstA64
hw
:
2
;
...
@@ -108,14 +114,14 @@ struct STRUCT_A64(MOV_WIDE) {
...
@@ -108,14 +114,14 @@ struct STRUCT_A64(MOV_WIDE) {
};
};
DEFINE_OPCODE
(
B_BL
,
0
b00101
)
DEFINE_OPCODE
(
B_BL
,
0
b00101
)
struct
STRUCT_A64
(
B_BL
)
{
DEFINE_
STRUCT_A64
(
B_BL
)
{
InstA64
imm26
:
26
;
InstA64
imm26
:
26
;
InstA64
opcode
:
5
;
InstA64
opcode
:
5
;
InstA64
op
:
1
;
InstA64
op
:
1
;
};
};
DEFINE_OPCODE
(
CBZ_CBNZ
,
0
b011010
)
DEFINE_OPCODE
(
CBZ_CBNZ
,
0
b011010
)
struct
STRUCT_A64
(
CBZ_CBNZ
)
{
DEFINE_
STRUCT_A64
(
CBZ_CBNZ
)
{
InstA64
rt
:
5
;
InstA64
rt
:
5
;
InstA64
imm19
:
19
;
InstA64
imm19
:
19
;
InstA64
op
:
1
;
InstA64
op
:
1
;
...
@@ -124,7 +130,7 @@ struct STRUCT_A64(CBZ_CBNZ) {
...
@@ -124,7 +130,7 @@ struct STRUCT_A64(CBZ_CBNZ) {
};
};
DEFINE_OPCODE
(
B_COND
,
0
b01010100
)
DEFINE_OPCODE
(
B_COND
,
0
b01010100
)
struct
STRUCT_A64
(
B_COND
)
{
DEFINE_
STRUCT_A64
(
B_COND
)
{
InstA64
cond
:
4
;
InstA64
cond
:
4
;
InstA64
unkown_0
:
1
;
InstA64
unkown_0
:
1
;
InstA64
imm19
:
19
;
InstA64
imm19
:
19
;
...
@@ -132,7 +138,7 @@ struct STRUCT_A64(B_COND) {
...
@@ -132,7 +138,7 @@ struct STRUCT_A64(B_COND) {
};
};
DEFINE_OPCODE
(
TBZ_TBNZ
,
0
b011011
)
DEFINE_OPCODE
(
TBZ_TBNZ
,
0
b011011
)
struct
STRUCT_A64
(
TBZ_TBNZ
)
{
DEFINE_
STRUCT_A64
(
TBZ_TBNZ
)
{
InstA64
rt
:
5
;
InstA64
rt
:
5
;
InstA64
imm14
:
14
;
InstA64
imm14
:
14
;
InstA64
b40
:
5
;
InstA64
b40
:
5
;
...
@@ -142,7 +148,7 @@ struct STRUCT_A64(TBZ_TBNZ) {
...
@@ -142,7 +148,7 @@ struct STRUCT_A64(TBZ_TBNZ) {
};
};
DEFINE_OPCODE
(
LDR_LIT
,
0
b011000
)
DEFINE_OPCODE
(
LDR_LIT
,
0
b011000
)
struct
STRUCT_A64
(
LDR_LIT
)
{
DEFINE_
STRUCT_A64
(
LDR_LIT
)
{
InstA64
rt
:
5
;
InstA64
rt
:
5
;
InstA64
imm19
:
19
;
InstA64
imm19
:
19
;
InstA64
opcode
:
6
;
InstA64
opcode
:
6
;
...
@@ -150,7 +156,7 @@ struct STRUCT_A64(LDR_LIT) {
...
@@ -150,7 +156,7 @@ struct STRUCT_A64(LDR_LIT) {
};
};
DEFINE_OPCODE
(
STR_IMM
,
0
b011
)
DEFINE_OPCODE
(
STR_IMM
,
0
b011
)
struct
STRUCT_A64
(
STR_IMM
)
{
DEFINE_
STRUCT_A64
(
STR_IMM
)
{
InstA64
imm12
:
12
;
InstA64
imm12
:
12
;
InstA64
rt
:
4
;
InstA64
rt
:
4
;
InstA64
rn
:
4
;
InstA64
rn
:
4
;
...
@@ -166,7 +172,7 @@ struct STRUCT_A64(STR_IMM) {
...
@@ -166,7 +172,7 @@ struct STRUCT_A64(STR_IMM) {
DEFINE_OPCODE
(
BR_BLR_RET_1
,
0
b110101100
)
DEFINE_OPCODE
(
BR_BLR_RET_1
,
0
b110101100
)
DEFINE_OPCODE
(
BR_BLR_RET_2
,
0
b11111000000
)
DEFINE_OPCODE
(
BR_BLR_RET_2
,
0
b11111000000
)
DEFINE_OPCODE
(
BR_BLR_RET_3
,
0
b00000
)
DEFINE_OPCODE
(
BR_BLR_RET_3
,
0
b00000
)
struct
STRUCT_A64
(
BR_BLR_RET
)
{
DEFINE_
STRUCT_A64
(
BR_BLR_RET
)
{
InstA64
opcode3
:
5
;
InstA64
opcode3
:
5
;
InstA64
rn
:
5
;
InstA64
rn
:
5
;
InstA64
opcode2
:
11
;
InstA64
opcode2
:
11
;
...
...
nativehook/src/main/cpp/asm/data.h
View file @
7617452e
...
@@ -7,31 +7,48 @@
...
@@ -7,31 +7,48 @@
#include "unit.h"
#include "unit.h"
#define DATA(BITS) STRUCT_DATA_##BITS
#define DEFINE_DATA(BITS) \
struct DATA(BITS) : public Base { \
U##BITS raw_; \
public: \
DATA(BITS)(U##BITS r) { \
raw_ = r; \
} \
};
namespace
SandHook
{
namespace
SandHook
{
namespace
Asm
{
namespace
Asm
{
template
<
typename
DType
>
template
<
typename
DType
>
class
Data
:
public
Unit
<
DType
>
{
class
Data
:
public
Unit
<
DType
>
{
public
:
public
:
Data
(
DType
raw
)
:
Unit
<
DType
>
(
raw
)
{}
Data
(
DType
raw
)
:
Unit
<
DType
>
()
{
this
->
set
(
raw
);
}
inline
UnitType
unitType
()
override
{
inline
UnitType
unitType
()
override
{
return
UnitType
::
Data
;
return
UnitType
::
Unit
Data
;
};
};
};
};
class
Data16
:
public
Data
<
U16
>
{
DEFINE_DATA
(
16
)
class
Data16
:
public
Data
<
DATA
(
16
)
>
{
public
:
public
:
Data16
(
U16
raw
)
:
Data
(
raw
)
{}
Data16
(
U16
raw
)
:
Data
(
DATA
(
16
)(
raw
)
)
{}
};
};
class
Data32
:
public
Data
<
U32
>
{
DEFINE_DATA
(
32
)
class
Data32
:
public
Data
<
DATA
(
32
)
>
{
public
:
public
:
Data32
(
U32
raw
)
:
Data
(
raw
)
{}
Data32
(
U32
raw
)
:
Data
(
DATA
(
32
)(
raw
)
)
{}
};
};
class
Data64
:
public
Data
<
U64
>
{
DEFINE_DATA
(
64
)
class
Data64
:
public
Data
<
DATA
(
64
)
>
{
public
:
public
:
Data64
(
U64
raw
)
:
Data
(
raw
)
{}
Data64
(
U64
raw
)
:
Data
(
DATA
(
64
)(
raw
)
)
{}
};
};
}
}
...
...
nativehook/src/main/cpp/asm/instruction.h
View file @
7617452e
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
#define SANDHOOK_INSTRUCTION_H
#define SANDHOOK_INSTRUCTION_H
#include "unit.h"
#include "unit.h"
#include "label.h"
//aarch64
//aarch64
typedef
U32
InstA64
;
typedef
U32
InstA64
;
...
@@ -36,23 +37,22 @@ namespace SandHook {
...
@@ -36,23 +37,22 @@ namespace SandHook {
namespace
Asm
{
namespace
Asm
{
template
<
typename
Inst
>
template
<
typename
Inst
>
class
Instruction
:
public
Unit
<
Inst
>
{
class
Instruction
:
public
Unit
<
Inst
>
,
public
LabelBinder
{
public
:
public
:
Instruction
()
{}
Instruction
()
{}
Instruction
(
Inst
*
inst
)
:
Unit
<
Inst
>
(
inst
)
{}
Instruction
(
Inst
*
inst
)
:
Unit
<
Inst
>
(
inst
)
{}
UnitType
unitType
()
override
{
UnitType
unitType
()
override
{
return
UnitType
::
Inst
;
return
UnitType
::
Unit
Inst
;
};
};
virtual
InstType
instType
()
{
virtual
InstType
instType
()
{
return
unk
own
Inst
;
return
unk
now
Inst
;
}
}
virtual
Arch
arch
()
{
virtual
Arch
arch
()
{
return
unk
own
Arch
;
return
unk
now
Arch
;
}
}
virtual
U32
instCode
()
{
virtual
U32
instCode
()
{
...
@@ -63,19 +63,35 @@ namespace SandHook {
...
@@ -63,19 +63,35 @@ namespace SandHook {
return
false
;
return
false
;
}
}
virtual
bool
unknow
()
{
return
false
;
}
inline
bool
isValid
()
const
{
inline
bool
isValid
()
const
{
return
valid
;
return
valid
;
}
}
virtual
void
onOffsetApply
(
Off
offset
)
{}
void
onLabelApply
(
void
*
pc
)
override
{
onOffsetApply
((
Addr
)
pc
-
(
Addr
)
this
->
getPC
());
}
inline
void
bindLabel
(
Label
&
l
)
{
label
=
&
l
;
l
.
addBinder
(
this
);
}
virtual
void
decode
(
Inst
*
inst
)
{}
virtual
void
decode
(
Inst
*
inst
)
{}
virtual
void
assembler
()
{}
virtual
void
assembler
()
{}
protected
:
protected
:
bool
valid
=
true
;
bool
valid
=
true
;
Label
*
label
=
nullptr
;
};
};
class
Void
:
public
Unit
<
Non
e
>
{
class
Void
:
public
Unit
<
Bas
e
>
{
public
:
public
:
Void
(
U32
size
)
:
size_
(
size
)
{}
Void
(
U32
size
)
:
size_
(
size
)
{}
...
...
nativehook/src/main/cpp/asm/label.h
View file @
7617452e
...
@@ -11,12 +11,12 @@
...
@@ -11,12 +11,12 @@
namespace
SandHook
{
namespace
SandHook
{
namespace
Asm
{
namespace
Asm
{
virtual
class
LabelBinder
{
class
LabelBinder
{
public
:
public
:
virtual
void
bindLabel
(
void
*
pc
)
=
0
;
virtual
void
onLabelApply
(
void
*
pc
)
=
0
;
};
};
class
Label
:
public
Unit
<
Non
e
>
{
class
Label
:
public
Unit
<
Bas
e
>
{
public
:
public
:
Label
()
{}
Label
()
{}
...
@@ -24,7 +24,7 @@ namespace SandHook {
...
@@ -24,7 +24,7 @@ namespace SandHook {
Label
(
void
*
pc
)
:
pc
(
pc
)
{}
Label
(
void
*
pc
)
:
pc
(
pc
)
{}
inline
UnitType
unitType
()
override
{
inline
UnitType
unitType
()
override
{
return
UnitType
::
Label
;
return
UnitType
::
Unit
Label
;
}
}
inline
U32
size
()
override
{
inline
U32
size
()
override
{
...
@@ -47,17 +47,16 @@ namespace SandHook {
...
@@ -47,17 +47,16 @@ namespace SandHook {
binders
.
push_back
(
binder
);
binders
.
push_back
(
binder
);
}
}
inline
void
bindLabel
(
void
*
pc
)
{
inline
void
bindLabel
()
{
setPC
(
pc
);
std
::
list
<
LabelBinder
*>::
iterator
binder
;
std
::
list
<
LabelBinder
*>::
iterator
binder
;
for
(
binder
=
binders
.
begin
();
binder
!=
binders
.
end
();
++
binder
)
{
for
(
binder
=
binders
.
begin
();
binder
!=
binders
.
end
();
++
binder
)
{
(
*
binder
)
->
bindLabel
(
pc
);
(
*
binder
)
->
onLabelApply
(
pc
);
}
}
}
}
private
:
private
:
void
*
pc
;
void
*
pc
;
std
::
list
<
LabelBinder
*>
binders
=
std
::
list
();
std
::
list
<
LabelBinder
*>
binders
=
std
::
list
<
LabelBinder
*>
();
};
};
}
}
...
...
nativehook/src/main/cpp/asm/unit.h
View file @
7617452e
...
@@ -16,7 +16,7 @@ namespace SandHook {
...
@@ -16,7 +16,7 @@ namespace SandHook {
public
:
public
:
Unit
()
{
Unit
()
{
if
(
unitType
()
!=
Void
)
{
if
(
unitType
()
!=
Unit
Void
)
{
raw
=
reinterpret_cast
<
Raw
*>
(
malloc
(
size
()));
raw
=
reinterpret_cast
<
Raw
*>
(
malloc
(
size
()));
memset
(
raw
,
0
,
size
());
memset
(
raw
,
0
,
size
());
auto_alloc
=
true
;
auto_alloc
=
true
;
...
@@ -42,12 +42,30 @@ namespace SandHook {
...
@@ -42,12 +42,30 @@ namespace SandHook {
*
this
->
raw
=
raw
;
*
this
->
raw
=
raw
;
}
}
inline
void
set
(
Raw
*
raw
)
{
if
(
auto_alloc
)
{
free
(
this
->
raw
);
auto_alloc
=
false
;
}
this
->
raw
=
raw
;
}
inline
void
copy
(
void
*
dest
)
{
inline
void
copy
(
void
*
dest
)
{
memcpy
(
dest
,
getPC
(),
size
());
memcpy
(
dest
,
getPC
(),
size
());
}
}
inline
void
move
(
Raw
*
dest
)
{
memcpy
(
dest
,
raw
,
size
());
if
(
auto_alloc
)
{
free
(
raw
);
auto_alloc
=
false
;
}
raw
=
dest
;
}
virtual
UnitType
unitType
()
{
virtual
UnitType
unitType
()
{
return
UnitType
::
Un
kown
;
return
UnitType
::
Un
itUnknow
;
};
};
virtual
U32
size
()
{
virtual
U32
size
()
{
...
...
nativehook/src/main/cpp/assembler/assembler.cpp
View file @
7617452e
...
@@ -2,3 +2,34 @@
...
@@ -2,3 +2,34 @@
// Created by swift on 2019/5/10.
// Created by swift on 2019/5/10.
//
//
#include <assembler.h>
using
namespace
SandHook
::
Assembler
;
using
namespace
SandHook
::
Asm
;
void
CodeContainer
::
append
(
Unit
<
Base
>
*
unit
)
{
units
.
push_back
(
unit
);
switch
(
unit
->
unitType
())
{
case
UnitLabel
:
labels
.
push_back
((
Label
*
)
unit
);
break
;
default
:
curPc
+=
unit
->
size
();
}
if
(
unit
->
unitType
()
==
UnitData
)
{
unit
->
move
((
Base
*
)
codeBuffer
->
getBuffer
(
unit
));
}
else
if
(
unit
->
unitType
()
!=
UnitLabel
)
{
unit
->
set
((
Base
*
)
codeBuffer
->
getBuffer
(
unit
));
}
}
void
CodeContainer
::
commit
()
{
std
::
list
<
Label
*>::
iterator
label
;
for
(
label
=
labels
.
begin
();
label
!=
labels
.
end
();
++
label
)
{
(
*
label
)
->
bindLabel
();
}
}
void
*
CodeBuffer
::
getBuffer
(
Unit
<
Base
>
*
unit
)
{
return
nullptr
;
}
nativehook/src/main/cpp/includes/assembler.h
View file @
7617452e
...
@@ -5,8 +5,14 @@
...
@@ -5,8 +5,14 @@
#ifndef SANDHOOK_NH_ASSEMBLER_H
#ifndef SANDHOOK_NH_ASSEMBLER_H
#define SANDHOOK_NH_ASSEMBLER_H
#define SANDHOOK_NH_ASSEMBLER_H
#include "label.h"
#include "instruction.h"
#include "data.h"
namespace
SandHook
{
namespace
SandHook
{
using
namespace
Asm
;
namespace
Assembler
{
namespace
Assembler
{
class
Assembler
{
class
Assembler
{
...
@@ -14,7 +20,21 @@ namespace SandHook {
...
@@ -14,7 +20,21 @@ namespace SandHook {
};
};
class
CodeBuffer
{
class
CodeBuffer
{
public
:
void
*
getBuffer
(
Unit
<
Base
>*
unit
);
};
class
CodeContainer
{
public
:
void
append
(
Unit
<
Base
>*
unit
);
void
commit
();
private
:
void
*
startPc
=
nullptr
;
Addr
curPc
=
0
;
Addr
maxSize
=
0
;
std
::
list
<
Unit
<
Base
>*>
units
=
std
::
list
<
Unit
<
Base
>*>
();
std
::
list
<
Label
*>
labels
=
std
::
list
<
Label
*>
();
CodeBuffer
*
codeBuffer
=
nullptr
;
};
};
...
...
nativehook/src/main/cpp/includes/base.h
View file @
7617452e
...
@@ -33,15 +33,15 @@ const Addr PAGE_SIZE = 2 << PAGE_OFFSET;
...
@@ -33,15 +33,15 @@ const Addr PAGE_SIZE = 2 << PAGE_OFFSET;
enum
Arch
{
enum
Arch
{
arm32
,
arm32
,
arm64
,
arm64
,
unk
own
Arch
unk
now
Arch
};
};
enum
UnitType
{
enum
UnitType
{
Inst
,
Unit
Inst
,
Data
,
Unit
Data
,
Label
,
Unit
Label
,
Void
,
Unit
Void
,
Un
kown
Un
itUnknow
};
};
enum
InstType
{
enum
InstType
{
...
@@ -49,7 +49,7 @@ enum InstType {
...
@@ -49,7 +49,7 @@ enum InstType {
thumb16
,
thumb16
,
thumb32
,
thumb32
,
A64
,
A64
,
unk
own
Inst
unk
now
Inst
};
};
template
<
int
SizeInBits
>
template
<
int
SizeInBits
>
...
@@ -71,7 +71,7 @@ struct Unsigned<64> {
...
@@ -71,7 +71,7 @@ struct Unsigned<64> {
};
};
class
Non
e
{};
struct
Bas
e
{};
template
<
typename
T
>
template
<
typename
T
>
T
AlignDown
(
T
pointer
,
T
AlignDown
(
T
pointer
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment