Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is <em>very</em> similar to a homework I once did. Basically I had a list of "pages" and a list of "frames", with associated information. Using <code>SIGSEGV</code> I would catch faults and alter the memory protection bits as necessary. I'll include parts that you may find useful.</p> <h3>Create mapping. Initially it has no permissions.</h3> <pre><code>int w_create_mapping(size_t size, void **addr) { *addr = mmap(NULL, size * w_get_page_size(), PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0 ); if (*addr == MAP_FAILED) { perror("mmap"); return FALSE; } return TRUE; } </code></pre> <h3>Install signal handler</h3> <pre><code>int w_set_exception_handler(w_exception_handler_t handler) { static struct sigaction sa; sa.sa_sigaction = handler; sigemptyset(&amp;sa.sa_mask); sigaddset(&amp;sa.sa_mask, SIGSEGV); sa.sa_flags = SA_SIGINFO; if (sigaction(SIGSEGV, &amp;sa, &amp;previous_action) &lt; 0) return FALSE; return TRUE; } </code></pre> <h3>Exception handler</h3> <pre><code>static void fault_handler(int signum, siginfo_t *info, void *context) { void *address; /* the address that faulted */ /* Memory location which caused fault */ address = info-&gt;si_addr; if (FALSE == page_fault(address)) { _exit(1); } } </code></pre> <h3>Increasing protection</h3> <pre><code>int w_protect_mapping(void *addr, size_t num_pages, w_prot_t protection) { int prot; switch (protection) { case PROTECTION_NONE: prot = PROT_NONE; break; case PROTECTION_READ: prot = PROT_READ; break; case PROTECTION_WRITE: prot = PROT_READ | PROT_WRITE; break; } if (mprotect(addr, num_pages * w_get_page_size(), prot) &lt; 0) return FALSE; return TRUE; } </code></pre> <p>I can't publicly make it all available since the team is likely to use that same homework again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload