Beispiel #1
0
        public void EmbeddedPortablePdb()
        {
            var b = new DebugDirectoryBuilder();

            var pdb = new BlobBuilder();

            pdb.WriteInt64(0x1122334455667788);

            b.AddEmbeddedPortablePdbEntry(pdb, portablePdbVersion: 0x0100);

            var blob = new BlobBuilder();

            b.Serialize(blob, new SectionLocation(0, 0), sectionOffset: 0);
            var bytes = blob.ToImmutableArray();

            AssertEx.Equal(new byte[]
            {
                0x00, 0x00, 0x00, 0x00,                                    // Characteristics
                0x00, 0x00, 0x00, 0x00,                                    // Stamp
                0x00, 0x01, 0x00, 0x01,                                    // Version
                0x11, 0x00, 0x00, 0x00,                                    // Type
                0x12, 0x00, 0x00, 0x00,                                    // SizeOfData
                0x1C, 0x00, 0x00, 0x00,                                    // AddressOfRawData
                0x1C, 0x00, 0x00, 0x00,                                    // PointerToRawData

                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0x08, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            }, bytes);

            using (var pinned = new PinnedBlob(bytes))
            {
                var actual = PEReader.ReadDebugDirectoryEntries(pinned.CreateReader(0, DebugDirectoryEntry.Size));
                Assert.Equal(1, actual.Length);
                Assert.Equal(0u, actual[0].Stamp);
                Assert.Equal(0x0100, actual[0].MajorVersion);
                Assert.Equal(0x0100, actual[0].MinorVersion);
                Assert.Equal(DebugDirectoryEntryType.EmbeddedPortablePdb, actual[0].Type);
                Assert.False(actual[0].IsPortableCodeView);
                Assert.Equal(0x00000012, actual[0].DataSize);
                Assert.Equal(0x0000001c, actual[0].DataRelativeVirtualAddress);
                Assert.Equal(0x0000001c, actual[0].DataPointer);

                var provider = new ByteArrayMemoryProvider(bytes);
                using (var block = provider.GetMemoryBlock(actual[0].DataPointer, actual[0].DataSize))
                {
                    var decoded = PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block);
                    AssertEx.Equal(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, decoded);
                }
            }
        }
Beispiel #2
0
        public void EmbeddedPortablePdb_Errors()
        {
            var bytes1 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0xFF, 0xFF, 0xFF, 0xFF,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes1).GetMemoryBlock(0, bytes1.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes2 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0x09, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes2).GetMemoryBlock(0, bytes2.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes3 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0x00, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes3).GetMemoryBlock(0, bytes3.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes4 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0xff, 0xff, 0xff, 0x7f,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes4).GetMemoryBlock(0, bytes4.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes5 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0x08, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEF, 0xFF, 0x4F, 0xFF, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes4).GetMemoryBlock(0, bytes4.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes6 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x43,                                    // signature
                0x08, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            });

            using (var block = new ByteArrayMemoryProvider(bytes6).GetMemoryBlock(0, bytes6.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes7 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x43, // signature
                0x08, 0x00, 0x00,
            });

            using (var block = new ByteArrayMemoryProvider(bytes7).GetMemoryBlock(0, bytes7.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes8 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x43, // signature
                0x08, 0x00, 0x00,
            });

            using (var block = new ByteArrayMemoryProvider(bytes8).GetMemoryBlock(0, bytes8.Length))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }

            var bytes9 = ImmutableArray.Create(new byte[]
            {
                0x4D, 0x50, 0x44, 0x43, // signature
                0x08, 0x00, 0x00, 0x00
            });

            using (var block = new ByteArrayMemoryProvider(bytes9).GetMemoryBlock(0, 1))
            {
                Assert.Throws <BadImageFormatException>(() => PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block));
            }
        }