Files
carbon-lang/explorer/fuzzing/fuzzer_util.cpp
T
Jon Ross-Perkins 9e03d5efe2 Change explorer's file_test to support argument passing to main. (#3032)
This shifts explorer's file_test to use ExplorerMain in order to be able
to pass arguments similar to how the command line would. It also means
that various output wrapping done by the command line is shared, no
longer copied by file_test.

In order to help make this work, I plugged vfs::FileSystem into
explorer, similar to how we're doing this on the toolchain side (might
try to share more code later). I was having trouble getting an overlay
working, I think due to how paths are specified -- but due to the issues
in fixing this, I'm just loading the prelude into the InMemoryFilesystem
for now.

Under this approach, I'm unifying more of the file versus string
handling. I think we should shift towards an approach where, like
toolchain code, anyone trying to use Explorer should use a vfs
implementation to supply code. This should reduce the number of distinct
code paths which we're maintaining/testing.

Short-term, this allows the trace testdata to be merged into file_test
with less special casing, using ARGS:. Long-term, it means that the
file_test knows the ARGS to pass to generate checks to match against,
which is the direction I'm planning for autoupdate.
2023-07-28 15:29:51 +00:00

58 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 "explorer/parse_and_execute/parse_and_execute.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "testing/fuzzing/proto_to_carbon.h"
#include "tools/cpp/runfiles/runfiles.h"
namespace Carbon::Testing {
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 ParseAndExecuteProto(const Fuzzing::Carbon& carbon) -> ErrorOr<int> {
llvm::vfs::InMemoryFileSystem fs;
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();
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
llvm::MemoryBuffer::getFile(*prelude_path);
CARBON_CHECK(!prelude.getError()) << prelude.getError().message();
CARBON_CHECK(fs.addFile("prelude.carbon", /*ModificationTime=*/0,
std::move(*prelude)));
const std::string source = ProtoToCarbon(carbon, /*maybe_add_main=*/true);
CARBON_CHECK(fs.addFile("fuzzer.carbon", /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBuffer(source)));
TraceStream trace_stream;
return ParseAndExecute(fs, "prelude.carbon", "fuzzer.carbon",
/*parser_debug=*/false, &trace_stream, &llvm::nulls());
}
} // namespace Carbon::Testing