Verify we have a correct ELF or Mach core file before we return a valid instace of...
authorGreg Clayton <gclayton@apple.com>
Fri, 7 Mar 2014 19:24:39 +0000 (19:24 +0000)
committerGreg Clayton <gclayton@apple.com>
Fri, 7 Mar 2014 19:24:39 +0000 (19:24 +0000)
llvm-svn: 203274

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp

index f6b0e3a..5a0ba40 100644 (file)
@@ -162,14 +162,13 @@ public:
     
     virtual uint32_t
     GetSDKVersion (uint32_t *versions, uint32_t num_versions);
-
-protected:
-
+    
     static bool
     ParseHeader (lldb_private::DataExtractor &data,
                  lldb::offset_t *data_offset_ptr,
                  llvm::MachO::mach_header &header);
     
+protected:
     
     static bool
     GetUUID (const llvm::MachO::mach_header &header,
index 7ea5c89..0b2ca24 100644 (file)
@@ -19,6 +19,9 @@
 #include "lldb/Core/DataBufferHeap.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Target/DynamicLoader.h"
+
+#include "llvm/Support/ELF.h"
+
 #include "ProcessPOSIXLog.h"
 
 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
@@ -54,8 +57,24 @@ lldb::ProcessSP
 ProcessElfCore::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file)
 {
     lldb::ProcessSP process_sp;
-    if (crash_file) 
-        process_sp.reset(new ProcessElfCore (target, listener, *crash_file));
+    if (crash_file)
+    {
+        // Read enough data for a ELF32 header or ELF64 header
+        const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
+        
+        lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size));
+        if (data_sp->GetByteSize() == header_size)
+        {
+            elf::ELFHeader elf_header;
+            DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
+            lldb::offset_t data_offset = 0;
+            if (elf_header.Parse(data, &data_offset))
+            {
+                if (elf_header.e_type == llvm::ELF::ET_CORE)
+                    process_sp.reset(new ProcessElfCore (target, listener, *crash_file));
+            }
+        }
+    }
     return process_sp;
 }
 
index 3992220..589f1e9 100644 (file)
@@ -35,6 +35,7 @@
 // Needed for the plug-in names for the dynamic loaders.
 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
+#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -64,7 +65,23 @@ ProcessMachCore::CreateInstance (Target &target, Listener &listener, const FileS
 {
     lldb::ProcessSP process_sp;
     if (crash_file)
-        process_sp.reset(new ProcessMachCore (target, listener, *crash_file));
+    {
+        const size_t header_size = sizeof(llvm::MachO::mach_header);
+        lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size));
+        if (data_sp->GetByteSize() == header_size)
+        {
+            DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
+            
+            lldb::offset_t data_offset = 0;
+            llvm::MachO::mach_header mach_header;
+            if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header))
+            {
+                if (mach_header.filetype == llvm::MachO::MH_CORE)
+                    process_sp.reset(new ProcessMachCore (target, listener, *crash_file));
+            }
+        }
+        
+    }
     return process_sp;
 }