1 : /*
2 : * Copyright (c) 2008 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 : // Testing NativeClient cross-platfom memory management functions
8 :
9 : #include "native_client/src/include/portability.h"
10 : #include "native_client/src/include/nacl_platform.h"
11 : #include "native_client/src/shared/platform/nacl_log.h"
12 :
13 : // TODO(robertm): eliminate need for this
14 : #if NACL_WINDOWS
15 : #include "native_client/src/include/win/mman.h"
16 : #endif
17 : #include "native_client/src/trusted/service_runtime/sel_memory.h"
18 :
19 : #include "gtest/gtest.h"
20 :
21 4 : class SelMemoryBasic : public testing::Test {
22 : protected:
23 : virtual void SetUp();
24 : virtual void TearDown();
25 : // TODO(gregoryd) - do we need a destructor here?
26 : };
27 :
28 : void SelMemoryBasic::SetUp() {
29 2 : NaClLogModuleInit();
30 2 : }
31 :
32 : void SelMemoryBasic::TearDown() {
33 2 : NaClLogModuleFini();
34 2 : }
35 :
36 :
37 8 : TEST_F(SelMemoryBasic, AllocationTests) {
38 1 : int res = 0;
39 1 : void *p = NULL;
40 1 : int size;
41 :
42 1 : size = 0x2001; // not power of two - should be supported
43 :
44 1 : res = NaClPageAlloc(&p, size);
45 4 : EXPECT_EQ(0, res);
46 4 : EXPECT_NE(static_cast<void *>(NULL), p);
47 :
48 1 : NaClPageFree(p, size);
49 1 : p = NULL;
50 :
51 : // Try to allocate large memory block
52 1 : size = 256 * 1024 * 1024; // 256M
53 :
54 1 : res = NaClPageAlloc(&p, size);
55 4 : EXPECT_EQ(0, res);
56 4 : EXPECT_NE(static_cast<void *>(NULL), p);
57 :
58 1 : NaClPageFree(p, size);
59 1 : }
60 :
61 8 : TEST_F(SelMemoryBasic, mprotect) {
62 1 : int res = 0;
63 1 : void *p = NULL;
64 1 : int size;
65 1 : char *addr;
66 :
67 1 : size = 0x100000;
68 :
69 1 : res = NaClPageAlloc(&p, size);
70 4 : EXPECT_EQ(0, res);
71 4 : EXPECT_NE(static_cast<void *>(NULL), p);
72 :
73 : // TODO(gregoryd) - since ASSERT_DEATH is not supported for client projects,
74 : // we cannot use gUnit to test the protection. We might want to add some
75 : // internal processing (based on SEH/signals) at some stage
76 :
77 1 : res = NaClMprotect(p, size, PROT_READ |PROT_WRITE);
78 4 : EXPECT_EQ(0, res);
79 1 : addr = reinterpret_cast<char*>(p);
80 1 : addr[0] = '5';
81 :
82 1 : res = NaClMprotect(p, size, PROT_READ);
83 4 : EXPECT_EQ(0, res);
84 4 : EXPECT_EQ('5', addr[0]);
85 :
86 1 : res = NaClMprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC);
87 :
88 1 : NaClPageFree(p, size);
89 1 : }
|