forked from qmonnet/rbpf
-
Notifications
You must be signed in to change notification settings - Fork 177
/
execution.rs
3957 lines (3780 loc) · 93.9 KB
/
execution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::arithmetic_side_effects)]
#![cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
// Copyright 2020 Solana Maintainers <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
// the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
extern crate byteorder;
extern crate libc;
extern crate solana_rbpf;
extern crate test_utils;
extern crate thiserror;
use byteorder::{ByteOrder, LittleEndian};
#[cfg(all(not(windows), target_arch = "x86_64"))]
use rand::{rngs::SmallRng, RngCore, SeedableRng};
use solana_rbpf::{
assembler::assemble,
declare_builtin_function, ebpf,
elf::Executable,
error::{EbpfError, ProgramResult},
memory_region::{AccessType, MemoryMapping, MemoryRegion},
program::{BuiltinFunction, BuiltinProgram, FunctionRegistry, SBPFVersion},
static_analysis::Analysis,
syscalls,
verifier::RequisiteVerifier,
vm::{Config, ContextObject, TestContextObject},
};
use std::{fs::File, io::Read, sync::Arc};
use test_utils::{
assert_error, create_vm, PROG_TCP_PORT_80, TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH,
};
const INSTRUCTION_METER_BUDGET: u64 = 1024;
macro_rules! test_interpreter_and_jit {
(register, $function_registry:expr, $location:expr => $syscall_function:expr) => {
$function_registry
.register_function_hashed($location.as_bytes(), $syscall_function)
.unwrap();
};
($executable:expr, $mem:tt, $context_object:expr, $expected_result:expr $(,)?) => {
let expected_instruction_count = $context_object.get_remaining();
#[allow(unused_mut)]
let mut context_object = $context_object;
let expected_result = format!("{:?}", $expected_result);
if !expected_result.contains("ExceededMaxInstructions") {
context_object.remaining = INSTRUCTION_METER_BUDGET;
}
$executable.verify::<RequisiteVerifier>().unwrap();
let (instruction_count_interpreter, interpreter_final_pc, _tracer_interpreter) = {
let mut mem = $mem;
let mem_region = MemoryRegion::new_writable(&mut mem, ebpf::MM_INPUT_START);
let mut context_object = context_object.clone();
create_vm!(
vm,
&$executable,
&mut context_object,
stack,
heap,
vec![mem_region],
None
);
let (instruction_count_interpreter, result) = vm.execute_program(&$executable, true);
assert_eq!(
format!("{:?}", result),
expected_result,
"Unexpected result for Interpreter"
);
(
instruction_count_interpreter,
vm.registers[11],
vm.context_object_pointer.clone(),
)
};
#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
{
#[allow(unused_mut)]
let compilation_result = $executable.jit_compile();
let mut mem = $mem;
let mem_region = MemoryRegion::new_writable(&mut mem, ebpf::MM_INPUT_START);
create_vm!(
vm,
&$executable,
&mut context_object,
stack,
heap,
vec![mem_region],
None
);
match compilation_result {
Err(err) => assert_eq!(
format!("{:?}", err),
expected_result,
"Unexpected result for JIT compilation"
),
Ok(()) => {
let (instruction_count_jit, result) = vm.execute_program(&$executable, false);
let tracer_jit = &vm.context_object_pointer;
if !TestContextObject::compare_trace_log(&_tracer_interpreter, tracer_jit) {
let analysis = Analysis::from_executable(&$executable).unwrap();
let stdout = std::io::stdout();
analysis
.disassemble_trace_log(
&mut stdout.lock(),
&_tracer_interpreter.trace_log,
)
.unwrap();
analysis
.disassemble_trace_log(&mut stdout.lock(), &tracer_jit.trace_log)
.unwrap();
panic!();
}
assert_eq!(
format!("{:?}", result),
expected_result,
"Unexpected result for JIT"
);
assert_eq!(
instruction_count_interpreter, instruction_count_jit,
"Interpreter and JIT instruction meter diverged",
);
assert_eq!(
interpreter_final_pc, vm.registers[11],
"Interpreter and JIT instruction final PC diverged",
);
}
}
}
if $executable.get_config().enable_instruction_meter {
assert_eq!(
instruction_count_interpreter, expected_instruction_count,
"Instruction meter did not consume expected amount"
);
}
};
}
macro_rules! test_interpreter_and_jit_asm {
($source:tt, $config:expr, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
#[allow(unused_mut)]
{
let mut config = $config;
config.enable_instruction_tracing = true;
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
$(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)*
let loader = Arc::new(BuiltinProgram::new_loader(config, function_registry));
let mut executable = assemble($source, loader).unwrap();
test_interpreter_and_jit!(executable, $mem, $context_object, $expected_result);
}
};
($source:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
#[allow(unused_mut)]
{
test_interpreter_and_jit_asm!($source, Config::default(), $mem, ($($location => $syscall_function),*), $context_object, $expected_result);
}
};
}
macro_rules! test_interpreter_and_jit_elf {
($source:tt, $config:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
let mut file = File::open($source).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
#[allow(unused_mut)]
{
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
$(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)*
let loader = Arc::new(BuiltinProgram::new_loader($config, function_registry));
let mut executable = Executable::<TestContextObject>::from_elf(&elf, loader).unwrap();
test_interpreter_and_jit!(executable, $mem, $context_object, $expected_result);
}
};
($source:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
let config = Config {
enable_instruction_tracing: true,
..Config::default()
};
test_interpreter_and_jit_elf!($source, config, $mem, ($($location => $syscall_function),*), $context_object, $expected_result);
};
}
// BPF_ALU : Arithmetic and Logic
#[test]
fn test_mov() {
test_interpreter_and_jit_asm!(
"
mov32 r1, 1
mov32 r0, r1
exit",
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x1),
);
}
#[test]
fn test_mov32_imm_large() {
test_interpreter_and_jit_asm!(
"
mov32 r0, -1
exit",
[],
(),
TestContextObject::new(2),
ProgramResult::Ok(0xffffffff),
);
}
#[test]
fn test_mov_large() {
test_interpreter_and_jit_asm!(
"
mov32 r1, -1
mov32 r0, r1
exit",
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(0xffffffff),
);
}
#[test]
fn test_bounce() {
test_interpreter_and_jit_asm!(
"
mov r0, 1
mov r6, r0
mov r7, r6
mov r8, r7
mov r9, r8
mov r0, r9
exit",
[],
(),
TestContextObject::new(7),
ProgramResult::Ok(0x1),
);
}
#[test]
fn test_add32() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 0
mov32 r1, 2
add32 r0, 1
add32 r0, r1
exit",
[],
(),
TestContextObject::new(5),
ProgramResult::Ok(0x3),
);
}
#[test]
fn test_alu32_arithmetic() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 0
mov32 r1, 1
mov32 r2, 2
mov32 r3, 3
mov32 r4, 4
mov32 r5, 5
mov32 r6, 6
mov32 r7, 7
mov32 r8, 8
mov32 r9, 9
sub32 r0, 13
sub32 r0, r1
add32 r0, 23
add32 r0, r7
lmul32 r0, 7
lmul32 r0, r3
udiv32 r0, 2
udiv32 r0, r4
exit",
[],
(),
TestContextObject::new(19),
ProgramResult::Ok(110),
);
}
#[test]
fn test_alu64_arithmetic() {
test_interpreter_and_jit_asm!(
"
mov r0, 0
mov r1, 1
mov r2, 2
mov r3, 3
mov r4, 4
mov r5, 5
mov r6, 6
mov r7, 7
mov r8, 8
mov r9, 9
sub r0, 13
sub r0, r1
add r0, 23
add r0, r7
lmul r0, 7
lmul r0, r3
udiv r0, 2
udiv r0, r4
exit",
[],
(),
TestContextObject::new(19),
ProgramResult::Ok(110),
);
}
#[test]
fn test_lmul128() {
test_interpreter_and_jit_asm!(
"
mov r0, r1
mov r2, 30
mov r3, 0
mov r4, 20
mov r5, 0
lmul64 r3, r4
lmul64 r5, r2
add64 r5, r3
mov64 r0, r2
rsh64 r0, 0x20
mov64 r3, r4
rsh64 r3, 0x20
mov64 r6, r3
lmul64 r6, r0
add64 r5, r6
lsh64 r4, 0x20
rsh64 r4, 0x20
mov64 r6, r4
lmul64 r6, r0
lsh64 r2, 0x20
rsh64 r2, 0x20
lmul64 r4, r2
mov64 r0, r4
rsh64 r0, 0x20
add64 r0, r6
mov64 r6, r0
rsh64 r6, 0x20
add64 r5, r6
lmul64 r3, r2
lsh64 r0, 0x20
rsh64 r0, 0x20
add64 r0, r3
mov64 r2, r0
rsh64 r2, 0x20
add64 r5, r2
stxdw [r1+0x8], r5
lsh64 r0, 0x20
lsh64 r4, 0x20
rsh64 r4, 0x20
or64 r0, r4
stxdw [r1+0x0], r0
exit",
[0; 16],
(),
TestContextObject::new(42),
ProgramResult::Ok(600),
);
}
#[test]
fn test_alu32_logic() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 0
mov32 r1, 1
mov32 r2, 2
mov32 r3, 3
mov32 r4, 4
mov32 r5, 5
mov32 r6, 6
mov32 r7, 7
mov32 r8, 8
or32 r0, r5
or32 r0, 0xa0
and32 r0, 0xa3
mov32 r9, 0x91
and32 r0, r9
lsh32 r0, 22
lsh32 r0, r8
rsh32 r0, 19
rsh32 r0, r7
xor32 r0, 0x03
xor32 r0, r2
exit",
[],
(),
TestContextObject::new(21),
ProgramResult::Ok(0x11),
);
}
#[test]
fn test_alu64_logic() {
test_interpreter_and_jit_asm!(
"
mov r0, 0
mov r1, 1
mov r2, 2
mov r3, 3
mov r4, 4
mov r5, 5
mov r6, 6
mov r7, 7
mov r8, 8
or r0, r5
or r0, 0xa0
and r0, 0xa3
mov r9, 0x91
and r0, r9
lsh r0, 32
lsh r0, 22
lsh r0, r8
rsh r0, 32
rsh r0, 19
rsh r0, r7
xor r0, 0x03
xor r0, r2
exit",
[],
(),
TestContextObject::new(23),
ProgramResult::Ok(0x11),
);
}
#[test]
fn test_arsh32_high_shift() {
test_interpreter_and_jit_asm!(
"
mov r0, 8
mov32 r1, 0x00000001
hor64 r1, 0x00000001
arsh32 r0, r1
exit",
[],
(),
TestContextObject::new(5),
ProgramResult::Ok(0x4),
);
}
#[test]
fn test_arsh32_imm() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 0xf8
lsh32 r0, 28
arsh32 r0, 16
exit",
[],
(),
TestContextObject::new(4),
ProgramResult::Ok(0xffff8000),
);
}
#[test]
fn test_arsh32_reg() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 0xf8
mov32 r1, 16
lsh32 r0, 28
arsh32 r0, r1
exit",
[],
(),
TestContextObject::new(5),
ProgramResult::Ok(0xffff8000),
);
}
#[test]
fn test_arsh64() {
test_interpreter_and_jit_asm!(
"
mov32 r0, 1
lsh r0, 63
arsh r0, 55
mov32 r1, 5
arsh r0, r1
exit",
[],
(),
TestContextObject::new(6),
ProgramResult::Ok(0xfffffffffffffff8),
);
}
#[test]
fn test_lsh64_reg() {
test_interpreter_and_jit_asm!(
"
mov r0, 0x1
mov r7, 4
lsh r0, r7
exit",
[],
(),
TestContextObject::new(4),
ProgramResult::Ok(0x10),
);
}
#[test]
fn test_rhs32_imm() {
test_interpreter_and_jit_asm!(
"
xor r0, r0
add r0, -1
rsh32 r0, 8
exit",
[],
(),
TestContextObject::new(4),
ProgramResult::Ok(0x00ffffff),
);
}
#[test]
fn test_rsh64_reg() {
test_interpreter_and_jit_asm!(
"
mov r0, 0x10
mov r7, 4
rsh r0, r7
exit",
[],
(),
TestContextObject::new(4),
ProgramResult::Ok(0x1),
);
}
#[test]
fn test_be16() {
test_interpreter_and_jit_asm!(
"
ldxh r0, [r1]
be16 r0
exit",
[0x11, 0x22],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x1122),
);
}
#[test]
fn test_be16_high() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1]
be16 r0
exit",
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x1122),
);
}
#[test]
fn test_be32() {
test_interpreter_and_jit_asm!(
"
ldxw r0, [r1]
be32 r0
exit",
[0x11, 0x22, 0x33, 0x44],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x11223344),
);
}
#[test]
fn test_be32_high() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1]
be32 r0
exit",
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x11223344),
);
}
#[test]
fn test_be64() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1]
be64 r0
exit",
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x1122334455667788),
);
}
// BPF_PQR : Product / Quotient / Remainder
#[test]
fn test_pqr() {
let mut prog = [0; 48];
prog[0] = ebpf::MOV64_IMM;
prog[8] = ebpf::HOR64_IMM;
prog[16] = ebpf::MOV64_IMM;
prog[17] = 1; // dst = R1
prog[24] = ebpf::HOR64_IMM;
prog[25] = 1; // dst = R1
prog[33] = 16; // src = R1
prog[40] = ebpf::EXIT;
let loader = Arc::new(BuiltinProgram::new_mock());
for (opc, dst, src, expected_result) in [
(ebpf::UHMUL64_IMM, 13u64, 4u64, 0u64),
(ebpf::UDIV32_IMM, 13u64, 4u64, 3u64),
(ebpf::UDIV64_IMM, 13u64, 4u64, 3u64),
(ebpf::UREM32_IMM, 13u64, 4u64, 1u64),
(ebpf::UREM64_IMM, 13u64, 4u64, 1u64),
(ebpf::UHMUL64_IMM, 13u64, u64::MAX, 12u64),
(ebpf::UDIV32_IMM, 13u64, u64::MAX, 0u64),
(ebpf::UDIV64_IMM, 13u64, u64::MAX, 0u64),
(ebpf::UREM32_IMM, 13u64, u64::MAX, 13u64),
(ebpf::UREM64_IMM, 13u64, u64::MAX, 13u64),
(ebpf::UHMUL64_IMM, u64::MAX, 4u64, 3u64),
(ebpf::UDIV32_IMM, u64::MAX, 4u64, (u32::MAX / 4) as u64),
(ebpf::UDIV64_IMM, u64::MAX, 4u64, u64::MAX / 4),
(ebpf::UREM32_IMM, u64::MAX, 4u64, 3u64),
(ebpf::UREM64_IMM, u64::MAX, 4u64, 3u64),
(ebpf::UHMUL64_IMM, u64::MAX, u64::MAX, u64::MAX - 1),
(ebpf::UDIV32_IMM, u64::MAX, u64::MAX, 1u64),
(ebpf::UDIV64_IMM, u64::MAX, u64::MAX, 1u64),
(ebpf::UREM32_IMM, u64::MAX, u64::MAX, 0u64),
(ebpf::UREM64_IMM, u64::MAX, u64::MAX, 0u64),
(ebpf::LMUL32_IMM, 13i64 as u64, 4i32 as u64, 52i32 as u64),
(ebpf::LMUL64_IMM, 13i64 as u64, 4i64 as u64, 52i64 as u64),
(ebpf::SHMUL64_IMM, 13i64 as u64, 4i64 as u64, 0i64 as u64),
(ebpf::SDIV32_IMM, 13i64 as u64, 4i32 as u64, 3i32 as u64),
(ebpf::SDIV64_IMM, 13i64 as u64, 4i64 as u64, 3i64 as u64),
(ebpf::SREM32_IMM, 13i64 as u64, 4i32 as u64, 1i64 as u64),
(ebpf::SREM64_IMM, 13i64 as u64, 4i64 as u64, 1i64 as u64),
(ebpf::LMUL32_IMM, 13i64 as u64, -4i32 as u64, -52i32 as u64),
(ebpf::LMUL64_IMM, 13i64 as u64, -4i64 as u64, -52i64 as u64),
(ebpf::SHMUL64_IMM, 13i64 as u64, -4i64 as u64, -1i64 as u64),
(ebpf::SDIV32_IMM, 13i64 as u64, -4i32 as u64, -3i32 as u64),
(ebpf::SDIV64_IMM, 13i64 as u64, -4i64 as u64, -3i64 as u64),
(ebpf::SREM32_IMM, 13i64 as u64, -4i32 as u64, 1i64 as u64),
(ebpf::SREM64_IMM, 13i64 as u64, -4i64 as u64, 1i64 as u64),
(ebpf::LMUL32_IMM, -13i64 as u64, 4i32 as u64, -52i32 as u64),
(ebpf::LMUL64_IMM, -13i64 as u64, 4i64 as u64, -52i64 as u64),
(ebpf::SHMUL64_IMM, -13i64 as u64, 4i64 as u64, -1i64 as u64),
(ebpf::SDIV32_IMM, -13i64 as u64, 4i32 as u64, -3i32 as u64),
(ebpf::SDIV64_IMM, -13i64 as u64, 4i64 as u64, -3i64 as u64),
(ebpf::SREM32_IMM, -13i64 as u64, 4i32 as u64, -1i64 as u64),
(ebpf::SREM64_IMM, -13i64 as u64, 4i64 as u64, -1i64 as u64),
(ebpf::LMUL32_IMM, -13i64 as u64, -4i32 as u64, 52i32 as u64),
(ebpf::LMUL64_IMM, -13i64 as u64, -4i64 as u64, 52i64 as u64),
(ebpf::SHMUL64_IMM, -13i64 as u64, -4i64 as u64, 0i64 as u64),
(ebpf::SDIV32_IMM, -13i64 as u64, -4i32 as u64, 3i32 as u64),
(ebpf::SDIV64_IMM, -13i64 as u64, -4i64 as u64, 3i64 as u64),
(ebpf::SREM32_IMM, -13i64 as u64, -4i32 as u64, -1i64 as u64),
(ebpf::SREM64_IMM, -13i64 as u64, -4i64 as u64, -1i64 as u64),
] {
LittleEndian::write_u32(&mut prog[4..], dst as u32);
LittleEndian::write_u32(&mut prog[12..], (dst >> 32) as u32);
LittleEndian::write_u32(&mut prog[20..], src as u32);
LittleEndian::write_u32(&mut prog[28..], (src >> 32) as u32);
LittleEndian::write_u32(&mut prog[36..], src as u32);
prog[32] = opc;
#[allow(unused_mut)]
let mut executable = Executable::<TestContextObject>::from_text_bytes(
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
executable,
[],
TestContextObject::new(6),
ProgramResult::Ok(expected_result),
);
prog[32] |= ebpf::BPF_X;
#[allow(unused_mut)]
let mut executable = Executable::<TestContextObject>::from_text_bytes(
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
executable,
[],
TestContextObject::new(6),
ProgramResult::Ok(expected_result),
);
}
}
#[test]
fn test_err_divide_by_zero() {
let mut prog = [0; 24];
prog[0] = ebpf::MOV32_IMM;
prog[16] = ebpf::EXIT;
let loader = Arc::new(BuiltinProgram::new_mock());
for opc in [
ebpf::UDIV32_REG,
ebpf::UDIV64_REG,
ebpf::UREM32_REG,
ebpf::UREM64_REG,
ebpf::SDIV32_REG,
ebpf::SDIV64_REG,
ebpf::SREM32_REG,
ebpf::SREM64_REG,
] {
prog[8] = opc;
#[allow(unused_mut)]
let mut executable = Executable::<TestContextObject>::from_text_bytes(
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
executable,
[],
TestContextObject::new(2),
ProgramResult::Err(EbpfError::DivideByZero),
);
}
}
#[test]
fn test_err_divide_overflow() {
let mut prog = [0; 40];
prog[0] = ebpf::MOV64_IMM;
LittleEndian::write_i32(&mut prog[4..], 1);
prog[8] = ebpf::LSH64_IMM;
prog[16] = ebpf::MOV64_IMM;
prog[17] = 1; // dst = R1
LittleEndian::write_i32(&mut prog[20..], -1);
prog[25] = 16; // src = R1
LittleEndian::write_i32(&mut prog[28..], -1);
prog[32] = ebpf::EXIT;
let loader = Arc::new(BuiltinProgram::new_mock());
for opc in [
ebpf::SDIV32_IMM,
ebpf::SDIV64_IMM,
ebpf::SREM32_IMM,
ebpf::SREM64_IMM,
ebpf::SDIV32_REG,
ebpf::SDIV64_REG,
ebpf::SREM32_REG,
ebpf::SREM64_REG,
] {
prog[12] = if opc & ebpf::BPF_B != 0 { 63 } else { 31 };
prog[24] = opc;
#[allow(unused_mut)]
let mut executable = Executable::<TestContextObject>::from_text_bytes(
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
executable,
[],
TestContextObject::new(4),
ProgramResult::Err(EbpfError::DivideOverflow),
);
}
}
// BPF_LD : Loads
#[test]
fn test_hor64() {
test_interpreter_and_jit_asm!(
"
hor64 r0, 0x10203040
hor64 r0, 0x01020304
exit",
[],
(),
TestContextObject::new(3),
ProgramResult::Ok(0x1122334400000000),
);
}
#[test]
fn test_ldxb() {
test_interpreter_and_jit_asm!(
"
ldxb r0, [r1+2]
exit",
[0xaa, 0xbb, 0x11, 0xcc, 0xdd],
(),
TestContextObject::new(2),
ProgramResult::Ok(0x11),
);
}
#[test]
fn test_ldxh() {
test_interpreter_and_jit_asm!(
"
ldxh r0, [r1+2]
exit",
[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd],
(),
TestContextObject::new(2),
ProgramResult::Ok(0x2211),
);
}
#[test]
fn test_ldxw() {
test_interpreter_and_jit_asm!(
"
ldxw r0, [r1+2]
exit",
[
0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd, //
],
(),
TestContextObject::new(2),
ProgramResult::Ok(0x44332211),
);
}
#[test]
fn test_ldxh_same_reg() {
test_interpreter_and_jit_asm!(
"
mov r0, r1
sth [r0], 0x1234
ldxh r0, [r0]
exit",
[0xff, 0xff],
(),
TestContextObject::new(4),
ProgramResult::Ok(0x1234),
);
}
#[test]
fn test_lldxdw() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1+2]
exit",
[
0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, //
0x77, 0x88, 0xcc, 0xdd, //
],
(),
TestContextObject::new(2),
ProgramResult::Ok(0x8877665544332211),
);
}
#[test]
fn test_err_ldxdw_oob() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1+6]
exit",
[
0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, //
0x77, 0x88, 0xcc, 0xdd, //
],
(),
TestContextObject::new(1),
ProgramResult::Err(EbpfError::AccessViolation(
AccessType::Load,
0x400000006,
8,
"input"
)),
);
}
#[test]
fn test_err_ldxdw_nomem() {
test_interpreter_and_jit_asm!(
"
ldxdw r0, [r1+6]
exit",
[],
(),
TestContextObject::new(1),
ProgramResult::Err(EbpfError::AccessViolation(
AccessType::Load,
0x400000006,
8,
"input"
)),
);
}
#[test]
fn test_ldxb_all() {
test_interpreter_and_jit_asm!(
"
mov r0, r1
ldxb r9, [r0+0]
lsh r9, 0
ldxb r8, [r0+1]
lsh r8, 4
ldxb r7, [r0+2]
lsh r7, 8
ldxb r6, [r0+3]
lsh r6, 12
ldxb r5, [r0+4]
lsh r5, 16
ldxb r4, [r0+5]
lsh r4, 20
ldxb r3, [r0+6]
lsh r3, 24
ldxb r2, [r0+7]
lsh r2, 28
ldxb r1, [r0+8]
lsh r1, 32
ldxb r0, [r0+9]
lsh r0, 36
or r0, r1
or r0, r2
or r0, r3
or r0, r4
or r0, r5
or r0, r6
or r0, r7
or r0, r8
or r0, r9
exit",
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, //
0x08, 0x09, //
],
(),
TestContextObject::new(31),
ProgramResult::Ok(0x9876543210),
);
}
#[test]
fn test_ldxh_all() {
test_interpreter_and_jit_asm!(
"
mov r0, r1
ldxh r9, [r0+0]
be16 r9
lsh r9, 0
ldxh r8, [r0+2]
be16 r8
lsh r8, 4
ldxh r7, [r0+4]
be16 r7
lsh r7, 8
ldxh r6, [r0+6]
be16 r6
lsh r6, 12
ldxh r5, [r0+8]
be16 r5
lsh r5, 16
ldxh r4, [r0+10]
be16 r4
lsh r4, 20
ldxh r3, [r0+12]
be16 r3
lsh r3, 24
ldxh r2, [r0+14]
be16 r2
lsh r2, 28
ldxh r1, [r0+16]
be16 r1
lsh r1, 32