OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #include <assert.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 #include <sys/nacl_syscalls.h> |
| 12 |
| 13 #include "native_client/tests/dynamic_code_loading/dynamic_segment.h" |
| 14 |
| 15 |
| 16 uint8_t halts[] = |
| 17 #if defined(__i386__) || defined(__x86_64__) |
| 18 { 0xf4 }; /* HLT */ |
| 19 #elif defined(__arm__) |
| 20 { 0x76, 0x66, 0x26, 0xe1 }; /* 0xe1266676 - BKPT 0x6666 */ |
| 21 #else |
| 22 # error "Unknown arch" |
| 23 #endif |
| 24 |
| 25 |
| 26 void load_into_page(uint8_t *dest) { |
| 27 uint8_t buf[32]; |
| 28 int rc; |
| 29 uint8_t *ptr; |
| 30 |
| 31 /* Touch the page by loading some halt instructions into it. */ |
| 32 for (ptr = buf; ptr < buf + sizeof(buf); ptr += sizeof(halts)) { |
| 33 memcpy(ptr, halts, sizeof(halts)); |
| 34 } |
| 35 rc = nacl_dyncode_create(dest, buf, sizeof(buf)); |
| 36 assert(rc == 0); |
| 37 |
| 38 /* Check that the whole page is correctly filled with halts. */ |
| 39 for (ptr = dest; ptr < dest + DYNAMIC_CODE_PAGE_SIZE; ptr += sizeof(halts)) { |
| 40 if (memcmp(ptr, halts, sizeof(halts)) != 0) { |
| 41 fprintf(stderr, "Mismatch at %p\n", ptr); |
| 42 exit(1); |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 int main() { |
| 48 uint8_t *dyncode = (uint8_t *) DYNAMIC_CODE_SEGMENT_START; |
| 49 int value; |
| 50 |
| 51 /* Sanity check: First check that two code pages can be written and |
| 52 read, before we check that the page inbetween is unreadable. */ |
| 53 load_into_page(dyncode); |
| 54 load_into_page(dyncode + DYNAMIC_CODE_PAGE_SIZE * 2); |
| 55 |
| 56 printf("Attempting to read from unallocated dyncode page. " |
| 57 "This should fault...\n"); |
| 58 value = dyncode[DYNAMIC_CODE_PAGE_SIZE]; |
| 59 printf("Failed: Dynamic code page was readable and contained the " |
| 60 "byte 0x%x.\n", value); |
| 61 return 1; |
| 62 } |
OLD | NEW |