mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
I'm partly doing this because the current setup would be difficult to share with the toolchain. e.g., ProtoToCarbon isn't explorer-specific, but the only way to run it via CLI is the explorer's fuzzverter. I want a separate tool.
This change:
- Adds a //common/fuzzing:proto_to_carbon tool.
- The rest of fuzzverter is now just //explorer/fuzzing:ast_to_proto.
- The change simplifies overall handling and removes a LLVM CLI dependency.
- Stops allowing unknown fields in the proto.
- This has mostly led to forgetting to remove fuzzer inputs that were for removed features.
- Moves more non-explorer-specific bits to //common/fuzzing.
- Cleans up remaining pieces in //explorer/fuzzing
- Merges the //explorer/fuzzing proto tests, which deduplicates AstToString copies.
- These tests also had duplicate dependencies, etc -- and all complete in ~6s.
- Updates and fixes regen_corpus which was previously broken by other changes.
- Updates the README to reflect changes.
- Removes obsolete proto-fuzzer build configuration (AFAICT this is no longer needed).
61 lines
2.2 KiB
C++
61 lines
2.2 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "explorer/fuzzing/fuzzer_util.h"
|
|
|
|
#include <google/protobuf/text_format.h>
|
|
|
|
#include "common/check.h"
|
|
#include "common/error.h"
|
|
#include "common/fuzzing/proto_to_carbon.h"
|
|
#include "explorer/interpreter/exec_program.h"
|
|
#include "explorer/interpreter/trace_stream.h"
|
|
#include "explorer/syntax/parse.h"
|
|
#include "explorer/syntax/prelude.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "tools/cpp/runfiles/runfiles.h"
|
|
|
|
namespace Carbon {
|
|
|
|
auto GetRunfilesFile(const std::string& file) -> ErrorOr<std::string> {
|
|
using bazel::tools::cpp::runfiles::Runfiles;
|
|
std::string error;
|
|
// `Runfiles::Create()` fails if passed an empty `argv0`.
|
|
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(
|
|
/*argv0=*/llvm::sys::fs::getMainExecutable(nullptr, nullptr), &error));
|
|
if (runfiles == nullptr) {
|
|
return Error(error);
|
|
}
|
|
std::string full_path = runfiles->Rlocation(file);
|
|
if (!llvm::sys::fs::exists(full_path)) {
|
|
return ErrorBuilder() << full_path << " doesn't exist";
|
|
}
|
|
return full_path;
|
|
}
|
|
|
|
auto ParseAndExecute(const Fuzzing::Carbon& carbon) -> ErrorOr<int> {
|
|
const std::string source = ProtoToCarbon(carbon, /*maybe_add_main=*/true);
|
|
|
|
Arena arena;
|
|
CARBON_ASSIGN_OR_RETURN(AST ast,
|
|
ParseFromString(&arena, "Fuzzer.carbon", source,
|
|
/*parser_debug=*/false));
|
|
const ErrorOr<std::string> prelude_path =
|
|
GetRunfilesFile("carbon/explorer/data/prelude.carbon");
|
|
// Can't do anything without a prelude, so it's a fatal error.
|
|
CARBON_CHECK(prelude_path.ok()) << prelude_path.error();
|
|
|
|
AddPrelude(*prelude_path, &arena, &ast.declarations,
|
|
&ast.num_prelude_declarations);
|
|
TraceStream trace_stream;
|
|
|
|
// Use llvm::nulls() to suppress output from the Print intrinsic.
|
|
CARBON_ASSIGN_OR_RETURN(
|
|
ast, AnalyzeProgram(&arena, ast, &trace_stream, &llvm::nulls()));
|
|
return ExecProgram(&arena, ast, &trace_stream, &llvm::nulls());
|
|
}
|
|
|
|
} // namespace Carbon
|