dart-sdk/tests/ffi/aliasing_test.dart
Martin Kustermann b0155a72a7 [vm/ffi] Add script to extract existing positive ffi tests into bundle to be used for flutter/flutter integration test
The `tests/ffi/prepare_flutter_bundle.dart` script will try to discover
all synchronous, positive test from the "ffi" test suite, rewrite them
slightly and output a directory which can be used in a flutter/flutter
FFI integration test.

We split the ffi test functions into two parts, because a subset of the
C functions will issue calls to the VM via `dart_api.h`, which is not
available for the flutter/flutter integration test.

=> We make `runtime/bin/ffi_test/ffi_test_functions.cc` a pure C
   library, usable without `dart_api.h` and move the remaining VM
   specific code to .../ffi_test_functions_special.cc contains.

All tests from `tests/ffi/*_test.dart` will be included in the generated
bundle, which

   * don't use async/isolates
   * don't rely on VM api
   * don't rely on DynamicLibrary.{process,executable}
   * don't take too long to execute

The script can be used as follows:

   sdk % dart tests/ffi/prepare_flutter_bundle.dart foo
   Using SDK root: .../sdk
   The following tests will be included:
      aliasing_test.dart
      data_not_asan_test.dart
      data_test.dart
      extension_methods_test.dart
      external_typed_data_test.dart
      function_structs_test.dart
      negative_function_test.dart
      regress_37254_test.dart
      regress_39044_test.dart
      regress_39063_test.dart
      regress_39068_test.dart
      stacktrace_regress_37910_test.dart
      structs_test.dart
      variance_function_test.dart
   The following tests were filtered due to using dart_api.h/async/DynamicLibrary.{process,executable}/...
      function_callbacks_test.dart
      function_gc_test.dart
      function_test.dart
      object_gc_test.dart
      regress_37100_test.dart
      regress_37511_callbacks_test.dart
      regress_37511_test.dart
      regress_37780_test.dart

   Please copy generated files into FFI flutter test application
      * foo/lib/src/generated
      * foo/ios/Classes

Change-Id: Ia13f97df3bbc90829bb8fde8265a7e1d2c0f8260
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127006
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2019-12-04 13:02:48 +00:00

213 lines
6.3 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// Dart test programing for testing that optimizations do wrongly assume loads
// from and stores to C memory are not aliased.
//
// SharedObjects=ffi_test_functions
// VMOptions=--deterministic --optimization-counter-threshold=50
import 'dart:ffi';
import "package:ffi/ffi.dart";
import "package:expect/expect.dart";
import 'ffi_test_helpers.dart';
void main() {
for (int i = 0; i < 100; ++i) {
testNonAlias();
testAliasCast();
testAliasCast2();
testAliasOffsetBy();
testAliasOffsetBy2();
testAliasElementAt();
testAliasElementAt2();
testAliasFromAddress();
testAliasFromAddress2();
testAliasFromAddressViaMemory();
testAliasFromAddressViaMemory2();
testAliasFromAddressViaNativeFunction();
testAliasFromAddressViaNativeFunction2();
testPartialOverlap();
}
}
void testNonAlias() {
final source = allocate<Int64>();
source.value = 42;
final int a = source.value;
source.value = 1984;
// alias.value should be re-executed, as we wrote to alias.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasCast() {
final source = allocate<Int64>();
final alias = source.cast<Int8>().cast<Int64>();
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasCast2() {
final source = allocate<Int64>();
final alias = source.cast<Int16>().cast<Int64>();
final alias2 = source.cast<Int8>().cast<Int64>();
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(source);
}
void testAliasOffsetBy() {
final source = allocate<Int64>(count: 2);
final alias = source.offsetBy(8).offsetBy(-8);
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasOffsetBy2() {
final source = allocate<Int64>(count: 3);
final alias = source.offsetBy(16).offsetBy(-16);
final alias2 = source.offsetBy(8).offsetBy(-8);
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(source);
}
void testAliasElementAt() {
final source = allocate<Int64>(count: 2);
final alias = source.elementAt(1).elementAt(-1);
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasElementAt2() {
final source = allocate<Int64>(count: 3);
final alias = source.elementAt(2).elementAt(-2);
final alias2 = source.elementAt(1).elementAt(-1);
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(source);
}
void testAliasFromAddress() {
final source = allocate<Int64>();
final alias = Pointer<Int64>.fromAddress(source.address);
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasFromAddress2() {
final source = allocate<Int64>();
final alias = Pointer<Int64>.fromAddress(source.address);
final alias2 = Pointer<Int64>.fromAddress(source.address);
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(source);
}
void testAliasFromAddressViaMemory() {
final helper = allocate<IntPtr>();
final source = allocate<Int64>();
helper.value = source.address;
final alias = Pointer<Int64>.fromAddress(helper.value);
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(helper);
free(source);
}
void testAliasFromAddressViaMemory2() {
final helper = allocate<IntPtr>();
final source = allocate<Int64>();
helper.value = source.address;
final alias = Pointer<Int64>.fromAddress(helper.value);
final alias2 = Pointer<Int64>.fromAddress(helper.value);
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(helper);
free(source);
}
typedef NativeQuadOpSigned = Int64 Function(Int8, Int16, Int32, Int64);
typedef QuadOp = int Function(int, int, int, int);
QuadOp intComputation = ffiTestFunctions
.lookupFunction<NativeQuadOpSigned, QuadOp>("IntComputation");
void testAliasFromAddressViaNativeFunction() {
final source = allocate<Int64>();
final alias =
Pointer<Int64>.fromAddress(intComputation(0, 0, 0, source.address));
source.value = 42;
final int a = source.value;
alias.value = 1984;
// source.value should be re-executed, we wrote alias which aliases source.
Expect.notEquals(a, source.value);
free(source);
}
void testAliasFromAddressViaNativeFunction2() {
final source = allocate<Int64>();
final alias =
Pointer<Int64>.fromAddress(intComputation(0, 0, 0, source.address));
final alias2 =
Pointer<Int64>.fromAddress(intComputation(0, 0, 0, source.address));
alias.value = 42;
final int a = alias.value;
alias2.value = 1984;
// alias.value should be re-executed, we wrote alias2 which aliases alias.
Expect.notEquals(a, alias.value);
free(source);
}
@pragma('vm:never-inline')
Pointer<Int8> makeDerived(Pointer<Int64> source) =>
source.offsetBy(7).cast<Int8>();
testPartialOverlap() {
final source = allocate<Int64>(count: 2);
final derived = makeDerived(source);
source.value = 0x1122334455667788;
final int value = source.value;
derived.value = 0xaa;
Expect.notEquals(value, source.value);
free(source);
}