From ec0d74690d45d0bc8c4be2d914755b4ce8c487d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20K=C3=B6plinger?= Date: Wed, 18 Jan 2017 16:48:07 +0100 Subject: [PATCH] [pdb2mdb] Detect portable PDB and fail gracefully instead of crashing (#4261) Before when trying to convert an assembly that had a portable PDB instead of a native PDB alongside it we'd just crash in the Microsoft.Cci pdb reader. We now check whether the symbol file is a portable PDB and exit gracefully with an error message and exit code 2 from the app. Users which access the Converter class (like e.g. Xamarin Android [1]) directly can now catch the PortablePdbNotSupportedException to do custom logic. [1] https://github.com/xamarin/xamarin-android/blob/15f7547fa673e02256a75910c60c5e13eb158d2b/src/Xamarin.Android.Build.Tasks/Tasks/ConvertDebuggingFiles.cs#L28 --- mcs/tools/pdb2mdb/Driver.cs | 49 +++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/mcs/tools/pdb2mdb/Driver.cs b/mcs/tools/pdb2mdb/Driver.cs index 93d9e8a4544..5aa1fcf0b5b 100644 --- a/mcs/tools/pdb2mdb/Driver.cs +++ b/mcs/tools/pdb2mdb/Driver.cs @@ -33,12 +33,31 @@ namespace Pdb2Mdb { var pdb = asm.Name.Name + ".pdb"; pdb = Path.Combine (Path.GetDirectoryName (filename), pdb); + if (!File.Exists (pdb)) + throw new FileNotFoundException ("PDB file doesn't exist: " + pdb); + using (var stream = File.OpenRead (pdb)) { + if (IsPortablePdb (stream)) + throw new PortablePdbNotSupportedException (); + var funcs = PdbFile.LoadFunctions (stream, true); Converter.Convert (asm, funcs, new MonoSymbolWriter (filename)); } } + static bool IsPortablePdb (FileStream stream) + { + const uint ppdb_signature = 0x424a5342; + + var position = stream.Position; + try { + var reader = new BinaryReader (stream); + return reader.ReadUInt32 () == ppdb_signature; + } finally { + stream.Position = position; + } + } + internal Converter (MonoSymbolWriter mdb) { this.mdb = mdb; @@ -158,6 +177,9 @@ namespace Pdb2Mdb { } } + public class PortablePdbNotSupportedException : Exception { + } + class Driver { static void Main (string [] args) @@ -170,25 +192,16 @@ namespace Pdb2Mdb { if (!File.Exists (asm)) Usage (); - var assembly = AssemblyDefinition.ReadAssembly (asm); - - var pdb = assembly.Name.Name + ".pdb"; - pdb = Path.Combine (Path.GetDirectoryName (asm), pdb); - - if (!File.Exists (pdb)) + try { + Converter.Convert (asm); + } catch (FileNotFoundException ex) { Usage (); - - using (var stream = File.OpenRead (pdb)) { - Convert (assembly, stream, new MonoSymbolWriter (asm)); + } catch (PortablePdbNotSupportedException) { + Console.WriteLine ("Error: A portable PDB can't be converted to mdb."); + Environment.Exit (2); } - } - - static void Convert (AssemblyDefinition assembly, Stream pdb, MonoSymbolWriter mdb) - { - try { - Converter.Convert (assembly, PdbFile.LoadFunctions (pdb, true), mdb); - } catch (Exception e) { - Error (e); + catch (Exception ex) { + Error (ex); } } @@ -204,6 +217,8 @@ namespace Pdb2Mdb { { Console.WriteLine ("Fatal error:"); Console.WriteLine (e); + + Environment.Exit (1); } } } -- 2.11.4.GIT