Skip to content

Commit 36087ec

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm] Remove unused kernel bytes in case of compile-time errors
In case of compile-time errors kernel service has been serializing kernel AST anyway and has been passing kernel bytes to the VM. VM does not use those kernel bytes except a few unit tests. This change removes the unnecessary kernel serialization and freeing of kernel bytes and cleans up unit tests. TEST=ci Change-Id: Ic2464e50f56227bc998df53dd2b594c1abcf9468 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436360 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent 66ba0cf commit 36087ec

File tree

10 files changed

+63
-174
lines changed

10 files changed

+63
-174
lines changed

pkg/vm/bin/kernel_service.dart

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ Future _processExpressionCompilationRequest(request) async {
643643
port.send(
644644
new CompilationResult.errors([
645645
"No platform found to initialize incremental compiler.",
646-
], null).toResponse(),
646+
]).toResponse(),
647647
);
648648
return;
649649
}
@@ -687,7 +687,7 @@ Future _processExpressionCompilationRequest(request) async {
687687
new CompilationResult.errors([
688688
"Error when trying to create a compiler for expression compilation: "
689689
"'$e'.",
690-
], null).toResponse(),
690+
]).toResponse(),
691691
);
692692
return;
693693
}
@@ -698,7 +698,7 @@ Future _processExpressionCompilationRequest(request) async {
698698
port.send(
699699
new CompilationResult.errors([
700700
"No incremental compiler available for this isolate.",
701-
], null).toResponse(),
701+
]).toResponse(),
702702
);
703703
return;
704704
}
@@ -724,9 +724,7 @@ Future _processExpressionCompilationRequest(request) async {
724724
);
725725

726726
if (procedure == null) {
727-
port.send(
728-
new CompilationResult.errors(["Invalid scope."], null).toResponse(),
729-
);
727+
port.send(new CompilationResult.errors(["Invalid scope."]).toResponse());
730728
return;
731729
}
732730

@@ -736,7 +734,7 @@ Future _processExpressionCompilationRequest(request) async {
736734
if (compiler.errorsPlain.isNotEmpty) {
737735
// TODO(sigmund): the compiler prints errors to the console, so we
738736
// shouldn't print those messages again here.
739-
result = new CompilationResult.errors(compiler.errorsPlain, null);
737+
result = new CompilationResult.errors(compiler.errorsPlain);
740738
} else {
741739
Component component = createExpressionEvaluationComponent(procedure);
742740
result = new CompilationResult.ok(serializeComponent(component));
@@ -899,7 +897,7 @@ Future _processLoadRequest(request) async {
899897
port.send(
900898
new CompilationResult.errors([
901899
"No incremental compiler available for this isolate.",
902-
], null).toResponse(),
900+
]).toResponse(),
903901
);
904902
return;
905903
}
@@ -1024,19 +1022,7 @@ Future _processLoadRequest(request) async {
10241022
...(enableColors) ? compiler.errorsColorized : compiler.errorsPlain,
10251023
...nativeAssetsErrors.map((e) => e.message),
10261024
];
1027-
final component = compilerResult.component;
1028-
if (component != null) {
1029-
result = new CompilationResult.errors(
1030-
errors,
1031-
serializeComponent(
1032-
component,
1033-
filter: (lib) => !loadedLibraries.contains(lib),
1034-
nativeAssetsComponent: nativeAssetsComponent,
1035-
),
1036-
);
1037-
} else {
1038-
result = new CompilationResult.errors(errors, null);
1039-
}
1025+
result = new CompilationResult.errors(errors);
10401026
} else {
10411027
// We serialize the component excluding vm_platform.dill because the VM has
10421028
// these sources built-in. Everything loaded as a summary in
@@ -1076,7 +1062,7 @@ Future _processLoadRequest(request) async {
10761062
inputFileUri,
10771063
inputFileUri,
10781064
null,
1079-
new CompilationResult.errors(<String>["unknown tag"], null).payload,
1065+
new CompilationResult.errors(<String>["unknown tag"]).payload,
10801066
]);
10811067
}
10821068
}
@@ -1288,8 +1274,7 @@ abstract class CompilationResult {
12881274

12891275
factory CompilationResult.ok(Uint8List? bytes) = _CompilationOk;
12901276

1291-
factory CompilationResult.errors(List<String> errors, Uint8List? bytes) =
1292-
_CompilationError;
1277+
factory CompilationResult.errors(List<String> errors) = _CompilationError;
12931278

12941279
factory CompilationResult.crash(Object exception, StackTrace stack) =
12951280
_CompilationCrash;
@@ -1332,10 +1317,9 @@ abstract class _CompilationFail extends CompilationResult {
13321317
}
13331318

13341319
class _CompilationError extends _CompilationFail {
1335-
final Uint8List? bytes;
13361320
final List<String> errors;
13371321

1338-
_CompilationError(this.errors, this.bytes);
1322+
_CompilationError(this.errors);
13391323

13401324
@override
13411325
Status get status => Status.error;
@@ -1344,8 +1328,6 @@ class _CompilationError extends _CompilationFail {
13441328
String get errorString => errors.join('\n');
13451329

13461330
String toString() => "_CompilationError(${errorString})";
1347-
1348-
List toResponse() => [status.index, payload, bytes];
13491331
}
13501332

13511333
class _CompilationCrash extends _CompilationFail {

pkg/vm/test/kernel_service_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ Future<kernel_service.Status> singleShotCompile(
169169
}
170170
return kernel_service.Status.ok;
171171
} else if (status == kernel_service.Status.error.index) {
172-
expectLength(m, 3);
172+
expectLength(m, 2);
173173
final String errors = m[1];
174-
final List<int> bytes = m[2];
175174
if (verbose) {
176-
print("Compiled with errors --- $errors and ${bytes.length} bytes dill");
175+
print("Compiled with errors --- $errors");
177176
}
178177
return kernel_service.Status.error;
179178
} else if (status == kernel_service.Status.crash.index) {

runtime/bin/dfe.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,15 @@ void DFE::CompileAndReadScript(const char* script_uri,
213213
*exit_code = 0;
214214
break;
215215
case Dart_KernelCompilationStatus_Error:
216-
free(result.kernel);
217216
*error = result.error; // Copy error message.
218217
*exit_code = kCompilationErrorExitCode;
219218
break;
220219
case Dart_KernelCompilationStatus_Crash:
221-
free(result.kernel);
222220
*error = result.error; // Copy error message.
223221
*exit_code = kDartFrontendErrorExitCode;
224222
break;
225223
case Dart_KernelCompilationStatus_Unknown:
226224
case Dart_KernelCompilationStatus_MsgFailed:
227-
free(result.kernel);
228225
*error = result.error; // Copy error message.
229226
*exit_code = kErrorExitCode;
230227
break;

runtime/vm/dart_api_impl_test.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8299,8 +8299,7 @@ TEST_CASE(DartAPI_Multiroot_Valid) {
82998299
int sourcefiles_count = sizeof(sourcefiles) / sizeof(Dart_SourceFile);
83008300
lib = TestCase::LoadTestScriptWithDFE(
83018301
sourcefiles_count, sourcefiles, nullptr, /* finalize= */ true,
8302-
/* incrementally= */ true, /* allow_compile_errors= */ false,
8303-
"foo:///main.dart",
8302+
/* incrementally= */ true, "foo:///main.dart",
83048303
/* multiroot_filepaths= */ "/bar,/baz",
83058304
/* multiroot_scheme= */ "foo");
83068305
EXPECT_VALID(lib);
@@ -8340,8 +8339,7 @@ TEST_CASE(DartAPI_Multiroot_FailWhenUriIsWrong) {
83408339
int sourcefiles_count = sizeof(sourcefiles) / sizeof(Dart_SourceFile);
83418340
lib = TestCase::LoadTestScriptWithDFE(
83428341
sourcefiles_count, sourcefiles, nullptr, /* finalize= */ true,
8343-
/* incrementally= */ true, /* allow_compile_errors= */ false,
8344-
"foo1:///main.dart",
8342+
/* incrementally= */ true, "foo1:///main.dart",
83458343
/* multiroot_filepaths= */ "/bar,/baz",
83468344
/* multiroot_scheme= */ "foo");
83478345
EXPECT_ERROR(lib,

runtime/vm/isolate_reload.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,6 @@ char* IsolateGroupReloadContext::CompileToKernel(bool force_reload,
11841184
/*multiroot_scheme=*/nullptr);
11851185
}
11861186
if (retval.status != Dart_KernelCompilationStatus_Ok) {
1187-
if (retval.kernel != nullptr) {
1188-
free(retval.kernel);
1189-
}
11901187
return retval.error;
11911188
}
11921189
*kernel_buffer = retval.kernel;

runtime/vm/isolate_reload_test.cc

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -614,17 +614,16 @@ TEST_CASE(IsolateReload_LibraryImportAdded) {
614614
" return max(3, 4);\n"
615615
"}\n";
616616

617-
const char* kReloadScript =
617+
const char* kScript2 =
618618
"import 'dart:math';\n"
619619
"main() {\n"
620620
" return max(3, 4);\n"
621621
"}\n";
622622

623-
Dart_Handle lib = TestCase::LoadTestScriptWithErrors(kScript);
624-
EXPECT_VALID(lib);
625-
EXPECT_ERROR(SimpleInvokeError(lib, "main"), "max");
623+
Dart_Handle lib = TestCase::LoadTestScript(kScript, nullptr);
624+
EXPECT_ERROR(lib, "Compilation failed");
626625

627-
lib = TestCase::ReloadTestScript(kReloadScript);
626+
lib = TestCase::LoadTestScript(kScript2, nullptr);
628627
EXPECT_VALID(lib);
629628
EXPECT_EQ(4, SimpleInvoke(lib, "main"));
630629
}
@@ -1124,8 +1123,8 @@ TEST_CASE(IsolateReload_LibraryHide) {
11241123
const char* kImportScript = "importedFunc() => 'a';\n";
11251124
TestCase::AddTestLib("test:lib1", kImportScript);
11261125

1127-
// Import 'test:lib1' with importedFunc hidden. Will result in an
1128-
// error.
1126+
// Import 'test:lib1' with importedFunc hidden. Will result in a
1127+
// compile-time error.
11291128
const char* kScript =
11301129
"import 'test:lib1' hide importedFunc;\n"
11311130
"main() {\n"
@@ -1134,18 +1133,17 @@ TEST_CASE(IsolateReload_LibraryHide) {
11341133

11351134
// Dart_Handle result;
11361135

1137-
Dart_Handle lib = TestCase::LoadTestScriptWithErrors(kScript);
1138-
EXPECT_VALID(lib);
1139-
EXPECT_ERROR(SimpleInvokeError(lib, "main"), "importedFunc");
1136+
Dart_Handle lib = TestCase::LoadTestScript(kScript, nullptr);
1137+
EXPECT_ERROR(lib, "Compilation failed");
11401138

11411139
// Import 'test:lib1'.
1142-
const char* kReloadScript =
1140+
const char* kScript2 =
11431141
"import 'test:lib1';\n"
11441142
"main() {\n"
11451143
" return importedFunc();\n"
11461144
"}\n";
11471145

1148-
lib = TestCase::ReloadTestScript(kReloadScript);
1146+
lib = TestCase::LoadTestScript(kScript2, nullptr);
11491147
EXPECT_VALID(lib);
11501148
EXPECT_STREQ("a", SimpleInvokeStr(lib, "main"));
11511149
}
@@ -1157,7 +1155,7 @@ TEST_CASE(IsolateReload_LibraryShow) {
11571155
TestCase::AddTestLib("test:lib1", kImportScript);
11581156

11591157
// Import 'test:lib1' with importedIntFunc visible. Will result in
1160-
// an error when 'main' is invoked.
1158+
// a compile-time error.
11611159
const char* kScript =
11621160
"import 'test:lib1' show importedIntFunc;\n"
11631161
"main() {\n"
@@ -1168,17 +1166,12 @@ TEST_CASE(IsolateReload_LibraryShow) {
11681166
" return importedIntFunc();\n"
11691167
"}\n";
11701168

1171-
Dart_Handle lib = TestCase::LoadTestScriptWithErrors(kScript);
1172-
EXPECT_VALID(lib);
1173-
1174-
// Works.
1175-
EXPECT_EQ(4, SimpleInvoke(lib, "mainInt"));
1176-
// Results in an error.
1177-
EXPECT_ERROR(SimpleInvokeError(lib, "main"), "importedFunc");
1169+
Dart_Handle lib = TestCase::LoadTestScript(kScript, nullptr);
1170+
EXPECT_ERROR(lib, "Compilation failed");
11781171

11791172
// Import 'test:lib1' with importedFunc visible. Will result in
1180-
// an error when 'mainInt' is invoked.
1181-
const char* kReloadScript =
1173+
// a compile-time error.
1174+
const char* kScript2 =
11821175
"import 'test:lib1' show importedFunc;\n"
11831176
"main() {\n"
11841177
" return importedFunc();\n"
@@ -1188,8 +1181,24 @@ TEST_CASE(IsolateReload_LibraryShow) {
11881181
" return importedIntFunc();\n"
11891182
"}\n";
11901183

1191-
lib = TestCase::ReloadTestScript(kReloadScript);
1192-
EXPECT_ERROR(lib, "importedIntFunc");
1184+
lib = TestCase::LoadTestScript(kScript2, nullptr);
1185+
EXPECT_ERROR(lib, "Compilation failed");
1186+
1187+
// Both imports 'test:lib1' are visible.
1188+
// Should be successful.
1189+
const char* kScript3 =
1190+
"import 'test:lib1' show importedFunc, importedIntFunc;\n"
1191+
"main() {\n"
1192+
" return importedFunc();\n"
1193+
"}\n"
1194+
"@pragma('vm:entry-point', 'call')\n"
1195+
"mainInt() {\n"
1196+
" return importedIntFunc();\n"
1197+
"}\n";
1198+
1199+
lib = TestCase::LoadTestScript(kScript3, nullptr);
1200+
EXPECT_VALID(lib);
1201+
EXPECT_EQ(4, SimpleInvoke(lib, "mainInt"));
11931202
}
11941203

11951204
// Verifies that we clear the ICs for the functions live on the stack in a way

runtime/vm/kernel_isolate.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,6 @@ class KernelCompilationRequest : public ValueObject {
988988
if (result_.status == Dart_KernelCompilationStatus_Ok) {
989989
LoadKernelFromResponse(response[1]);
990990
} else {
991-
if (result_.status == Dart_KernelCompilationStatus_Error) {
992-
LoadKernelFromResponse(response[2]);
993-
}
994991
// This is an error.
995992
ASSERT(response[1]->type == Dart_CObject_kString);
996993
result_.error = Utils::StrDup(response[1]->value.as_string);

runtime/vm/source_report_test.cc

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ namespace dart {
1010

1111
#ifndef PRODUCT
1212

13-
static ObjectPtr ExecuteScript(const char* script, bool allow_errors = false) {
13+
static ObjectPtr ExecuteScript(const char* script) {
1414
Dart_Handle lib;
1515
{
1616
TransitionVMToNative transition(Thread::Current());
17-
if (allow_errors) {
18-
lib = TestCase::LoadTestScriptWithErrors(script, nullptr);
19-
} else {
20-
lib = TestCase::LoadTestScript(script, nullptr);
21-
}
17+
lib = TestCase::LoadTestScript(script, nullptr);
2218
EXPECT_VALID(lib);
2319
Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, nullptr);
2420
EXPECT_VALID(result);
@@ -315,59 +311,6 @@ ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_UnusedClass_ForceCompile) {
315311
buffer);
316312
}
317313

318-
ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_UnusedClass_ForceCompileError) {
319-
// WARNING: This MUST be big enough for the serialized JSON string.
320-
const int kBufferSize = 1024;
321-
char buffer[kBufferSize];
322-
const char* kScript =
323-
"helper0() {}\n"
324-
"class Unused {\n"
325-
" helper1() { helper0()+ }\n" // syntax error
326-
"}\n"
327-
"main() {\n"
328-
" helper0();\n"
329-
"}";
330-
331-
Library& lib = Library::Handle();
332-
lib ^= ExecuteScript(kScript, true);
333-
ASSERT(!lib.IsNull());
334-
const Script& script =
335-
Script::Handle(lib.LookupScript(String::Handle(String::New("test-lib"))));
336-
337-
SourceReport report(SourceReport::kCoverage, SourceReport::kForceCompile);
338-
JSONStream js;
339-
js.set_id_zone(thread->isolate()->EnsureDefaultServiceIdZone());
340-
report.PrintJSON(&js, script);
341-
const char* json_str = js.ToCString();
342-
ASSERT(strlen(json_str) < kBufferSize);
343-
ElideJSONSubstring("classes", json_str, buffer);
344-
ElideJSONSubstring("libraries", buffer, buffer);
345-
EXPECT_STREQ(
346-
"{\"type\":\"SourceReport\",\"ranges\":["
347-
348-
// UnusedClass has a syntax error.
349-
"{\"scriptIndex\":0,\"startPos\":30,\"endPos\":53,\"compiled\":false,"
350-
"\"error\":{\"type\":\"@Error\",\"_vmType\":\"LanguageError\","
351-
"\"kind\":\"LanguageError\",\"id\":\"objects\\/0\\/0\","
352-
"\"message\":\"'file:\\/\\/\\/test-lib': error: "
353-
"\\/test-lib:3:26: "
354-
"Error: This couldn't be parsed.\\n"
355-
" helper1() { helper0()+ }\\n ^\"}},"
356-
357-
// helper0 is compiled.
358-
"{\"scriptIndex\":0,\"startPos\":0,\"endPos\":11,\"compiled\":true,"
359-
"\"coverage\":{\"hits\":[0],\"misses\":[]}},"
360-
361-
// One range with two hits (main).
362-
"{\"scriptIndex\":0,\"startPos\":57,\"endPos\":79,\"compiled\":true,"
363-
"\"coverage\":{\"hits\":[57,68],\"misses\":[]}}],"
364-
365-
// Only one script in the script table.
366-
"\"scripts\":[{\"type\":\"@Script\",\"fixedId\":true,\"id\":\"\","
367-
"\"uri\":\"file:\\/\\/\\/test-lib\",\"_kind\":\"kernel\"}]}",
368-
buffer);
369-
}
370-
371314
ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_LibrariesAlreadyCompiled) {
372315
// WARNING: This MUST be big enough for the serialized JSON string.
373316
const int kBufferSize = 1024;

0 commit comments

Comments
 (0)