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