Skip to content

Commit

Permalink
Fix unaligned access to TypedData from native code.
Browse files Browse the repository at this point in the history
Split unaligned access test and mark the float half as failing on Android.

R=zra@google.com

Review URL: https://codereview.chromium.org/2473553002 .
  • Loading branch information
rmacnak-google committed Nov 2, 2016
1 parent d8bb15f commit d5580f3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 52 deletions.
15 changes: 15 additions & 0 deletions runtime/vm/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -7930,6 +7930,7 @@ class TypedData : public Instance {
virtual bool CanonicalizeEquals(const Instance& other) const;
virtual uword ComputeCanonicalTableHash() const;

#if defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64)
#define TYPED_GETTER_SETTER(name, type) \
type Get##name(intptr_t byte_offset) const { \
NoSafepointScope no_safepoint; \
Expand All @@ -7939,6 +7940,20 @@ class TypedData : public Instance {
NoSafepointScope no_safepoint; \
*reinterpret_cast<type*>(DataAddr(byte_offset)) = value; \
}
#else // defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64)
#define TYPED_GETTER_SETTER(name, type) \
type Get##name(intptr_t byte_offset) const { \
NoSafepointScope no_safepoint; \
type result; \
memmove(&result, DataAddr(byte_offset), sizeof(type)); \
return result; \
} \
void Set##name(intptr_t byte_offset, type value) const { \
NoSafepointScope no_safepoint; \
memmove(DataAddr(byte_offset), &value, sizeof(type)); \
}
#endif // defined(HOST_ARCH_IA32) || defined(HOST_ARCH_X64)

TYPED_GETTER_SETTER(Int8, int8_t)
TYPED_GETTER_SETTER(Uint8, uint8_t)
TYPED_GETTER_SETTER(Int16, int16_t)
Expand Down
3 changes: 3 additions & 0 deletions tests/language/language.status
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ large_class_declaration_test: SkipSlow # Times out. Issue 20352

[ ($runtime == vm || $runtime == dart_precompiled || $runtime == dart_app) && ( $arch == simarm || $arch == arm || $arch == simarmv6 || $arch == armv6 || $arch == simarmv5te || $arch == armv5te || $arch == simarm64 || $arch == arm64 || $arch == simmips || $arch == mips) ]
vm/load_to_load_unaligned_forwarding_vm_test: Pass, Crash # Unaligned offset. Issue 22151
vm/unaligned_float_access_literal_index_test: Pass, Crash # Unaligned offset. Issue 22151
vm/unaligned_float_access_literal_index_test: Pass, Crash # Unaligned offset. Issue 22151


[ $compiler == none && ($runtime == dartium || $runtime == drt) ]
issue23244_test: Fail # Issue 23244
Expand Down
46 changes: 46 additions & 0 deletions tests/language/vm/unaligned_float_access_literal_index_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2016, 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.
// VMOptions=--optimization_counter_threshold=10 --no-background_compilation

import 'dart:typed_data';
import 'package:expect/expect.dart';

unalignedFloat32() {
var bytes = new ByteData(64);
bytes.setFloat32(0, 16.25);
Expect.equals(16.25, bytes.getFloat32(0));
bytes.setFloat32(1, 32.125);
Expect.equals(32.125, bytes.getFloat32(1));
bytes.setFloat32(2, 16.25);
Expect.equals(16.25, bytes.getFloat32(2));
bytes.setFloat32(3, 32.125);
Expect.equals(32.125, bytes.getFloat32(3));
}

unalignedFloat64() {
var bytes = new ByteData(64);
bytes.setFloat64(0, 16.25);
Expect.equals(16.25, bytes.getFloat64(0));
bytes.setFloat64(1, 32.125);
Expect.equals(32.125, bytes.getFloat64(1));
bytes.setFloat64(2, 16.25);
Expect.equals(16.25, bytes.getFloat64(2));
bytes.setFloat64(3, 32.125);
Expect.equals(32.125, bytes.getFloat64(3));
bytes.setFloat64(4, 16.25);
Expect.equals(16.25, bytes.getFloat64(4));
bytes.setFloat64(5, 32.125);
Expect.equals(32.125, bytes.getFloat64(5));
bytes.setFloat64(6, 16.25);
Expect.equals(16.25, bytes.getFloat64(6));
bytes.setFloat64(7, 32.125);
Expect.equals(32.125, bytes.getFloat64(7));
}

main() {
for (var i = 0; i < 20; i++) {
unalignedFloat32();
unalignedFloat64();
}
}
30 changes: 30 additions & 0 deletions tests/language/vm/unaligned_float_access_register_index_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2016, 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.
// VMOptions=--optimization_counter_threshold=10 --no-background_compilation

import 'dart:typed_data';
import 'package:expect/expect.dart';

unalignedFloat32() {
var bytes = new ByteData(64);
for (var i = 0; i < 4; i++) {
bytes.setFloat32(i, 16.25);
Expect.equals(16.25, bytes.getFloat32(i));
}
}

unalignedFloat64() {
var bytes = new ByteData(64);
for (var i = 0; i < 8; i++) {
bytes.setFloat64(i, 16.25);
Expect.equals(16.25, bytes.getFloat64(i));
}
}

main() {
for (var i = 0; i < 20; i++) {
unalignedFloat32();
unalignedFloat64();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,6 @@ unalignedInt64() {
Expect.equals(-0x23452345, bytes.getInt64(7));
}

unalignedFloat32() {
var bytes = new ByteData(64);
bytes.setFloat32(0, 16.25);
Expect.equals(16.25, bytes.getFloat32(0));
bytes.setFloat32(1, 32.125);
Expect.equals(32.125, bytes.getFloat32(1));
bytes.setFloat32(2, 16.25);
Expect.equals(16.25, bytes.getFloat32(2));
bytes.setFloat32(3, 32.125);
Expect.equals(32.125, bytes.getFloat32(3));
}

unalignedFloat64() {
var bytes = new ByteData(64);
bytes.setFloat64(0, 16.25);
Expect.equals(16.25, bytes.getFloat64(0));
bytes.setFloat64(1, 32.125);
Expect.equals(32.125, bytes.getFloat64(1));
bytes.setFloat64(2, 16.25);
Expect.equals(16.25, bytes.getFloat64(2));
bytes.setFloat64(3, 32.125);
Expect.equals(32.125, bytes.getFloat64(3));
bytes.setFloat64(4, 16.25);
Expect.equals(16.25, bytes.getFloat64(4));
bytes.setFloat64(5, 32.125);
Expect.equals(32.125, bytes.getFloat64(5));
bytes.setFloat64(6, 16.25);
Expect.equals(16.25, bytes.getFloat64(6));
bytes.setFloat64(7, 32.125);
Expect.equals(32.125, bytes.getFloat64(7));
}

main() {
for (var i = 0; i < 20; i++) {
unalignedUint16();
Expand All @@ -126,7 +94,5 @@ main() {
unalignedInt32();
unalignedUint64();
unalignedInt64();
unalignedFloat32();
unalignedFloat64();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ unalignedInt64() {
}
}

unalignedFloat32() {
var bytes = new ByteData(64);
for (var i = 0; i < 4; i++) {
bytes.setFloat32(i, 16.25);
Expect.equals(16.25, bytes.getFloat32(i));
}
}

unalignedFloat64() {
var bytes = new ByteData(64);
for (var i = 0; i < 8; i++) {
bytes.setFloat64(i, 16.25);
Expect.equals(16.25, bytes.getFloat64(i));
}
}

main() {
for (var i = 0; i < 20; i++) {
unalignedUint16();
Expand All @@ -78,7 +62,5 @@ main() {
unalignedInt32();
unalignedUint64();
unalignedInt64();
unalignedFloat32();
unalignedFloat64();
}
}

0 comments on commit d5580f3

Please sign in to comment.