Is forced access violation possible when accessing a specific address?

We have an array that is large for alignment purposes, so those disconnected by a single error are not caught by the usual mechanisms.

Is it possible to protect a small arbitrary area (16 bytes at the beginning and end of the array) in Windows so that it causes an access violation? Language is C ++.

+4
source share
4 answers

I believe that in x86, the best level of detail you can achieve by marking your memory as secure is the page (I think 4K). You can configure the array so that the beginning or end falls on the border of the page (and this page was protected). But in order for both ends to fall on such boundaries, of course, a very specific array length would be required.

The following is an example of setting up security pages.

+4
source

You can do this on UNIXish with a combination of __attribute__((aligned (PAGESIZE))) and mprotect . On Windows, I think there is an equivalent to mprotect , but it is also limited to one page of memory.

The reason it is impossible to protect things with a higher degree of granularity is because the memory is accessed using hardware rather than software. This would be very slow if each individual memory access required passing through some data structure to check if the page is protected.

+1
source

Not directly. The closest thing you can do is set up a data breakpoint for these buffers. However, x86 has a total of 4 such breakpoints, and they are a maximum of 8 bytes. In addition, you need to be in ring 0 (kernel mode) to set them.

+1
source

I do not think it can be done. You can always make an exception yourself.

0
source

All Articles