Skip to content

Commit ac2cd2d

Browse files
lfkdskcommit-bot@chromium.org
authored andcommitted
[vm/ffi] Array dimensions non-positive check in CFE
Bug: dart-lang/sdk#45540 Closes: dart-lang/sdk#45785 TEST=tests/ffi/vmspecific_static_checks_test.dart GitOrigin-RevId: e4c091a617acf36f60de7ddfc957a58323663813 Change-Id: I393e1dbbf34aa9f72e2e942c3a40c2f37c90e0c4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196246 Reviewed-by: Daco Harkes <dacoharkes@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
1 parent 8713744 commit ac2cd2d

File tree

7 files changed

+76
-7
lines changed

7 files changed

+76
-7
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7276,6 +7276,15 @@ const MessageCode messageNonPartOfDirectiveInPart = const MessageCode(
72767276
tip:
72777277
r"""Try removing the other directives, or moving them to the library for which this is a part.""");
72787278

7279+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
7280+
const Code<Null> codeNonPositiveArrayDimensions =
7281+
messageNonPositiveArrayDimensions;
7282+
7283+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
7284+
const MessageCode messageNonPositiveArrayDimensions = const MessageCode(
7285+
"NonPositiveArrayDimensions",
7286+
message: r"""Array dimensions must be positive numbers.""");
7287+
72797288
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
72807289
const Template<Message Function(String name)>
72817290
templateNonSimpleBoundViaReference =

pkg/front_end/lib/src/api_unstable/vm.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export '../fasta/fasta_codes.dart'
5353
messageFfiExceptionalReturnNull,
5454
messageFfiExpectedConstant,
5555
messageFfiPackedAnnotationAlignment,
56+
messageNonPositiveArrayDimensions,
5657
noLength,
5758
templateFfiDartTypeMismatch,
5859
templateFfiEmptyStruct,

pkg/front_end/messages.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ NonNullableOptOutImplicit/analyzerCode: Fail
606606
NonNullableOptOutImplicit/example: Fail
607607
NonPartOfDirectiveInPart/part_wrapped_script1: Fail
608608
NonPartOfDirectiveInPart/script1: Fail
609+
NonPositiveArrayDimensions/analyzerCode: Fail
609610
NotAConstantExpression/example: Fail
610611
NotAType/example: Fail
611612
NotAnLvalue/example: Fail

pkg/front_end/messages.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4646,6 +4646,11 @@ NonNullAwareSpreadIsNull:
46464646
<int>[...null];
46474647
}
46484648
4649+
NonPositiveArrayDimensions:
4650+
# Used by dart:ffi
4651+
template: "Array dimensions must be positive numbers."
4652+
external: test/ffi_test.dart
4653+
46494654
InvalidTypeVariableInSupertype:
46504655
template: "Can't use implicitly 'out' variable '#name' in an '#string2' position in supertype '#name2'."
46514656
script: >

pkg/vm/lib/transformations/ffi_definitions.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:math' as math;
99
import 'package:front_end/src/api_unstable/vm.dart'
1010
show
1111
messageFfiPackedAnnotationAlignment,
12+
messageNonPositiveArrayDimensions,
1213
templateFfiEmptyStruct,
1314
templateFfiFieldAnnotation,
1415
templateFfiFieldNull,
@@ -323,14 +324,24 @@ class _FfiDefinitionTransformer extends FfiTransformer {
323324
compoundClassDependencies[node].add(clazz);
324325
_checkPacking(node, packing, clazz, f);
325326
}
326-
if (arrayDimensions(type) != sizeAnnotations.single.length) {
327+
final dimensions = sizeAnnotations.single;
328+
if (arrayDimensions(type) != dimensions.length) {
327329
diagnosticReporter.report(
328330
templateFfiSizeAnnotationDimensions
329331
.withArguments(f.name.text),
330332
f.fileOffset,
331333
f.name.text.length,
332334
f.fileUri);
333335
}
336+
for (var dimension in dimensions) {
337+
if (dimension < 0) {
338+
diagnosticReporter.report(
339+
messageNonPositiveArrayDimensions, f.fileOffset,
340+
f.name.text.length,
341+
f.fileUri);
342+
success = false;
343+
}
344+
}
334345
} else {
335346
diagnosticReporter.report(
336347
templateFfiSizeAnnotation.withArguments(f.name.text),
@@ -478,8 +489,8 @@ class _FfiDefinitionTransformer extends FfiTransformer {
478489
final sizeAnnotations = _getArraySizeAnnotations(m).toList();
479490
if (sizeAnnotations.length == 1) {
480491
final arrayDimensions = sizeAnnotations.single;
481-
type = NativeTypeCfe(this, dartType,
482-
compoundCache: compoundCache, arrayDimensions: arrayDimensions);
492+
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache,
493+
arrayDimensions: arrayDimensions);
483494
}
484495
} else if (isPointerType(dartType) || isCompoundSubtype(dartType)) {
485496
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache);
@@ -749,7 +760,7 @@ class CompoundLayout {
749760
abstract class NativeTypeCfe {
750761
factory NativeTypeCfe(FfiTransformer transformer, DartType dartType,
751762
{List<int> arrayDimensions,
752-
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
763+
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
753764
if (transformer.isPrimitiveType(dartType)) {
754765
final clazz = (dartType as InterfaceType).classNode;
755766
final nativeType = transformer.getType(clazz);
@@ -772,7 +783,7 @@ abstract class NativeTypeCfe {
772783
}
773784
final elementType = transformer.arraySingleElementType(dartType);
774785
final elementCfeType =
775-
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
786+
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
776787
return ArrayNativeTypeCfe.multi(elementCfeType, arrayDimensions);
777788
}
778789
throw "Invalid type $dartType";
@@ -1135,8 +1146,8 @@ class ArrayNativeTypeCfe implements NativeTypeCfe {
11351146

11361147
ArrayNativeTypeCfe(this.elementType, this.length);
11371148

1138-
factory ArrayNativeTypeCfe.multi(
1139-
NativeTypeCfe elementType, List<int> dimensions) {
1149+
factory ArrayNativeTypeCfe.multi(NativeTypeCfe elementType,
1150+
List<int> dimensions) {
11401151
if (dimensions.length == 1) {
11411152
return ArrayNativeTypeCfe(elementType, dimensions.single);
11421153
}

tests/ffi/vmspecific_static_checks_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,24 @@ class TestStruct1606Packed extends Struct {
760760
external Array<TestStruct1604> //# 1606: compile-time error
761761
nestedLooselyPacked; //# 1606: compile-time error
762762
}
763+
764+
class TestStruct1800 extends Struct {
765+
external Pointer<Uint8> notEmpty;
766+
767+
@Array(-1) //# 1800: compile-time error
768+
external Array<Uint8> inlineArray; //# 1800: compile-time error
769+
}
770+
771+
class TestStruct1801 extends Struct {
772+
external Pointer<Uint8> notEmpty;
773+
774+
@Array(1, -1) //# 1801: compile-time error
775+
external Array<Uint8> inlineArray; //# 1801: compile-time error
776+
}
777+
778+
class TestStruct1802 extends Struct {
779+
external Pointer<Uint8> notEmpty;
780+
781+
@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
782+
external Array<Uint8> inlineArray; //# 1802: compile-time error
783+
}

tests/ffi_2/vmspecific_static_checks_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,24 @@ class TestStruct1606Packed extends Struct {
759759
@Array(2) //# 1606: compile-time error
760760
Array<TestStruct1604> nestedLooselyPacked; //# 1606: compile-time error
761761
}
762+
763+
class TestStruct1800 extends Struct {
764+
Pointer<Uint8> notEmpty;
765+
766+
@Array(-1) //# 1800: compile-time error
767+
Array<Uint8> inlineArray; //# 1800: compile-time error
768+
}
769+
770+
class TestStruct1801 extends Struct {
771+
Pointer<Uint8> notEmpty;
772+
773+
@Array(1, -1) //# 1801: compile-time error
774+
Array<Uint8> inlineArray; //# 1801: compile-time error
775+
}
776+
777+
class TestStruct1802 extends Struct {
778+
Pointer<Uint8> notEmpty;
779+
780+
@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
781+
Array<Uint8> inlineArray; //# 1802: compile-time error
782+
}

0 commit comments

Comments
 (0)