1578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// Copyright 2013, ARM Limited
2578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// All rights reserved.
3578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
4578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// Redistribution and use in source and binary forms, with or without
5578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// modification, are permitted provided that the following conditions are met:
6578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
7578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Redistributions of source code must retain the above copyright notice,
8578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     this list of conditions and the following disclaimer.
9578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Redistributions in binary form must reproduce the above copyright notice,
10578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     this list of conditions and the following disclaimer in the documentation
11578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     and/or other materials provided with the distribution.
12578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
13578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     used to endorse or promote products derived from this software without
14578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     specific prior written permission.
15578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
16578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
27578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#include <stdlib.h>
28578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#include "cctest.h"
29578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
30578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#include "a64/decoder-a64.h"
31578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#include "a64/disasm-a64.h"
32578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
33578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#define TEST(name)  TEST_(FUZZ_##name)
34578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
35578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
36578645f14e122d2b87d907e298cda7e7d0babf1farmvixlnamespace vixl {
37578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
38578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
39578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(decoder) {
40578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Feed noise into the decoder to check that it doesn't crash.
41578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // 43 million = ~1% of the instruction space.
42578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  static const int instruction_count = 43 * 1024 * 1024;
43578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
44578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint16_t seed[3] = {1, 2, 3};
45578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  seed48(seed);
46578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
47578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
48578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
49578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
50578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  for (int i = 0; i < instruction_count; i++) {
51578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    uint32_t instr = mrand48();
52578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(instr);
53578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
54578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
55578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
56578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
57578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(disasm) {
58578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Feed noise into the disassembler to check that it doesn't crash.
59578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // 9 million = ~0.2% of the instruction space.
60578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  static const int instruction_count = 9 * 1024 * 1024;
61578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
62578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint16_t seed[3] = {42, 43, 44};
63578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  seed48(seed);
64578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
65578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
66578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Disassembler disasm;
67578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
68578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
69578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  decoder.AppendVisitor(&disasm);
70578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  for (int i = 0; i < instruction_count; i++) {
71578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    uint32_t instr = mrand48();
72578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(instr);
73578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
74578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
75578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
76578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
77578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#if 0
78578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// These tests are commented out as they take a long time to run, causing the
79578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// test script to timeout. After enabling them, they are best run individually
80578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// using cctest:
81578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
82578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     cctest_sim FUZZ_decoder_pedantic
83578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     cctest_sim FUZZ_disasm_pedantic
84578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
85578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// or cctest_sim_g for debug builds.
86578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
87578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(decoder_pedantic) {
88578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Test the entire instruction space.
89578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
90578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
91578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
921123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  for (uint64_t i = 0; i < (UINT64_C(1) << 32); i++) {
93578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    if ((i & 0xffffff) == 0) {
94578645f14e122d2b87d907e298cda7e7d0babf1farmvixl      fprintf(stderr, "0x%08" PRIx32 "\n", static_cast<uint32_t>(i));
95578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    }
96578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(static_cast<uint32_t>(i));
97578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
98578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
99578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
100578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
101578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(disasm_pedantic) {
102578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Test the entire instruction space. Warning: takes about 30 minutes on a
103578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // high-end CPU.
104578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
105578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  PrintDisassembler disasm(stdout);
106578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
107578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
108578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  decoder.AppendVisitor(&disasm);
1091123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  for (uint64_t i = 0; i < (UINT64_C(1) << 32); i++) {
110578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    if ((i & 0xffff) == 0) {
111578645f14e122d2b87d907e298cda7e7d0babf1farmvixl      fprintf(stderr, "0x%08" PRIx32 "\n", static_cast<uint32_t>(i));
112578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    }
113578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(static_cast<uint32_t>(i));
114578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
115578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
116578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
117578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#endif
118578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
119578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}   // namespace vixl
120