[Bug,libelf/28101] elf_strptr slow with address sanitizer, passes entire section range to memrchr.

Message ID bug-28101-10460-NydYNmfPGz@http.sourceware.org/bugzilla/
State Committed
Headers
Series [Bug,libelf/28101] elf_strptr slow with address sanitizer, passes entire section range to memrchr. |

Commit Message

fche at redhat dot com July 19, 2021, 8:10 a.m. UTC
  https://sourceware.org/bugzilla/show_bug.cgi?id=28101

Mark Wielaard <mark at klomp dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mark at klomp dot org

--- Comment #1 from Mark Wielaard <mark at klomp dot org> ---
I think it really is a bug/performance issue in asan. But "optimizing" it in
libelf by first checking the last char is zero, before calling memrchr wouldn't
hurt (and should normally prevent a function call). Does the following help?

   do {
     if (to <= from)
  

Patch

diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index 76f2caf1..dc9b76c0 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -56,7 +56,9 @@  get_zdata (Elf_Scn *strscn)
 static bool validate_str (const char *str, size_t from, size_t to)
 {
 #if HAVE_DECL_MEMRCHR
-  return memrchr (&str[from], '\0', to - from) != NULL;
+  // Check end first, which is likely a zero terminator, to prevent function
call
+  return (str[to - 1]  == '\0'
+         || (to - from > 0 && memrchr (&str[from], '\0', to - from - 1) !=
NULL));
 #else