Skip to content

Commit 1381832

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[beta] [analysis_server] Ensure plugin protocol classes only use types valid for Isolate.send()
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/273200 Bug: #50594 Change-Id: I8f1fac9cb95a638408ecaa7751f02ebe8d427d79 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274200 Commit-Queue: Kevin Chisholm <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9571327 commit 1381832

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

pkg/analysis_server/lib/src/protocol/protocol_internal.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool mapEqual<K, V>(
7676
/// [valueCallback] to all its values.
7777
Map<KR, VR> mapMap<KP, VP, KR, VR>(Map<KP, VP> map,
7878
{KR Function(KP key)? keyCallback, VR Function(VP value)? valueCallback}) {
79-
Map<KR, VR> result = HashMap<KR, VR>();
79+
var result = <KR, VR>{};
8080
map.forEach((key, value) {
8181
KR resultKey;
8282
VR resultValue;

pkg/analysis_server_client/lib/src/protocol/protocol_internal.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool mapEqual<K, V>(
142142
/// [valueCallback] to all its values.
143143
Map<KR, VR> mapMap<KP, VP, KR, VR>(Map<KP, VP> map,
144144
{KR Function(KP key)? keyCallback, VR Function(VP value)? valueCallback}) {
145-
Map<KR, VR> result = HashMap<KR, VR>();
145+
var result = <KR, VR>{};
146146
map.forEach((key, value) {
147147
KR resultKey;
148148
VR resultValue;

pkg/analyzer_plugin/lib/src/protocol/protocol_internal.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool mapEqual<K, V>(
174174
/// [valueCallback] to all its values.
175175
Map<KR, VR> mapMap<KP, VP, KR, VR>(Map<KP, VP> map,
176176
{KR Function(KP key)? keyCallback, VR Function(VP value)? valueCallback}) {
177-
Map<KR, VR> result = HashMap<KR, VR>();
177+
var result = <KR, VR>{};
178178
map.forEach((key, value) {
179179
KR resultKey;
180180
VR resultValue;

pkg/analyzer_plugin/test/plugin/plugin_test.dart

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
6+
import 'dart:isolate';
7+
58
import 'package:analyzer/dart/analysis/analysis_context.dart';
69
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
710
import 'package:analyzer/file_system/file_system.dart';
@@ -11,6 +14,7 @@ import 'package:analyzer_plugin/plugin/plugin.dart';
1114
import 'package:analyzer_plugin/protocol/protocol.dart';
1215
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1316
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
17+
import 'package:analyzer_plugin/src/protocol/protocol_internal.dart';
1418
import 'package:meta/meta.dart';
1519
import 'package:pub_semver/pub_semver.dart';
1620
import 'package:test/test.dart';
@@ -64,6 +68,31 @@ class ServerPluginTest extends AbstractPluginTest {
6468
late String filePath2;
6569
late ContextRoot contextRoot2;
6670

71+
/// Asserts that [params] is valid to send to an [Isolate] started with
72+
/// [Isolate.spawnUri].
73+
Future<void> assertValidForIsolateSend(RequestParams params) async {
74+
const isolateSource = r'''
75+
import 'dart:isolate';
76+
77+
void main(List<String> args, SendPort sendPort) {
78+
final receivePort = ReceivePort();
79+
sendPort.send(receivePort.sendPort);
80+
receivePort.listen((msg) => sendPort.send('ECHO: $msg'));
81+
}
82+
''';
83+
final isolateUri = Uri.dataFromString(isolateSource, encoding: utf8);
84+
final receivePort = ReceivePort();
85+
final isolate =
86+
await Isolate.spawnUri(isolateUri, [], receivePort.sendPort);
87+
final sendPort = (await receivePort.first) as SendPort;
88+
try {
89+
sendPort.send(params.toJson());
90+
} catch (e) {
91+
fail('Failed to send ${params.runtimeType} across Isolate: $e');
92+
}
93+
isolate.kill();
94+
}
95+
6796
@override
6897
ServerPlugin createPlugin() {
6998
return _TestServerPlugin(resourceProvider);
@@ -229,6 +258,78 @@ class ServerPluginTest extends AbstractPluginTest {
229258
fail('Not yet implemented.');
230259
}
231260

261+
Future<void> test_isolateSend_analysisGetNavigation() async {
262+
await assertValidForIsolateSend(AnalysisGetNavigationParams('', 1, 2));
263+
}
264+
265+
Future<void> test_isolateSend_analysisHandleWatchEvents() async {
266+
await assertValidForIsolateSend(AnalysisHandleWatchEventsParams([]));
267+
}
268+
269+
Future<void> test_isolateSend_analysisSetPriorityFiles() async {
270+
await assertValidForIsolateSend(
271+
AnalysisSetPriorityFilesParams([filePath1]));
272+
}
273+
274+
Future<void> test_isolateSend_analysisSetSubscriptions() async {
275+
await assertValidForIsolateSend(AnalysisSetSubscriptionsParams({
276+
AnalysisService.OUTLINE: [filePath1]
277+
}));
278+
}
279+
280+
Future<void> test_isolateSend_analysisUpdateContent_add() async {
281+
await assertValidForIsolateSend(AnalysisUpdateContentParams(
282+
{filePath1: AddContentOverlay('class C {}')}));
283+
}
284+
285+
Future<void> test_isolateSend_analysisUpdateContent_change() async {
286+
await assertValidForIsolateSend(AnalysisUpdateContentParams({
287+
filePath1: ChangeContentOverlay([SourceEdit(7, 0, ' extends Object')])
288+
}));
289+
}
290+
291+
Future<void> test_isolateSend_analysisUpdateContent_remove() async {
292+
await assertValidForIsolateSend(
293+
AnalysisUpdateContentParams({filePath1: RemoveContentOverlay()}));
294+
}
295+
296+
Future<void> test_isolateSend_completionGetSuggestions() async {
297+
await assertValidForIsolateSend(
298+
CompletionGetSuggestionsParams(filePath1, 12));
299+
}
300+
301+
Future<void> test_isolateSend_editGetAssists() async {
302+
await assertValidForIsolateSend(EditGetAssistsParams(filePath1, 10, 0));
303+
}
304+
305+
Future<void> test_isolateSend_editGetAvailableRefactorings() async {
306+
await assertValidForIsolateSend(
307+
EditGetAvailableRefactoringsParams(filePath1, 10, 0));
308+
}
309+
310+
Future<void> test_isolateSend_editGetFixes() async {
311+
await assertValidForIsolateSend(EditGetFixesParams(filePath1, 13));
312+
}
313+
314+
Future<void> test_isolateSend_editGetRefactoring() async {
315+
await assertValidForIsolateSend(EditGetRefactoringParams(
316+
RefactoringKind.RENAME, filePath1, 7, 0, false));
317+
}
318+
319+
Future<void> test_isolateSend_pluginShutdown() async {
320+
await assertValidForIsolateSend(PluginShutdownParams());
321+
}
322+
323+
Future<void> test_isolateSend_pluginVersionCheck() async {
324+
await assertValidForIsolateSend(
325+
PluginVersionCheckParams('byteStorePath', 'sdkPath', '0.1.0'));
326+
}
327+
328+
Future<void> test_isolateSend_setContextRoots() async {
329+
await assertValidForIsolateSend(
330+
AnalysisSetContextRootsParams([contextRoot1]));
331+
}
332+
232333
void test_onDone() {
233334
channel.sendDone();
234335
}

0 commit comments

Comments
 (0)