forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_spec.lua
More file actions
1035 lines (1028 loc) · 23.5 KB
/
Copy pathcodegen_spec.lua
File metadata and controls
1035 lines (1028 loc) · 23.5 KB
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
local ffi = require('ffi')
local S = require('syscall')
-- Normalize whitespace and remove empty lines
local function normalize_code(c)
local res = {}
for line in string.gmatch(c,'[^\r\n]+') do
local op, d, s, t = line:match('(%S+)%s+(%S+)%s+(%S+)%s*([^-]*)')
if op then
t = t and t:match('^%s*(.-)%s*$')
table.insert(res, string.format('%s\t%s %s %s', op, d, s, t))
end
end
return table.concat(res, '\n')
end
-- Compile code and check result
local function compile(t)
local bpf = require('bpf')
-- require('jit.bc').dump(t.input)
local code, err = bpf(t.input)
assert.truthy(code)
assert.falsy(err)
if code then
if t.expect then
local got = normalize_code(bpf.dump_string(code, 1, true))
-- if normalize_code(t.expect) ~= got then print(bpf.dump_string(code, 1)) end
assert.same(normalize_code(t.expect), got)
end
end
end
-- Make a mock map variable
local function makemap(type, max_entries, key_ctype, val_ctype)
if not key_ctype then key_ctype = ffi.typeof('uint32_t') end
if not val_ctype then val_ctype = ffi.typeof('uint32_t') end
if not max_entries then max_entries = 4096 end
return {
__map = true,
max_entries = max_entries,
key = ffi.new(ffi.typeof('$ [1]', key_ctype)),
val = ffi.new(ffi.typeof('$ [1]', val_ctype)),
map_type = S.c.BPF_MAP[type],
key_type = key_ctype,
val_type = val_ctype,
fd = 42,
}
end
describe('codegen', function()
-- luacheck: ignore 113 211 212 311 511
describe('constants', function()
it('remove dead constant store', function()
compile {
input = function ()
local proto = 5
end,
expect = [[
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('materialize constant', function()
compile {
input = function ()
return 5
end,
expect = [[
MOV R0 #5
EXIT R0 #0
]]
}
end)
it('materialize constant longer than i32', function()
compile {
input = function ()
return 4294967295
end,
expect = [[
LDDW R0 #4294967295
EXIT R0 #0
]]
}
end)
it('materialize cdata constant', function()
compile {
input = function ()
return 5ULL
end,
expect = [[
LDDW R0 #5 -- composed instruction
EXIT R0 #0
]]
}
end)
it('materialize signed cdata constant', function()
compile {
input = function ()
return 5LL
end,
expect = [[
LDDW R0 #5 -- composed instruction
EXIT R0 #0
]]
}
end)
it('materialize coercible numeric cdata constant', function()
compile {
input = function ()
return 0x00005
end,
expect = [[
MOV R0 #5
EXIT R0 #0
]]
}
end)
it('materialize constant through variable', function()
compile {
input = function ()
local proto = 5
return proto
end,
expect = [[
MOV R0 #5
EXIT R0 #0
]]
}
end)
it('eliminate constant expressions', function()
compile {
input = function ()
return 2 + 3 - 0
end,
expect = [[
MOV R0 #5
EXIT R0 #0
]]
}
end)
it('eliminate constant expressions (if block)', function()
compile {
input = function ()
local proto = 5
if proto == 5 then
proto = 1
end
return proto
end,
expect = [[
MOV R0 #1
EXIT R0 #0
]]
}
end)
it('eliminate negative constant expressions (if block) NYI', function()
-- always negative condition is not fully eliminated
compile {
input = function ()
local proto = 5
if false then
proto = 1
end
return proto
end,
expect = [[
MOV R7 #5
STXDW [R10-8] R7
MOV R7 #0
JEQ R7 #0 => 0005
LDXDW R0 [R10-8]
EXIT R0 #0
]]
}
end)
end)
describe('variables', function()
it('classic packet access (fold constant offset)', function()
compile {
input = function (skb)
return eth.ip.tos -- constant expression will fold
end,
expect = [[
LDB R0 skb[15]
EXIT R0 #0
]]
}
end)
it('classic packet access (load non-constant offset)', function()
compile {
input = function (skb)
return eth.ip.udp.src_port -- need to skip variable-length header
end,
expect = [[
LDB R0 skb[14]
AND R0 #15
LSH R0 #2
ADD R0 #14
STXDW [R10-16] R0 -- NYI: erase dead store
LDH R0 skb[R0+0]
END R0 R0
EXIT R0 #0
]]
}
end)
it('classic packet access (manipulate dissector offset)', function()
compile {
input = function (skb)
local ptr = eth.ip.udp.data + 1
return ptr[0] -- dereference dissector pointer
end,
expect = [[
LDB R0 skb[14]
AND R0 #15
LSH R0 #2
ADD R0 #14 -- NYI: fuse commutative operations in second pass
ADD R0 #8
ADD R0 #1
STXDW [R10-16] R0
LDB R0 skb[R0+0]
EXIT R0 #0
]]
}
end)
it('classic packet access (multi-byte load)', function()
compile {
input = function (skb)
local ptr = eth.ip.udp.data
return ptr(1, 5) -- load 4 bytes
end,
expect = [[
LDB R0 skb[14]
AND R0 #15
LSH R0 #2
ADD R0 #14
ADD R0 #8
MOV R7 R0
STXDW [R10-16] R0 -- NYI: erase dead store
LDW R0 skb[R7+1]
END R0 R0
EXIT R0 #0
]]
}
end)
it('direct skb field access', function()
compile {
input = function (skb)
return skb.len
end,
expect = [[
LDXW R7 [R6+0]
MOV R0 R7
EXIT R0 #0
]]
}
end)
it('direct skb data access (manipulate offset)', function()
compile {
input = function (skb)
local ptr = skb.data + 5
return ptr[0]
end,
expect = [[
LDXW R7 [R6+76]
ADD R7 #5
LDXB R8 [R7+0] -- NYI: transform LD + ADD to LD + offset addressing
MOV R0 R8
EXIT R0 #0
]]
}
end)
it('direct skb data access (offset boundary check)', function()
compile {
input = function (skb)
local ptr = skb.data + 5
if ptr < skb.data_end then
return ptr[0]
end
end,
expect = [[
LDXW R7 [R6+76]
ADD R7 #5
LDXW R8 [R6+80]
JGE R7 R8 => 0008
LDXB R8 [R7+0]
MOV R0 R8
EXIT R0 #0
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (array, const load, const store)', function()
compile {
input = function (skb)
local mem = ffi.new('uint8_t [16]')
mem[0] = 5
end,
expect = [[
MOV R0 #0
STXDW [R10-40] R0
STXDW [R10-48] R0 -- NYI: erase zero-fill on allocation when it's loaded later
STB [R10-48] #5
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (array, const load, packet store)', function()
compile {
input = function (skb)
local mem = ffi.new('uint8_t [7]')
mem[0] = eth.ip.tos
end,
expect = [[
MOV R0 #0
STXDW [R10-40] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
STXB [R10-40] R0
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (array, packet load, const store)', function()
compile {
input = function (skb)
local mem = ffi.new('uint8_t [1]')
mem[eth.ip.tos] = 5
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
MOV R7 R0
ADD R7 R10
STB [R7-48] #5
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (array, packet load, packet store)', function()
compile {
input = function (skb)
local mem = ffi.new('uint8_t [7]')
local v = eth.ip.tos
mem[v] = v
end,
expect = [[
MOV R0 #0
STXDW [R10-40] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
MOV R7 R0
ADD R7 R10
STXB [R7-40] R0
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (struct, const/packet store)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
compile {
input = function (skb)
local mem = ffi.new(kv_t)
mem.a = 5
mem.b = eth.ip.tos
end,
expect = [[
MOV R0 #0
STXDW [R10-40] R0
STXDW [R10-48] R0 -- NYI: erase zero-fill on allocation when it's loaded later
MOV R7 #5
STXDW [R10-48] R7
LDB R0 skb[15]
STXDW [R10-40] R0
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('access stack memory (struct, const/stack store)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
compile {
input = function (skb)
local m1 = ffi.new(kv_t)
local m2 = ffi.new(kv_t)
m1.a = 5
m2.b = m1.a
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0 -- NYI: erase zero-fill on allocation when it's loaded later
MOV R0 #0
STXDW [R10-64] R0
STXDW [R10-72] R0 -- NYI: erase zero-fill on allocation when it's loaded later
MOV R7 #5
STXDW [R10-56] R7
LDXDW R7 [R10-56]
STXDW [R10-64] R7
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key load)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
return array_map[0]
end,
expect = [[
LDDW R1 #42
STW [R10-28] #0
MOV R2 R10
ADD R2 #4294967268
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0009
LDXW R0 [R0+0]
EXIT R0 #0
]]
}
end)
it('array map (u32, packet key load)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
return array_map[eth.ip.tos]
end,
expect = [[
LDB R0 skb[15]
LDDW R1 #42
STXW [R10-36] R0
MOV R2 R10
ADD R2 #4294967260
STXDW [R10-24] R0 -- NYI: erase dead store
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0011
LDXW R0 [R0+0]
EXIT R0 #0
]]
}
end)
it('array map (u32, const key store, const value)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
array_map[0] = 5
end,
expect = [[
LDDW R1 #42
STW [R10-36] #0
MOV R2 R10
ADD R2 #4294967260
MOV R4 #0
STW [R10-40] #5
MOV R3 R10
ADD R3 #4294967256
CALL R0 #2 ; map_update_elem
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key store, packet value)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
array_map[0] = eth.ip.tos
end,
expect = [[
LDB R0 skb[15]
STXDW [R10-24] R0
LDDW R1 #42
STW [R10-36] #0
MOV R2 R10
ADD R2 #4294967260
MOV R4 #0
MOV R3 R10
ADD R3 #4294967272
CALL R0 #2 ; map_update_elem
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key store, map value)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
array_map[0] = array_map[1]
end,
expect = [[
LDDW R1 #42
STW [R10-36] #1
MOV R2 R10
ADD R2 #4294967260
CALL R0 #1 ; map_lookup_elem
STXDW [R10-24] R0
LDDW R1 #42
STW [R10-36] #0
MOV R2 R10
ADD R2 #4294967260
MOV R4 #0
LDXDW R3 [R10-24]
JEQ R3 #0 => 0017
LDXW R3 [R3+0]
STXW [R10-40] R3
MOV R3 R10
ADD R3 #4294967256
CALL R0 #2 ; map_update_elem
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key replace, const value)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
local val = array_map[0]
if val then
val[0] = val[0] + 1
else
array_map[0] = 5
end
end,
expect = [[
LDDW R1 #42
STW [R10-44] #0
MOV R2 R10
ADD R2 #4294967252
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0013 -- if (map_value ~= NULL)
LDXW R7 [R0+0]
ADD R7 #1
STXW [R0+0] R7
MOV R7 #0
JEQ R7 #0 => 0025 -- skip false branch
STXDW [R10-16] R0
LDDW R1 #42
STW [R10-44] #0
MOV R2 R10
ADD R2 #4294967252
MOV R4 #0
STW [R10-48] #5
MOV R3 R10
ADD R3 #4294967248
CALL R0 #2 ; map_update_elem
LDXDW R0 [R10-16]
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key replace xadd, const value)', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
local val = array_map[0]
if val then
xadd(val, 1)
else
array_map[0] = 5
end
end,
expect = [[
LDDW R1 #42
STW [R10-52] #0
MOV R2 R10
ADD R2 #4294967244
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0014 -- if (map_value ~= NULL)
MOV R7 #1
MOV R8 R0
STXDW [R10-16] R0
XADDW [R8+0] R7
MOV R7 #0
JEQ R7 #0 => 0025 -- skip false branch
STXDW [R10-16] R0
LDDW R1 #42
STW [R10-52] #0
MOV R2 R10
ADD R2 #4294967244
MOV R4 #0
STW [R10-56] #5
MOV R3 R10
ADD R3 #4294967240
CALL R0 #2 ; map_update_elem
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (u32, const key replace xadd, const value) inverse nil check', function()
local array_map = makemap('array', 256)
compile {
input = function (skb)
local val = array_map[0]
if not val then
array_map[0] = 5
else
xadd(val, 1)
end
end,
expect = [[
LDDW R1 #42
STW [R10-52] #0
MOV R2 R10
ADD R2 #4294967244
CALL R0 #1 ; map_lookup_elem
JNE R0 #0 => 0021
STXDW [R10-16] R0
LDDW R1 #42
STW [R10-52] #0
MOV R2 R10
ADD R2 #4294967244
MOV R4 #0
STW [R10-56] #5
MOV R3 R10
ADD R3 #4294967240
CALL R0 #2 ; map_update_elem
MOV R7 #0
JEQ R7 #0 => 0025
MOV R7 #1
MOV R8 R0
STXDW [R10-16] R0
XADDW [R8+0] R7
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack key load)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = 2
key.b = 3
local val = array_map[key] -- Use composite key from stack memory
if val then
return val.a
end
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0 -- NYI: erase zero-fill on allocation when it's loaded later
MOV R7 #2
STXDW [R10-56] R7
MOV R7 #3
STXDW [R10-48] R7
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0017
LDXDW R7 [R0+0]
MOV R0 R7
EXIT R0 #0
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack key store)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = 2
key.b = 3
array_map[key] = key -- Use composite key from stack memory
end,
expect = [[
MOV R0 #0
STXDW [R10-40] R0
STXDW [R10-48] R0 -- NYI: erase zero-fill on allocation when it's loaded later
MOV R7 #2
STXDW [R10-48] R7
MOV R7 #3
STXDW [R10-40] R7
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967248
MOV R4 #0
MOV R3 R10
ADD R3 #4294967248
CALL R0 #2 ; map_update_elem
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack/packet key update, const value)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = eth.ip.tos -- Load key part from dissector
local val = array_map[key]
if val then
val.a = 5
end
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
STXDW [R10-56] R0
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0014
MOV R7 #5
STXDW [R0+0] R7
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack/packet key update, map value)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = eth.ip.tos -- Load key part from dissector
local val = array_map[key]
if val then
val.a = val.b
end
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
STXDW [R10-56] R0
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0014
LDXDW R7 [R0+8]
STXDW [R0+0] R7
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack/packet key update, stack value)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = eth.ip.tos -- Load key part from dissector
local val = array_map[key]
if val then
val.a = key.b
end
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0 -- NYI: erase zero-fill on allocation when it's loaded later
LDB R0 skb[15]
STXDW [R10-56] R0
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0014
LDXDW R7 [R10-48]
STXDW [R0+0] R7
MOV R0 #0
EXIT R0 #0
]]
}
end)
it('array map (struct, stack/packet key replace, stack value)', function()
local kv_t = 'struct { uint64_t a; uint64_t b; }'
local array_map = makemap('array', 256, ffi.typeof(kv_t), ffi.typeof(kv_t))
compile {
input = function (skb)
local key = ffi.new(kv_t)
key.a = eth.ip.tos -- Load key part from dissector
local val = array_map[key]
if val then
val.a = key.b
else
array_map[key] = key
end
end,
expect = [[
MOV R0 #0
STXDW [R10-48] R0
STXDW [R10-56] R0
LDB R0 skb[15]
STXDW [R10-56] R0
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
CALL R0 #1 ; map_lookup_elem
JEQ R0 #0 => 0016 -- if (map_value ~= NULL)
LDXDW R7 [R10-48]
STXDW [R0+0] R7
MOV R7 #0
JEQ R7 #0 => 0026 -- jump over false branch
STXDW [R10-24] R0
LDDW R1 #42
MOV R2 R10
ADD R2 #4294967240
MOV R4 #0
MOV R3 R10
ADD R3 #4294967240
CALL R0 #2 ; map_update_elem
LDXDW R0 [R10-24]
MOV R0 #0
EXIT R0 #0
]]
}
end)
end)
describe('control flow', function()
it('condition with constant return', function()
compile {
input = function (skb)
local v = eth.ip.tos
if v then
return 1
else
return 0
end
end,
expect = [[
LDB R0 skb[15]
JEQ R0 #0 => 0005
MOV R0 #1
EXIT R0 #0
MOV R0 #0 -- 0005 jump target
EXIT R0 #0
]]
}
end)
it('condition with cdata constant return', function()
local cdata = 2ULL
compile {
input = function (skb)
local v = eth.ip.tos
if v then
return cdata + 1
else
return 0
end
end,
expect = [[
LDB R0 skb[15]
JEQ R0 #0 => 0006
LDDW R0 #3
EXIT R0 #0
MOV R0 #0 -- 0006 jump target
EXIT R0 #0
]]
}
end)
it('condition with constant return (inversed)', function()
compile {
input = function (skb)
local v = eth.ip.tos
if not v then
return 1
else
return 0
end
end,
expect = [[
LDB R0 skb[15]
JNE R0 #0 => 0005
MOV R0 #1
EXIT R0 #0
MOV R0 #0 -- 0005 jump target
EXIT R0 #0
]]
}
end)
it('condition with variable mutation', function()
compile {
input = function (skb)
local v = 0
if eth.ip.tos then
v = 1
end
return v
end,
expect = [[
LDB R0 skb[15]
MOV R1 #0
STXDW [R10-16] R1
JEQ R0 #0 => 0007
MOV R7 #1
STXDW [R10-16] R7
LDXDW R0 [R10-16]
EXIT R0 #0
]]
}
end)
it('condition with nil variable mutation', function()
compile {
input = function (skb)
local v -- nil, will be elided
if eth.ip.tos then
v = 1
else
v = 0
end
return v
end,
expect = [[
LDB R0 skb[15]
JEQ R0 #0 => 0007
MOV R7 #1
STXDW [R10-16] R7
MOV R7 #0
JEQ R7 #0 => 0009
MOV R7 #0
STXDW [R10-16] R7
LDXDW R0 [R10-16]
EXIT R0 #0
]]
}
end)
it('nested condition with variable mutation', function()
compile {
input = function (skb)
local v = 0
local tos = eth.ip.tos
if tos then
if tos > 5 then
v = 5
else
v = 1
end
end
return v
end,
expect = [[
LDB R0 skb[15]
MOV R1 #0
STXDW [R10-16] R1 -- materialize v = 0
JEQ R0 #0 => 0013 -- if not tos
MOV R7 #5
JGE R7 R0 => 0011 -- if 5 > tos
MOV R7 #5
STXDW [R10-16] R7 -- materialize v = 5
MOV R7 #0
JEQ R7 #0 => 0013
MOV R7 #1 -- 0011 jump target
STXDW [R10-16] R7 -- materialize v = 1
LDXDW R0 [R10-16]
EXIT R0 #0
]]
}
end)
it('nested condition with variable shadowing', function()
compile {
input = function (skb)
local v = 0
local tos = eth.ip.tos
if tos then
local v = 0 -- luacheck: ignore 231
if tos > 5 then
v = 5 -- changing shadowing variable
end
else
v = 1
end
return v
end,
expect = [[
LDB R0 skb[15]
MOV R1 #0
STXDW [R10-16] R1 -- materialize v = 0
JEQ R0 #0 => 0011 -- if not tos
MOV R7 #5
MOV R1 #0
STXDW [R10-32] R1 -- materialize shadowing variable
JGE R7 R0 => 0013 -- if 5 > tos