Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix opcode handling when reading DWARF line number programs #482

Merged
merged 4 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/BinaryParsers/ElfBinary/Dwarf/DwarfEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ public enum DwarfAttribute
/// </summary>
public enum DwarfLineNumberStandardOpcode
{
// Extended opcodes (0)
eddynaka marked this conversation as resolved.
Show resolved Hide resolved
// Standard opcodes (1 to 12)
// Special opcodes (13 or greater)
Extended = 0,
Copy = 0x01,
AdvancePc = 0x02,
Expand Down
19 changes: 12 additions & 7 deletions src/BinaryParsers/ElfBinary/Dwarf/DwarfLineNumberProgram.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -193,6 +192,11 @@ private static List<DwarfFileInformation> ReadData(DwarfMemoryReader debugLine,
byte lineRange = debugLine.ReadByte();
byte operationCodeBase = debugLine.ReadByte();

if (operationCodeBase <= 0)
{
return new List<DwarfFileInformation>();
}

// Read operation code lengths
uint[] operationCodeLengths = new uint[operationCodeBase];

Expand Down Expand Up @@ -312,7 +316,11 @@ private static List<DwarfFileInformation> ReadData(DwarfMemoryReader debugLine,
break;

case DwarfLineNumberStandardOpcode.SetFile:
state.File = files[(int)debugLine.LEB128() - 1];
int index = (int)debugLine.LEB128() - 1;
if (index >= 0 && index < files.Count)
{
state.File = files[index];
}
break;

case DwarfLineNumberStandardOpcode.SetColumn:
Expand Down Expand Up @@ -348,12 +356,9 @@ private static List<DwarfFileInformation> ReadData(DwarfMemoryReader debugLine,
state.Isa = debugLine.LEB128();
break;

case (DwarfLineNumberStandardOpcode)13:
// when reading some dwarf debugline data, get OpCode 13 which is not in dwarf official document
break;

default:
throw new Exception($"Unsupported DwarfLineNumberStandardOpcode: {(DwarfLineNumberStandardOpcode)operationCode}");
// Special opcodes(13 or greater)
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* BUGFIX: Fix opcode handling when reading DWARF line number programs. [#482](https://github.com/microsoft/binskim/pull/482)
* BUGFIX: Fix exception `System.ArgumentException` when checking file format. [#481](https://github.com/microsoft/binskim/pull/481)
* BUGFIX: Change compiler report rule to report all modules in file. [#476](https://github.com/microsoft/binskim/pull/476)
* FEATURE: Add dialects to the reporting rules. [#475](https://github.com/microsoft/binskim/pull/475)
Expand Down