[llvm] e4344e1 - [DWARFYAML][debug_ranges] Emit an error message for invalid offset.
Xing GUO via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 9 03:49:45 PDT 2020
Author: Xing GUO
Date: 2020-06-09T18:53:38+08:00
New Revision: e4344e1466420e3f17b268916558249d5f1b8f49
URL: https://github.com/llvm/llvm-project/commit/e4344e1466420e3f17b268916558249d5f1b8f49
DIFF: https://github.com/llvm/llvm-project/commit/e4344e1466420e3f17b268916558249d5f1b8f49.diff
LOG: [DWARFYAML][debug_ranges] Emit an error message for invalid offset.
This patch helps make yaml2obj emit an error message when we try to assign an invalid offset to the entry of the 'debug_ranges' section.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D81357
Added:
Modified:
llvm/include/llvm/ObjectYAML/DWARFYAML.h
llvm/lib/ObjectYAML/DWARFEmitter.cpp
llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
Removed:
################################################################################
diff --git a/llvm/include/llvm/ObjectYAML/DWARFYAML.h b/llvm/include/llvm/ObjectYAML/DWARFYAML.h
index f270b09b1916..b829c7e32896 100644
--- a/llvm/include/llvm/ObjectYAML/DWARFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DWARFYAML.h
@@ -82,7 +82,7 @@ struct RangeEntry {
/// Class that describes a single range list inside the .debug_ranges section.
struct Ranges {
- llvm::yaml::Hex32 Offset;
+ llvm::yaml::Hex64 Offset;
llvm::yaml::Hex8 AddrSize;
std::vector<RangeEntry> Entries;
};
diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
index 32828adab1b6..d7c233964d62 100644
--- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp
@@ -17,6 +17,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/ObjectYAML/DWARFYAML.h"
+#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/LEB128.h"
@@ -127,11 +128,16 @@ Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
const size_t RangesOffset = OS.tell();
+ uint64_t EntryIndex = 0;
for (auto DebugRanges : DI.DebugRanges) {
const size_t CurrOffset = OS.tell() - RangesOffset;
- assert(DebugRanges.Offset >= CurrOffset &&
- "Offset should be greater than or equal to the bytes that we have "
- "written");
+ if ((uint64_t)DebugRanges.Offset < CurrOffset)
+ return createStringError(errc::invalid_argument,
+ "'Offset' for 'debug_ranges' with index " +
+ Twine(EntryIndex) +
+ " must be greater than or equal to the "
+ "number of bytes written already (0x" +
+ Twine::utohexstr(CurrOffset) + ")");
if (DebugRanges.Offset > CurrOffset)
ZeroFillBytes(OS, DebugRanges.Offset - CurrOffset);
for (auto Entry : DebugRanges.Entries) {
@@ -141,6 +147,7 @@ Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
DI.IsLittleEndian);
}
ZeroFillBytes(OS, DebugRanges.AddrSize * 2);
+ ++EntryIndex;
}
return Error::success();
diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
index 48fdc6cd7147..8ec17fd8b597 100644
--- a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
@@ -313,3 +313,29 @@ FileHeader:
Type: ET_EXEC
Machine: EM_X86_64
DWARF:
+
+## i) Test that yaml2obj emits an error message if we try to assign an invalid offset to an
+## entry of the '.debug_ranges' section.
+
+# RUN: not yaml2obj --docnum=9 %s -o %t9.o 2>&1 | FileCheck %s --check-prefix=INVALID-OFFSET
+
+# INVALID-OFFSET: yaml2obj: error: 'Offset' for 'debug_ranges' with index 1 must be greater than or equal to the number of bytes written already (0x20)
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+DWARF:
+ debug_ranges:
+ - Offset: 0x00
+ AddrSize: 0x08
+ Entries:
+ - LowOffset: 0x01
+ HighOffset: 0x02
+ - Offset: 0x1F ## Must be greater than or equal to 0x20.
+ AddrSize: 0x08
+ Entries:
+ - LowOffset: 0x01
+ HighOffset: 0x02
More information about the llvm-commits
mailing list