OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/shared_memory.h" | 7 #include "base/shared_memory.h" |
8 #include "base/test/multiprocess_test.h" | 8 #include "base/test/multiprocess_test.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/multiprocess_func_list.h" | 12 #include "testing/multiprocess_func_list.h" |
13 | 13 |
14 #if defined(OS_MACOSX) | 14 #if defined(OS_MACOSX) |
15 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
16 #endif | 16 #endif |
17 | 17 |
| 18 #if defined(OS_POSIX) |
| 19 #include <sys/mman.h> |
| 20 #endif |
| 21 |
18 static const int kNumThreads = 5; | 22 static const int kNumThreads = 5; |
19 static const int kNumTasks = 5; | 23 static const int kNumTasks = 5; |
20 | 24 |
21 namespace base { | 25 namespace base { |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 // Each thread will open the shared memory. Each thread will take a different 4 | 29 // Each thread will open the shared memory. Each thread will take a different 4 |
26 // byte int pointer, and keep changing it, with some small pauses in between. | 30 // byte int pointer, and keep changing it, with some small pauses in between. |
27 // Verify that each thread's value in the shared memory is always correct. | 31 // Verify that each thread's value in the shared memory is always correct. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 explicit MultipleLockThread(int id) : id_(id) {} | 88 explicit MultipleLockThread(int id) : id_(id) {} |
85 ~MultipleLockThread() {} | 89 ~MultipleLockThread() {} |
86 | 90 |
87 // PlatformThread::Delegate interface. | 91 // PlatformThread::Delegate interface. |
88 void ThreadMain() { | 92 void ThreadMain() { |
89 const uint32 kDataSize = sizeof(int); | 93 const uint32 kDataSize = sizeof(int); |
90 SharedMemoryHandle handle = NULL; | 94 SharedMemoryHandle handle = NULL; |
91 { | 95 { |
92 SharedMemory memory1; | 96 SharedMemory memory1; |
93 EXPECT_TRUE(memory1.CreateNamed("SharedMemoryMultipleLockThreadTest", | 97 EXPECT_TRUE(memory1.CreateNamed("SharedMemoryMultipleLockThreadTest", |
94 true, kDataSize)); | 98 true, kDataSize, false)); |
95 EXPECT_TRUE(memory1.ShareToProcess(GetCurrentProcess(), &handle)); | 99 EXPECT_TRUE(memory1.ShareToProcess(GetCurrentProcess(), &handle)); |
96 // TODO(paulg): Implement this once we have a posix version of | 100 // TODO(paulg): Implement this once we have a posix version of |
97 // SharedMemory::ShareToProcess. | 101 // SharedMemory::ShareToProcess. |
98 EXPECT_TRUE(true); | 102 EXPECT_TRUE(true); |
99 } | 103 } |
100 | 104 |
101 SharedMemory memory2(handle, false); | 105 SharedMemory memory2(handle, false); |
102 EXPECT_TRUE(memory2.Map(kDataSize)); | 106 EXPECT_TRUE(memory2.Map(kDataSize)); |
103 volatile int* const ptr = static_cast<int*>(memory2.memory()); | 107 volatile int* const ptr = static_cast<int*>(memory2.memory()); |
104 | 108 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 else | 329 else |
326 EXPECT_EQ(0, pointers[j][0]); | 330 EXPECT_EQ(0, pointers[j][0]); |
327 } | 331 } |
328 } | 332 } |
329 | 333 |
330 for (int i = 0; i < count; i++) { | 334 for (int i = 0; i < count; i++) { |
331 memories[i].Close(); | 335 memories[i].Close(); |
332 } | 336 } |
333 } | 337 } |
334 | 338 |
| 339 #if defined(OS_POSIX) |
| 340 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. |
| 341 TEST(SharedMemoryTest, AnonymousExecutable) { |
| 342 const uint32 kTestSize = 1 << 16; |
| 343 |
| 344 SharedMemory shared_memory; |
| 345 SharedMemoryCreateOptions options(kTestSize); |
| 346 options.executable = true; |
| 347 |
| 348 EXPECT_TRUE(shared_memory.Create(options)); |
| 349 EXPECT_TRUE(shared_memory.Map(shared_memory.created_size())); |
| 350 |
| 351 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.created_size(), |
| 352 PROT_READ | PROT_EXEC)); |
| 353 } |
| 354 #endif |
| 355 |
335 // On POSIX it is especially important we test shmem across processes, | 356 // On POSIX it is especially important we test shmem across processes, |
336 // not just across threads. But the test is enabled on all platforms. | 357 // not just across threads. But the test is enabled on all platforms. |
337 class SharedMemoryProcessTest : public MultiProcessTest { | 358 class SharedMemoryProcessTest : public MultiProcessTest { |
338 public: | 359 public: |
339 | 360 |
340 static void CleanUp() { | 361 static void CleanUp() { |
341 SharedMemory memory; | 362 SharedMemory memory; |
342 memory.Delete(s_test_name_); | 363 memory.Delete(s_test_name_); |
343 } | 364 } |
344 | 365 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 } | 423 } |
403 | 424 |
404 SharedMemoryProcessTest::CleanUp(); | 425 SharedMemoryProcessTest::CleanUp(); |
405 } | 426 } |
406 | 427 |
407 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 428 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
408 return SharedMemoryProcessTest::TaskTestMain(); | 429 return SharedMemoryProcessTest::TaskTestMain(); |
409 } | 430 } |
410 | 431 |
411 } // namespace base | 432 } // namespace base |
OLD | NEW |