1

What I would like to do is mark a specific area of memory as being automatically shared between processes of the same image/binary, similar to __declspec(allocate)... and __pragma(section...).

I know that I can use names pipes or equivalent, but for this purpose I would like to avoid system calls or additional overhead. I'm just unsure if there is any way to inform the NT kernel to map a specific range of pages automatically for each process of an image. I haven't found anything on MSDN, though MSDN doesn't include undocumented functionally (by definition), which I am fine with using.

I also don't see any specific PE section names/flags which would indicate such, though it is possible that I am missing something.

Ed: I've noticed that there is actually a PE section flag IMAGE_SCN_MEM_SHARED, though I need to investigate how it works.

ameisen
  • 33
  • 1
  • 7

1 Answers1

4

You can use #pragma comment(linker, "/SECTION:.shared,RWS") and #pragma data_seg(".shared") to declare things in a shared memory segment (only works in Visual Studio). See Sharing Variables Between Win32 Executables.

Otherwise, if that is not an option for you, the only other way to share memory between processes is to use a Memory Mapped File via CreateFileMapping() and MapViewOfFile/Ex(). See Creating Named Shared Memory.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I've noticed that there is a PE flag IMAGE_SCN_MEM_SHARED, which those probably use. – ameisen Sep 13 '18 at 15:53
  • 3
    Note that you cannot secure shared sections in binaries. File mappings, on the other hand, use standard access control. – IInspectable Sep 13 '18 at 16:26
  • Mandatory read: [Why .shared sections are a security hole](https://blogs.msdn.microsoft.com/oldnewthing/20040804-00/?p=38253). – zett42 Sep 13 '18 at 18:17