Files
carbon-lang/explorer/parse_and_execute/parse_and_execute.cpp
T
Richard Smith 81e53886a8 Fix crash on use of uninitialized array element, and improve unformed checking (#2862)
Per #257, we should be treating unformedness as all-or-nothing, rather than being a per-field or per-array-element property. Previously we initialized an array with no explicit initializer as containing a sequence of uninitialized values, but that led to crashes when attempting to access those values, as the checks for reading an uninitialized value only expected values to be uninitialized at the top level.

Also, we had existing tests that attempt to store to an element of an uninitialized array. We now detect that and treat it as UB during evaluation, rather than crashing due to trying to perform field access into an uninitialized value.

Finally, many of these problems can be detected statically, but the resolve_unformed pass wasn't catching them because it missed a few expression and declaration forms. Support for those cases has been added too. This causes the pass to recurse more often, and in particular our existing recursion test started hitting a stack overflow after this, so resolve_unformed now uses `RunWithExtraStack`. In passing, remove the need to explicitly tell `RunWithExtraStack` the return type, and infer it as the return type of the callable instead.
2023-05-31 16:35:21 -07:00

102 lines
3.7 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/parse_and_execute/parse_and_execute.h"
#include <locale>
#include "common/check.h"
#include "common/error.h"
#include "explorer/interpreter/exec_program.h"
#include "explorer/interpreter/stack_space.h"
#include "explorer/syntax/parse.h"
#include "explorer/syntax/prelude.h"
#include "llvm/ADT/ScopeExit.h"
namespace Carbon {
// Returns a scope exit function for printing the timing of a step on scope
// exit. Note the use prints step timings in reverse order.
static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
std::chrono::steady_clock::time_point* cursor) {
auto end = std::chrono::steady_clock::now();
auto duration = end - *cursor;
*cursor = end;
return llvm::make_scope_exit([=]() {
if (trace_stream->is_enabled()) {
*trace_stream << "Time elapsed in " << label << ": "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
duration)
.count()
<< "ms\n";
}
});
}
static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
const std::string& prelude_path,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream)
-> ErrorOr<int> {
return RunWithExtraStack([&]() -> ErrorOr<int> {
Arena arena;
auto cursor = std::chrono::steady_clock::now();
ErrorOr<AST> parse_result = parse(&arena);
auto print_parse_time = PrintTimingOnExit(trace_stream, "Parse", &cursor);
if (!parse_result.ok()) {
return ErrorBuilder() << "SYNTAX ERROR: " << parse_result.error();
}
AddPrelude(prelude_path, &arena, &parse_result->declarations,
&parse_result->num_prelude_declarations);
auto print_prelude_time =
PrintTimingOnExit(trace_stream, "AddPrelude", &cursor);
// Semantically analyze the parsed program.
ErrorOr<AST> analyze_result =
AnalyzeProgram(&arena, *parse_result, trace_stream, print_stream);
auto print_analyze_time =
PrintTimingOnExit(trace_stream, "AnalyzeProgram", &cursor);
if (!analyze_result.ok()) {
return ErrorBuilder() << "COMPILATION ERROR: " << analyze_result.error();
}
// Run the program.
ErrorOr<int> exec_result =
ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
auto print_exec_time =
PrintTimingOnExit(trace_stream, "ExecProgram", &cursor);
if (!exec_result.ok()) {
return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
}
return exec_result;
});
}
auto ParseAndExecuteFile(const std::string& prelude_path,
const std::string& input_file_name, bool parser_debug,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream)
-> ErrorOr<int> {
auto parse = [&](Arena* arena) {
return Parse(arena, input_file_name, parser_debug);
};
return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
}
auto ParseAndExecute(const std::string& prelude_path, const std::string& source)
-> ErrorOr<int> {
auto parse = [&](Arena* arena) {
return ParseFromString(arena, "test.carbon", source,
/*parser_debug=*/false);
};
TraceStream trace_stream;
return ParseAndExecuteHelper(parse, prelude_path, &trace_stream,
&llvm::nulls());
}
} // namespace Carbon