Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / minsfi / test_pointer_conversion.c
1 /*
2  * Copyright (c) 2014 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 /*
8  * These are simple unit tests which exercise the conversion between trusted
9  * and untrusted pointers in MinSFI.
10  */
11
12 /*
13  * Tell the minsfi_ptr.h header to generate code which returns a magic value
14  * if an error is detected, rather than abort.
15  */
16 #define MINSFI_PTR_CONVERSION_TEST
17
18 #include "native_client/src/include/minsfi_ptr.h"
19 #include "native_client/src/include/nacl_assert.h"
20
21 static MinsfiSandbox sb;
22 static sfiptr_t untrusted_ptr;
23 static char *native_ptr;
24
25 void test_from_ptr_masks_pointer(void) {
26   sb.mem_base   = (char*)    0x00000000;
27   sb.ptr_mask   = (sfiptr_t) 0x0000FFFF;
28
29   untrusted_ptr = (sfiptr_t) 0xABCDEF12;
30   native_ptr    = (char*)    0x0000EF12;
31
32   ASSERT_EQ(native_ptr, FromMinsfiPtr(untrusted_ptr, &sb));
33 }
34
35 void test_from_ptr_adds_base(void) {
36   sb.mem_base   = (char*)    0xABCD1000;
37   sb.ptr_mask   = (sfiptr_t) 0xFFFFFFFF;
38
39   untrusted_ptr = (sfiptr_t) 0x00123456;
40   native_ptr    = (char*)    0xABDF4456;
41
42   ASSERT_EQ(native_ptr, FromMinsfiPtr(untrusted_ptr, &sb));
43 }
44
45 void test_to_ptr_subtracts_base(void) {
46   sb.mem_base   = (char*)    0x11111000;
47   sb.ptr_mask   = (sfiptr_t) 0xFFFFFFFF;
48
49   native_ptr    = (char*)    0x11234567;
50   untrusted_ptr = (sfiptr_t) 0x00123567;
51
52   ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
53 }
54
55 void test_to_ptr_checks_lower_bound(void) {
56   sb.mem_base   = (char*)    0x11111000;
57   sb.ptr_mask   = (sfiptr_t) 0xFFFFFFFF;
58
59   /* last rejected pointer */
60   native_ptr    = (char*)    0x11110FFF;
61   untrusted_ptr = (sfiptr_t) 0xCAFEBABE;  /* mock error code */
62
63   ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
64
65   /* first accepted pointer */
66   native_ptr    = (char*)    0x11111000;
67   untrusted_ptr = (sfiptr_t) 0x00000000;
68
69   ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
70 }
71
72 void test_to_ptr_checks_upper_bound(void) {
73   sb.mem_base   = (char*)    0x11110000;
74   sb.ptr_mask   = (sfiptr_t) 0x0000FFFF;
75
76   /* last accepted pointer */
77   native_ptr    = (char*)    0x1111FFFF;
78   untrusted_ptr = (sfiptr_t) 0x0000FFFF;
79
80   ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
81
82   /* first rejected pointer */
83   native_ptr    = (char*)    0x11120000;
84   untrusted_ptr = (sfiptr_t) 0xCAFEBABE;  /* mock error code */
85
86   ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
87 }
88
89 int main(void) {
90   test_from_ptr_masks_pointer();
91   test_from_ptr_adds_base();
92   test_to_ptr_subtracts_base();
93   test_to_ptr_checks_lower_bound();
94   test_to_ptr_checks_upper_bound();
95   return 0;
96 }