[vm/ffi] Array dimensions non-positive check in CFE

Bug: https://github.com/dart-lang/sdk/issues/45540
Closes: https://github.com/dart-lang/sdk/pull/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>
This commit is contained in:
JustWe 2021-04-27 06:56:28 +00:00 committed by commit-bot@chromium.org
parent 871374447f
commit ac2cd2d73c
7 changed files with 76 additions and 7 deletions

View file

@ -7276,6 +7276,15 @@ const MessageCode messageNonPartOfDirectiveInPart = const MessageCode(
tip:
r"""Try removing the other directives, or moving them to the library for which this is a part.""");
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeNonPositiveArrayDimensions =
messageNonPositiveArrayDimensions;
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const MessageCode messageNonPositiveArrayDimensions = const MessageCode(
"NonPositiveArrayDimensions",
message: r"""Array dimensions must be positive numbers.""");
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Template<Message Function(String name)>
templateNonSimpleBoundViaReference =

View file

@ -53,6 +53,7 @@ export '../fasta/fasta_codes.dart'
messageFfiExceptionalReturnNull,
messageFfiExpectedConstant,
messageFfiPackedAnnotationAlignment,
messageNonPositiveArrayDimensions,
noLength,
templateFfiDartTypeMismatch,
templateFfiEmptyStruct,

View file

@ -606,6 +606,7 @@ NonNullableOptOutImplicit/analyzerCode: Fail
NonNullableOptOutImplicit/example: Fail
NonPartOfDirectiveInPart/part_wrapped_script1: Fail
NonPartOfDirectiveInPart/script1: Fail
NonPositiveArrayDimensions/analyzerCode: Fail
NotAConstantExpression/example: Fail
NotAType/example: Fail
NotAnLvalue/example: Fail

View file

@ -4646,6 +4646,11 @@ NonNullAwareSpreadIsNull:
<int>[...null];
}
NonPositiveArrayDimensions:
# Used by dart:ffi
template: "Array dimensions must be positive numbers."
external: test/ffi_test.dart
InvalidTypeVariableInSupertype:
template: "Can't use implicitly 'out' variable '#name' in an '#string2' position in supertype '#name2'."
script: >

View file

@ -9,6 +9,7 @@ import 'dart:math' as math;
import 'package:front_end/src/api_unstable/vm.dart'
show
messageFfiPackedAnnotationAlignment,
messageNonPositiveArrayDimensions,
templateFfiEmptyStruct,
templateFfiFieldAnnotation,
templateFfiFieldNull,
@ -323,7 +324,8 @@ class _FfiDefinitionTransformer extends FfiTransformer {
compoundClassDependencies[node].add(clazz);
_checkPacking(node, packing, clazz, f);
}
if (arrayDimensions(type) != sizeAnnotations.single.length) {
final dimensions = sizeAnnotations.single;
if (arrayDimensions(type) != dimensions.length) {
diagnosticReporter.report(
templateFfiSizeAnnotationDimensions
.withArguments(f.name.text),
@ -331,6 +333,15 @@ class _FfiDefinitionTransformer extends FfiTransformer {
f.name.text.length,
f.fileUri);
}
for (var dimension in dimensions) {
if (dimension < 0) {
diagnosticReporter.report(
messageNonPositiveArrayDimensions, f.fileOffset,
f.name.text.length,
f.fileUri);
success = false;
}
}
} else {
diagnosticReporter.report(
templateFfiSizeAnnotation.withArguments(f.name.text),
@ -478,8 +489,8 @@ class _FfiDefinitionTransformer extends FfiTransformer {
final sizeAnnotations = _getArraySizeAnnotations(m).toList();
if (sizeAnnotations.length == 1) {
final arrayDimensions = sizeAnnotations.single;
type = NativeTypeCfe(this, dartType,
compoundCache: compoundCache, arrayDimensions: arrayDimensions);
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache,
arrayDimensions: arrayDimensions);
}
} else if (isPointerType(dartType) || isCompoundSubtype(dartType)) {
type = NativeTypeCfe(this, dartType, compoundCache: compoundCache);
@ -749,7 +760,7 @@ class CompoundLayout {
abstract class NativeTypeCfe {
factory NativeTypeCfe(FfiTransformer transformer, DartType dartType,
{List<int> arrayDimensions,
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
if (transformer.isPrimitiveType(dartType)) {
final clazz = (dartType as InterfaceType).classNode;
final nativeType = transformer.getType(clazz);
@ -772,7 +783,7 @@ abstract class NativeTypeCfe {
}
final elementType = transformer.arraySingleElementType(dartType);
final elementCfeType =
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
return ArrayNativeTypeCfe.multi(elementCfeType, arrayDimensions);
}
throw "Invalid type $dartType";
@ -1135,8 +1146,8 @@ class ArrayNativeTypeCfe implements NativeTypeCfe {
ArrayNativeTypeCfe(this.elementType, this.length);
factory ArrayNativeTypeCfe.multi(
NativeTypeCfe elementType, List<int> dimensions) {
factory ArrayNativeTypeCfe.multi(NativeTypeCfe elementType,
List<int> dimensions) {
if (dimensions.length == 1) {
return ArrayNativeTypeCfe(elementType, dimensions.single);
}

View file

@ -760,3 +760,24 @@ class TestStruct1606Packed extends Struct {
external Array<TestStruct1604> //# 1606: compile-time error
nestedLooselyPacked; //# 1606: compile-time error
}
class TestStruct1800 extends Struct {
external Pointer<Uint8> notEmpty;
@Array(-1) //# 1800: compile-time error
external Array<Uint8> inlineArray; //# 1800: compile-time error
}
class TestStruct1801 extends Struct {
external Pointer<Uint8> notEmpty;
@Array(1, -1) //# 1801: compile-time error
external Array<Uint8> inlineArray; //# 1801: compile-time error
}
class TestStruct1802 extends Struct {
external Pointer<Uint8> notEmpty;
@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
external Array<Uint8> inlineArray; //# 1802: compile-time error
}

View file

@ -759,3 +759,24 @@ class TestStruct1606Packed extends Struct {
@Array(2) //# 1606: compile-time error
Array<TestStruct1604> nestedLooselyPacked; //# 1606: compile-time error
}
class TestStruct1800 extends Struct {
Pointer<Uint8> notEmpty;
@Array(-1) //# 1800: compile-time error
Array<Uint8> inlineArray; //# 1800: compile-time error
}
class TestStruct1801 extends Struct {
Pointer<Uint8> notEmpty;
@Array(1, -1) //# 1801: compile-time error
Array<Uint8> inlineArray; //# 1801: compile-time error
}
class TestStruct1802 extends Struct {
Pointer<Uint8> notEmpty;
@Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
Array<Uint8> inlineArray; //# 1802: compile-time error
}