OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #if NACL_LINUX | 10 #if NACL_LINUX |
11 # include <sys/mman.h> | 11 # include <sys/mman.h> |
12 #elif NACL_OSX | 12 #elif NACL_OSX |
13 # include <mach/mach.h> | 13 # include <mach/mach.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include "gtest/gtest.h" | 16 #include "gtest/gtest.h" |
17 | 17 |
18 #include "native_client/src/include/portability_io.h" | 18 #include "native_client/src/include/portability_io.h" |
19 #include "native_client/src/trusted/desc/linux/nacl_desc_sysv_shm.h" | |
20 #include "native_client/src/trusted/desc/nacl_desc_imc_shm.h" | 19 #include "native_client/src/trusted/desc/nacl_desc_imc_shm.h" |
21 #include "native_client/src/trusted/desc/nacl_desc_io.h" | 20 #include "native_client/src/trusted/desc/nacl_desc_io.h" |
22 #include "native_client/src/trusted/desc/nrd_all_modules.h" | 21 #include "native_client/src/trusted/desc/nrd_all_modules.h" |
23 #include "native_client/src/trusted/service_runtime/include/bits/mman.h" | 22 #include "native_client/src/trusted/service_runtime/include/bits/mman.h" |
24 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h" | 23 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h" |
25 #include "native_client/src/trusted/service_runtime/mmap_test_check.h" | 24 #include "native_client/src/trusted/service_runtime/mmap_test_check.h" |
26 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" | 25 #include "native_client/src/trusted/service_runtime/nacl_app_thread.h" |
27 #include "native_client/src/trusted/service_runtime/sel_addrspace.h" | 26 #include "native_client/src/trusted/service_runtime/sel_addrspace.h" |
28 #include "native_client/src/trusted/service_runtime/sel_ldr.h" | 27 #include "native_client/src/trusted/service_runtime/sel_ldr.h" |
29 #include "native_client/src/trusted/service_runtime/sys_memory.h" | 28 #include "native_client/src/trusted/service_runtime/sys_memory.h" |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 CheckMapping(sysaddr, size, PROT_NONE, MAP_PRIVATE); | 390 CheckMapping(sysaddr, size, PROT_NONE, MAP_PRIVATE); |
392 #elif NACL_OSX | 391 #elif NACL_OSX |
393 CheckMapping(sysaddr, size, VM_PROT_NONE, SM_EMPTY); | 392 CheckMapping(sysaddr, size, VM_PROT_NONE, SM_EMPTY); |
394 #else | 393 #else |
395 # error Unsupported platform | 394 # error Unsupported platform |
396 #endif | 395 #endif |
397 | 396 |
398 NaClAddrSpaceFree(&app); | 397 NaClAddrSpaceFree(&app); |
399 } | 398 } |
400 | 399 |
401 // NaCl uses SysV shared memory only on Linux, because X Windows | |
402 // depends on it. | |
403 #if NACL_LINUX | |
404 TEST_F(MmapTest, TestSysvShmMapping) { | |
405 struct NaClApp app; | |
406 ASSERT_EQ(NaClAppCtor(&app), 1); | |
407 ASSERT_EQ(NaClAllocAddrSpace(&app), LOAD_OK); | |
408 | |
409 size_t shm_size = 0x12000; | |
410 size_t rounded_size = NaClRoundAllocPage(shm_size); | |
411 ASSERT_EQ(rounded_size, (size_t) 0x20000); | |
412 | |
413 struct NaClDescSysvShm *shm_desc = | |
414 (struct NaClDescSysvShm *) malloc(sizeof(*shm_desc)); | |
415 ASSERT_TRUE(NaClDescSysvShmCtor(shm_desc, shm_size)); | |
416 struct NaClDesc *desc = &shm_desc->base; | |
417 int fd = NaClAppSetDescAvail(&app, desc); | |
418 | |
419 uintptr_t mapping_addr = 0x200000; | |
420 | |
421 // First, map something with PROT_READ, so that we can later check | |
422 // that this is correctly overwritten by PROT_READ|PROT_WRITE and | |
423 // PROT_NONE mappings. | |
424 uintptr_t result_addr = (uint32_t) NaClSysMmapIntern( | |
425 &app, (void *) mapping_addr, shm_size, | |
426 NACL_ABI_PROT_READ, | |
427 NACL_ABI_MAP_FIXED | NACL_ABI_MAP_PRIVATE | NACL_ABI_MAP_ANONYMOUS, | |
428 -1, 0); | |
429 ASSERT_EQ(result_addr, mapping_addr); | |
430 | |
431 result_addr = (uint32_t) NaClSysMmapIntern( | |
432 &app, (void *) mapping_addr, shm_size, | |
433 NACL_ABI_PROT_READ | NACL_ABI_PROT_WRITE, | |
434 NACL_ABI_MAP_FIXED | NACL_ABI_MAP_SHARED, fd, 0); | |
435 ASSERT_EQ(result_addr, mapping_addr); | |
436 uintptr_t sysaddr = NaClUserToSys(&app, mapping_addr); | |
437 // We should see two mappings. The first is the SysV shared memory | |
438 // segment. The second is padding upto the 64k page size. | |
439 CheckMapping(sysaddr, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED); | |
440 CheckMapping(sysaddr + shm_size, rounded_size - shm_size, | |
441 PROT_NONE, MAP_PRIVATE); | |
442 | |
443 NaClDescUnref(desc); | |
444 NaClAddrSpaceFree(&app); | |
445 } | |
446 #endif | |
447 | |
448 #endif /* NACL_ARCH(NACL_BUILD_ARCH) != NACL_arm */ | 400 #endif /* NACL_ARCH(NACL_BUILD_ARCH) != NACL_arm */ |
OLD | NEW |