Memory Protection

When an application allocates a memory object, it can specify the type of access to allow to the object. Memory access protection provides a program with control over the type of access that its threads have to a page of memory.

Access protection can only be defined for committed pages of memory and is initially set at the time the memory is committed. Different pages within the same memory object can have different access attributes and access attributes can be changed on a page-by-page basis at any time.

An application can request any combination of the following access protection attributes:

Memory Access Protection Attributes

┌────────────────────┬────────────────────┬────────────────────┐
│Access              │Defined Constant    │Description         │
├────────────────────┼────────────────────┼────────────────────┤
│Read Access         │PAG_READ            │The object can be   │
│                    │                    │read from, but not  │
│                    │                    │written to.         │
├────────────────────┼────────────────────┼────────────────────┤
│Write Access        │PAG_WRITE           │The object can be   │
│                    │                    │written to.  On the │
│                    │                    │80386               │
│                    │                    │microprocessor,     │
│                    │                    │write access implies│
│                    │                    │both read and       │
│                    │                    │execute access.     │
├────────────────────┼────────────────────┼────────────────────┤
│Execute Access      │PAG_EXECUTE         │This is equivalent  │
│                    │                    │to read access on   │
│                    │                    │the 80386.          │
├────────────────────┼────────────────────┼────────────────────┤
│Guard Page Access   │PAG_GUARD           │Causes a            │
│                    │                    │guard-page-entered  │
│                    │                    │exception to be     │
│                    │                    │raised in a process │
│                    │                    │that attempts to    │
│                    │                    │access the memory.  │
│                    │                    │This exception can  │
│                    │                    │be ignored or       │
│                    │                    │handled by the      │
│                    │                    │application's       │
│                    │                    │exception handler,  │
│                    │                    │if one is           │
│                    │                    │registered.         │
└────────────────────┴────────────────────┴────────────────────┘

The guard page attribute is intended to provide automatic stack growth and stack limit checking. An application can also use it in other data structures, such as arrays. For example, if an application is using an array of 4096 bytes (one page), the application can allocate and commit two pages, one with read and write access and one designated as a guard page. If the application tries to write past the end of the array a page guard exception will be generated.

Any reference-read, write, or execute-to a guard page causes an access violation (page fault) to be generated. This fault causes a Guard Page Entered exception to occur for the thread that referred to the guard page. The exception can be handled by the exception handler of the process, if one is registered. If the process does not have an exception handler registered, OS/2's default exception handler will handle the exception. The default action by the system exception handler is to convert the page from a guard page to a committed page, then try to mark the next page in memory as a guard page. If the system is not successful in marking the next page as a guard page, an Unable-To-Grow-Stack exception occurs. The thread is allowed to continue execution, but must be aware that it has at most 4KB of stack remaining.


[Back: Memory Resizing and Reallocation]
[Next: Obtaining Information about a Page Range]