Skip to content

Commit

Permalink
Fix opcode handling when reading DWARF line number programs (#482)
Browse files Browse the repository at this point in the history
* Fix OpCode handling

* update ReleaseHistory.md

* fix ordering
  • Loading branch information
shaopeng-gh authored Sep 9, 2021
1 parent fbcdacb commit 374c420
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
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)
// 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

0 comments on commit 374c420

Please sign in to comment.