1b8021494Sopenharmony_ci// Copyright 2018, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ci#include "test-runner.h"
30b8021494Sopenharmony_ci
31b8021494Sopenharmony_ci#include "aarch64/simulator-aarch64.h"
32b8021494Sopenharmony_ci
33b8021494Sopenharmony_ci#define TEST(name) TEST_(AARCH64_POINTER_AUTH_##name)
34b8021494Sopenharmony_ci
35b8021494Sopenharmony_cinamespace vixl {
36b8021494Sopenharmony_cinamespace aarch64 {
37b8021494Sopenharmony_ci
38b8021494Sopenharmony_ci
39b8021494Sopenharmony_ciTEST(compute_pac) {
40b8021494Sopenharmony_ci  Decoder decoder;
41b8021494Sopenharmony_ci  Simulator sim(&decoder);
42b8021494Sopenharmony_ci
43b8021494Sopenharmony_ci  uint64_t data1 = 0xfb623599da6e8127;
44b8021494Sopenharmony_ci  uint64_t data2 = 0x27979fadf7d53cb7;
45b8021494Sopenharmony_ci  uint64_t context = 0x477d469dec0b8762;
46b8021494Sopenharmony_ci  Simulator::PACKey key = {0x84be85ce9804e94b, 0xec2802d4e0a488e9, -1};
47b8021494Sopenharmony_ci
48b8021494Sopenharmony_ci  uint64_t pac1 = sim.ComputePAC(data1, context, key);
49b8021494Sopenharmony_ci  uint64_t pac2 = sim.ComputePAC(data2, context, key);
50b8021494Sopenharmony_ci
51b8021494Sopenharmony_ci  // NOTE: If the PAC implementation is changed, this may fail due to a hash
52b8021494Sopenharmony_ci  // collision.
53b8021494Sopenharmony_ci  VIXL_CHECK(pac1 != pac2);
54b8021494Sopenharmony_ci}
55b8021494Sopenharmony_ci
56b8021494Sopenharmony_ciTEST(add_and_auth_pac) {
57b8021494Sopenharmony_ci  Decoder decoder;
58b8021494Sopenharmony_ci  Simulator sim(&decoder);
59b8021494Sopenharmony_ci
60b8021494Sopenharmony_ci  uint64_t ptr = 0x0000000012345678;
61b8021494Sopenharmony_ci  uint64_t context = 0x477d469dec0b8762;
62b8021494Sopenharmony_ci  Simulator::PACKey key_a = {0x84be85ce9804e94b, 0xec2802d4e0a488e9, 0};
63b8021494Sopenharmony_ci  Simulator::PACKey key_b = {0xec1119e288704d13, 0xd7f6b76e1cea585e, 1};
64b8021494Sopenharmony_ci
65b8021494Sopenharmony_ci  uint64_t ptr_a =
66b8021494Sopenharmony_ci      sim.AddPAC(ptr, context, key_a, Simulator::kInstructionPointer);
67b8021494Sopenharmony_ci
68b8021494Sopenharmony_ci  // Attempt to authenticate the pointer with PAC using different keys.
69b8021494Sopenharmony_ci  uint64_t success =
70b8021494Sopenharmony_ci      sim.AuthPAC(ptr_a, context, key_a, Simulator::kInstructionPointer);
71b8021494Sopenharmony_ci  uint64_t fail =
72b8021494Sopenharmony_ci      sim.AuthPAC(ptr_a, context, key_b, Simulator::kInstructionPointer);
73b8021494Sopenharmony_ci
74b8021494Sopenharmony_ci  uint64_t pac_mask =
75b8021494Sopenharmony_ci      sim.CalculatePACMask(ptr, Simulator::kInstructionPointer, 0);
76b8021494Sopenharmony_ci
77b8021494Sopenharmony_ci  // NOTE: If the PAC implementation is changed, this may fail due to a hash
78b8021494Sopenharmony_ci  // collision.
79b8021494Sopenharmony_ci  VIXL_CHECK((ptr_a & pac_mask) != 0x0);
80b8021494Sopenharmony_ci  VIXL_CHECK(success == ptr);
81b8021494Sopenharmony_ci  VIXL_CHECK(fail != ptr);
82b8021494Sopenharmony_ci}
83b8021494Sopenharmony_ci
84b8021494Sopenharmony_ciTEST(add_and_strip_pac) {
85b8021494Sopenharmony_ci  Decoder decoder;
86b8021494Sopenharmony_ci  Simulator sim(&decoder);
87b8021494Sopenharmony_ci
88b8021494Sopenharmony_ci  uint64_t ptr = 0xff00000012345678;
89b8021494Sopenharmony_ci  uint64_t pac_mask =
90b8021494Sopenharmony_ci      sim.CalculatePACMask(ptr, Simulator::kInstructionPointer, 0);
91b8021494Sopenharmony_ci  uint64_t ptr_a = ptr | pac_mask;
92b8021494Sopenharmony_ci
93b8021494Sopenharmony_ci
94b8021494Sopenharmony_ci  VIXL_CHECK(sim.StripPAC(ptr_a, Simulator::kInstructionPointer) == ptr);
95b8021494Sopenharmony_ci}
96b8021494Sopenharmony_ci}  // namespace aarch64
97b8021494Sopenharmony_ci}  // namespace vixl
98b8021494Sopenharmony_ci
99b8021494Sopenharmony_ci#endif  // VIXL_INCLUDE_SIMULATOR_AARCH64
100