1 // Copyright 2014, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include <cstdlib>
28 #include <string>
29
30 #include "test-runner.h"
31
32 #include "aarch64/decoder-aarch64.h"
33 #include "aarch64/disasm-aarch64.h"
34
35 #define TEST(name) TEST_(AARCH64_FUZZ_##name)
36
37
38 namespace vixl {
39 namespace aarch64 {
40
FuzzHelper(std::string mode,int step_size,int offset,int shift)41 static void FuzzHelper(std::string mode, int step_size, int offset, int shift) {
42 Decoder decoder;
43 PrintDisassembler disasm(stdout);
44 Instruction buffer[kInstructionSize];
45
46 if (mode == "disasm") {
47 decoder.AppendVisitor(&disasm);
48 } else {
49 VIXL_CHECK(mode == "decoder");
50 }
51
52 for (uint64_t i = offset << shift; i < (UINT64_C(1) << 32); i += step_size) {
53 buffer->SetInstructionBits(static_cast<uint32_t>(i));
54 decoder.Decode(buffer);
55 }
56 }
57
58 // Number of shards used to split fuzz tests. This value isn't used in the macro
59 // below, so if you change this, ensure more FUZZ_SHARD instances are
60 // instantiated.
61 static const int kShardCount = 16;
62
63 // Test approximately 1% of the instruction space for the decoder, and 0.2% for
64 // the disassembler. Multiply the step size by the number of shards issued.
65 static const int kDecoderStep = 100 * kShardCount + 1;
66 static const int kDisasmStep = 500 * kShardCount + 1;
67
68 // Shift the offset argument into the top-level opcode bits, which helps to
69 // spread the fuzz coverage across instruction classes.
70 static const int kOpFieldShift = 25;
71
72 #define FUZZ_SHARD(mode, step, i, shift) \
73 TEST(mode##_##i) { FuzzHelper(#mode, step, i, shift); }
74
75 FUZZ_SHARD(decoder, kDecoderStep, 0, kOpFieldShift)
76 FUZZ_SHARD(decoder, kDecoderStep, 1, kOpFieldShift)
77 FUZZ_SHARD(decoder, kDecoderStep, 2, kOpFieldShift)
78 FUZZ_SHARD(decoder, kDecoderStep, 3, kOpFieldShift)
79 FUZZ_SHARD(decoder, kDecoderStep, 4, kOpFieldShift)
80 FUZZ_SHARD(decoder, kDecoderStep, 5, kOpFieldShift)
81 FUZZ_SHARD(decoder, kDecoderStep, 6, kOpFieldShift)
82 FUZZ_SHARD(decoder, kDecoderStep, 7, kOpFieldShift)
83 FUZZ_SHARD(decoder, kDecoderStep, 8, kOpFieldShift)
84 FUZZ_SHARD(decoder, kDecoderStep, 9, kOpFieldShift)
85 FUZZ_SHARD(decoder, kDecoderStep, 10, kOpFieldShift)
86 FUZZ_SHARD(decoder, kDecoderStep, 11, kOpFieldShift)
87 FUZZ_SHARD(decoder, kDecoderStep, 12, kOpFieldShift)
88 FUZZ_SHARD(decoder, kDecoderStep, 13, kOpFieldShift)
89 FUZZ_SHARD(decoder, kDecoderStep, 14, kOpFieldShift)
90 FUZZ_SHARD(decoder, kDecoderStep, 15, kOpFieldShift)
91
92 FUZZ_SHARD(disasm, kDisasmStep, 0, kOpFieldShift)
93 FUZZ_SHARD(disasm, kDisasmStep, 1, kOpFieldShift)
94 FUZZ_SHARD(disasm, kDisasmStep, 2, kOpFieldShift)
95 FUZZ_SHARD(disasm, kDisasmStep, 3, kOpFieldShift)
96 FUZZ_SHARD(disasm, kDisasmStep, 4, kOpFieldShift)
97 FUZZ_SHARD(disasm, kDisasmStep, 5, kOpFieldShift)
98 FUZZ_SHARD(disasm, kDisasmStep, 6, kOpFieldShift)
99 FUZZ_SHARD(disasm, kDisasmStep, 7, kOpFieldShift)
100 FUZZ_SHARD(disasm, kDisasmStep, 8, kOpFieldShift)
101 FUZZ_SHARD(disasm, kDisasmStep, 9, kOpFieldShift)
102 FUZZ_SHARD(disasm, kDisasmStep, 10, kOpFieldShift)
103 FUZZ_SHARD(disasm, kDisasmStep, 11, kOpFieldShift)
104 FUZZ_SHARD(disasm, kDisasmStep, 12, kOpFieldShift)
105 FUZZ_SHARD(disasm, kDisasmStep, 13, kOpFieldShift)
106 FUZZ_SHARD(disasm, kDisasmStep, 14, kOpFieldShift)
107 FUZZ_SHARD(disasm, kDisasmStep, 15, kOpFieldShift)
108
109 } // namespace aarch64
110 } // namespace vixl
111