<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 2, 2015 at 8:03 PM, Justin Bogner <span dir="ltr"><<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">Alexey Samsonov <<a href="mailto:vonosmas@gmail.com">vonosmas@gmail.com</a>> writes:<br>
> Author: samsonov<br>
> Date: Thu May 28 15:25:42 2015<br>
> New Revision: 238481<br>
><br>
> URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D238481-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=EsvPczQvyzihBozBApcNLhVWZb72CQ5_GDWZuVxD0qk&s=CI7HLC1JiJ4s3jhuwgMmogqr18mT5a9Jv_Nhw25v4iA&e=" target="_blank">http://llvm.org/viewvc/llvm-project?rev=238481&view=rev</a><br>
> Log:<br>
> Object, ELF: Use error code instead of calling report_fatal_error()<br>
><br>
> Make createELFObjectFile() return object_error::parse_failed on<br>
> encountering invalid ELF file, instead of crashing the program.<br>
><br>
> Added:<br>
> llvm/trunk/test/DebugInfo/Inputs/invalid.elf<br>
> llvm/trunk/test/DebugInfo/dwarfdump-invalid.test<br>
> Modified:<br>
> llvm/trunk/include/llvm/Object/ELF.h<br>
><br>
> Modified: llvm/trunk/include/llvm/Object/ELF.h<br>
> URL:<br>
> <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_include_llvm_Object_ELF.h-3Frev-3D238481-26r1-3D238480-26r2-3D238481-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=EsvPczQvyzihBozBApcNLhVWZb72CQ5_GDWZuVxD0qk&s=SCIYelEAAIZrsKH6U4EvBnZfaDsHj0-o_hD5XGxlh0M&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=238481&r1=238480&r2=238481&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/include/llvm/Object/ELF.h (original)<br>
> +++ llvm/trunk/include/llvm/Object/ELF.h Thu May 28 15:25:42 2015<br>
> @@ -318,7 +318,7 @@ public:<br>
> std::pair<const Elf_Shdr *, const Elf_Sym *><br>
> getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;<br>
><br>
> - ELFFile(StringRef Object, std::error_code &ec);<br>
> + ELFFile(StringRef Object, std::error_code &EC);<br>
><br>
> bool isMipsELF64() const {<br>
> return Header->e_machine == ELF::EM_MIPS &&<br>
> @@ -622,7 +622,7 @@ typename ELFFile<ELFT>::uintX_t ELFFile<<br>
> }<br>
><br>
> template <class ELFT><br>
> -ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &ec)<br>
> +ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)<br>
> : Buf(Object), SectionHeaderTable(nullptr), dot_shstrtab_sec(nullptr),<br>
> dot_strtab_sec(nullptr), dot_symtab_sec(nullptr),<br>
> SymbolTableSectionHeaderIndex(nullptr), dot_gnu_version_sec(nullptr),<br>
> @@ -630,9 +630,11 @@ ELFFile<ELFT>::ELFFile(StringRef Object,<br>
> dt_soname(nullptr) {<br>
> const uint64_t FileSize = Buf.size();<br>
><br>
> - if (sizeof(Elf_Ehdr) > FileSize)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("File too short!");<br>
> + if (sizeof(Elf_Ehdr) > FileSize) {<br>
> + // File too short!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
><br>
> Header = reinterpret_cast<const Elf_Ehdr *>(base());<br>
><br>
> @@ -641,40 +643,50 @@ ELFFile<ELFT>::ELFFile(StringRef Object,<br>
><br>
> const uint64_t SectionTableOffset = Header->e_shoff;<br>
><br>
> - if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("Section header table goes past end of file!");<br>
> + if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) {<br>
> + // Section header table goes past end of file!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
><br>
> // The getNumSections() call below depends on SectionHeaderTable being set.<br>
> SectionHeaderTable =<br>
> reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);<br>
> const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;<br>
><br>
> - if (SectionTableOffset + SectionTableSize > FileSize)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("Section table goes past end of file!");<br>
> + if (SectionTableOffset + SectionTableSize > FileSize) {<br>
> + // Section table goes past end of file!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
><br>
> // Scan sections for special sections.<br>
><br>
> for (const Elf_Shdr &Sec : sections()) {<br>
> switch (Sec.sh_type) {<br>
> case ELF::SHT_SYMTAB_SHNDX:<br>
> - if (SymbolTableSectionHeaderIndex)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .symtab_shndx!");<br>
> + if (SymbolTableSectionHeaderIndex) {<br>
> + // More than one .symtab_shndx!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> SymbolTableSectionHeaderIndex = &Sec;<br>
> break;<br>
> case ELF::SHT_SYMTAB:<br>
> - if (dot_symtab_sec)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .symtab!");<br>
> + if (dot_symtab_sec) {<br>
> + // More than one .symtab!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> dot_symtab_sec = &Sec;<br>
> dot_strtab_sec = getSection(Sec.sh_link);<br>
> break;<br>
> case ELF::SHT_DYNSYM: {<br>
> - if (DynSymRegion.Addr)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .dynsym!");<br>
> + if (DynSymRegion.Addr) {<br>
> + // More than one .dynsym!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> DynSymRegion.Addr = base() + Sec.sh_offset;<br>
> DynSymRegion.Size = Sec.sh_size;<br>
> DynSymRegion.EntSize = Sec.sh_entsize;<br>
> @@ -685,29 +697,37 @@ ELFFile<ELFT>::ELFFile(StringRef Object,<br>
> break;<br>
> }<br>
> case ELF::SHT_DYNAMIC:<br>
> - if (DynamicRegion.Addr)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .dynamic!");<br>
> + if (DynamicRegion.Addr) {<br>
> + // More than one .dynamic!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> DynamicRegion.Addr = base() + Sec.sh_offset;<br>
> DynamicRegion.Size = Sec.sh_size;<br>
> DynamicRegion.EntSize = Sec.sh_entsize;<br>
> break;<br>
> case ELF::SHT_GNU_versym:<br>
> - if (dot_gnu_version_sec != nullptr)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .gnu.version section!");<br>
> + if (dot_gnu_version_sec != nullptr) {<br>
> + // More than one .gnu.version section!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> dot_gnu_version_sec = &Sec;<br>
> break;<br>
> case ELF::SHT_GNU_verdef:<br>
> - if (dot_gnu_version_d_sec != nullptr)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .gnu.version_d section!");<br>
> + if (dot_gnu_version_d_sec != nullptr) {<br>
> + // More than one .gnu.version_d section!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> dot_gnu_version_d_sec = &Sec;<br>
> break;<br>
> case ELF::SHT_GNU_verneed:<br>
> - if (dot_gnu_version_r_sec != nullptr)<br>
> - // FIXME: Proper error handling.<br>
> - report_fatal_error("More than one .gnu.version_r section!");<br>
> + if (dot_gnu_version_r_sec != nullptr) {<br>
> + // More than one .gnu.version_r section!<br>
> + EC = object_error::parse_failed;<br>
> + return;<br>
> + }<br>
> dot_gnu_version_r_sec = &Sec;<br>
> break;<br>
> }<br>
> @@ -744,7 +764,7 @@ ELFFile<ELFT>::ELFFile(StringRef Object,<br>
> }<br>
> }<br>
><br>
> - ec = std::error_code();<br>
> + EC = std::error_code();<br>
> }<br>
><br>
> // Get the symbol table index in the symtab section given a symbol<br>
><br>
> Added: llvm/trunk/test/DebugInfo/Inputs/invalid.elf<br>
> URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_DebugInfo_Inputs_invalid.elf-3Frev-3D238481-26view-3Dauto&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=EsvPczQvyzihBozBApcNLhVWZb72CQ5_GDWZuVxD0qk&s=8trXeL81XQsVAUVLjcMilBngCXTje_BVSMYahYBvlpk&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DebugInfo/Inputs/invalid.elf?rev=238481&view=auto</a><br>
> ==============================================================================<br>
> Binary files llvm/trunk/test/DebugInfo/Inputs/invalid.elf (added) and llvm/trunk/test/DebugInfo/Inputs/invalid.elf Thu May 28 15:25:42 2015 differ<br>
><br>
> Added: llvm/trunk/test/DebugInfo/dwarfdump-invalid.test<br>
> URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_DebugInfo_dwarfdump-2Dinvalid.test-3Frev-3D238481-26view-3Dauto&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=EsvPczQvyzihBozBApcNLhVWZb72CQ5_GDWZuVxD0qk&s=NicK24hlWdyz1P0KjUO1eXlLtYbXYAvSAU6YaUidX_o&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DebugInfo/dwarfdump-invalid.test?rev=238481&view=auto</a><br>
> ==============================================================================<br>
> --- llvm/trunk/test/DebugInfo/dwarfdump-invalid.test (added)<br>
> +++ llvm/trunk/test/DebugInfo/dwarfdump-invalid.test Thu May 28 15:25:42 2015<br>
> @@ -0,0 +1,4 @@<br>
> +; Verify that llvm-dwarfdump doesn't crash on broken input files.<br>
> +<br>
> +RUN: llvm-dwarfdump %p/Inputs/invalid.elf<br>
> +<br>
<br>
</div></div>"Don't crash" tests are best avoided. Presumably this prints an error<br>
now? Can we test for that?<br></blockquote><div><br></div><div>Right, thanks for suggestion! Done in r238985.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
><br>
><br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div></div>