Skip to content

Commit 8c833a7

Browse files
bkonyisuryaKommana2662
authored andcommitted
[flutter_tools] Guard against null vmService and uiIsolate in debugDumpFocusTree and debugDumpLayerTree (flutter#192940)
## Description In `ResidentHandlers.debugDumpFocusTree` and `ResidentHandlers.debugDumpLayerTree`, force unwrapping `device.vmService` and `view.uiIsolate.id` caused a null check operator exception when the VM service was null or when a `FlutterView` had no attached UI isolate (e.g., during initialization or teardown). This change safely guards `device.vmService` and `view.uiIsolate?.id` before invoking the respective VM service methods. ## Related Issue Fixes flutter#192934 ## Tests Adds unit tests in `packages/flutter_tools/test/general.shard/terminal_handler_test.dart` verifying that `debugDumpFocusTree` and `debugDumpLayerTree` do not crash when `vmService` or `uiIsolate` is null.
1 parent 98a4d30 commit 8c833a7

2 files changed

Lines changed: 133 additions & 74 deletions

File tree

‎packages/flutter_tools/lib/src/resident_runner.dart‎

Lines changed: 43 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -611,107 +611,76 @@ abstract class ResidentHandlers {
611611
}
612612

613613
/// Dump the application's current widget tree to the terminal.
614-
Future<bool> debugDumpApp() async {
615-
if (!supportsServiceProtocol) {
616-
return false;
617-
}
618-
for (final FlutterDevice? device in flutterDevices) {
619-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
620-
for (final view in views) {
621-
final String data = await device.vmService!.flutterDebugDumpApp(
622-
isolateId: view.uiIsolate!.id!,
623-
);
624-
logger.printStatus(data);
625-
}
626-
}
627-
return true;
628-
}
614+
Future<bool> debugDumpApp() => _debugDumpTree(
615+
(FlutterVmService vmService, String isolateId) =>
616+
vmService.flutterDebugDumpApp(isolateId: isolateId),
617+
);
629618

630619
/// Dump the application's current render tree to the terminal.
631-
Future<bool> debugDumpRenderTree() async {
632-
if (!supportsServiceProtocol) {
633-
return false;
634-
}
635-
for (final FlutterDevice? device in flutterDevices) {
636-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
637-
for (final view in views) {
638-
final String data = await device.vmService!.flutterDebugDumpRenderTree(
639-
isolateId: view.uiIsolate!.id!,
640-
);
641-
logger.printStatus(data);
642-
}
643-
}
644-
return true;
645-
}
620+
Future<bool> debugDumpRenderTree() => _debugDumpTree(
621+
(FlutterVmService vmService, String isolateId) =>
622+
vmService.flutterDebugDumpRenderTree(isolateId: isolateId),
623+
);
646624

647625
/// Dump the application's current layer tree to the terminal.
648626
Future<bool> debugDumpLayerTree() async {
649-
if (!supportsServiceProtocol || !isRunningDebug) {
627+
if (!isRunningDebug) {
650628
return false;
651629
}
652-
for (final FlutterDevice? device in flutterDevices) {
653-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
654-
for (final view in views) {
655-
final String data = await device.vmService!.flutterDebugDumpLayerTree(
656-
isolateId: view.uiIsolate!.id!,
657-
);
658-
logger.printStatus(data);
659-
}
660-
}
661-
return true;
630+
return _debugDumpTree(
631+
(FlutterVmService vmService, String isolateId) =>
632+
vmService.flutterDebugDumpLayerTree(isolateId: isolateId),
633+
);
662634
}
663635

636+
/// Dump the application's current focus tree to the terminal.
664637
Future<bool> debugDumpFocusTree() async {
665-
if (!supportsServiceProtocol || !isRunningDebug) {
638+
if (!isRunningDebug) {
666639
return false;
667640
}
668-
for (final FlutterDevice? device in flutterDevices) {
669-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
670-
for (final view in views) {
671-
final String data = await device.vmService!.flutterDebugDumpFocusTree(
672-
isolateId: view.uiIsolate!.id!,
673-
);
674-
logger.printStatus(data);
675-
}
676-
}
677-
return true;
641+
return _debugDumpTree(
642+
(FlutterVmService vmService, String isolateId) =>
643+
vmService.flutterDebugDumpFocusTree(isolateId: isolateId),
644+
);
678645
}
679646

680647
/// Dump the application's current semantics tree to the terminal.
681648
///
682649
/// If semantics are not enabled, nothing is returned.
683-
Future<bool> debugDumpSemanticsTreeInTraversalOrder() async {
684-
if (!supportsServiceProtocol) {
685-
return false;
686-
}
687-
for (final FlutterDevice? device in flutterDevices) {
688-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
689-
for (final view in views) {
690-
final String data = await device.vmService!.flutterDebugDumpSemanticsTreeInTraversalOrder(
691-
isolateId: view.uiIsolate!.id!,
692-
);
693-
logger.printStatus(data);
694-
}
695-
}
696-
return true;
697-
}
650+
Future<bool> debugDumpSemanticsTreeInTraversalOrder() => _debugDumpTree(
651+
(FlutterVmService vmService, String isolateId) =>
652+
vmService.flutterDebugDumpSemanticsTreeInTraversalOrder(isolateId: isolateId),
653+
);
698654

699655
/// Dump the application's current semantics tree to the terminal.
700656
///
701657
/// If semantics are not enabled, nothing is returned.
702-
Future<bool> debugDumpSemanticsTreeInInverseHitTestOrder() async {
658+
Future<bool> debugDumpSemanticsTreeInInverseHitTestOrder() => _debugDumpTree(
659+
(FlutterVmService vmService, String isolateId) =>
660+
vmService.flutterDebugDumpSemanticsTreeInInverseHitTestOrder(isolateId: isolateId),
661+
);
662+
663+
/// Returns whether at least one Flutter view was dumped.
664+
Future<bool> _debugDumpTree(
665+
Future<String> Function(FlutterVmService vmService, String isolateId) dumpCall,
666+
) async {
703667
if (!supportsServiceProtocol) {
704668
return false;
705669
}
670+
var dumped = false;
706671
for (final FlutterDevice? device in flutterDevices) {
707-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
708-
for (final view in views) {
709-
final String data = await device.vmService!
710-
.flutterDebugDumpSemanticsTreeInInverseHitTestOrder(isolateId: view.uiIsolate!.id!);
711-
logger.printStatus(data);
672+
if (device?.vmService case final vmService?) {
673+
final List<FlutterView> views = await vmService.getFlutterViews();
674+
for (final view in views) {
675+
if (view.uiIsolate?.id case final isolateId?) {
676+
final String data = await dumpCall(vmService, isolateId);
677+
logger.printStatus(data);
678+
dumped = true;
679+
}
680+
}
712681
}
713682
}
714-
return true;
683+
return dumped;
715684
}
716685

717686
/// Toggle the "paint size" debugging feature.

‎packages/flutter_tools/test/general.shard/terminal_handler_test.dart‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ final listViews = FakeVmServiceRequest(
5555
},
5656
);
5757

58+
final fakeFlutterViewWithoutIsolate = FlutterView(id: 'a', uiIsolate: null);
59+
60+
final listViewsWithoutIsolate = FakeVmServiceRequest(
61+
method: kListViewsMethod,
62+
jsonResponse: <String, Object>{
63+
'views': <Object>[fakeFlutterViewWithoutIsolate.toJson()],
64+
},
65+
);
66+
5867
void main() {
5968
testWithoutContext('keyboard input handling single help character', () async {
6069
final testRunner = TestRunner();
@@ -415,6 +424,87 @@ void main() {
415424
await terminalHandler.processTerminalInput('f');
416425
});
417426

427+
testWithoutContext('debugDump* with null vmService does not crash', () async {
428+
final TerminalHandler terminalHandler = setUpTerminalHandler(
429+
<FakeVmServiceRequest>[],
430+
nullVmService: true,
431+
);
432+
expect(await terminalHandler.residentRunner.debugDumpApp(), isFalse);
433+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isFalse);
434+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
435+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
436+
expect(
437+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
438+
isFalse,
439+
);
440+
expect(
441+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
442+
isFalse,
443+
);
444+
});
445+
446+
testWithoutContext('debugDump* with null uiIsolate does not crash', () async {
447+
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
448+
for (var i = 0; i < 6; i++) listViewsWithoutIsolate,
449+
]);
450+
expect(await terminalHandler.residentRunner.debugDumpApp(), isFalse);
451+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isFalse);
452+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
453+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
454+
expect(
455+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
456+
isFalse,
457+
);
458+
expect(
459+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
460+
isFalse,
461+
);
462+
});
463+
464+
testWithoutContext(
465+
'debugDump* in profile mode dumps supported trees and skips debug-only trees',
466+
() async {
467+
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
468+
listViews,
469+
const FakeVmServiceRequest(
470+
method: 'ext.flutter.debugDumpApp',
471+
args: <String, Object>{'isolateId': '1'},
472+
jsonResponse: <String, Object>{'data': 'WIDGET DATA'},
473+
),
474+
listViews,
475+
const FakeVmServiceRequest(
476+
method: 'ext.flutter.debugDumpRenderTree',
477+
args: <String, Object>{'isolateId': '1'},
478+
jsonResponse: <String, Object>{'data': 'RENDER DATA'},
479+
),
480+
listViews,
481+
const FakeVmServiceRequest(
482+
method: 'ext.flutter.debugDumpSemanticsTreeInTraversalOrder',
483+
args: <String, Object>{'isolateId': '1'},
484+
jsonResponse: <String, Object>{'data': 'SEMANTICS DATA 1'},
485+
),
486+
listViews,
487+
const FakeVmServiceRequest(
488+
method: 'ext.flutter.debugDumpSemanticsTreeInInverseHitTestOrder',
489+
args: <String, Object>{'isolateId': '1'},
490+
jsonResponse: <String, Object>{'data': 'SEMANTICS DATA 2'},
491+
),
492+
], buildMode: BuildMode.profile);
493+
expect(await terminalHandler.residentRunner.debugDumpApp(), isTrue);
494+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isTrue);
495+
expect(
496+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
497+
isTrue,
498+
);
499+
expect(
500+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
501+
isTrue,
502+
);
503+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
504+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
505+
},
506+
);
507+
418508
testWithoutContext('o,O - debugTogglePlatform', () async {
419509
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
420510
// Request 1.

0 commit comments

Comments
 (0)