Skip to contentSkip to conversation

bpftool: Align output skeleton ELF code

PATCH Superseded Ian Rogers 2 participants 5 messages Oct 4, 2023 – Oct 5, 2023

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH v4 1/2 bpftool: Align output skeleton ELF code

libbpf accesses the ELF data requiring at least 8 byte alignment, however, the data is generated into a C string that doesn't guarantee alignment. Fix this by assigning to an aligned char array. Use sizeof on the array, less one for the \0 terminator, rather than generating a constant.

Fixes a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data")
---
 tools/bpf/bpftool/gen.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/bpf/bpftool/gen.c +9 -6
index 2883660d6b67..b8ebcee9bc56 100644
@@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv)
 	codegen("\
 		\n\
 									    \n\
-			s->data = (void *)%2$s__elf_bytes(&s->data_sz);	    \n\
+			s->data = (void *)%1$s__elf_bytes(&s->data_sz);	    \n\
 									    \n\
 			obj->skeleton = s;				    \n\
 			return 0;					    \n\
@@ -1218,12 +1218,12 @@ static int do_skeleton(int argc, char **argv)
 			return err;					    \n\
 		}							    \n\
 									    \n\
-		static inline const void *%2$s__elf_bytes(size_t *sz)	    \n\
+		static inline const void *%1$s__elf_bytes(size_t *sz)	    \n\
 		{							    \n\
-			*sz = %1$d;					    \n\
-			return (const void *)\"\\			    \n\
-		"
-		, file_sz, obj_name);
+			static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
+		",
+		obj_name
+	);
 
 	/* embed contents of BPF object file */
 	print_hex(obj_data, file_sz);
@@ -1231,6 +1231,9 @@ static int do_skeleton(int argc, char **argv)
 	codegen("\
 		\n\
 		\";							    \n\
+									    \n\
+			*sz = sizeof(data) - 1;				    \n\
+			return (const void *)data;			    \n\
 		}							    \n\
 									    \n\
 		#ifdef __cplusplus					    \n\
Signature · 1 line
2.42.0.609.gbb76f46606-goog
author
#1 lore raw

PATCH v4 2/2 bpftool: Align bpf_load_and_run_opts insns and data

A C string lacks alignment so use aligned arrays to avoid potential alignment problems. Switch to using sizeof (less 1 for the \0 terminator) rather than a hardcode size constant.

---
 tools/bpf/bpftool/gen.c | 47 ++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 22 deletions(-)
tools/bpf/bpftool/gen.c +25 -22
index b8ebcee9bc56..7a545dcabe38 100644
@@ -408,8 +408,8 @@ static void codegen(const char *template, ...)
 		/* skip baseline indentation tabs */
 		for (n = skip_tabs; n > 0; n--, src++) {
 			if (*src != '\t') {
-				p_err("not enough tabs at pos %td in template '%s'",
-				      src - template - 1, template);
+				p_err("not enough tabs at pos %td in template '%s'\n'%s'",
+					src - template - 1, template, src);
 				free(s);
 				exit(-1);
 			}
@@ -708,17 +708,22 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 
 		codegen("\
 		\n\
-			skel->%1$s = skel_prep_map_data((void *)\"\\	    \n\
-		", ident);
+			{						    \n\
+				static const char data[] __attribute__((__aligned__(8))) = \"\\\n\
+		");
 		mmap_data = bpf_map__initial_value(map, &mmap_size);
 		print_hex(mmap_data, mmap_size);
 		codegen("\
 		\n\
-		\", %1$zd, %2$zd);					    \n\
-			if (!skel->%3$s)				    \n\
-				goto cleanup;				    \n\
-			skel->maps.%3$s.initial_value = (__u64) (long) skel->%3$s;\n\
-		", bpf_map_mmap_sz(map), mmap_size, ident);
+		\";							    \n\
+									    \n\
+				skel->%1$s = skel_prep_map_data((void *)data, %2$zd,\n\
+								sizeof(data) - 1);\n\
+				if (!skel->%1$s)			    \n\
+					goto cleanup;			    \n\
+				skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\
+			}						    \n\
+			", ident, bpf_map_mmap_sz(map));
 	}
 	codegen("\
 		\n\
@@ -733,32 +738,30 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 		{							    \n\
 			struct bpf_load_and_run_opts opts = {};		    \n\
 			int err;					    \n\
-									    \n\
-			opts.ctx = (struct bpf_loader_ctx *)skel;	    \n\
-			opts.data_sz = %2$d;				    \n\
-			opts.data = (void *)\"\\			    \n\
+			static const char opts_data[] __attribute__((__aligned__(8))) = \"\\\n\
 		",
-		obj_name, opts.data_sz);
+		obj_name);
 	print_hex(opts.data, opts.data_sz);
 	codegen("\
 		\n\
 		\";							    \n\
+			static const char opts_insn[] __attribute__((__aligned__(8))) = \"\\\n\
 		");
-
-	codegen("\
-		\n\
-			opts.insns_sz = %d;				    \n\
-			opts.insns = (void *)\"\\			    \n\
-		",
-		opts.insns_sz);
 	print_hex(opts.insns, opts.insns_sz);
 	codegen("\
 		\n\
 		\";							    \n\
+									    \n\
+			opts.ctx = (struct bpf_loader_ctx *)skel;	    \n\
+			opts.data_sz = sizeof(opts_data) - 1;		    \n\
+			opts.data = (void *)opts_data;			    \n\
+			opts.insns_sz = sizeof(opts_insn) - 1;		    \n\
+			opts.insns = (void *)opts_insn;			    \n\
+									    \n\
 			err = bpf_load_and_run(&opts);			    \n\
 			if (err < 0)					    \n\
 				return err;				    \n\
-		", obj_name);
+		");
 	bpf_object__for_each_map(map, obj) {
 		const char *mmap_flags;
 
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Acked-by on v4 2/2
#2 lore raw

Re: [PATCH v4 2/2] bpftool: Align bpf_load_and_run_opts insns and data

Quoted 14 lines · Ian Rogers

A C string lacks alignment so use aligned arrays to avoid potential alignment problems. Switch to using sizeof (less 1 for the \0 terminator) rather than a hardcode size constant.

---
 tools/bpf/bpftool/gen.c | 47 ++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 22 deletions(-)
tools/bpf/bpftool/gen.c +0 -0
index b8ebcee9bc56..7a545dcabe38 100644

tools/bpf/bpftool/gen.c static void codegen(const char *template, ...) · line 412

Quoted 7 lines
tools/bpf/bpftool/gen.c +2 -2
@@ -408,8 +408,8 @@ static void codegen(const char *template, ...)
 		/* skip baseline indentation tabs */
 		for (n = skip_tabs; n > 0; n--, src++) {
 			if (*src != '\t') {
-				p_err("not enough tabs at pos %td in template '%s'",
-				      src - template - 1, template);
+				p_err("not enough tabs at pos %td in template '%s'\n'%s'",
+					src - template - 1, template, src);

Nit: This line is no longer aligned with the opening parenthesis.

Other than that:

author
#3 lore raw

Re: [PATCH v4 2/2] bpftool: Align bpf_load_and_run_opts insns and data

Quoted 26 lines · Quentin Monnet
Quoted 14 lines · Ian Rogers

A C string lacks alignment so use aligned arrays to avoid potential alignment problems. Switch to using sizeof (less 1 for the \0 terminator) rather than a hardcode size constant.

---
 tools/bpf/bpftool/gen.c | 47 ++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 22 deletions(-)
tools/bpf/bpftool/gen.c +0 -0
index b8ebcee9bc56..7a545dcabe38 100644

tools/bpf/bpftool/gen.c static void codegen(const char *template, ...) · line 412

Quoted 7 lines
tools/bpf/bpftool/gen.c +2 -2
@@ -408,8 +408,8 @@ static void codegen(const char *template, ...)
              /* skip baseline indentation tabs */
              for (n = skip_tabs; n > 0; n--, src++) {
                      if (*src != '\t') {
-                             p_err("not enough tabs at pos %td in template '%s'",
-                                   src - template - 1, template);
+                             p_err("not enough tabs at pos %td in template '%s'\n'%s'",
+                                     src - template - 1, template, src);

Nit: This line is no longer aligned with the opening parenthesis.

Quoted 3 lines

Other than that:

Thanks! Nit fixed in v5.

Ian

Acked-by on v4 1/2
#4 lore raw
In reply to Ian Rogers
Quoted 9 lines · Ian Rogers

libbpf accesses the ELF data requiring at least 8 byte alignment, however, the data is generated into a C string that doesn't guarantee alignment. Fix this by assigning to an aligned char array. Use sizeof on the array, less one for the \0 terminator, rather than generating a constant.

Fixes a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data")

Thanks