Function allocates a memory region with two sub-regions. The first one contains addresses from range [0:valid_size[ with access allowed. The second region contains addresses from range [valid_size:overflow_size[ and access to this region triggers SIGSEGV.
This mechanism is useful for checks which fail very rarely and need to be as fast as possible. Signed-off-by: Tomek Grabiec <tgrab...@gmail.com> --- include/vm/guard-page.h | 1 + vm/guard-page.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 0 deletions(-) diff --git a/include/vm/guard-page.h b/include/vm/guard-page.h index 7f8ac58..a779b02 100644 --- a/include/vm/guard-page.h +++ b/include/vm/guard-page.h @@ -2,6 +2,7 @@ #define _VM_GUARD_PAGE_ void *alloc_guard_page(void); +void *alloc_offset_guard(unsigned long valid_size, unsigned long overflow_size); int hide_guard_page(void *page_ptr); int unhide_guard_page(void *page_ptr); diff --git a/vm/guard-page.c b/vm/guard-page.c index 5404787..420230a 100644 --- a/vm/guard-page.c +++ b/vm/guard-page.c @@ -30,6 +30,41 @@ #include <sys/mman.h> #include <unistd.h> +#include <assert.h> +#include <stdio.h> + +/** + * alloc_offset_guard - allocates a memory region with two + * sub-regions. The first one contains addresses from [0:valid_size[ + * with access allowed. The second region contains addresses + * [valid_size:overflow_size[ and access to this region triggers + * SIGSEGV. + */ +void *alloc_offset_guard(unsigned long valid_size, unsigned long overflow_size) +{ + int valid_pages, overflow_pages; + int page_size; + void *result; + + assert(overflow_size > 0); + + page_size = getpagesize(); + + valid_pages = (valid_size + page_size - 1) / page_size; + overflow_pages = (overflow_size + page_size - 1) / page_size; + + result = alloc_pages(valid_pages + overflow_pages); + + mprotect(result + valid_pages * page_size, + overflow_pages * page_size, + PROT_NONE); + + unsigned long offset = page_size - valid_size % page_size; + if (offset == (unsigned long) page_size) + offset = 0; + + return result + offset; +} void *alloc_guard_page(void) { -- 1.6.0.6 ------------------------------------------------------------------------------ _______________________________________________ Jatovm-devel mailing list Jatovm-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jatovm-devel