[PATCH] D81702: [WASM] Avoid passing uninit section indices when not creating code or data sections

Gui Andrade via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 11 17:38:49 PDT 2020


guiand created this revision.
guiand added a reviewer: sbc100.
Herald added subscribers: llvm-commits, sunfish, aheejin, hiraditya.
Herald added a project: LLVM.

Wraps calls to `writeRelocSection` in if statements depending on the success of writing the corresponding code or data section. Otherwise, section indices may be passed uninitialized by value if writing the section fails.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81702

Files:
  llvm/lib/MC/WasmObjectWriter.cpp


Index: llvm/lib/MC/WasmObjectWriter.cpp
===================================================================
--- llvm/lib/MC/WasmObjectWriter.cpp
+++ llvm/lib/MC/WasmObjectWriter.cpp
@@ -218,11 +218,11 @@
 
   // Relocations for fixing up references in the code section.
   std::vector<WasmRelocationEntry> CodeRelocations;
-  uint32_t CodeSectionIndex;
+  Optional<uint32_t> CodeSectionIndex;
 
   // Relocations for fixing up references in the data section.
   std::vector<WasmRelocationEntry> DataRelocations;
-  uint32_t DataSectionIndex;
+  Optional<uint32_t> DataSectionIndex;
 
   // Index values to use for fixing up call_indirect type indices.
   // Maps function symbols to the index of the type of the function
@@ -329,9 +329,9 @@
   void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
   void writeElemSection(ArrayRef<uint32_t> TableElems);
   void writeDataCountSection();
-  void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
-                        ArrayRef<WasmFunction> Functions);
-  void writeDataSection();
+  int writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
+                       ArrayRef<WasmFunction> Functions);
+  int writeDataSection();
   void writeEventSection(ArrayRef<wasm::WasmEventType> Events);
   void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals);
   void writeRelocSection(uint32_t SectionIndex, StringRef Name,
@@ -876,11 +876,11 @@
   endSection(Section);
 }
 
-void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
-                                        const MCAsmLayout &Layout,
-                                        ArrayRef<WasmFunction> Functions) {
+int WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
+                                       const MCAsmLayout &Layout,
+                                       ArrayRef<WasmFunction> Functions) {
   if (Functions.empty())
-    return;
+    return -1;
 
   SectionBookkeeping Section;
   startSection(Section, wasm::WASM_SEC_CODE);
@@ -904,11 +904,12 @@
   applyRelocations(CodeRelocations, Section.ContentsOffset);
 
   endSection(Section);
+  return 0;
 }
 
-void WasmObjectWriter::writeDataSection() {
+int WasmObjectWriter::writeDataSection() {
   if (DataSegments.empty())
-    return;
+    return -1;
 
   SectionBookkeeping Section;
   startSection(Section, wasm::WASM_SEC_DATA);
@@ -934,6 +935,7 @@
   applyRelocations(DataRelocations, Section.ContentsOffset);
 
   endSection(Section);
+  return 0;
 }
 
 void WasmObjectWriter::writeRelocSection(
@@ -1661,13 +1663,15 @@
   writeExportSection(Exports);
   writeElemSection(TableElems);
   writeDataCountSection();
-  writeCodeSection(Asm, Layout, Functions);
-  writeDataSection();
+  int code_ret = writeCodeSection(Asm, Layout, Functions);
+  int data_ret = writeDataSection();
   for (auto &CustomSection : CustomSections)
     writeCustomSection(CustomSection, Asm, Layout);
   writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
-  writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
-  writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
+  if (!code_ret)
+    writeRelocSection(*CodeSectionIndex, "CODE", CodeRelocations);
+  if (!data_ret)
+    writeRelocSection(*DataSectionIndex, "DATA", DataRelocations);
   writeCustomRelocSections();
   if (ProducersSection)
     writeCustomSection(*ProducersSection, Asm, Layout);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81702.270266.patch
Type: text/x-patch
Size: 3403 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200612/e2eb9c02/attachment.bin>


More information about the llvm-commits mailing list