1//===-- lldb-commandinterpreter-fuzzer.cpp -------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===---------------------------------------------------------------------===//
8
9#include <string>
10
11#include "lldb/API/SBCommandInterpreter.h"
12#include "lldb/API/SBCommandInterpreterRunOptions.h"
13#include "lldb/API/SBCommandReturnObject.h"
14#include "lldb/API/SBDebugger.h"
15#include "lldb/API/SBTarget.h"
16
17using namespace lldb;
18
19extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
20 SBDebugger::Initialize();
21 return 0;
22}
23
24extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
25 // Convert the data into a null-terminated string
26 std::string str((char *)data, size);
27
28 // Create a debugger and a dummy target
29 SBDebugger debugger = SBDebugger::Create(source_init_files: false);
30 SBTarget target = debugger.GetDummyTarget();
31
32 // Create a command interpreter for the current debugger
33 // A return object is needed to run the command interpreter
34 SBCommandReturnObject ro = SBCommandReturnObject();
35 SBCommandInterpreter ci = debugger.GetCommandInterpreter();
36
37 // Use the fuzzer generated input as input for the command interpreter
38 if (ci.IsValid()) {
39 ci.HandleCommand(command_line: str.c_str(), result&: ro, add_to_history: false);
40 }
41
42 debugger.DeleteTarget(target);
43 SBDebugger::Destroy(debugger);
44 SBModule::GarbageCollectAllocatedModules();
45
46 return 0;
47}
48

source code of lldb/tools/lldb-fuzzer/lldb-commandinterpreter-fuzzer/lldb-commandinterpreter-fuzzer.cpp