1b78f13911bfe6eda303e91ef215c87a165aae8aeAlexandre Rames// Copyright 2014, VIXL authors
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
2778973f258039f6e96eba85f1b5ecdb14b3c51dbbPierre Langlois#include <cstdlib>
28b68bacb75c1ab265fc539afa93964c7f51f35589Alexandre Rames
29330dc7153e671968beb67f09ed2cb7b5bda334dbarmvixl#include "test-runner.h"
30578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
31d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#include "aarch64/decoder-aarch64.h"
32d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#include "aarch64/disasm-aarch64.h"
33578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
34bde2e4b5ce376456d50a972b6f3aaee3475f8786Pierre Langlois#define TEST(name) TEST_(AARCH64_FUZZ_##name)
35578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
36578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
37578645f14e122d2b87d907e298cda7e7d0babf1farmvixlnamespace vixl {
3888c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisnamespace aarch64 {
39578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
40578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
41578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(decoder) {
42578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Feed noise into the decoder to check that it doesn't crash.
43578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // 43 million = ~1% of the instruction space.
44578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  static const int instruction_count = 43 * 1024 * 1024;
45578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
46578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint16_t seed[3] = {1, 2, 3};
47578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  seed48(seed);
48578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
49578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
50578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
51578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
52578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  for (int i = 0; i < instruction_count; i++) {
53db6443499376478f5281607a3923e6ffc4c8d8ecarmvixl    uint32_t instr = static_cast<uint32_t>(mrand48());
54578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(instr);
55578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
56578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
57578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
58578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
59578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(disasm) {
60578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Feed noise into the disassembler to check that it doesn't crash.
61578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // 9 million = ~0.2% of the instruction space.
62578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  static const int instruction_count = 9 * 1024 * 1024;
63578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
64578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint16_t seed[3] = {42, 43, 44};
65578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  seed48(seed);
66578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
67578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
68578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Disassembler disasm;
69578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
70578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
71578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  decoder.AppendVisitor(&disasm);
72578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  for (int i = 0; i < instruction_count; i++) {
73db6443499376478f5281607a3923e6ffc4c8d8ecarmvixl    uint32_t instr = static_cast<uint32_t>(mrand48());
74578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(instr);
75578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
76578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
77578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
78578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
79578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#if 0
80578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// These tests are commented out as they take a long time to run, causing the
81330dc7153e671968beb67f09ed2cb7b5bda334dbarmvixl// test script to timeout. After enabling them, they are best run manually:
82578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
83330dc7153e671968beb67f09ed2cb7b5bda334dbarmvixl//     test-runner_sim FUZZ_decoder_pedantic
84330dc7153e671968beb67f09ed2cb7b5bda334dbarmvixl//     test-runner_sim FUZZ_disasm_pedantic
85578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
86330dc7153e671968beb67f09ed2cb7b5bda334dbarmvixl// or test-runner_sim_g for debug builds.
87578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
88578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(decoder_pedantic) {
89578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Test the entire instruction space.
90578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
91578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
92578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
93b0c8ae2a5f0abc58f67322052d39bfd47edb2892armvixl  for (uint64_t i = 0; i < (UINT64_C(1) << 32); i++) {
94578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    if ((i & 0xffffff) == 0) {
95578645f14e122d2b87d907e298cda7e7d0babf1farmvixl      fprintf(stderr, "0x%08" PRIx32 "\n", static_cast<uint32_t>(i));
96578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    }
97578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(static_cast<uint32_t>(i));
98578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
99578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
100578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
101578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
102578645f14e122d2b87d907e298cda7e7d0babf1farmvixlTEST(disasm_pedantic) {
103578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // Test the entire instruction space. Warning: takes about 30 minutes on a
104578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  // high-end CPU.
105578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Decoder decoder;
106578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  PrintDisassembler disasm(stdout);
107578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Instruction buffer[kInstructionSize];
108578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
109578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  decoder.AppendVisitor(&disasm);
110b0c8ae2a5f0abc58f67322052d39bfd47edb2892armvixl  for (uint64_t i = 0; i < (UINT64_C(1) << 32); i++) {
111578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    if ((i & 0xffff) == 0) {
112578645f14e122d2b87d907e298cda7e7d0babf1farmvixl      fprintf(stderr, "0x%08" PRIx32 "\n", static_cast<uint32_t>(i));
113578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    }
114578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    buffer->SetInstructionBits(static_cast<uint32_t>(i));
115578645f14e122d2b87d907e298cda7e7d0babf1farmvixl    decoder.Decode(buffer);
116578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  }
117578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}
118578645f14e122d2b87d907e298cda7e7d0babf1farmvixl#endif
119578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
120bde2e4b5ce376456d50a972b6f3aaee3475f8786Pierre Langlois}  // namespace aarch64
121bde2e4b5ce376456d50a972b6f3aaee3475f8786Pierre Langlois}  // namespace vixl
122