mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
![Oleh Prypin](/assets/img/avatar_default.png)
This reverts commitf706ff4ee2
. Reason for revert: b/321667799 - package:win32 uses this method -a78ff108fb/lib/src/com/iapplicationactivationmanager.dart (L46)
Original change's description: > [ffi]: Remove pointer elementAt method. > > Closes #54250 > > TEST=test/ffi > > R=dacoharkes@google.com > Change-Id: I0e88adfcfe3caef0ad3bb6814ad8f27dce5dc7f4 > CoreLibraryReviewExempt: FFI only > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346380 > Reviewed-by: Daco Harkes <dacoharkes@google.com> > Commit-Queue: Martin Kustermann <kustermann@google.com> > Reviewed-by: Martin Kustermann <kustermann@google.com> > Auto-Submit: Shikhar <shikharish05@gmail.com> Change-Id: I1b7a48d14e9b85676a27f76a926e21cac9c76c85 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347600 Reviewed-by: Martin Kustermann <kustermann@google.com> Reviewed-by: Daco Harkes <dacoharkes@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
213 lines
6.4 KiB
Dart
213 lines
6.4 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 = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasCast() {
|
|
final source = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasCast2() {
|
|
final source = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasOffsetBy() {
|
|
final source = calloc<Int64>(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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasOffsetBy2() {
|
|
final source = calloc<Int64>(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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasElementAt() {
|
|
final source = calloc<Int64>(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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasElementAt2() {
|
|
final source = calloc<Int64>(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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasFromAddress() {
|
|
final source = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasFromAddress2() {
|
|
final source = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasFromAddressViaMemory() {
|
|
final helper = calloc<IntPtr>();
|
|
final source = calloc<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);
|
|
calloc.free(helper);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasFromAddressViaMemory2() {
|
|
final helper = calloc<IntPtr>();
|
|
final source = calloc<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);
|
|
calloc.free(helper);
|
|
calloc.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 = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
void testAliasFromAddressViaNativeFunction2() {
|
|
final source = calloc<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);
|
|
calloc.free(source);
|
|
}
|
|
|
|
@pragma('vm:never-inline')
|
|
Pointer<Int8> makeDerived(Pointer<Int64> source) =>
|
|
source.offsetBy(7).cast<Int8>();
|
|
|
|
testPartialOverlap() {
|
|
final source = calloc<Int64>(2);
|
|
final derived = makeDerived(source);
|
|
source.value = 0x1122334455667788;
|
|
final int value = source.value;
|
|
derived.value = 0xaa;
|
|
Expect.notEquals(value, source.value);
|
|
calloc.free(source);
|
|
}
|