[dart2wasm] Disallow dart:ffi in user code

Change-Id: I1d99637e4538a183d8fa567399bfb7c55675e60e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368568
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
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 d1a71cd..5654df8 100644
--- a/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart
@@ -2743,6 +2743,16 @@
 }
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const Code<Null> 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<Message Function(String name, String string)>
     templateDebugTrace =
     const Template<Message Function(String name, String string)>(
diff --git a/pkg/_js_interop_checks/lib/js_interop_checks.dart b/pkg/_js_interop_checks/lib/js_interop_checks.dart
index 8a7d355..5230637 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 @@
     show
         Message,
         LocatedMessage,
+        messageDartFfiLibraryInDart2Wasm,
         messageJsInteropDartJsInteropAnnotationForStaticInteropOnly,
         messageJsInteropEnclosingClassJSAnnotation,
         messageJsInteropEnclosingClassJSAnnotationContext,
@@ -134,7 +135,8 @@
     '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 @@
             _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 dd21882..0fc61fd 100644
--- a/pkg/front_end/messages.status
+++ b/pkg/front_end/messages.status
@@ -188,6 +188,8 @@
 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 @@
 # 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 ca1acd4..d5d7dfe 100644
--- a/pkg/front_end/messages.yaml
+++ b/pkg/front_end/messages.yaml
@@ -7355,4 +7355,7 @@
   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 37cb8bc..a8f6c4b 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() {}