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

Improve handling of nul-terminated string in Globals. #1350

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions _.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this file is here, and not in .vscode? I'm not familiar with the significance of the leading underscore.

// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/Drivers/WindowsDecompiler/bin/x64/Debug/net6.0-windows/WindowsDecompiler.exe",
"args": [],
"cwd": "${workspaceFolder}/src/Drivers/WindowsDecompiler",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
39 changes: 39 additions & 0 deletions src/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"version": "0.2.0",
"configurations": [

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/astred.dll", "cwd": "${workspaceFolder}",
"args": [],
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": "debug Astred easy", "stopAtEntry": false,
"type": "coreclr", "request": "launch", "console": "integratedTerminal", "preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net8.0/astred.dll", "cwd": "${workspaceFolder}",
"args": [ "-full", "tests/easycpp/easy.cpp", "-treedump", "tests/easycpp/easy.cpp",],
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
},
{
"name": "reko li.dcproject", "stopAtEntry": false,
"type": "coreclr", "request": "launch", "console": "integratedTerminal",
"program": "${workspaceFolder}\\..\\..\\LaserType\\LaserImage\\reko-private\\reko.dll",
"cwd": "${workspaceFolder}\\..\\..\\LaserType\\LaserImage",
"args": [ "--env", "ms-dos", "--dasm-bytes", "--dasm-address", "li.dcproject" ],
},
]
}
2 changes: 2 additions & 0 deletions src/Core/Output/AbsynCodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ private string UnsignedRepresentation(Constant u)
else
{
var sb = new StringBuilder();
#if USE_TILDA_IF_SMALLER
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a user preference, and it shouldn't be coded using #if / #endif. Instead consider opening an issue to add support for changing the formatting of unsigned numbers, controlled by user preference. There we can discuss how this would best be accomplished.

if ((p & msb) != 0 &&
Bits.BitCount(m & p) > Bits.BitCount(m & ~p))
{
sb.Append('~');
p = m & ~p;
hexRep = p.ToString("X", CultureInfo.InvariantCulture);
}
#endif
sb.Append("0x");
int length = hexRep.Length;
int pad;
Expand Down
4 changes: 4 additions & 0 deletions src/Core/Output/Dumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ private void DumpTypedData(SegmentMap map, IProcessorArchitecture arch, ImageMap
{
if (!map.TryFindSegment(item.Address, out var segment) || segment.MemoryArea == null)
return;
if (item.Name != null) {
w.Write("\t\t;;; {0}", item.Name);
w.WriteLine();
}
WriteLabel(item.Address, w);

var rdr = arch.CreateImageReader(segment.MemoryArea, item.Address);
Expand Down
10 changes: 4 additions & 6 deletions src/Core/Output/GlobalDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,17 @@ public bool VisitArray(ArrayType at)
fmt.Terminate();
fmt.Indent();
fmt.Write("{");
fmt.Terminate();
fmt.Indentation += fmt.TabSize;
fmt.Write(" ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've changed the rendering of arrays so that they are rendered on a single line. What happens when an array of 1000 entries is encountered? I prefer the current state of affairs, until Reko can prettify the output source.

In addition, you haven't changed the GlobalDataWriterTests.cs unit tests to match, so the CI pipeline will fail. Make sure there is a unit tests to cover this case.

There is the beginning of a code prettifier in Core\Output\PrettyPrinter.cs that should be able to format arrays nicely. Consider opening an issue and using that class to make data output prettier.


bool ok = true;
for (int i = 0; i < at.Length; ++i)
{
fmt.Indent();
ok = at.ElementType.Accept(this);
fmt.Terminate(",");
if (i < at.Length - 1) {
fmt.Write(",");
}
}

fmt.Indentation -= fmt.TabSize;
fmt.Indent();
fmt.Write("}");
return ok;
}
Expand Down
109 changes: 103 additions & 6 deletions src/Core/Output/TypedDataDumper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region License
/*
/*
* Copyright (C) 1999-2024 John Källén.
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -31,7 +31,7 @@ public class TypedDataDumper : IDataTypeVisitor
private readonly uint cbSize;
private readonly Formatter fmt;

public TypedDataDumper(EndianImageReader rdr, uint cbSize, Formatter stm)
public TypedDataDumper(EndianImageReader rdr, uint cbSize, Formatter stm)
{
this.rdr = rdr;
this.cbSize = cbSize;
Expand All @@ -41,11 +41,99 @@ public TypedDataDumper(EndianImageReader rdr, uint cbSize, Formatter stm)
public void VisitArray(ArrayType at)
{
var addrEnd = rdr.Address + cbSize;
for (int i = 0; at.IsUnbounded || i < at.Length; ++i)

if (at.ElementType.Domain < Domain.Composite)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying on an enum's value in this way is fragile. Consider exposing a DataType.IsComposite property in a separate PR` to accomplish this.

{
if (!rdr.IsValid || addrEnd <= rdr.Address)
return;
at.ElementType.Accept(this);
int offset = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There appears to be a lot of duplication here; could this code and the code in VisitPrimitiveType be harmonized?

bool clean = true;
for (int i = 0; at.IsUnbounded || i < at.Length; ++i)
{
if (!rdr.IsValid || addrEnd <= rdr.Address)
{
return;
}
if (offset % 16 == 0)
{
if (!clean)
{
fmt.WriteLine();
fmt.Write(" ");
clean = true;
}
switch (at.ElementType.Size)
{
default:
case 1:
fmt.WriteKeyword("db");
fmt.Write("\t");
break;
case 2:
fmt.WriteKeyword("dw");
fmt.Write("\t");
break;
case 4:
fmt.WriteKeyword("dd");
fmt.Write("\t");
break;
case 8:
fmt.WriteKeyword("dq");
fmt.Write("\t");
break;
}
}
if (!clean)
{
fmt.Write(",");
}
switch (at.ElementType.Size)
{
default:
case 1:
byte b = rdr.ReadByte();
if (at.ElementType.Domain == Domain.Character && 0x20 <= b && b < 0x7F)
{
fmt.Write(string.Format("'{0}'", (char)b));
}
else
{
fmt.Write(string.Format("0x{0:X2}", b));
}
offset += 1;
break;
case 2:
fmt.Write(string.Format("0x{0:X4}", rdr.ReadUInt16()));
offset += 2;
break;
case 4:
fmt.Write(string.Format("0x{0:X8}", rdr.ReadUInt32()));
offset += 4;
break;
case 8:
fmt.Write(string.Format("0x{0:X16}", rdr.ReadUInt64()));
offset += 8;
break;
}
clean = false;
}
if (!clean) {
fmt.WriteLine();
clean = true;
}
}
else {
bool clean = true;
for (int i = 0; at.IsUnbounded || i < at.Length; ++i)
{
if (!rdr.IsValid || addrEnd <= rdr.Address)
return;

if (!clean) {
fmt.Write(" ");
clean = true;
}
at.ElementType.Accept(this);
clean = false;
}
}
}

Expand Down Expand Up @@ -179,6 +267,8 @@ public void VisitString(StringType str)
fmt.Write("\t");
bool inStringLiteral = false;
string sep = "";
int size = str.Length;
int i = 0;
while (rdr.TryReadByte(out byte b))
{
//$REVIEW: assumes ASCII.
Expand All @@ -198,13 +288,20 @@ public void VisitString(StringType str)
if (inStringLiteral)
{
fmt.Write('\'');
inStringLiteral = false;
}
fmt.Write(sep);
sep = ",";
fmt.Write(string.Format("0x{0:X2}", b));
if (b == 0)
break;
}
i++;
Debug.Assert(i < size);
}
if (inStringLiteral)
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a unit tests to show how this is an improvement.

fmt.Write('\'');
}
fmt.WriteLine();
return;
Expand Down
24 changes: 24 additions & 0 deletions src/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ public uint GetDataSize(IProcessorArchitecture arch, Address addr, DataType dt)
return 0;
while (rdr.IsValid && !rdr.ReadNullCharTerminator(strDt.ElementType))
;
strDt.Length = (int) (rdr.Address - addr);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little unhappy about mutating the string data type when it's being used in what semantically is a read-only query type function. Consider looking at setting the size when the string is created.

return (uint) (rdr.Address - addr);
}
else
Expand Down Expand Up @@ -840,6 +841,29 @@ public void AddGlobalField(Address address, DataType dt, string name)
fields.Add(offset, dt, name);
}

public DataType? FindGlobalField(Address address)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a doc comment for this new method.

{
int offset;
StructureFieldCollection fields;
if (address.Selector.HasValue &&
SegmentMap.TryFindSegment(address, out var seg))
{
offset = (int) address.Offset;
fields = seg.Fields.Fields;
}
else
{
offset = (int) address.ToLinear();
fields = GlobalFields.Fields;
}
var globalField = fields.AtOffset(offset);
if (globalField != null)
{
return globalField.DataType;
}
return null;
}

/// <summary>
/// Add new imported external procedure if there is not one with same
/// import name.
Expand Down
1 change: 1 addition & 0 deletions src/Decompiler/Scanning/GlobalDataWorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public void VisitString(StringType str)
}

var len = rdr.Offset - offsetStart;
str.Length = (int)len;
program.ImageMap.AddItemWithSize(
addr, new ImageMapItem(addr, (uint) len)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Decompiler/Typing/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ public void CollectUserGlobalVariableTypes()
{
if (ud.Value.DataType != null)
{
var dt = ud.Value.DataType.Accept(deser);
var dt = program.FindGlobalField(ud.Key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a unit tests in TypeCollectorTests.cs that illustrates the problem this change is trying to solve?

if (dt == null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the dt is null syntax here.

{
dt = ud.Value.DataType.Accept(deser);
}
AddGlobalField(dt, ud.Key, ud.Value.Name);
}
}
Expand Down
Loading