Use VirtualProtect to mark pages as copy to write (white plug on windows)

I use VirtualAlloc (Ex) to select and commit a large number of pages.

Later in the execution, I want to “unlock” this memory, start a new process that can read it in its current state, while the parent process considers it as a write memory.

Can this be done with VirtualAlloc (Ex) and VirtualProtect (Ex)?

This is trivial on posix systems thanks to fork (). Can I emulate this particular part of the plug efficiently on windows?

Thanks Dan

+6
c ++ c # windows fork
source share
2 answers

I do not believe that any of them fully answers the question posed by the original poster. Perhaps I can risk the following example. Suppose you have some data in "bufferA" and one instance of it "bufferB", and then modifies both buffers. Now consider both buffers and decides to copy "bufferA" or "bufferB" to "bufferC" and make additional changes to "bufferC", etc.

Assuming that the buffers are large and the modifications are small, the question is whether the optimization will use copy-write capabilities on the MMU on the host machine and if there is an API that allows this.

Using memory-mapped files doesn't quite work, because as soon as one uses PAGE_WRITECOPY, there seems to be no way to say "now copy the virtual pages containing the changes." For example, if "bufferA" and "bufferB" were both mappings of the same file, and during the display one of them was marked as "WRITECOPY", there seems to be no way to map these changes to "bufferC". You can, of course, just copy the bit, but the question is whether there is a potential optimization.

There is a function called VirtualCopy (), but it is similar to Windows CE, and works with physical addresses. The question here is whether there is something like "VirtualCopy", but the virtual address is used as the source.

Please note that while fork () "(on systems that have it) does this, one" fork "is an entire process, not just a buffer. A discussion of" copy-on-write memcpy "was discussed here ( Can I do i make memcpy to copy to write on linux? ).

So, an API search might look something like this: void * VirualCopyVirtualAddressSpace (void * src, size_t length);

So far, I have not seen the API on any system that would allow copying buffers either in a process or between existing processes. Although I am not an expert, I would think that copying a correctly aligned virtual memory segment using the write-to-write mechanism would be quite simple, at least within the same address space.

+4
source share

I think you can do this if you use memory mapping (CreateFileMapping / MapViewOfFile) with PAGE_WRITECOPY.

This one on MSDN can serve as a starting point if you change FILE_MAP_ALL_ACCESS to PAGE_WRITECOPY for the second process.

If you need time-consistent copying, you will most likely need a VirtualProtect () displayed area with PAGE_WRITECOPY in the first process, so the second process does not see any changes.

+1
source share

All Articles