How can you determine if a memory page is marked as read-only?

When using copy-on-write semantics to exchange memory between processes, how can you check if a page with memory is available or if it is marked as read-only? Is it possible to do this by calling a specific assembler code or by reading a specific place in memory or through the OS API?

+3
source share
4 answers

On Linux, you can check / proc / pid / maps:

$ cat /proc/self/maps

002b3000-002cc000 r-xp 00000000 68:01 143009   /lib/ld-2.5.so
002cc000-002cd000 r-xp 00018000 68:01 143009   /lib/ld-2.5.so
002cd000-002ce000 rwxp 00019000 68:01 143009   /lib/ld-2.5.so
002d0000-00407000 r-xp 00000000 68:01 143010   /lib/libc-2.5.so
00407000-00409000 r-xp 00137000 68:01 143010   /lib/libc-2.5.so
00409000-0040a000 rwxp 00139000 68:01 143010   /lib/libc-2.5.so
0040a000-0040d000 rwxp 0040a000 00:00 0
00c6f000-00c70000 r-xp 00c6f000 00:00 0        [vdso]
08048000-0804d000 r-xp 00000000 68:01 379298   /bin/cat
0804d000-0804e000 rw-p 00004000 68:01 379298   /bin/cat
08326000-08347000 rw-p 08326000 00:00 0
b7d1b000-b7f1b000 r--p 00000000 68:01 226705   /usr/lib/locale/locale-archive
b7f1b000-b7f1c000 rw-p b7f1b000 00:00 0
b7f28000-b7f29000 rw-p b7f28000 00:00 0
bfe37000-bfe4d000 rw-p bfe37000 00:00 0        [stack]

The first column is the range of virtual memory addresses, the second column contains permissions (read, write, execute and private), columns 3-6 contain the offset, major and minor device numbers, index and file name with memory mapping.

+3
source

Win32 - VirtualQuery. MEMORY_BASIC_INFORMATION , . Protect, , . , , , , .

OS API - . CPU , .

+3

Win32, IsBadReadPtr IsBadWritePtr. :

" , IsBad (IsBadReadPtr, IsBadWritePtr ..) ."

Raymond Chen : "IsBadXxxPtr CrashProgramRandomly"

Chen has some helpful advice on how to deal with this issue here .

Result: you should not test such things at runtime. The code so that you know what you were given, and if this is not as expected, treat it as an error. If you really have no choice, look at SEH to handle the exception.

+1
source

Are you talking about the variety of shared memory allocated via shmget (on Unix)? I.e.

int shmget(key_t, size_t, int);

If so, you can request this memory with

int shmctl(int, int, struct shmid_ds *);

For instance:

key_t key = /* your choice of memory api */
int flag = /* set of flags for your app */
int shmid = shmget(key, 4096, flag);

struct shmid_ds buf;
int result = shmctl(shmid, IPC_STAT, &buf);
/* buf.ipc_perm.mode contains the permissions for the memory segment */
+1
source

Source: https://habr.com/ru/post/1699009/


All Articles