4
0

to_json.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2017 Quentin Monnet <quentin.monnet@6wind.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. #[macro_use]
  7. extern crate json;
  8. extern crate rbpf;
  9. use rbpf::disassembler;
  10. fn to_json(prog: &std::vec::Vec<u8>) -> String {
  11. let insns = disassembler::to_insn_vec(&prog);
  12. let mut json_insns = vec![];
  13. for insn in insns {
  14. json_insns.push(object!(
  15. "opc" => format!("{:#x}", insn.opc), // => insn.opc,
  16. "dst" => format!("{:#x}", insn.dst), // => insn.dst,
  17. "src" => format!("{:#x}", insn.src), // => insn.src,
  18. "off" => format!("{:#x}", insn.off), // => insn.off,
  19. // Warning: for imm we use a i64 instead of a i32 (to have correct values for
  20. // `lddw` operation. If we print a number in the JSON this is not a problem, the
  21. // internal i64 has the same value with extended sign on 32 most significant bytes.
  22. // If we print the hexadecimal value as a string however, we want to cast as a i32
  23. // to prevent all other instructions to print spurious `ffffffff` prefix if the
  24. // number is negative. When values takes more than 32 bits with `lddw`, the cast
  25. // has no effect and the complete value is printed anyway.
  26. "imm" => format!("{:#x}", insn.imm as i32), // => insn.imm,
  27. "desc" => format!("{}", insn.desc)
  28. )
  29. );
  30. }
  31. json::stringify_pretty(object!(
  32. "size" => json_insns.len(),
  33. "insns" => json_insns
  34. ), 4)
  35. }
  36. fn main() {
  37. let prog = vec![
  38. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  43. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  46. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  48. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  53. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  54. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  58. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  63. ];
  64. println!("{}", to_json(&prog));
  65. }