java: ensure direct buffer is returned via HostContext
@@ -16,6 +16,13 @@ import java.util.List;
* <p>The set of all callback functions expected by VM instances.
*/
final class Host {
+ static private ByteBuffer ensureDirectBuffer(ByteBuffer input) {
+ // Reallocate if needed.
+ if (!input.isDirect())
+ return ByteBuffer.allocateDirect(input.remaining()).put(input);
+ return input;
+ }
+
/** Check account existence callback function. */
static boolean account_exists(int context_index, byte[] address) {
HostContext context =
@@ -31,7 +38,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.getStorage(address, key);
+ return ensureDirectBuffer(context.getStorage(address, key));
}
/** Set storage callback function. */
@@ -48,7 +55,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.getBalance(address);
+ return ensureDirectBuffer(context.getBalance(address));
}
/** Get code size callback function. */
@@ -66,7 +73,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.getCodeHash(address);
+ return ensureDirectBuffer(context.getCodeHash(address));
}
/** Copy code callback function. */
@@ -100,7 +107,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.call(msg);
+ return ensureDirectBuffer(context.call(msg));
}
/** Get transaction context callback function. */
@@ -109,7 +116,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.getTxContext();
+ return ensureDirectBuffer(context.getTxContext());
}
/** Get block hash callback function. */
@@ -118,7 +125,7 @@ final class Host {
requireNonNull(
getContext(context_index),
"HostContext does not exist for context_index: " + context_index);
- return context.getBlockHash(number);
+ return ensureDirectBuffer(context.getBlockHash(number));
}
/** Emit log callback function. */
