Skip to contentSkip to conversation

btf_encoder: Add extra checks for symbol names

PATCH In review Jiri Olsa 4 participants 4 messages Jan 13, 2021 – Jan 14, 2021 BPF

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v2 1/1 btf_encoder: Add extra checks for symbol names

When processing kernel image build by clang we can find some functions without the name, which causes pahole to segfault.

Adding extra checks to make sure we always have
function's name defined before using it.

---
v2 changes:
- reorg the code based on Andrii's suggestion

 btf_encoder.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
btf_encoder.c +11 -2
index 333973054b61..5557c9efd365 100644
@@ -68,10 +68,14 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
 	struct elf_function *new;
 	static GElf_Shdr sh;
 	static int last_idx;
+	const char *name;
 	int idx;
 
 	if (elf_sym__type(sym) != STT_FUNC)
 		return 0;
+	name = elf_sym__name(sym, btfe->symtab);
+	if (!name)
+		return 0;
 
 	if (functions_cnt == functions_alloc) {
 		functions_alloc = max(1000, functions_alloc * 3 / 2);
@@ -94,7 +98,7 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
 		last_idx = idx;
 	}
 
-	functions[functions_cnt].name = elf_sym__name(sym, btfe->symtab);
+	functions[functions_cnt].name = name;
 	functions[functions_cnt].addr = elf_sym__value(sym);
 	functions[functions_cnt].sh_addr = sh.sh_addr;
 	functions[functions_cnt].generated = false;
@@ -731,8 +735,13 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 			continue;
 		if (functions_cnt) {
 			struct elf_function *func;
+			const char *name;
+
+			name = function__name(fn, cu);
+			if (!name)
+				continue;
 
-			func = find_function(btfe, function__name(fn, cu));
+			func = find_function(btfe, name);
 			if (!func || func->generated)
 				continue;
 			func->generated = true;
Signature · 1 line
2.26.2
#1 lore raw
Quoted 3 lines · Jiri Olsa

When processing kernel image build by clang we can find some functions without the name, which causes pahole to segfault.

Just curious. What kinds of functions gcc generates names and clang doesn't?

Quoted 56 lines

Adding extra checks to make sure we always have
function's name defined before using it.

---
v2 changes:
- reorg the code based on Andrii's suggestion

  btf_encoder.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
btf_encoder.c +11 -2
index 333973054b61..5557c9efd365 100644
@@ -68,10 +68,14 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
  	struct elf_function *new;
  	static GElf_Shdr sh;
  	static int last_idx;
+	const char *name;
  	int idx;
 
  	if (elf_sym__type(sym) != STT_FUNC)
  		return 0;
+	name = elf_sym__name(sym, btfe->symtab);
+	if (!name)
+		return 0;
 
  	if (functions_cnt == functions_alloc) {
  		functions_alloc = max(1000, functions_alloc * 3 / 2);
@@ -94,7 +98,7 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
  		last_idx = idx;
  	}
 
-	functions[functions_cnt].name = elf_sym__name(sym, btfe->symtab);
+	functions[functions_cnt].name = name;
  	functions[functions_cnt].addr = elf_sym__value(sym);
  	functions[functions_cnt].sh_addr = sh.sh_addr;
  	functions[functions_cnt].generated = false;
@@ -731,8 +735,13 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
  			continue;
  		if (functions_cnt) {
  			struct elf_function *func;
+			const char *name;
+
+			name = function__name(fn, cu);
+			if (!name)
+				continue;
 
-			func = find_function(btfe, function__name(fn, cu));
+			func = find_function(btfe, name);
  			if (!func || func->generated)
  				continue;
  			func->generated = true;
#2 lore raw
Quoted 9 lines · Yonghong Song
Quoted 3 lines · Jiri Olsa

When processing kernel image build by clang we can find some functions without the name, which causes pahole to segfault.

Just curious. What kinds of functions gcc generates names and clang doesn't?

can't say.. I guess we could compare nm output
of both gcc and clang vmlinux for same .config

jirka

Quoted 54 lines
Quoted 53 lines

Adding extra checks to make sure we always have
function's name defined before using it.

---
v2 changes:
- reorg the code based on Andrii's suggestion

  btf_encoder.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
btf_encoder.c +11 -2
index 333973054b61..5557c9efd365 100644
@@ -68,10 +68,14 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
  	struct elf_function *new;
  	static GElf_Shdr sh;
  	static int last_idx;
+	const char *name;
  	int idx;
  	if (elf_sym__type(sym) != STT_FUNC)
  		return 0;
+	name = elf_sym__name(sym, btfe->symtab);
+	if (!name)
+		return 0;
  	if (functions_cnt == functions_alloc) {
  		functions_alloc = max(1000, functions_alloc * 3 / 2);
@@ -94,7 +98,7 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
  		last_idx = idx;
  	}
-	functions[functions_cnt].name = elf_sym__name(sym, btfe->symtab);
+	functions[functions_cnt].name = name;
  	functions[functions_cnt].addr = elf_sym__value(sym);
  	functions[functions_cnt].sh_addr = sh.sh_addr;
  	functions[functions_cnt].generated = false;
@@ -731,8 +735,13 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
  			continue;
  		if (functions_cnt) {
  			struct elf_function *func;
+			const char *name;
+
+			name = function__name(fn, cu);
+			if (!name)
+				continue;
-			func = find_function(btfe, function__name(fn, cu));
+			func = find_function(btfe, name);
  			if (!func || func->generated)
  				continue;
  			func->generated = true;
 
Tested-by on v2 1/1
#3 lore raw
In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

When processing kernel image build by clang we can find some functions without the name, which causes pahole to segfault.

Adding extra checks to make sure we always have
function's name defined before using it.

Applied on top of latest pahole Git.

- Sedat -

Quoted 53 lines

---
v2 changes:
- reorg the code based on Andrii's suggestion

 btf_encoder.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
btf_encoder.c +11 -3
index 333973054b61..5557c9efd365 100644
@@ -68,10 +68,14 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
        struct elf_function *new;
        static GElf_Shdr sh;
        static int last_idx;
+       const char *name;
        int idx;
 
        if (elf_sym__type(sym) != STT_FUNC)
                return 0;
+       name = elf_sym__name(sym, btfe->symtab);
+       if (!name)
+               return 0;
 
        if (functions_cnt == functions_alloc) {
                functions_alloc = max(1000, functions_alloc * 3 / 2);
@@ -94,7 +98,7 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
                last_idx = idx;
        }
 
-       functions[functions_cnt].name = elf_sym__name(sym, btfe->symtab);
+       functions[functions_cnt].name = name;
        functions[functions_cnt].addr = elf_sym__value(sym);
        functions[functions_cnt].sh_addr = sh.sh_addr;
        functions[functions_cnt].generated = false;
@@ -731,8 +735,13 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
                        continue;
                if (functions_cnt) {
                        struct elf_function *func;
+                       const char *name;
+
+                       name = function__name(fn, cu);
+                       if (!name)
+                               continue;
 
-                       func = find_function(btfe, function__name(fn, cu));
+                       func = find_function(btfe, name);
                        if (!func || func->generated)
                                continue;
                        func->generated = true;
--

2.26.2