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.
8 * These are simple unit tests which exercise the conversion between trusted
9 * and untrusted pointers in MinSFI.
13 * Tell the minsfi_ptr.h header to generate code which returns a magic value
14 * if an error is detected, rather than abort.
16 #define MINSFI_PTR_CONVERSION_TEST
18 #include "native_client/src/include/minsfi_ptr.h"
19 #include "native_client/src/include/nacl_assert.h"
21 static MinsfiSandbox sb;
22 static sfiptr_t untrusted_ptr;
23 static char *native_ptr;
25 void test_from_ptr_masks_pointer(void) {
26 sb.mem_base = (char*) 0x00000000;
27 sb.ptr_mask = (sfiptr_t) 0x0000FFFF;
29 untrusted_ptr = (sfiptr_t) 0xABCDEF12;
30 native_ptr = (char*) 0x0000EF12;
32 ASSERT_EQ(native_ptr, FromMinsfiPtr(untrusted_ptr, &sb));
35 void test_from_ptr_adds_base(void) {
36 sb.mem_base = (char*) 0xABCD1000;
37 sb.ptr_mask = (sfiptr_t) 0xFFFFFFFF;
39 untrusted_ptr = (sfiptr_t) 0x00123456;
40 native_ptr = (char*) 0xABDF4456;
42 ASSERT_EQ(native_ptr, FromMinsfiPtr(untrusted_ptr, &sb));
45 void test_to_ptr_subtracts_base(void) {
46 sb.mem_base = (char*) 0x11111000;
47 sb.ptr_mask = (sfiptr_t) 0xFFFFFFFF;
49 native_ptr = (char*) 0x11234567;
50 untrusted_ptr = (sfiptr_t) 0x00123567;
52 ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
55 void test_to_ptr_checks_lower_bound(void) {
56 sb.mem_base = (char*) 0x11111000;
57 sb.ptr_mask = (sfiptr_t) 0xFFFFFFFF;
59 /* last rejected pointer */
60 native_ptr = (char*) 0x11110FFF;
61 untrusted_ptr = (sfiptr_t) 0xCAFEBABE; /* mock error code */
63 ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
65 /* first accepted pointer */
66 native_ptr = (char*) 0x11111000;
67 untrusted_ptr = (sfiptr_t) 0x00000000;
69 ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
72 void test_to_ptr_checks_upper_bound(void) {
73 sb.mem_base = (char*) 0x11110000;
74 sb.ptr_mask = (sfiptr_t) 0x0000FFFF;
76 /* last accepted pointer */
77 native_ptr = (char*) 0x1111FFFF;
78 untrusted_ptr = (sfiptr_t) 0x0000FFFF;
80 ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
82 /* first rejected pointer */
83 native_ptr = (char*) 0x11120000;
84 untrusted_ptr = (sfiptr_t) 0xCAFEBABE; /* mock error code */
86 ASSERT_EQ(untrusted_ptr, ToMinsfiPtr(native_ptr, &sb));
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();