Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Fix EntryPoints for MoveMemory, CopyMemory, FillMemory, ZeroMemory #431

Closed
vatsan-madhavan opened this issue Apr 15, 2019 · 7 comments
Closed
Assignees

Comments

@vatsan-madhavan
Copy link
Member

Please see https://github.com/dotnet/coreclr/issues/24008 for background.

.NET Framework has special case logic to ensure that MoveMemory, CopyMemory, FillMemory, and ZeroMemory are correctly mapped to RtlMoveMemory, RtlCopyMemory, RtlFillMemory, and RtlZeroMemory respectively.

.NET Core does not have this this special case, so the following P/Invoke definition will fail on .NET Core.

    [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
    public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

At least two definitions in PInvoke.Kernel32 are likely to fail in .NET Core - MoveMemory, and CopyMemory. (I did not inspect to see whether FillMemory and ZeroMemory have this bug).

@AArnott
Copy link
Collaborator

AArnott commented Dec 23, 2019

FillMemory and ZeroMemory are macros that are defined in terms of memset rather than a native exported function from a Win32 DLL. I will fix MoveMemory and CopyMemory though.

Thanks for the report.

@hugmyndakassi
Copy link

While it is technically true that these are macros in the header, kernel32.dll has long exported RtlZeroMemory as function forwarder into a function of the same name in ntdll.dll and also provides an exported RtlFillMemory function.

As a side-note, Vanara also relies on these four functions existing in kernel32.dll.

@AArnott
Copy link
Collaborator

AArnott commented Jul 9, 2021

@hugmyndakassi Are RtlZeroMemory or RtlFillMemory as exported functions documented? I can't find any documentation for them outside of macros. We don't expose undocumented Windows features in this library.

@mpolicki
Copy link

mpolicki commented Nov 1, 2021

I'll just leave a quick note here that I got a System.EntryPointNotFoundException: 'Unable to find an entry point named 'RtlCopyMemory' in DLL 'Kernel32'. in a unit test with a CopyMemory call. The exception went away after I set Processor Architecture for AnyCPU Projects to x64 in Visual Studio Test Explorer settings.

.NET Framework 4.7.2, PInvoke 0.7.104

@AArnott
Copy link
Collaborator

AArnott commented Nov 1, 2021

Thanks, @mpolicki. To be clear, it sounds like you're saying that x86 processes don't get a kernel32 export of RtlCopyMemory, but x64 processes do. Does that sound about right? If so, can you open a separate issue to track that?

@mpolicki
Copy link

mpolicki commented Nov 1, 2021

@AArnott Yes, that's exactly what seems to be the case. The issue is #587.

@AArnott
Copy link
Collaborator

AArnott commented Nov 1, 2021

Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants