1b8021494Sopenharmony_ci// Copyright 2014, 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#include <cstdlib>
28b8021494Sopenharmony_ci#include <string>
29b8021494Sopenharmony_ci
30b8021494Sopenharmony_ci#include "test-runner.h"
31b8021494Sopenharmony_ci
32b8021494Sopenharmony_ci#include "aarch64/decoder-aarch64.h"
33b8021494Sopenharmony_ci#include "aarch64/disasm-aarch64.h"
34b8021494Sopenharmony_ci
35b8021494Sopenharmony_ci#define TEST(name) TEST_(AARCH64_FUZZ_##name)
36b8021494Sopenharmony_ci
37b8021494Sopenharmony_ci
38b8021494Sopenharmony_cinamespace vixl {
39b8021494Sopenharmony_cinamespace aarch64 {
40b8021494Sopenharmony_ci
41b8021494Sopenharmony_cistatic void FuzzHelper(std::string mode, int step_size, int offset, int shift) {
42b8021494Sopenharmony_ci  Decoder decoder;
43b8021494Sopenharmony_ci  PrintDisassembler disasm(stdout);
44b8021494Sopenharmony_ci  Instruction buffer[kInstructionSize];
45b8021494Sopenharmony_ci
46b8021494Sopenharmony_ci  if (mode == "disasm") {
47b8021494Sopenharmony_ci    decoder.AppendVisitor(&disasm);
48b8021494Sopenharmony_ci  } else {
49b8021494Sopenharmony_ci    VIXL_CHECK(mode == "decoder");
50b8021494Sopenharmony_ci  }
51b8021494Sopenharmony_ci
52b8021494Sopenharmony_ci  for (uint64_t i = offset << shift; i < (UINT64_C(1) << 32); i += step_size) {
53b8021494Sopenharmony_ci    buffer->SetInstructionBits(static_cast<uint32_t>(i));
54b8021494Sopenharmony_ci    decoder.Decode(buffer);
55b8021494Sopenharmony_ci  }
56b8021494Sopenharmony_ci}
57b8021494Sopenharmony_ci
58b8021494Sopenharmony_ci// Number of shards used to split fuzz tests. This value isn't used in the macro
59b8021494Sopenharmony_ci// below, so if you change this, ensure more FUZZ_SHARD instances are
60b8021494Sopenharmony_ci// instantiated.
61b8021494Sopenharmony_cistatic const int kShardCount = 16;
62b8021494Sopenharmony_ci
63b8021494Sopenharmony_ci// Test approximately 1% of the instruction space for the decoder, and 0.2% for
64b8021494Sopenharmony_ci// the disassembler. Multiply the step size by the number of shards issued.
65b8021494Sopenharmony_cistatic const int kDecoderStep = 100 * kShardCount + 1;
66b8021494Sopenharmony_cistatic const int kDisasmStep = 500 * kShardCount + 1;
67b8021494Sopenharmony_ci
68b8021494Sopenharmony_ci// Shift the offset argument into the top-level opcode bits, which helps to
69b8021494Sopenharmony_ci// spread the fuzz coverage across instruction classes.
70b8021494Sopenharmony_cistatic const int kOpFieldShift = 25;
71b8021494Sopenharmony_ci
72b8021494Sopenharmony_ci#define FUZZ_SHARD(mode, step, i, shift) \
73b8021494Sopenharmony_ci  TEST(mode##_##i) { FuzzHelper(#mode, step, i, shift); }
74b8021494Sopenharmony_ci
75b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 0, kOpFieldShift)
76b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 1, kOpFieldShift)
77b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 2, kOpFieldShift)
78b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 3, kOpFieldShift)
79b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 4, kOpFieldShift)
80b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 5, kOpFieldShift)
81b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 6, kOpFieldShift)
82b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 7, kOpFieldShift)
83b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 8, kOpFieldShift)
84b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 9, kOpFieldShift)
85b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 10, kOpFieldShift)
86b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 11, kOpFieldShift)
87b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 12, kOpFieldShift)
88b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 13, kOpFieldShift)
89b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 14, kOpFieldShift)
90b8021494Sopenharmony_ciFUZZ_SHARD(decoder, kDecoderStep, 15, kOpFieldShift)
91b8021494Sopenharmony_ci
92b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 0, kOpFieldShift)
93b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 1, kOpFieldShift)
94b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 2, kOpFieldShift)
95b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 3, kOpFieldShift)
96b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 4, kOpFieldShift)
97b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 5, kOpFieldShift)
98b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 6, kOpFieldShift)
99b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 7, kOpFieldShift)
100b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 8, kOpFieldShift)
101b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 9, kOpFieldShift)
102b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 10, kOpFieldShift)
103b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 11, kOpFieldShift)
104b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 12, kOpFieldShift)
105b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 13, kOpFieldShift)
106b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 14, kOpFieldShift)
107b8021494Sopenharmony_ciFUZZ_SHARD(disasm, kDisasmStep, 15, kOpFieldShift)
108b8021494Sopenharmony_ci
109b8021494Sopenharmony_ci}  // namespace aarch64
110b8021494Sopenharmony_ci}  // namespace vixl
111