Skip to content

Commit 23fe39e

Browse files
zandersocommit-bot@chromium.org
authored andcommitted
[dart:typed_data] Adds unmodifiable list views
On Fuchsia, we need external typed data arrays that are backed by read-only regions of memory. Today, writes to these typed data lists crash the process. This change allows us to wrap the external typed data in unmodifiable views so that an exception is thrown instead of crashing. related #32028 Change-Id: I94dc5e1771b73480cb0eb21799215c090000fd5f Reviewed-on: https://dart-review.googlesource.com/40201 Reviewed-by: Lasse R.H. Nielsen <[email protected]> Commit-Queue: Zach Anderson <[email protected]>
1 parent 400c6c1 commit 23fe39e

File tree

6 files changed

+475
-1
lines changed

6 files changed

+475
-1
lines changed

sdk/lib/typed_data/typed_data.dart

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
/// import 'dart:typed_data';
1111
library dart.typed_data;
1212

13+
import "dart:_internal" show UnmodifiableListBase;
14+
15+
part "unmodifiable_typed_data.dart";
16+
1317
/**
1418
* A sequence of bytes underlying a typed data object.
1519
*

sdk/lib/typed_data/typed_data_sources.gni

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
typed_data_sdk_sources = [
66
"typed_data.dart",
77
# The above file needs to be first if additional parts are added to the lib.
8+
"unmodifiable_typed_data.dart",
89
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of dart.typed_data;
6+
7+
/**
8+
* A read-only view of a [ByteBuffer].
9+
*/
10+
class UnmodifiableByteBufferView implements ByteBuffer {
11+
final ByteBuffer _data;
12+
13+
UnmodifiableByteBufferView(ByteBuffer data) : _data = data;
14+
15+
int get lengthInBytes => _data.lengthInBytes;
16+
17+
Uint8List asUint8List([int offsetInBytes = 0, int length]) =>
18+
new UnmodifiableUint8ListView(_data.asUint8List(offsetInBytes, length));
19+
20+
Int8List asInt8List([int offsetInBytes = 0, int length]) =>
21+
new UnmodifiableInt8ListView(_data.asInt8List(offsetInBytes, length));
22+
23+
Uint8ClampedList asUint8ClampedList([int offsetInBytes = 0, int length]) =>
24+
new UnmodifiableUint8ClampedListView(
25+
_data.asUint8ClampedList(offsetInBytes, length));
26+
27+
Uint16List asUint16List([int offsetInBytes = 0, int length]) =>
28+
new UnmodifiableUint16ListView(_data.asUint16List(offsetInBytes, length));
29+
30+
Int16List asInt16List([int offsetInBytes = 0, int length]) =>
31+
new UnmodifiableInt16ListView(_data.asInt16List(offsetInBytes, length));
32+
33+
Uint32List asUint32List([int offsetInBytes = 0, int length]) =>
34+
new UnmodifiableUint32ListView(_data.asUint32List(offsetInBytes, length));
35+
36+
Int32List asInt32List([int offsetInBytes = 0, int length]) =>
37+
new UnmodifiableInt32ListView(_data.asInt32List(offsetInBytes, length));
38+
39+
Uint64List asUint64List([int offsetInBytes = 0, int length]) =>
40+
new UnmodifiableUint64ListView(_data.asUint64List(offsetInBytes, length));
41+
42+
Int64List asInt64List([int offsetInBytes = 0, int length]) =>
43+
new UnmodifiableInt64ListView(_data.asInt64List(offsetInBytes, length));
44+
45+
Int32x4List asInt32x4List([int offsetInBytes = 0, int length]) =>
46+
new UnmodifiableInt32x4ListView(
47+
_data.asInt32x4List(offsetInBytes, length));
48+
49+
Float32List asFloat32List([int offsetInBytes = 0, int length]) =>
50+
new UnmodifiableFloat32ListView(
51+
_data.asFloat32List(offsetInBytes, length));
52+
53+
Float64List asFloat64List([int offsetInBytes = 0, int length]) =>
54+
new UnmodifiableFloat64ListView(
55+
_data.asFloat64List(offsetInBytes, length));
56+
57+
Float32x4List asFloat32x4List([int offsetInBytes = 0, int length]) =>
58+
new UnmodifiableFloat32x4ListView(
59+
_data.asFloat32x4List(offsetInBytes, length));
60+
61+
Float64x2List asFloat64x2List([int offsetInBytes = 0, int length]) =>
62+
new UnmodifiableFloat64x2ListView(
63+
_data.asFloat64x2List(offsetInBytes, length));
64+
65+
ByteData asByteData([int offsetInBytes = 0, int length]) =>
66+
new UnmodifiableByteDataView(_data.asByteData(offsetInBytes, length));
67+
}
68+
69+
/**
70+
* A read-only view of a [ByteData].
71+
*/
72+
class UnmodifiableByteDataView implements ByteData {
73+
final ByteData _data;
74+
75+
UnmodifiableByteDataView(ByteData data) : _data = data;
76+
77+
int getInt8(int byteOffset) => _data.getInt8(byteOffset);
78+
79+
void setInt8(int byteOffset, int value) => _unsupported();
80+
81+
int getUint8(int byteOffset) => _data.getUint8(byteOffset);
82+
83+
void setUint8(int byteOffset, int value) => _unsupported();
84+
85+
int getInt16(int byteOffset, [Endian endian = Endian.big]) =>
86+
_data.getInt16(byteOffset, endian);
87+
88+
void setInt16(int byteOffset, int value, [Endian endian = Endian.big]) =>
89+
_unsupported();
90+
91+
int getUint16(int byteOffset, [Endian endian = Endian.big]) =>
92+
_data.getUint16(byteOffset, endian);
93+
94+
void setUint16(int byteOffset, int value, [Endian endian = Endian.big]) =>
95+
_unsupported();
96+
97+
int getInt32(int byteOffset, [Endian endian = Endian.big]) =>
98+
_data.getInt32(byteOffset, endian);
99+
100+
void setInt32(int byteOffset, int value, [Endian endian = Endian.big]) =>
101+
_unsupported();
102+
103+
int getUint32(int byteOffset, [Endian endian = Endian.big]) =>
104+
_data.getUint32(byteOffset, endian);
105+
106+
void setUint32(int byteOffset, int value, [Endian endian = Endian.big]) =>
107+
_unsupported();
108+
109+
int getInt64(int byteOffset, [Endian endian = Endian.big]) =>
110+
_data.getInt64(byteOffset, endian);
111+
112+
void setInt64(int byteOffset, int value, [Endian endian = Endian.big]) =>
113+
_unsupported();
114+
115+
int getUint64(int byteOffset, [Endian endian = Endian.big]) =>
116+
_data.getUint64(byteOffset, endian);
117+
118+
void setUint64(int byteOffset, int value, [Endian endian = Endian.big]) =>
119+
_unsupported();
120+
121+
double getFloat32(int byteOffset, [Endian endian = Endian.big]) =>
122+
_data.getFloat32(byteOffset, endian);
123+
124+
void setFloat32(int byteOffset, double value, [Endian endian = Endian.big]) =>
125+
_unsupported();
126+
127+
double getFloat64(int byteOffset, [Endian endian = Endian.big]) =>
128+
_data.getFloat64(byteOffset, endian);
129+
130+
void setFloat64(int byteOffset, double value, [Endian endian = Endian.big]) =>
131+
_unsupported();
132+
133+
int get elementSizeInBytes => _data.elementSizeInBytes;
134+
135+
int get offsetInBytes => _data.offsetInBytes;
136+
137+
int get lengthInBytes => _data.lengthInBytes;
138+
139+
ByteBuffer get buffer => new UnmodifiableByteBufferView(_data.buffer);
140+
141+
void _unsupported() {
142+
throw new UnsupportedError(
143+
"An UnmodifiableByteDataView may not be modified");
144+
}
145+
}
146+
147+
abstract class _UnmodifiableListMixin<N, L extends List<N>,
148+
TD extends TypedData> {
149+
L get _list;
150+
TD get _data => (_list as TD);
151+
152+
int get length => _list.length;
153+
154+
N operator [](int index) => _list[index];
155+
156+
int get elementSizeInBytes => _data.elementSizeInBytes;
157+
158+
int get offsetInBytes => _data.offsetInBytes;
159+
160+
int get lengthInBytes => _data.lengthInBytes;
161+
162+
ByteBuffer get buffer => new UnmodifiableByteBufferView(_data.buffer);
163+
}
164+
165+
/**
166+
* View of a [Uint8List] that disallows modification.
167+
*/
168+
class UnmodifiableUint8ListView extends UnmodifiableListBase<int>
169+
with _UnmodifiableListMixin<int, Uint8List, Uint8List>
170+
implements Uint8List {
171+
final Uint8List _list;
172+
UnmodifiableUint8ListView(Uint8List list) : _list = list;
173+
}
174+
175+
/**
176+
* View of a [Int8List] that disallows modification.
177+
*/
178+
class UnmodifiableInt8ListView extends UnmodifiableListBase<int>
179+
with _UnmodifiableListMixin<int, Int8List, Int8List>
180+
implements Int8List {
181+
final Int8List _list;
182+
UnmodifiableInt8ListView(Int8List list) : _list = list;
183+
}
184+
185+
/**
186+
* View of a [Uint8ClampedList] that disallows modification.
187+
*/
188+
class UnmodifiableUint8ClampedListView extends UnmodifiableListBase<int>
189+
with _UnmodifiableListMixin<int, Uint8ClampedList, Uint8ClampedList>
190+
implements Uint8ClampedList {
191+
final Uint8ClampedList _list;
192+
UnmodifiableUint8ClampedListView(Uint8ClampedList list) : _list = list;
193+
}
194+
195+
/**
196+
* View of a [Uint16List] that disallows modification.
197+
*/
198+
class UnmodifiableUint16ListView extends UnmodifiableListBase<int>
199+
with _UnmodifiableListMixin<int, Uint16List, Uint16List>
200+
implements Uint16List {
201+
final Uint16List _list;
202+
UnmodifiableUint16ListView(Uint16List list) : _list = list;
203+
}
204+
205+
/**
206+
* View of a [Int16List] that disallows modification.
207+
*/
208+
class UnmodifiableInt16ListView extends UnmodifiableListBase<int>
209+
with _UnmodifiableListMixin<int, Int16List, Int16List>
210+
implements Int16List {
211+
final Int16List _list;
212+
UnmodifiableInt16ListView(Int16List list) : _list = list;
213+
}
214+
215+
/**
216+
* View of a [Uint32List] that disallows modification.
217+
*/
218+
class UnmodifiableUint32ListView extends UnmodifiableListBase<int>
219+
with _UnmodifiableListMixin<int, Uint32List, Uint32List>
220+
implements Uint32List {
221+
final Uint32List _list;
222+
UnmodifiableUint32ListView(Uint32List list) : _list = list;
223+
}
224+
225+
/**
226+
* View of a [Int32List] that disallows modification.
227+
*/
228+
class UnmodifiableInt32ListView extends UnmodifiableListBase<int>
229+
with _UnmodifiableListMixin<int, Int32List, Int32List>
230+
implements Int32List {
231+
final Int32List _list;
232+
UnmodifiableInt32ListView(Int32List list) : _list = list;
233+
}
234+
235+
/**
236+
* View of a [Uint64List] that disallows modification.
237+
*/
238+
class UnmodifiableUint64ListView extends UnmodifiableListBase<int>
239+
with _UnmodifiableListMixin<int, Uint64List, Uint64List>
240+
implements Uint64List {
241+
final Uint64List _list;
242+
UnmodifiableUint64ListView(Uint64List list) : _list = list;
243+
}
244+
245+
/**
246+
* View of a [Int64List] that disallows modification.
247+
*/
248+
class UnmodifiableInt64ListView extends UnmodifiableListBase<int>
249+
with _UnmodifiableListMixin<int, Int64List, Int64List>
250+
implements Int64List {
251+
final Int64List _list;
252+
UnmodifiableInt64ListView(Int64List list) : _list = list;
253+
}
254+
255+
/**
256+
* View of a [Int32x4List] that disallows modification.
257+
*/
258+
class UnmodifiableInt32x4ListView extends UnmodifiableListBase<Int32x4>
259+
with _UnmodifiableListMixin<Int32x4, Int32x4List, Int32x4List>
260+
implements Int32x4List {
261+
final Int32x4List _list;
262+
UnmodifiableInt32x4ListView(Int32x4List list) : _list = list;
263+
}
264+
265+
/**
266+
* View of a [Float32x4List] that disallows modification.
267+
*/
268+
class UnmodifiableFloat32x4ListView extends UnmodifiableListBase<Float32x4>
269+
with _UnmodifiableListMixin<Float32x4, Float32x4List, Float32x4List>
270+
implements Float32x4List {
271+
final Float32x4List _list;
272+
UnmodifiableFloat32x4ListView(Float32x4List list) : _list = list;
273+
}
274+
275+
/**
276+
* View of a [Float64x2List] that disallows modification.
277+
*/
278+
class UnmodifiableFloat64x2ListView extends UnmodifiableListBase<Float64x2>
279+
with _UnmodifiableListMixin<Float64x2, Float64x2List, Float64x2List>
280+
implements Float64x2List {
281+
final Float64x2List _list;
282+
UnmodifiableFloat64x2ListView(Float64x2List list) : _list = list;
283+
}
284+
285+
/**
286+
* View of a [Float32List] that disallows modification.
287+
*/
288+
class UnmodifiableFloat32ListView extends UnmodifiableListBase<double>
289+
with _UnmodifiableListMixin<double, Float32List, Float32List>
290+
implements Float32List {
291+
final Float32List _list;
292+
UnmodifiableFloat32ListView(Float32List list) : _list = list;
293+
}
294+
295+
/**
296+
* View of a [Float64List] that disallows modification.
297+
*/
298+
class UnmodifiableFloat64ListView extends UnmodifiableListBase<double>
299+
with _UnmodifiableListMixin<double, Float64List, Float64List>
300+
implements Float64List {
301+
final Float64List _list;
302+
UnmodifiableFloat64ListView(Float64List list) : _list = list;
303+
}

tests/lib_2/lib_2_dart2js.status

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ profiler/metrics_num_test: Skip # Because of an int / double type test.
6868
typed_data/int32x4_arithmetic_test/int64: RuntimeError # Issue 1533
6969
typed_data/int64_list_load_store_test: RuntimeError # Issue 10275
7070
typed_data/typed_data_hierarchy_int64_test: RuntimeError # Issue 10275
71+
typed_data/unmodifiable_typed_data_test: RuntimeError # Issue 10275
7172

7273
[ $compiler != dart2js ]
7374
async/dart2js_uncaught_error_test: Skip # JS-integration only test

tests/lib_2/lib_2_dartdevc.status

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,4 @@ profiler/metrics_num_test: Skip # Because of an int / double type test.
130130
typed_data/int32x4_arithmetic_test/int64: RuntimeError # Issue 29922
131131
typed_data/int64_list_load_store_test: RuntimeError # Issue 29922
132132
typed_data/typed_data_hierarchy_int64_test: RuntimeError # Issue 29922
133-
133+
typed_data/unmodifiable_typed_data_test: RuntimeError # Issue 10275

0 commit comments

Comments
 (0)