Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x86 RzIL uplifting #2747

Merged
merged 102 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 98 commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
f3c873c
Setup initial headers and typedefs
DMaroo Jun 21, 2022
a7fee4d
Add x86 register names
DMaroo Jun 27, 2022
47fb613
Implement IL for `AAA`
DMaroo Jun 30, 2022
2b62160
Implement IL for `AAM` and `AAD`
DMaroo Jun 30, 2022
9f4d05e
Implement IL for `AAS`
DMaroo Jul 1, 2022
782d65f
Add `x86_il_{set,get}_gpr` to allow for setting and getting overlappi…
DMaroo Jul 1, 2022
1a1d9b7
Use `x86_il_{get,set}_reg` for getting operands
DMaroo Jul 1, 2022
f05778b
Implement `x86_il_is_{add,sub}_{underflow,overflow,carry,borrow}`
DMaroo Jul 4, 2022
b83bb67
Implement `ADC` in IL
DMaroo Jul 4, 2022
48b04c5
Add utility functions to set flags
DMaroo Jul 4, 2022
0205ada
Implement `ADD` instruction
DMaroo Jul 4, 2022
f09a9ff
Implement IL for the following instructions
DMaroo Jul 5, 2022
5ce0ad9
Implement `CMP`, `CMPSB`, `CMPSW`, `DAA`
DMaroo Jul 5, 2022
126928d
Integrate IL with analysis
DMaroo Aug 6, 2022
e51edf2
Use flags as IL variables instead of `eflags`
DMaroo Aug 7, 2022
6ed911e
Remove unnecessary `DUP`s
DMaroo Aug 9, 2022
1aba20b
Remove unnecessary casting; Add asm test for x86_16
DMaroo Aug 9, 2022
4f1eb24
Cast immediate value to the correct length
DMaroo Aug 9, 2022
5649468
Add asm tests for `AAD`
DMaroo Aug 12, 2022
2965f56
Use `REPEAT` effect to set the parity bit
DMaroo Aug 14, 2022
867ce1a
Add tests for `AAD`
DMaroo Aug 14, 2022
39c4699
Fix assembly of `AAD` instruction
DMaroo Aug 14, 2022
6e30d07
Add asm tests for `AAS`
DMaroo Aug 14, 2022
03d4ddb
Fix register profile for 32 bit x86
DMaroo Aug 14, 2022
cfcbe0b
Use correct operand size for the IL params in `x86_il_get_operand_bits`
DMaroo Aug 14, 2022
da40f93
Add tests for `ADC` instructions
DMaroo Aug 25, 2022
9798cda
Add tests for `ADD` instruction
DMaroo Aug 25, 2022
d004c75
Add tests for `AND` instruction
DMaroo Aug 25, 2022
35f6363
Add IL tests for `CBW`, `CLC`, `CLD`, `CLI`
DMaroo Aug 25, 2022
282d79b
Implement segmentation fro real mode (32-bit)
DMaroo Aug 25, 2022
0c861dd
Fix `CMPSW` and `DAA` ; Add asm tests for `DAA`
DMaroo Aug 25, 2022
febd72b
Add asm test and lifting for `DAS` instruction
DMaroo Aug 27, 2022
55c4d1f
Implement and add tests for `DEC` instruction
DMaroo Aug 27, 2022
29ca693
Implement `DIV` instruction and add tests
DMaroo Aug 27, 2022
a3824a3
Implement `HLT` and `IDIV` and add asm tests
DMaroo Aug 29, 2022
fd62af6
Move the arrays back into the source files ; To avoid compile warnings
DMaroo Aug 30, 2022
22b216d
Add IL implementation for `IMUL` and add asm tests
DMaroo Aug 30, 2022
095c385
Implement IL lifting for `INC` and add asm tests
DMaroo Aug 30, 2022
e92da7b
Add empty implementation and tests for `INT`
DMaroo Aug 30, 2022
eeb3247
Implement empty `GOTO` in `INT` and `HLT` ; also update tests
DMaroo Aug 31, 2022
c3d5aae
Implement lifter and asm tests for `INTO`
DMaroo Aug 31, 2022
5a46ee6
Use macro for IL lifter definition
DMaroo Aug 31, 2022
d48902b
Implement IL lifting and asm tests for conditional jump instructions
DMaroo Sep 2, 2022
d069c72
Implement `JMP` instruction IL lifting and asm tests
DMaroo Sep 3, 2022
f181a60
Implement `LAHF` and `LDS`
DMaroo Sep 3, 2022
8ab51e6
Implement `LEA`, `LES` and `LODSB`
DMaroo Sep 3, 2022
25d38d5
Implement `LODSW` IL lifting
DMaroo Sep 3, 2022
0a938ce
Add dummy implementation and asm tests for `LOOP{,E,NE}` instructions
DMaroo Sep 3, 2022
2818c1a
Implement `MOV`
DMaroo Sep 3, 2022
33f5c75
Implement `MOVSB` and `MOVSW`
DMaroo Sep 3, 2022
02e7e64
Fix broken asm tests
DMaroo Sep 3, 2022
df33e33
Implement IL lifting for `MUL`, `NEG`, `NOP`, `NOT`, `OR`
DMaroo Sep 4, 2022
3f1fe7a
Add dummy implementation and asm tests for `OUT` instruction
DMaroo Sep 4, 2022
0a15548
Implement `POP`, `POPF{,D,Q}`
DMaroo Sep 5, 2022
ef7ccb7
Implement `PUSH` family instructions
DMaroo Sep 12, 2022
b014a98
Implement IL lifting for `RCL`, `RCR`, `ROL`, `ROR`
DMaroo Sep 14, 2022
54b061d
Implement `RET{,F,FQ}` instruction lifting and add asm tests
DMaroo Sep 14, 2022
7fb7532
Implement `SAHF` lifting and asm test
DMaroo Sep 14, 2022
b861dd2
Implement IL lifting for shift family of instructions
DMaroo Sep 14, 2022
ad9989e
Implement and add asm tests for `SBB`
DMaroo Sep 14, 2022
69f3637
Fix incorrect `ROR` implementation
DMaroo Sep 15, 2022
e863afc
Implement `SCAS` family of instructions
DMaroo Sep 15, 2022
b388156
Implement remainig instructions in `LODS`, `MOVS` and `CMPS` family
DMaroo Sep 15, 2022
b39b934
Fix implementation and tests for incorrect instructions
DMaroo Sep 15, 2022
e9bb43f
Implement IL lifting for `STAC`, `STC`, `STD`, `STI`
DMaroo Sep 17, 2022
f67f11c
Implement `STOS` family of instructions and add asm tests
DMaroo Sep 17, 2022
6f23038
Implement `SUB` instruction
DMaroo Sep 17, 2022
8e2e54e
Implement `TEST` IL lifting
DMaroo Sep 17, 2022
5def4ac
Implement `XCHG` IL lifting and asm tests
DMaroo Sep 17, 2022
e01b20b
Implement lifting for `XLATB` and `XOR`
DMaroo Sep 17, 2022
9998a78
Update implmenetation of unimplemented IL lifting
DMaroo Sep 17, 2022
c55a25f
Implement `BOUND` instruction IL and asm tests
DMaroo Sep 17, 2022
b9177d0
Implement IL lifting for `ENTER`
DMaroo Sep 17, 2022
fefde4a
Add empty implementation for `{IN,OUT}S{B,W}`
DMaroo Sep 17, 2022
63e387b
Implement IL lifting and add test for `LEAVE`
DMaroo Sep 17, 2022
57dd89d
Implement IL for `CALL` and fix implementation of pushing
DMaroo Nov 20, 2022
a4cb7f8
Add `NULL` checks to fix breaking unit and integraton tests
DMaroo Nov 20, 2022
973dfb3
Fix broken integration tests
DMaroo Nov 20, 2022
bd9fc2a
Fix broken test because of merge
DMaroo Nov 20, 2022
3f4f325
Change control flow to avoid unused variable warning
DMaroo Nov 20, 2022
668a557
Fix failing tests because of `SMOD` instead of `MOD`
DMaroo Nov 20, 2022
32d1c3c
Remove warning and NULL return whe doing segmentation in 16-bit
DMaroo Nov 21, 2022
3b3dd75
Fix failing test for x86_16
DMaroo Nov 21, 2022
f52ed5a
Add support for extended registers (r8-r15)
DMaroo Nov 25, 2022
db6e522
format according to clang-format
DMaroo Nov 25, 2022
e81c350
Add tests for `cmps` instructions
DMaroo Nov 25, 2022
cbbcb85
Update test case to have correct bv size
DMaroo Nov 25, 2022
2ad2b0c
Add tests and fix IL implementation
DMaroo Nov 25, 2022
49d09e8
Add coverage tests for `IDIV` instruction
DMaroo Nov 25, 2022
9a1dc92
Add tests for covering lifting of `IMUL`
DMaroo Nov 25, 2022
e3d614c
Remove empty IL test from broken tests
DMaroo Nov 25, 2022
0fc94bb
Add tests for more coverage of `LODS` instructions
DMaroo Nov 25, 2022
d3f727c
Improve coverage of tests, as mentioned in the review
DMaroo Nov 25, 2022
5dc5356
Add tests to cover lifting of `PUSH`, `ENTER`, `LEAVE`
DMaroo Nov 25, 2022
7d8bdcd
Add documentation for the helper functions
DMaroo Nov 25, 2022
93dda12
Update `CMP` to remove unnecessary size check
DMaroo Nov 25, 2022
5c74227
Combine if conditions into one
DMaroo Nov 26, 2022
4513b28
Merge branch 'dev' into x86-il-migration
DMaroo Nov 27, 2022
93f8ce1
Make changes according to review
DMaroo Nov 27, 2022
1e3094f
Add tests for `IN`, `OUT`, `WAIT` and `LOOP` family
DMaroo Nov 27, 2022
c844e31
Merge branch 'dev' into x86-il-migration
DMaroo Nov 27, 2022
a8d38ec
Format according to clang-format
DMaroo Nov 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,806 changes: 3,806 additions & 0 deletions librz/analysis/arch/x86/x86_il.c

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions librz/analysis/arch/x86/x86_il.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2022 Dhruv Maroo <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#ifndef RZIL_ANALYSIS_X86_IL_H
#define RZIL_ANALYSIS_X86_IL_H

#include <rz_lib.h>
#include <rz_analysis.h>
#include <capstone/capstone.h>
#include <capstone/x86.h>

#define BITS_PER_BYTE 8
#define GPR_FAMILY_COUNT 10

typedef x86_reg X86Reg;
typedef cs_x86_op X86Op;
typedef x86_op_mem X86Mem;
typedef cs_x86 X86Ins;
typedef x86_insn X86InsMnem;

typedef struct x86_il_instruction_t {
const X86Ins *structure;
X86InsMnem mnem;
} X86ILIns;

RZ_IPI bool rz_x86_il_opcode(RZ_NONNULL RzAnalysis *analysis, RZ_NONNULL RzAnalysisOp *aop, ut64 pc, RZ_BORROW RZ_NONNULL const X86ILIns *ins);
RZ_IPI RzAnalysisILConfig *rz_x86_il_config(RZ_NONNULL RzAnalysis *analysis);

#endif /* RZIL_ANALYSIS_X86_IL_H */
1 change: 1 addition & 0 deletions librz/analysis/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ rz_analysis_sources = [
'arch/ppc/ppc_il_ops.c',
'arch/ppc/ppc_il_flag_ops.c',
'arch/sh/sh_il.c',
'arch/x86/x86_il.c',
'../asm/arch/amd29k/amd29k.c',
'../asm/arch/avr/disassembler.c',
'../asm/arch/cil/cil_dis.c',
Expand Down
68 changes: 53 additions & 15 deletions librz/analysis/p/analysis_x86_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <rz_lib.h>
#include <capstone/capstone.h>
#include <capstone/x86.h>
#include "../arch/x86/x86_il.h"

#if 0
CYCLES:
Expand Down Expand Up @@ -3214,8 +3215,16 @@ static int analop(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf, in
op_fillval(a, op, &ctx->handle, ctx->insn, mode);
}
}
//#if X86_GRP_PRIVILEGE>0

if (ctx->insn) {
// x86 RzIL uplifting
X86ILIns x86_il_ins = {
.structure = &ctx->insn->detail->x86,
.mnem = ctx->insn->id
};
rz_x86_il_opcode(a, op, addr, &x86_il_ins);

//#if X86_GRP_PRIVILEGE>0
#if HAVE_CSGRP_PRIVILEGE
if (cs_insn_group(ctx->handle, ctx->insn, X86_GRP_PRIVILEGE)) {
op->family = RZ_ANALYSIS_OP_FAMILY_PRIV;
Expand Down Expand Up @@ -3296,15 +3305,15 @@ static char *get_reg_profile(RzAnalysis *analysis) {
"seg es .16 58 0\n"
"gpr flags .16 56 0\n"
"flg cf .1 .448 0\n"
"flg pf .1 .449 0\n"
"flg af .1 .450 0\n"
"flg zf .1 .451 0\n"
"flg sf .1 .452 0\n"
"flg tf .1 .453 0\n"
"flg if .1 .454 0\n"
"flg df .1 .455 0\n"
"flg of .1 .456 0\n"
"flg rf .1 .457 0\n";
"flg pf .1 .450 0\n"
"flg af .1 .452 0\n"
"flg zf .1 .454 0\n"
"flg sf .1 .455 0\n"
"flg tf .1 .456 0\n"
"flg if .1 .457 0\n"
"flg df .1 .458 0\n"
"flg of .1 .459 0\n"
"flg nt .1 .462 0\n";
#if 0
"drx dr0 .32 0 0\n"
"drx dr1 .32 4 0\n"
Expand Down Expand Up @@ -3355,11 +3364,18 @@ static char *get_reg_profile(RzAnalysis *analysis) {
"gpr bp .16 20 0\n"
"gpr eip .32 48 0\n"
"gpr ip .16 48 0\n"
"seg xds .32 28 0\n"
"seg ds .16 28 0\n"
"seg xes .32 32 0\n"
"seg es .16 32 0\n"
"seg xfs .32 36 0\n"
"seg fs .16 36 0\n"
"seg xgs .32 40 0\n"
"seg gs .16 40 0\n"
"seg xss .32 64 0\n"
"seg ss .16 64 0\n"
"seg xcs .32 52 0\n"
"seg cs .16 52 0\n"
"seg xss .32 52 0\n"
"flg eflags .32 .448 0 c1p.a.zstido.n.rv\n"
"flg flags .16 .448 0\n"
"flg cf .1 .448 0\n"
Expand All @@ -3374,14 +3390,23 @@ static char *get_reg_profile(RzAnalysis *analysis) {
"flg nt .1 .462 0\n"
"flg rf .1 .464 0\n"
"flg vm .1 .465 0\n"
"flg ac .1 .466 0\n"
"drx dr0 .32 0 0\n"
"drx dr1 .32 4 0\n"
"drx dr2 .32 8 0\n"
"drx dr3 .32 12 0\n"
//"drx dr4 .32 16 0\n"
//"drx dr5 .32 20 0\n"
"drx dr4 .32 16 0\n"
"drx dr5 .32 20 0\n"
"drx dr6 .32 24 0\n"
"drx dr7 .32 28 0\n"
"ctr cr0 .32 0 0\n"
"ctr cr1 .32 4 0\n"
"ctr cr2 .32 8 0\n"
"ctr cr3 .32 12 0\n"
"ctr cr4 .32 16 0\n"
"ctr cr5 .32 20 0\n"
"ctr cr6 .32 24 0\n"
"ctr cr7 .32 28 0\n"
"xmm@fpu xmm0 .128 160 4\n"
"fpu xmm0l .64 160 0\n"
"fpu xmm0h .64 168 0\n"
Expand Down Expand Up @@ -3530,6 +3555,10 @@ static char *get_reg_profile(RzAnalysis *analysis) {
"flg if .1 .1161 0 interrupt\n"
"flg df .1 .1162 0 direction\n"
"flg of .1 .1163 0 overflow\n"
"flg nt .1 .1166 0\n"
"flg rf .1 .1168 0\n"
"flg vm .1 .1169 0\n"
"flg ac .1 .1170 0\n"

"gpr rsp .64 152 0\n"
"gpr esp .32 152 0\n"
Expand All @@ -3546,10 +3575,18 @@ static char *get_reg_profile(RzAnalysis *analysis) {
"drx dr1 .64 8 0\n"
"drx dr2 .64 16 0\n"
"drx dr3 .64 24 0\n"
// dr4 32
// dr5 40
"drx dr4 .64 32 0\n"
"drx dr5 .64 40 0\n"
"drx dr6 .64 48 0\n"
"drx dr7 .64 56 0\n"
"ctr cr0 .64 0 0\n"
"ctr cr1 .64 8 0\n"
"ctr cr2 .64 16 0\n"
"ctr cr3 .64 24 0\n"
"ctr cr4 .64 32 0\n"
"ctr cr5 .64 40 0\n"
"ctr cr6 .64 48 0\n"
"ctr cr7 .64 56 0\n"

/*0030 struct user_fpregs_struct
0031 {
Expand Down Expand Up @@ -3743,6 +3780,7 @@ RzAnalysisPlugin rz_analysis_plugin_x86_cs = {
.fini = x86_fini,
.esil_init = esil_x86_cs_init,
.esil_fini = esil_x86_cs_fini,
.il_config = rz_x86_il_config,
// .esil_intr = esil_x86_cs_intr,
};

Expand Down
16 changes: 15 additions & 1 deletion librz/asm/p/asm_x86_nz.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,20 @@ static int opaam(RzAsm *a, ut8 *data, const Opcode *op) {
return l;
}

static int opaad(RzAsm *a, ut8 *data, const Opcode *op) {
is_valid_registers(op);
int l = 0;
int immediate = op->operands[0].immediate * op->operands[0].sign;
data[l++] = 0xd5;
if (immediate == 0) {
data[l++] = 0x0a;
} else if (immediate < 256 && immediate > -129) {
data[l++] = immediate;
}

return l;
}

static int opdec(RzAsm *a, ut8 *data, const Opcode *op) {
if (op->operands[1].type) {
RZ_LOG_ERROR("assembler: x86.nz: %s: invalid operands\n", op->mnemonic);
Expand Down Expand Up @@ -4267,7 +4281,7 @@ typedef struct lookup_t {

LookupTable oplookup[] = {
{ "aaa", 0, NULL, 0x37, 1 },
{ "aad", 0, NULL, 0xd50a, 2 },
{ "aad", 0, opaad, 0 },
{ "aam", 0, opaam, 0 },
{ "aas", 0, NULL, 0x3f, 1 },
{ "adc", 0, &opadc, 0 },
Expand Down
Loading