aboutsummaryrefslogtreecommitdiff
path: root/fuzzers/disassembler_win32_fuzzer.cc
blob: f432dddc960f8add7116f68ce01bfae8c2cd0dcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include "base/logging.h"
#include "components/zucchini/buffer_view.h"
#include "components/zucchini/disassembler.h"
#include "components/zucchini/disassembler_win32.h"

struct Environment {
  Environment() {
    logging::SetMinLogLevel(3);  // Disable console spamming.
  }
};

Environment* env = new Environment();

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  // Prep data.
  zucchini::ConstBufferView image(data, size);

  // One of x86 or x64 should return a non-nullptr if the data is valid.

  // Output will be a pointer to zucchini::DisassemblerWin32X86 if successful
  // or nullptr otherwise.
  auto disassembler_win32x86 =
      zucchini::Disassembler::Make<zucchini::DisassemblerWin32X86>(image);
  if (disassembler_win32x86 != nullptr) {
    // Get the image size which has been shruken to the size understood by the
    // parser.
    auto parsed_image_size = disassembler_win32x86->image().size();

    // Parse the Win32 PE file and ensure nothing bad occurs.
    // TODO(ckitagawa): Actually validate that the output reference is within
    // the image.
    auto relocx86 = disassembler_win32x86->MakeReadRelocs(0, parsed_image_size);
    while (relocx86->GetNext().has_value()) {
    }
    auto abs32x86 = disassembler_win32x86->MakeReadAbs32(0, parsed_image_size);
    while (abs32x86->GetNext().has_value()) {
    }
    auto rel32x86 = disassembler_win32x86->MakeReadRel32(0, parsed_image_size);
    while (rel32x86->GetNext().has_value()) {
    }
  }

  // Output will be a pointer to zucchini::DisassemblerWin32X64 if successful
  // or nullptr otherwise.
  auto disassembler_win32x64 =
      zucchini::Disassembler::Make<zucchini::DisassemblerWin32X64>(image);
  if (disassembler_win32x64 != nullptr) {
    // Get the image size which has been shruken to the size understood by the
    // parser.
    auto parsed_image_size = disassembler_win32x64->image().size();

    // Parse the Win32 PE file and ensure nothing bad occurs.
    auto relocx64 = disassembler_win32x64->MakeReadRelocs(0, parsed_image_size);
    while (relocx64->GetNext().has_value()) {
    }
    auto abs32x64 = disassembler_win32x64->MakeReadAbs32(0, parsed_image_size);
    while (abs32x64->GetNext().has_value()) {
    }
    auto rel32x64 = disassembler_win32x64->MakeReadRel32(0, parsed_image_size);
    while (rel32x64->GetNext().has_value()) {
    }
  }
  return 0;
}