diff --git a/plugins/primus_lisp/primus_lisp_semantic_primitives.ml b/plugins/primus_lisp/primus_lisp_semantic_primitives.ml index ee4b2e9bc..4fddc684d 100644 --- a/plugins/primus_lisp/primus_lisp_semantic_primitives.ml +++ b/plugins/primus_lisp/primus_lisp_semantic_primitives.ml @@ -256,6 +256,10 @@ let export = Primus.Lisp.Type.Spec.[ "get-current-program-counter", unit @-> int, "(get-current-program-counter) is an alias to (get-program-counter)"; + "get-instruction-length", unit @-> int, + "(get-instruction-length) returns the length of the current instruction \ + in bytes."; + "set-symbol-value", tuple [any; a] @-> a, "(set-symbol-value S X) sets the value of the symbol S to X. Returns X"; @@ -839,6 +843,12 @@ module Primitives(CT : Theory.Core)(T : Target) = struct | None -> !!(empty s) | Some addr -> forget@@const_int s addr + let get_instruction_length s lbl = + let open Bap.Std in + KB.collect Memory.slot lbl >>= function + | None -> !!(empty s) + | Some mem -> forget@@int s (Memory.length mem) + let require_symbol v k = match symbol v with | Some name -> k name @@ -1479,6 +1489,7 @@ module Primitives(CT : Theory.Core)(T : Target) = struct | "store-word",_-> data@@store_word t args | "get-program-counter",[] | "get-current-program-counter",[] -> pure@@get_pc s lbl + | "get-instruction-length",[] -> pure@@get_instruction_length s lbl | "set-symbol-value",[sym;x] -> data@@set_symbol t sym x | "symbol-concat",syms -> pure@@symbol_concat s syms | "symbol",[x] -> pure@@mksymbol s x diff --git a/plugins/primus_lisp/site-lisp/llvm-x86-64-floats.lisp b/plugins/primus_lisp/site-lisp/llvm-x86-64-floats.lisp index e8980b26d..a75b833c5 100644 --- a/plugins/primus_lisp/site-lisp/llvm-x86-64-floats.lisp +++ b/plugins/primus_lisp/site-lisp/llvm-x86-64-floats.lisp @@ -24,7 +24,7 @@ (defun reg-val (reg) (case (reg-name reg) - 'RIP (+ (get-current-program-counter) 8) + 'RIP (+ (get-current-program-counter) (get-instruction-length)) 'RSP RSP 'RBP RBP (error "unknown register"))) diff --git a/plugins/x86/semantics/x86-64.lisp b/plugins/x86/semantics/x86-64.lisp index d2551ccda..045c5fc5b 100644 --- a/plugins/x86/semantics/x86-64.lisp +++ b/plugins/x86/semantics/x86-64.lisp @@ -24,7 +24,7 @@ (defun reg# (reg) (if (is-rip reg) - (+ (get-program-counter) 8) + (+ (get-program-counter) (get-instruction-length)) reg)) (defun load-mem (reg off) @@ -478,4 +478,4 @@ (set rd (opo (opi rn rm)))) (defmacro bitwise-rrm (set opo opi rd rn ptr off) - (set rd (opo (opi rn (load-bits (word-width (unquote rn)) (+ ptr off)))))) + (set rd (opo (opi rn (load-bits (word-width (unquote rn)) (+ (reg# ptr) off))))))