From dbcf23a5ed935ab67659930a526c3e2aea6c60fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 31 May 2024 07:12:21 +0000 Subject: [PATCH] [dart2wasm] Disallow dart:ffi in user code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1d99637e4538a183d8fa567399bfb7c55675e60e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368568 Reviewed-by: Martin Kustermann Reviewed-by: Srujan Gaddam Commit-Queue: Ömer Ağacan --- .../lib/src/messages/codes_generated.dart | 10 ++++++++++ .../lib/js_interop_checks.dart | 16 +++++++++------- pkg/front_end/messages.status | 4 +++- pkg/front_end/messages.yaml | 5 ++++- .../disallowed_interop_libraries_test.dart | 4 ++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart b/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart index d1a71cd320b1..5654df8a14fe 100644 --- a/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart +++ b/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart @@ -2742,6 +2742,16 @@ Message _withArgumentsCyclicTypedef(String name) { ); } +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const Code codeDartFfiLibraryInDart2Wasm = + messageDartFfiLibraryInDart2Wasm; + +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const MessageCode messageDartFfiLibraryInDart2Wasm = const MessageCode( + "DartFfiLibraryInDart2Wasm", + problemMessage: r"""'dart:ffi' can't be imported when compiling to Wasm.""", +); + // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. const Template templateDebugTrace = diff --git a/pkg/_js_interop_checks/lib/js_interop_checks.dart b/pkg/_js_interop_checks/lib/js_interop_checks.dart index 8a7d35555ed4..52306374294e 100644 --- a/pkg/_js_interop_checks/lib/js_interop_checks.dart +++ b/pkg/_js_interop_checks/lib/js_interop_checks.dart @@ -8,6 +8,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart' show Message, LocatedMessage, + messageDartFfiLibraryInDart2Wasm, messageJsInteropDartJsInteropAnnotationForStaticInteropOnly, messageJsInteropEnclosingClassJSAnnotation, messageJsInteropEnclosingClassJSAnnotationContext, @@ -134,7 +135,8 @@ class JsInteropChecks extends RecursiveVisitor { 'package:js/js.dart', 'package:js/js_util.dart', 'dart:js_util', - 'dart:js' + 'dart:js', + 'dart:ffi', ]; /// Libraries that use `external` to exclude from checks on external. @@ -550,12 +552,12 @@ class JsInteropChecks extends RecursiveVisitor { _allowedUseOfDart2WasmDisallowedInteropLibrariesTestPatterns .any((pattern) => uri.path.contains(pattern)); if (allowedToImport) return; - _reporter.report( - templateJsInteropDisallowedInteropLibraryInDart2Wasm - .withArguments(dependencyUriString), - dependency.fileOffset, - dependencyUriString.length, - node.fileUri); + final message = dependencyUriString == 'dart:ffi' + ? messageDartFfiLibraryInDart2Wasm + : templateJsInteropDisallowedInteropLibraryInDart2Wasm + .withArguments(dependencyUriString); + _reporter.report(message, dependency.fileOffset, + dependencyUriString.length, node.fileUri); } } } diff --git a/pkg/front_end/messages.status b/pkg/front_end/messages.status index dd218829e718..0fc61fd51f29 100644 --- a/pkg/front_end/messages.status +++ b/pkg/front_end/messages.status @@ -188,6 +188,8 @@ CyclicRedirectingFactoryConstructors/part_wrapped_script: Fail # Error is report CyclicRedirectingFactoryConstructors/script: Fail # Error is reported for each factory involved in the cycle. CyclicRepresentationDependency/analyzerCode: Fail CyclicTypedef/example: Fail +DartFfiLibraryInDart2Wasm/analyzerCode: Fail +DartFfiLibraryInDart2Wasm/example: Fail DeferredAfterPrefix/example: Fail DeferredExtensionImport/analyzerCode: Fail DeferredExtensionImport/part_wrapped_script: Fail @@ -1136,4 +1138,4 @@ YieldNotGenerator/example: Fail # TODO(johnniwinther): Remove these. ExternalField/example: Fail UnexpectedModifierInNonNnbd/example: Fail -SwitchExpressionNotAssignable/example: Fail \ No newline at end of file +SwitchExpressionNotAssignable/example: Fail diff --git a/pkg/front_end/messages.yaml b/pkg/front_end/messages.yaml index ca1acd4dabb9..d5d7dfe297ec 100644 --- a/pkg/front_end/messages.yaml +++ b/pkg/front_end/messages.yaml @@ -7355,4 +7355,7 @@ ExperimentExpiredEnabled: problemMessage: "The experiment '#name' has expired and can't be enabled." ExperimentExpiredDisabled: - problemMessage: "The experiment '#name' has expired and can't be disabled." \ No newline at end of file + problemMessage: "The experiment '#name' has expired and can't be disabled." + +DartFfiLibraryInDart2Wasm: + problemMessage: "'dart:ffi' can't be imported when compiling to Wasm." diff --git a/tests/lib/js/static_interop_test/disallowed_interop_libraries_test.dart b/tests/lib/js/static_interop_test/disallowed_interop_libraries_test.dart index 37cb8bc4e8f5..a8f6c4b9f54c 100644 --- a/tests/lib/js/static_interop_test/disallowed_interop_libraries_test.dart +++ b/tests/lib/js/static_interop_test/disallowed_interop_libraries_test.dart @@ -20,4 +20,8 @@ // ^ // [web] JS interop library 'package:js/js_util.dart' can't be imported when compiling to Wasm. +/**/ import 'dart:ffi'; +// ^ +// [web] 'dart:ffi' can't be imported when compiling to Wasm. + void main() {}