Skip to content

Commit

Permalink
Fix calculation of segment
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 15, 2024
1 parent 24fe035 commit c647cf1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Ultra.Sampler/MacOS/MacOSLibSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal static partial class MacOSLibSystem
public const int KERN_INVALID_ADDRESS = 1;

public const int PROT_READ = 0x01;
public const int PROT_WRITE = 0x02;
public const int PROT_EXEC = 0x04;

public const int PT_ATTACH = 10; // TODO: deprecated
public const int PT_DETACH = 11;
Expand Down
4 changes: 2 additions & 2 deletions src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static ulong GetDyldCodeSize(nint headerPtr)
if (command.cmd == MacOSLibSystem.LC_SEGMENT_64)
{
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command,MacOSLibSystem.segment_command_64>(ref command);
if (segment.vmaddr < startAddress)
if ((segment.initprot & MacOSLibSystem.PROT_EXEC) != 0 && segment.vmaddr < startAddress)
{
startAddress = segment.vmaddr;
}
Expand All @@ -253,7 +253,7 @@ private static ulong GetDyldCodeSize(nint headerPtr)
ref var segment = ref Unsafe.As<MacOSLibSystem.load_command, MacOSLibSystem.segment_command_64>(ref command);

var newSize = (ulong)((long)segment.vmaddr + (long)segment.vmsize - (long)startAddress);
if (newSize > size)
if ((segment.initprot & MacOSLibSystem.PROT_EXEC) != 0 && newSize > size)
{
size = newSize;
}
Expand Down

0 comments on commit c647cf1

Please sign in to comment.