ubpf_verifier.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  2. // Copyright 2015 Big Switch Networks, Inc
  3. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  6. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  7. // copied, modified, or distributed except according to those terms.
  8. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  9. // in this file has a name in the form `test_verifier_<name>`, and corresponds to the
  10. // (human-readable) code in `ubpf/tree/master/tests/<name>`, available at
  11. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  12. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  13. // version to understand what these program do.
  14. //
  15. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  16. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  17. // The very few modifications that have been realized should be indicated.
  18. // These are unit tests for the eBPF “verifier”.
  19. extern crate rbpf;
  20. use rbpf::assembler::assemble;
  21. use rbpf::ebpf;
  22. #[test]
  23. #[should_panic(expected = "[Verifier] Error: division by 0 (insn #1)")]
  24. fn test_verifier_err_div_by_zero_imm() {
  25. let prog = assemble("
  26. mov32 r0, 1
  27. div32 r0, 0
  28. exit").unwrap();
  29. let vm = rbpf::EbpfVmNoData::new(&prog);
  30. vm.prog_exec();
  31. }
  32. #[test]
  33. #[should_panic(expected = "[Verifier] Error: unsupported argument for LE/BE (insn #0)")]
  34. fn test_verifier_err_endian_size() {
  35. let prog = &[
  36. 0xdc, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  37. 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  39. ];
  40. let vm = rbpf::EbpfVmNoData::new(prog);
  41. vm.prog_exec();
  42. }
  43. #[test]
  44. #[should_panic(expected = "[Verifier] Error: incomplete LD_DW instruction (insn #0)")]
  45. fn test_verifier_err_incomplete_lddw() { // Note: ubpf has test-err-incomplete-lddw2, which is the same
  46. let prog = &[
  47. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  48. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  49. ];
  50. let vm = rbpf::EbpfVmNoData::new(prog);
  51. vm.prog_exec();
  52. }
  53. #[test]
  54. #[should_panic(expected = "[Verifier] Error: infinite loop")]
  55. fn test_verifier_err_infinite_loop() {
  56. let prog = assemble("
  57. ja -1
  58. exit").unwrap();
  59. let vm = rbpf::EbpfVmNoData::new(&prog);
  60. vm.prog_exec();
  61. }
  62. #[test]
  63. #[should_panic(expected = "[Verifier] Error: invalid destination register (insn #0)")]
  64. fn test_verifier_err_invalid_reg_dst() {
  65. let prog = assemble("
  66. mov r11, 1
  67. exit").unwrap();
  68. let vm = rbpf::EbpfVmNoData::new(&prog);
  69. vm.prog_exec();
  70. }
  71. #[test]
  72. #[should_panic(expected = "[Verifier] Error: invalid source register (insn #0)")]
  73. fn test_verifier_err_invalid_reg_src() {
  74. let prog = assemble("
  75. mov r0, r11
  76. exit").unwrap();
  77. let vm = rbpf::EbpfVmNoData::new(&prog);
  78. vm.prog_exec();
  79. }
  80. #[test]
  81. #[should_panic(expected = "[Verifier] Error: jump to middle of LD_DW at #2 (insn #0)")]
  82. fn test_verifier_err_jmp_lddw() {
  83. let prog = assemble("
  84. ja +1
  85. lddw r0, 0x1122334455667788
  86. exit").unwrap();
  87. let vm = rbpf::EbpfVmNoData::new(&prog);
  88. vm.prog_exec();
  89. }
  90. #[test]
  91. #[should_panic(expected = "[Verifier] Error: jump out of code to #3 (insn #0)")]
  92. fn test_verifier_err_jmp_out() {
  93. let prog = assemble("
  94. ja +2
  95. exit").unwrap();
  96. let vm = rbpf::EbpfVmNoData::new(&prog);
  97. vm.prog_exec();
  98. }
  99. #[test]
  100. #[should_panic(expected = "[Verifier] Error: program does not end with “EXIT” instruction")]
  101. fn test_verifier_err_no_exit() {
  102. let prog = assemble("
  103. mov32 r0, 0").unwrap();
  104. let vm = rbpf::EbpfVmNoData::new(&prog);
  105. vm.prog_exec();
  106. }
  107. #[test]
  108. #[should_panic(expected = "[Verifier] Error: eBPF program length limited to 4096, here 4097")]
  109. fn test_verifier_err_too_many_instructions() {
  110. // uBPF uses 65637 instructions, because it sets its limit at 65636.
  111. // We use the classic 4096 limit from kernel, so no need to produce as many instructions.
  112. let mut prog = (0..(4096 * ebpf::INSN_SIZE)).map( |x| match x % 8 {
  113. 0 => 0xb7,
  114. 1 => 0x01,
  115. _ => 0
  116. }).collect::<Vec<u8>>();
  117. prog.append(&mut vec![ 0x95, 0, 0, 0, 0, 0, 0, 0 ]);
  118. let vm = rbpf::EbpfVmNoData::new(&prog);
  119. vm.prog_exec();
  120. }
  121. #[test]
  122. #[should_panic(expected = "[Verifier] Error: unknown eBPF opcode 0x6 (insn #0)")]
  123. fn test_verifier_err_unknown_opcode() {
  124. let prog = &[
  125. 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  127. ];
  128. let vm = rbpf::EbpfVmNoData::new(prog);
  129. vm.prog_exec();
  130. }
  131. #[test]
  132. #[should_panic(expected = "[Verifier] Error: cannot write into register r10 (insn #0)")]
  133. fn test_verifier_err_write_r10() {
  134. let prog = assemble("
  135. mov r10, 1
  136. exit").unwrap();
  137. let vm = rbpf::EbpfVmNoData::new(&prog);
  138. vm.prog_exec();
  139. }