[lld] r312705 - [ELF] Prevent crash with binary inputs with non-ascii file names

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 7 01:30:09 PDT 2017


Author: jhenderson
Date: Thu Sep  7 01:30:09 2017
New Revision: 312705

URL: http://llvm.org/viewvc/llvm-project?rev=312705&view=rev
Log:
[ELF] Prevent crash with binary inputs with non-ascii file names

If using --format=binary with an input file name that has one or more non-ascii
characters in, LLD has undefined behaviour (it crashes on my Windows Debug build)
when calling isalnum with these non-ascii characters. Instead, of calling
std::isalnum, this patch uses an internal version that ignores the locale and
checks a specific subset of characters.

Reviewers: ruiu

Differential Revision: https://reviews.llvm.org/D37331

Added:
    lld/trunk/test/ELF/format-binary-non-ascii.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/Strings.cpp
    lld/trunk/ELF/Strings.h

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=312705&r1=312704&r2=312705&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Thu Sep  7 01:30:09 2017
@@ -931,7 +931,7 @@ template <class ELFT> void BinaryFile::p
   // characters in a filename are replaced with underscore.
   std::string S = "_binary_" + MB.getBufferIdentifier().str();
   for (size_t I = 0; I < S.size(); ++I)
-    if (!isalnum(S[I]))
+    if (!elf::isAlnum(S[I]))
       S[I] = '_';
 
   Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,

Modified: lld/trunk/ELF/Strings.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.cpp?rev=312705&r1=312704&r2=312705&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.cpp (original)
+++ lld/trunk/ELF/Strings.cpp Thu Sep  7 01:30:09 2017
@@ -58,7 +58,9 @@ static bool isAlpha(char C) {
   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
 }
 
-static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
+// Returns true if C is a valid letter, digit or underscore as defined in the
+// "C" locale.
+bool elf::isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
 
 // Returns true if S is valid as a C language identifier.
 bool elf::isValidCIdentifier(StringRef S) {

Modified: lld/trunk/ELF/Strings.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Strings.h?rev=312705&r1=312704&r2=312705&view=diff
==============================================================================
--- lld/trunk/ELF/Strings.h (original)
+++ lld/trunk/ELF/Strings.h Thu Sep  7 01:30:09 2017
@@ -22,6 +22,7 @@ namespace lld {
 namespace elf {
 
 std::vector<uint8_t> parseHex(StringRef S);
+bool isAlnum(char C);
 bool isValidCIdentifier(StringRef S);
 
 // This is a lazy version of StringRef. String size is computed lazily

Added: lld/trunk/test/ELF/format-binary-non-ascii.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/format-binary-non-ascii.s?rev=312705&view=auto
==============================================================================
--- lld/trunk/test/ELF/format-binary-non-ascii.s (added)
+++ lld/trunk/test/ELF/format-binary-non-ascii.s Thu Sep  7 01:30:09 2017
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t£.o
+
+# RUN: ld.lld -o %t.elf %t£.o --format=binary %t£.o
+# RUN: llvm-readobj -symbols %t.elf | FileCheck %s
+
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_start
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_end
+# CHECK: Name: _binary_{{[a-zA-Z0-9_]+}}test_ELF_Output_format_binary_non_ascii_s_tmp___o_size
+
+.text
+.align 4
+.globl _start
+_start:
+    nop




More information about the llvm-commits mailing list