Skip to content

Commit 27c195c

Browse files
authored
adds the missing ux*, sx*, s?bz instructions (#1297)
* adds the missing ux* and sx* instructions We could use Primus Lisp for that but apparently we already have them implemented but just forgot to update the code and to use it. * adds cbz and cnbz instructions
1 parent 36a04ba commit 27c195c

File tree

6 files changed

+42
-39
lines changed

6 files changed

+42
-39
lines changed

oasis/thumb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Library thumb_plugin
1111
bap, bap-arm, bitvec
1212
FindlibName: bap-plugin-thumb
1313
InternalModules: Thumb_main,
14+
Thumb_bits,
1415
Thumb_branch,
1516
Thumb_core,
1617
Thumb_mem,

plugins/thumb/thumb_bits.ml

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
open Bap_core_theory
22
open Base
33
open KB.Syntax
4+
open Thumb_core
45

5-
module Env = Thumb_env.Env
6-
module Defs = Thumb_defs
6+
module Make(CT : Theory.Core) = struct
7+
open Thumb_core.Make(CT)
8+
open Syntax
79

8-
module Bits(Core : Theory.Core) = struct
9-
open Core
10-
11-
module Utils = Thumb_util.Utils(Core)
12-
module DSL = Thumb_dsl.Make(Core)
13-
14-
open Utils
15-
16-
let sxtb dest src =
17-
DSL.[
18-
!$$dest := extend_to Env.byte !$src |> extend_signed
19-
]
20-
21-
let sxth dest src =
22-
DSL.[
23-
!$$dest := extend_to Env.half_word !$src |> extend_signed
24-
]
25-
26-
let uxtb dest src =
27-
DSL.[
28-
!$$dest := extend_to Env.byte !$src |> extend
10+
let sx rd rm = data [
11+
rd := CT.signed s32 (var rm)
2912
]
3013

31-
let uxth dest src =
32-
DSL.[
33-
!$$dest := extend_to Env.half_word !$src |> extend
14+
let ux rd rm = data [
15+
rd := CT.unsigned s32 (var rm)
3416
]
35-
3617
end

plugins/thumb/thumb_branch.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ module Make(CT : Theory.Core) = struct
7878
]
7979

8080
let blxr pc dst = blr pc dst
81+
82+
let cbnz pc rn dst =
83+
CT.branch CT.(inv@@is_zero (var rn))
84+
(goto (pc +> dst))
85+
(seq [])
86+
87+
let cbz pc rn dst =
88+
CT.branch CT.(inv@@is_zero (var rn))
89+
(goto (pc +> dst))
90+
(seq [])
8191
end

plugins/thumb/thumb_branch.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ module Make(CT : Theory.Core) : sig
2929

3030
(** [bl rm] or [blx rm] *)
3131
val blr : Bitvec.t -> r32 reg -> eff
32+
33+
(** [cbnz rm <label>] *)
34+
val cbnz : Bitvec.t -> r32 reg -> int -> eff
35+
36+
(** [cbz rm <label>] *)
37+
val cbz : Bitvec.t -> r32 reg -> int -> eff
3238
end

plugins/thumb/thumb_main.ml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,17 @@ module Thumb(CT : Theory.Core) = struct
169169
info "unhandled memory operation: %a" pp_insn insn;
170170
!!Insn.empty
171171

172+
let lift_bits opcode insn =
173+
let open Thumb_bits.Make(CT) in
174+
match opcode, (MC.Insn.ops insn : Op.t array) with
175+
| `tSXTB, [|Reg rd; Reg rm; _; _|] -> sx (reg rd) (reg rm)
176+
| `tSXTH, [|Reg rd; Reg rm; _; _|] -> sx (reg rd) (reg rm)
177+
| `tUXTB, [|Reg rd; Reg rm; _; _|] -> ux (reg rd) (reg rm)
178+
| `tUXTH, [|Reg rd; Reg rm; _; _|] -> ux (reg rd) (reg rm)
179+
| insn ->
180+
info "unhandled bit-wise instruction: %a" pp_insn insn;
181+
!!Insn.empty
172182

173-
(* let lift_bits insn ops =
174-
* let open Bits in
175-
* match insn, ops with
176-
* | `tSXTB, [|dest; src; _unknown; _|] -> sxtb dest src
177-
* | `tSXTH, [|dest; src; _unknown; _|] -> sxth dest src
178-
* | `tUXTB, [|dest; src; _unknown; _|] -> uxtb dest src
179-
* | `tUXTH, [|dest; src; _unknown; _|] -> uxth dest src
180-
* | _ -> [] *)
181183

182184
(* these are not entirely complete *)
183185
let lift_branch pc opcode insn =
@@ -192,6 +194,8 @@ module Thumb(CT : Theory.Core) = struct
192194
| `tBLXr, [|_; _; Reg dst|]-> blxr pc (reg dst)
193195
| `tBX, [|Reg dst; _; _|]when is_pc (reg dst) -> bxi pc 0
194196
| `tBX, [|Reg dst;_;_|] -> bxr (reg dst)
197+
| `tCBNZ, [|Reg rn; Imm c|] -> cbnz pc (reg rn) (imm c)
198+
| `tCBZ, [|Reg rn; Imm c|] -> cbz pc (reg rn) (imm c)
195199
| insn ->
196200
info "unhandled branch: %a" pp_insn insn;
197201
!!Insn.empty
@@ -200,9 +204,7 @@ module Thumb(CT : Theory.Core) = struct
200204
| #opmem as op -> lift_mem addr op insn
201205
| #opmov as op -> lift_move addr op insn
202206
| #opbranch as op -> lift_branch addr op insn
203-
| op ->
204-
info "unsupported opcode: %s" (string_of_opcode op);
205-
!!Insn.empty
207+
| #opbit as op -> lift_bits op insn
206208
end
207209

208210

@@ -237,7 +239,8 @@ module Main = struct
237239
match decode_opcode (MC.Insn.name insn) with
238240
| None ->
239241
info "failed to decode MC instruction, unknown opcode: \
240-
%s => %a"
242+
%a: %s => %a"
243+
Memory.pp mem
241244
(MC.Insn.asm insn)
242245
Sexp.pp_hum (MC.Insn.sexp_of_t insn);
243246
!!Insn.empty

plugins/thumb/thumb_opcodes.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type opbranch = [
8989
| `tBLXi
9090
| `tBLXr
9191
| `tBX
92+
| `tCBNZ
93+
| `tCBZ
9294
] [@@deriving bin_io, compare, sexp, enumerate]
9395

9496
type opcode = [

0 commit comments

Comments
 (0)