blib/lib/Sys/Ebpf/Asm.pm

filter: Statementstmt 79.7% 75 / 94 Branchbran 70.0% 7 / 10 Conditioncond 92.3% 12 / 13 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 75.7% 25 / 33 Podpod 0.0% 0 / 24 totaltotal 69.0% 125 / 181 CC 13 SCAR 12.6
CC
13
Cov
80%
new
10.1
serialize_sequence
6.0
deserialize
3.1
CRAP 3.5
« blib/lib/Sys/Ebpf/Link/Netlink/Socket.pm ↑ Summary blib/lib/Sys/Ebpf/Constants/BpfProgType.pm »
1 package Sys::Ebpf::Asm;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # cf. https://www.kernel.org/doc/html/v6.11-rc7/bpf/standardization/instruction-set.html
8 # cf. https://github.com/iovisor/bpf-docs/blob/master/eBPF.md
9 # BPF instruction classes
10 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 BPF_LD => 0x00,
12 BPF_LDX => 0x01,
13 BPF_ST => 0x02,
14 BPF_STX => 0x03,
15 BPF_ALU => 0x04,
16 BPF_JMP => 0x05,
17 BPF_RET => 0x06,
18 BPF_MISC => 0x07,
19 BPF_ALU64 => 0x07, # Added ALU64
20 2 };
21  
22 # BPF LD/LDX size modifiers
23 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 BPF_W => 0x00,
25 BPF_H => 0x08,
26 BPF_B => 0x10,
27 BPF_DW => 0x18,
28 2 };
29  
30 # BPF LD/LDX mode modifiers
31 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 BPF_IMM => 0x00,
33 BPF_ABS => 0x20,
34 BPF_IND => 0x40,
35 BPF_MEM => 0x60,
36 BPF_XADD => 0xc0,
37 2 };
38  
39 # BPF ALU operations
40 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 BPF_ADD => 0x00,
42 BPF_SUB => 0x10,
43 BPF_MUL => 0x20,
44 BPF_DIV => 0x30,
45 BPF_OR => 0x40,
46 BPF_AND => 0x50,
47 BPF_LSH => 0x60,
48 BPF_RSH => 0x70,
49 BPF_NEG => 0x80,
50 BPF_MOD => 0x90,
51 BPF_XOR => 0xa0,
52 BPF_MOV => 0xb0,
53 BPF_ARSH => 0xc0,
54 BPF_END => 0xd0,
55 2 };
56  
57 # BPF JMP operations
58 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 BPF_JA => 0x00,
60 BPF_JEQ => 0x10,
61 BPF_JGT => 0x20,
62 BPF_JGE => 0x30,
63 BPF_JSET => 0x40,
64 BPF_JNE => 0x50,
65 BPF_JSGT => 0x60,
66 BPF_JSGE => 0x70,
67 BPF_CALL => 0x80,
68 BPF_EXIT => 0x90,
69 BPF_JLT => 0xa0,
70 BPF_JLE => 0xb0,
71 BPF_JSLT => 0xc0,
72 BPF_JSLE => 0xd0,
73 2 };
74  
75 # BPF source operand
76 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 BPF_K => 0x00,
78 BPF_X => 0x08,
79 2 };
80  
81 23 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
Pod undocumented
82 23 my $class = shift;
83 23 my %args;
84  
85 23 ▶ if ( @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 22 23
elsif (@_ == 5) { } 1 21 22
elsif (@_ % 2 == 0) { } 21 0 21
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2201
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
86 1 %args = %{ $_[0] };
87 }
88 elsif ( @_ == 5 ) {
89 1 %args = (
90 code => $_[0],
91 dst_reg => $_[1],
92 src_reg => $_[2],
93 off => $_[3],
94 imm => $_[4],
95 );
96 }
97 elsif ( @_ % 2 == 0 ) {
98 21 %args = @_;
99 }
100 else {
101 0 die "Invalid arguments for constructor";
102 }
103  
104 my $self = {
105 code => $args{code} || 0,
106 dst_reg => $args{dst_reg} || 0,
107 src_reg => $args{src_reg} || 0,
108 off => $args{off} || 0,
109 23 ▶ imm => $args{imm} || 0,
Condition100%10/10
$args{'code'} || 0
l!l
221
$args{'dst_reg'} || 0
l!l
221
$args{'src_reg'} || 0
l!l
1211
$args{'off'} || 0
l!l
419
$args{'imm'} || 0
l!l
149
MC/DC100%1/1
$args{'code'} || 0
$args{'code'}
MC/DC100%1/1
$args{'dst_reg'} || 0
$args{'dst_reg'}
MC/DC100%1/1
$args{'src_reg'} || 0
$args{'src_reg'}
MC/DC100%1/1
$args{'off'} || 0
$args{'off'}
MC/DC100%1/1
$args{'imm'} || 0
$args{'imm'}
Truth table100%2/2
A
A= $args{'code'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'dst_reg'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'src_reg'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'off'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'imm'}
Aresult
11
00
110 };
111  
112 # bitsize bindings
113 23 $self->{code} &= 0xFF;
114 23 $self->{dst_reg} &= 0x0F;
115 23 $self->{src_reg} &= 0x0F;
116 23 $self->{off} &= 0xFFFF;
117 23 $self->{imm} &= 0xFFFFFFFF;
118  
119 23 bless $self, $class;
120 23 return $self;
121 }
122  
123 0 ▶sub serialize_sequence {
Subroutine serialize_sequence: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my ($instructions_ref) = @_;
125 0 my $serialized = '';
126 0 for my $insn (@$instructions_ref) {
127 0 $serialized .= $insn->serialize();
128 }
129 0 return $serialized;
130 }
131  
132 1 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod undocumented
133 1 my ($self) = @_;
134  
135 return pack( 'CCsL',
136 $self->{code},
137 ( $self->{src_reg} << 4 ) & 0b11110000 | $self->{dst_reg}
138 & 0b00001111,
139 $self->{off},
140 1 $self->{imm} );
141 }
142  
143 1 ▶sub deserialize {
Subroutine deserialize: called CC=3 SCAR=11.2
Pod undocumented
144 1 my ( $class, $raw_insn ) = @_;
145  
146 1 ▶ die "Undefined instruction" unless defined $raw_insn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_insn 0 1 1
147 1 ▶ die "instruction too short" unless length($raw_insn) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw_insn == 8 0 1 1
148  
149 1 my ( $code, $dst_src, $off, $imm ) = unpack( 'CCsL', $raw_insn );
150  
151 1 my $self = {
152 code => $code,
153 dst_reg => $dst_src & 0b00001111,
154 src_reg => ( $dst_src & 0b11110000 ) >> 4,
155 off => $off,
156 imm => $imm,
157 };
158  
159 1 bless $self, $class;
160 1 return $self;
161 }
162  
163 0 ▶sub set_code { $_[0]->{code} = $_[1] }
Subroutine set_code: not called CC=1 SCAR=6.9
Pod undocumented
164 0 ▶sub set_dst_reg { $_[0]->{dst_reg} = $_[1] }
Subroutine set_dst_reg: not called CC=1 SCAR=6.9
Pod undocumented
165 0 ▶sub set_src_reg { $_[0]->{src_reg} = $_[1] }
Subroutine set_src_reg: not called CC=1 SCAR=6.9
Pod undocumented
166 0 ▶sub set_off { $_[0]->{off} = $_[1] }
Subroutine set_off: not called CC=1 SCAR=6.9
Pod undocumented
167 0 ▶sub set_imm { $_[0]->{imm} = $_[1] }
Subroutine set_imm: not called CC=1 SCAR=6.9
Pod undocumented
168  
169 19 ▶sub get_code { $_[0]->{code} }
Subroutine get_code: called CC=1 SCAR=0.0
Pod undocumented
170 17 ▶sub get_dst_reg { $_[0]->{dst_reg} }
Subroutine get_dst_reg: called CC=1 SCAR=0.0
Pod undocumented
171 8 ▶sub get_src_reg { $_[0]->{src_reg} }
Subroutine get_src_reg: called CC=1 SCAR=0.0
Pod undocumented
172 2 ▶sub get_off { $_[0]->{off} }
Subroutine get_off: called CC=1 SCAR=0.0
Pod undocumented
173 12 ▶sub get_imm { $_[0]->{imm} }
Subroutine get_imm: called CC=1 SCAR=0.0
Pod undocumented
174  
175 0 ▶sub deserialize_128bit_instruction {
Subroutine deserialize_128bit_instruction: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my ($binary) = @_;
177  
178 0 my $high = Sys::Ebpf::Asm->deserialize( substr( $binary, 0, 8 ) );
179 0 my $low = Sys::Ebpf::Asm->deserialize( substr( $binary, 8, 8 ) );
180  
181 0 return ( $high, $low );
182 }
183  
184 0 ▶sub serialize_128bit_instruction {
Subroutine serialize_128bit_instruction: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my ( $high, $low ) = @_;
186  
187 0 my $serialized_high = $high->serialize();
188 0 my $serialized_low = $low->serialize();
189  
190 0 return $serialized_high . $serialized_low;
191 }
192  
193 ## BPF instruction helpers
194  
195 3 ▶sub BPF_ALU32_REG {
Subroutine BPF_ALU32_REG: called CC=1 SCAR=0.0
Pod undocumented
196 3 my ( $OP, $DST, $SRC ) = @_;
197 3 return __PACKAGE__->new(
198 code => BPF_ALU | $OP | BPF_X,
199 dst_reg => $DST,
200 src_reg => $SRC,
201 );
202 }
203  
204 3 ▶sub BPF_ALU32_IMM {
Subroutine BPF_ALU32_IMM: called CC=1 SCAR=0.0
Pod undocumented
205 3 my ( $OP, $DST, $IMM ) = @_;
206 3 return __PACKAGE__->new(
207 code => BPF_ALU | $OP | BPF_K,
208 dst_reg => $DST,
209 imm => $IMM,
210 );
211 }
212  
213 3 ▶sub BPF_ALU64_REG {
Subroutine BPF_ALU64_REG: called CC=1 SCAR=0.0
Pod undocumented
214 3 my ( $OP, $DST, $SRC ) = @_;
215 3 return __PACKAGE__->new(
216 code => BPF_ALU64 | $OP | BPF_X,
217 dst_reg => $DST,
218 src_reg => $SRC,
219 );
220 }
221  
222 3 ▶sub BPF_ALU64_IMM {
Subroutine BPF_ALU64_IMM: called CC=1 SCAR=0.0
Pod undocumented
223 3 my ( $OP, $DST, $IMM ) = @_;
224 3 return __PACKAGE__->new(
225 code => BPF_ALU64 | $OP | BPF_K,
226 dst_reg => $DST,
227 imm => $IMM,
228 );
229 }
230  
231 2 ▶sub BPF_JMP_REG {
Subroutine BPF_JMP_REG: called CC=1 SCAR=0.0
Pod undocumented
232 2 my ( $OP, $DST, $SRC, $OFF ) = @_;
233 2 return __PACKAGE__->new(
234 code => BPF_JMP | $OP | BPF_X,
235 dst_reg => $DST,
236 src_reg => $SRC,
237 off => $OFF,
238 );
239 }
240  
241 2 ▶sub BPF_JMP_IMM {
Subroutine BPF_JMP_IMM: called CC=1 SCAR=0.0
Pod undocumented
242 2 my ( $OP, $DST, $IMM, $OFF ) = @_;
243 2 return __PACKAGE__->new(
244 code => BPF_JMP | $OP | BPF_K,
245 dst_reg => $DST,
246 imm => $IMM,
247 off => $OFF,
248 );
249 }
250  
251 1 ▶sub BPF_LD_IMM32 {
Subroutine BPF_LD_IMM32: called CC=1 SCAR=0.0
Pod undocumented
252 1 my ( $DST, $IMM ) = @_;
253 1 return __PACKAGE__->new(
254 code => BPF_ALU | BPF_MOV | BPF_K,
255 dst_reg => $DST,
256 imm => $IMM & 0xFFFFFFFF,
257 );
258 }
259  
260 1 ▶sub BPF_LD_IMM64 {
Subroutine BPF_LD_IMM64: called CC=1 SCAR=0.0
Pod undocumented
261 1 my ( $DST, $IMM ) = @_;
262 1 my $high = __PACKAGE__->new(
263 code => BPF_LD | BPF_DW | BPF_IMM,
264 dst_reg => $DST,
265 imm => $IMM & 0xFFFFFFFF,
266 );
267 1 my $low = __PACKAGE__->new( imm => $IMM >> 32, );
268 1 return ( $high, $low );
269 }
270 2 1;
« blib/lib/Sys/Ebpf/Link/Netlink/Socket.pm ↑ Summary blib/lib/Sys/Ebpf/Constants/BpfProgType.pm »