Skip to content

Commit

Permalink
Readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyz87 committed Jan 23, 2023
1 parent bedb5a1 commit 6bd155f
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ pip3 install -r requirements.txt

```bash
./tests.py
.
--------------------
Ran 1 test in 0.010s
...........WARNING:root:Can't convert BPF_LD_IMM64()
DEBUG:root:
Errors:
BPF_LD_IMM64 : None
Parse tree:
call_expression:
identifier: BPF_LD_IMM64
argument_list:
.........
----------------------------------------------------------------------
Ran 20 tests in 0.072s
OK
```
Expand All @@ -33,6 +42,7 @@ Usage:
```bash
usage: rewrite.py [--debug]
[--newlines]
[--replace-st-mem]
file_name
C code for converted test cases is printed to stdout.
Expand All @@ -43,6 +53,8 @@ positional arguments:
options:
--debug print some debug info
--newlines add \n after each asm instruction
--replace-st-mem replace BPF_ST_MEM insns with pairs
of MOV/BPF_STX_MEM instructions.
```
Example:
Expand All @@ -63,45 +75,44 @@ Output:
#define MAX_ENTRIES 11
struct test_val {
unsigned int index;
int foo[MAX_ENTRIES];
unsigned int index;
int foo[MAX_ENTRIES];
};
struct map_struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, long long);
__type(value, struct test_val);
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, long long);
__type(value, struct test_val);
} map_hash_48b SEC(".maps");
/* invalid and of negative number */
__description("invalid and of negative number")
__failure __msg("R0 max value is outside of the allowed memory range")
__failure_unpriv
__flag(BPF_F_ANY_ALIGNMENT)
SEC("socket")
__naked
__failure
__msg("R0 max value is outside of the allowed memory range")
__needs_efficient_unaligned_access
void invalid_and_of_negative_number(void)
__naked void invalid_and_of_negative_number(void)
{
asm volatile (
"*(u64*)(r10 -8) = 0;"
"r2 = r10;"
"r2 += -8;"
"r1 = %[map_hash_48b] ll;"
"call %[bpf_map_lookup_elem];"
"if r0 == 0 goto l0_0;"
"r1 = *(u8*)(r0 +0);"
"r1 &= -4;"
"r1 <<= 2;"
"r0 += r1;"
"l0_0:"
"*(u64*)(r0 +0) = %[test_val_foo_offset];"
"exit;"
:
: [test_val_foo_offset]"i"(offsetof(struct test_val, foo)),
__imm(bpf_map_lookup_elem),
__imm_addr(map_hash_48b)
: __clobber_all);
asm volatile (
" *(u64*)(r10 - 8) = 0; \
r2 = r10; \
r2 += -8; \
r1 = %[map_hash_48b] ll; \
call %[bpf_map_lookup_elem]; \
if r0 == 0 goto l0_%=; \
r1 = *(u8*)(r0 + 0); \
r1 &= -4; \
r1 <<= 2; \
r0 += r1; \
l0_%=: \
// yes, label is misplaced in the original \
*(u64*)(r0 + 0) = %[test_val_foo_offset]; \
exit; \
" :
: [test_val_foo_offset]"i"(offsetof(struct test_val, foo)),
__imm(bpf_map_lookup_elem),
__imm_addr(map_hash_48b)
: __clobber_all);
}

...
```

0 comments on commit 6bd155f

Please sign in to comment.