public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: David Faust <david.faust@oracle.com>
To: binutils@sourceware.org
Cc: jose.marchesi@oracle.com
Subject: [PATCH 2/2] bpf: remove symbols created during failed parse
Date: Tue, 14 Nov 2023 09:58:05 -0800	[thread overview]
Message-ID: <20231114175805.7783-3-david.faust@oracle.com> (raw)
In-Reply-To: <20231114175805.7783-1-david.faust@oracle.com>

Parsing the BPF pseudo-c asm syntax requires attempting to parse an
instruction using a template that may later be determined to not match.
During this parsing, a call to expression () may end up creating one or
more symbols.  If the parsed instruction is later determined to not
match the template, then any symbols created during this process should
be discarded.

If such unused symbols are not discarded, they impede the loading of the
resulting BPF object by the Linux kernel.

gas/

	* config/tc-bpf.c (last_parsed_expr, old_symbol_lastP): New.
	(parse_expression): Track last_parsed_expr and old_symbol_lastP.
	(parse_error): Cleanup symbols created during a failed parse.
	* testsuite/gas/bpf/asm-extra-sym-1.d: New.
	* testsuite/gas/bpf/asm-extra-sym-1.s: New.
	* testsuite/gas/bpf/asm-extra-sym-2.d: New.
	* testsuite/gas/bpf/asm-extra-sym-2.s: New.
	* testsuite/gas/bpf/bpf.exp: Run new tests.
---
 gas/config/tc-bpf.c                     | 30 +++++++++++++++++++++++++
 gas/testsuite/gas/bpf/asm-extra-sym-1.d |  7 ++++++
 gas/testsuite/gas/bpf/asm-extra-sym-1.s |  1 +
 gas/testsuite/gas/bpf/asm-extra-sym-2.d |  7 ++++++
 gas/testsuite/gas/bpf/asm-extra-sym-2.s |  8 +++++++
 gas/testsuite/gas/bpf/bpf.exp           |  4 ++++
 6 files changed, 57 insertions(+)
 create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-1.d
 create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-1.s
 create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-2.d
 create mode 100644 gas/testsuite/gas/bpf/asm-extra-sym-2.s

diff --git a/gas/config/tc-bpf.c b/gas/config/tc-bpf.c
index fd4144a354b..d64576415e1 100644
--- a/gas/config/tc-bpf.c
+++ b/gas/config/tc-bpf.c
@@ -1223,6 +1223,8 @@ add_relaxed_insn (struct bpf_insn *insn, expressionS *exp)
    See md_operand below to see how exp_parse_failed is used.  */
 
 static int exp_parse_failed = 0;
+static expressionS *last_parsed_expr = NULL;
+static symbolS *old_symbol_lastP = NULL;
 
 static char *
 parse_expression (char *s, expressionS *exp)
@@ -1232,10 +1234,13 @@ parse_expression (char *s, expressionS *exp)
 
   exp_parse_failed = 0;
   input_line_pointer = s;
+  old_symbol_lastP = symbol_lastP;
   expression (exp);
   s = input_line_pointer;
   input_line_pointer = saved_input_line_pointer;
 
+  last_parsed_expr = exp;
+
   switch (exp->X_op == O_absent || exp_parse_failed)
     return NULL;
 
@@ -1317,6 +1322,25 @@ parse_error (int length, const char *fmt, ...)
       va_end (args);
       partial_match_length = length;
     }
+
+  /* Cleanup any symbols created during the failed parsing.  */
+  if (last_parsed_expr
+      && (last_parsed_expr->X_add_symbol || last_parsed_expr->X_op_symbol))
+    {
+      /* NOTE: this logic exploits the implementation detail that a symbol
+	 created by expression () during parsing is appended to the list
+	 rather than potentially being inserted somewhere in the middle.  */
+      symbolS *sym = symbol_lastP;
+      while (sym != old_symbol_lastP)
+	{
+	  /* Must have created at least one symbol.  */
+	  symbol_remove (sym, &symbol_rootP, &symbol_lastP);
+	  symbol_table_remove (sym);
+	  sym = symbol_lastP;
+	}
+
+      old_symbol_lastP = symbol_lastP;
+    }
 }
 
 /* Assemble a machine instruction in STR and emit the frags/bytes it
@@ -1368,6 +1392,12 @@ md_assemble (char *str ATTRIBUTE_UNUSED)
       if (opcode->version > isa_spec)
         continue;
 
+      /* Track expression parsed while trying this opcode.  If this turns
+	 out to be the wrong opcode, we need to undo side effects of the
+	 expression parsing, such as creating a new undefined symbol.
+	 Set by parse_expression () and used by parse_error ().  */
+      last_parsed_expr = NULL;
+
       memset (&insn, 0, sizeof (struct bpf_insn));
       insn.size = 8;
       for (s = str, p = template; *p != '\0';)
diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-1.d b/gas/testsuite/gas/bpf/asm-extra-sym-1.d
new file mode 100644
index 00000000000..56bdb7082f5
--- /dev/null
+++ b/gas/testsuite/gas/bpf/asm-extra-sym-1.d
@@ -0,0 +1,7 @@
+#as: -EL -mdialect=pseudoc
+#nm: --numeric-sort
+#source: asm-extra-sym-1.s
+#name: BPF pseudoc no extra symbols 1
+
+# Note: there should be no output from nm.
+# Previously a bug created an UND '*' symbol.
diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-1.s b/gas/testsuite/gas/bpf/asm-extra-sym-1.s
new file mode 100644
index 00000000000..2cfa605a259
--- /dev/null
+++ b/gas/testsuite/gas/bpf/asm-extra-sym-1.s
@@ -0,0 +1 @@
+    r2 = *(u32*)(r1 + 8)
diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-2.d b/gas/testsuite/gas/bpf/asm-extra-sym-2.d
new file mode 100644
index 00000000000..e17ae0f2422
--- /dev/null
+++ b/gas/testsuite/gas/bpf/asm-extra-sym-2.d
@@ -0,0 +1,7 @@
+#as: -EL -mdialect=pseudoc
+#nm: --numeric-sort
+#source: asm-extra-sym-2.s
+#name: BPF pseudoc no extra symbols 2
+
+[0-9a-f]+ t main
+[0-9a-f]+ t foo
diff --git a/gas/testsuite/gas/bpf/asm-extra-sym-2.s b/gas/testsuite/gas/bpf/asm-extra-sym-2.s
new file mode 100644
index 00000000000..ccbf43065d9
--- /dev/null
+++ b/gas/testsuite/gas/bpf/asm-extra-sym-2.s
@@ -0,0 +1,8 @@
+
+    .text
+main:
+    call foo
+    call foo
+foo:
+    r1 = 1
+    exit
diff --git a/gas/testsuite/gas/bpf/bpf.exp b/gas/testsuite/gas/bpf/bpf.exp
index 80f5a1dbc2d..680b8dbdb10 100644
--- a/gas/testsuite/gas/bpf/bpf.exp
+++ b/gas/testsuite/gas/bpf/bpf.exp
@@ -72,4 +72,8 @@ if {[istarget bpf*-*-*]} {
     run_dump_test disp16-overflow-relax
     run_dump_test disp32-overflow
     run_dump_test imm32-overflow
+
+    # Test that parser does not create undefined symbols
+    run_dump_test asm-extra-sym-1
+    run_dump_test asm-extra-sym-2
 }
-- 
2.42.0


  parent reply	other threads:[~2023-11-14 17:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-14 17:58 [PATCH 0/2] gas,bpf: cleanup bad symbols created while parsing David Faust
2023-11-14 17:58 ` [PATCH 1/2] gas: add symbol_table_remove David Faust
2023-11-14 17:58 ` David Faust [this message]
2023-11-14 22:13 ` [PATCH 0/2] gas,bpf: cleanup bad symbols created while parsing David Faust
2023-11-15  9:49 ` Jan Beulich
2023-11-15 21:57   ` David Faust

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231114175805.7783-3-david.faust@oracle.com \
    --to=david.faust@oracle.com \
    --cc=binutils@sourceware.org \
    --cc=jose.marchesi@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).