50
50
import java .io .OutputStream ;
51
51
import java .util .Arrays ;
52
52
53
- import com .oracle .truffle .api .library .ExportLibrary ;
54
- import com .oracle .truffle .api .library .ExportMessage ;
55
53
import org .graalvm .wasm .api .Vector128 ;
56
54
import org .graalvm .wasm .api .Vector128Ops ;
57
55
import org .graalvm .wasm .exception .Failure ;
58
56
import org .graalvm .wasm .exception .WasmException ;
59
57
60
58
import com .oracle .truffle .api .CompilerDirectives ;
61
59
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
60
+ import com .oracle .truffle .api .library .ExportLibrary ;
61
+ import com .oracle .truffle .api .library .ExportMessage ;
62
62
import com .oracle .truffle .api .memory .ByteArraySupport ;
63
63
import com .oracle .truffle .api .nodes .Node ;
64
64
@@ -71,7 +71,7 @@ final class ByteArrayWasmMemory extends WasmMemory {
71
71
@ TruffleBoundary
72
72
private ByteArrayWasmMemory (long declaredMinSize , long declaredMaxSize , long initialSize , long maxAllowedSize , boolean indexType64 ) {
73
73
super (declaredMinSize , declaredMaxSize , initialSize , maxAllowedSize , indexType64 , false );
74
- this .dynamicBuffer = allocateStatic (initialSize * MEMORY_PAGE_SIZE );
74
+ this .dynamicBuffer = allocateBuffer (initialSize * MEMORY_PAGE_SIZE );
75
75
}
76
76
77
77
@ TruffleBoundary
@@ -101,14 +101,23 @@ public synchronized long grow(long extraPageSize) {
101
101
invokeGrowCallback ();
102
102
return previousSize ;
103
103
} else if (compareUnsigned (extraPageSize , maxAllowedSize ()) <= 0 && compareUnsigned (previousSize + extraPageSize , maxAllowedSize ()) <= 0 ) {
104
- // Condition above and limit on maxAllowedSize (see
105
- // ByteArrayWasmMemory#MAX_ALLOWED_SIZE) ensure computation of targetByteSize does not
106
- // overflow.
107
- final long targetByteSize = multiplyExact (addExact (previousSize , extraPageSize ), MEMORY_PAGE_SIZE );
104
+ /*
105
+ * Condition above and limit on maxAllowedSize (see
106
+ * ByteArrayWasmMemory#MAX_ALLOWED_SIZE) ensure computation of targetByteSize does not
107
+ * overflow.
108
+ */
109
+ final long targetPageSize = addExact (previousSize , extraPageSize );
110
+ final long targetByteSize = multiplyExact (targetPageSize , MEMORY_PAGE_SIZE );
108
111
final byte [] currentBuffer = buffer ();
109
- allocate (targetByteSize );
110
- System .arraycopy (currentBuffer , 0 , buffer (), 0 , currentBuffer .length );
111
- currentMinSize = previousSize + extraPageSize ;
112
+ final byte [] newBuffer ;
113
+ try {
114
+ newBuffer = allocateBuffer (targetByteSize );
115
+ } catch (WasmException oome ) {
116
+ return -1 ;
117
+ }
118
+ System .arraycopy (currentBuffer , 0 , newBuffer , 0 , currentBuffer .length );
119
+ dynamicBuffer = newBuffer ;
120
+ currentMinSize = targetPageSize ;
112
121
invokeGrowCallback ();
113
122
return previousSize ;
114
123
} else {
@@ -119,7 +128,7 @@ public synchronized long grow(long extraPageSize) {
119
128
@ ExportMessage
120
129
@ TruffleBoundary
121
130
public void reset () {
122
- allocate (declaredMinSize * MEMORY_PAGE_SIZE );
131
+ dynamicBuffer = allocateBuffer (declaredMinSize * MEMORY_PAGE_SIZE );
123
132
currentMinSize = declaredMinSize ;
124
133
}
125
134
@@ -1091,17 +1100,9 @@ public void copyToBuffer(Node node, byte[] dst, long srcOffset, int dstOffset, i
1091
1100
}
1092
1101
1093
1102
@ TruffleBoundary
1094
- private void allocate (long byteSize ) {
1095
- dynamicBuffer = null ;
1096
- dynamicBuffer = allocateStatic (byteSize );
1097
- }
1098
-
1099
- @ TruffleBoundary
1100
- private static byte [] allocateStatic (long byteSize ) {
1101
- assert byteSize <= Integer .MAX_VALUE : byteSize ;
1102
- final int effectiveByteSize = (int ) byteSize ;
1103
+ private static byte [] allocateBuffer (long byteSize ) {
1103
1104
try {
1104
- return new byte [effectiveByteSize ];
1105
+ return new byte [Math . toIntExact ( byteSize ) ];
1105
1106
} catch (OutOfMemoryError error ) {
1106
1107
throw WasmException .create (Failure .MEMORY_ALLOCATION_FAILED );
1107
1108
}
0 commit comments