Skip to contentSkip to conversation

samples/bpf: fix sockex2 example

PATCH Applied Alexei Starovoitov 3 participants 6 messages Nov 23, 2016 – Nov 24, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 samples/bpf: fix sockex2 example

since llvm commit "Do not expand UNDEF SDNode during insn selection lowering" llvm will generate code that uses uninitialized registers for cases where C code is actually uses uninitialized data.
So this sockex2 example is technically broken.
Fix it by initializing on the stack variable fully.
Also increase verifier buffer limit, since verifier output
may not fit in 64k for this sockex2 code depending on llvm version.

---
 samples/bpf/libbpf.h       | 2 +-
 samples/bpf/sockex2_kern.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
samples/bpf/libbpf.h +1 -1
index ac6edb61b64a..de96a935068d 100644
@@ -18,7 +18,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
 int bpf_obj_pin(int fd, const char *pathname);
 int bpf_obj_get(const char *pathname);
 
-#define LOG_BUF_SIZE 65536
+#define LOG_BUF_SIZE (256 * 1024)
 extern char bpf_log_buf[LOG_BUF_SIZE];
 
 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
samples/bpf/sockex2_kern.c +1 -1
index 44e5846c988f..f58acfc92556 100644
@@ -198,7 +198,7 @@ struct bpf_map_def SEC("maps") hash_map = {
 SEC("socket2")
 int bpf_prog2(struct __sk_buff *skb)
 {
-	struct bpf_flow_keys flow;
+	struct bpf_flow_keys flow = {};
 	struct pair *value;
 	u32 key;
 
Signature · 1 line
2.8.0
#1 lore raw

PATCH 2/2 samples/bpf: fix bpf loader

llvm can emit relocations into sections other than program code (like debug info sections). Ignore them during parsing of elf file

---
 samples/bpf/bpf_load.c | 4 ++++
 1 file changed, 4 insertions(+)
samples/bpf/bpf_load.c +4 -0
index 97913e109b14..62f54d6eb8bf 100644
@@ -317,6 +317,10 @@ int load_bpf_file(char *path)
 				    &shdr_prog, &data_prog))
 				continue;
 
+			if (shdr_prog.sh_type != SHT_PROGBITS ||
+			    !(shdr_prog.sh_flags & SHF_EXECINSTR))
+				continue;
+
 			insns = (struct bpf_insn *) data_prog->d_buf;
 
 			processed_sec[shdr.sh_info] = true;
Signature · 1 line
2.8.0
maintainer Acked-by on v1 1/2
#4 lore raw
In reply to Alexei Starovoitov
Quoted 9 lines · Alexei Starovoitov

since llvm commit "Do not expand UNDEF SDNode during insn selection lowering" llvm will generate code that uses uninitialized registers for cases where C code is actually uses uninitialized data.
So this sockex2 example is technically broken.
Fix it by initializing on the stack variable fully.
Also increase verifier buffer limit, since verifier output
may not fit in 64k for this sockex2 code depending on llvm version.

maintainer Applied
#5 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Tue, 22 Nov 2016 16:52:08 -0800

Quoted 9 lines

since llvm commit "Do not expand UNDEF SDNode during insn selection lowering" llvm will generate code that uses uninitialized registers for cases where C code is actually uses uninitialized data.
So this sockex2 example is technically broken.
Fix it by initializing on the stack variable fully.
Also increase verifier buffer limit, since verifier output
may not fit in 64k for this sockex2 code depending on llvm version.

Applied.