Skip to content

Commit

Permalink
Merge pull request #551 from ethereum/java-execute-ensure-buffer
Browse files Browse the repository at this point in the history
java: add CopyFromBuffer helper and ensure the properly sized ByteBuffer is used
  • Loading branch information
axic authored Oct 9, 2020
2 parents 136d643 + fe5420f commit 57bbfb5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
53 changes: 32 additions & 21 deletions bindings/java/c/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "host.h"

Expand Down Expand Up @@ -34,6 +35,20 @@ static jbyteArray CopyDataToJava(JNIEnv* jenv, const void* ptr, size_t size)
return ret;
}

static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size)
{
size_t src_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, src);
if (src_size != size)
{
jclass exception_class = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
assert(exception_class != NULL);
(*jenv)->ThrowNew(jenv, exception_class, "Unexpected length.");
}
void* ptr = (*jenv)->GetDirectBufferAddress(jenv, src);
assert(ptr != NULL);
memcpy(dst, ptr, size);
}

static bool account_exists_fn(struct evmc_host_context* context, const evmc_address* address)
{
const char java_method_name[] = "account_exists";
Expand Down Expand Up @@ -85,11 +100,11 @@ static evmc_bytes32 get_storage_fn(struct evmc_host_context* context,
// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress, jkey);

assert(jresult != NULL);
evmc_bytes32* result_ptr = (struct evmc_bytes32*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
return *result_ptr; // copy here

evmc_bytes32 result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(evmc_bytes32));
return result;
}

static enum evmc_storage_status set_storage_fn(struct evmc_host_context* context,
Expand Down Expand Up @@ -148,9 +163,8 @@ static evmc_uint256be get_balance_fn(struct evmc_host_context* context, const ev
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress);
assert(jresult != NULL);

evmc_uint256be* result_ptr = (evmc_uint256be*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
evmc_uint256be result = *result_ptr; // copy here
evmc_uint256be result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(evmc_uint256be));

(*jenv)->ReleaseByteArrayElements(jenv, jaddress, (jbyte*)address, 0);

Expand Down Expand Up @@ -207,9 +221,8 @@ static evmc_bytes32 get_code_hash_fn(struct evmc_host_context* context, const ev
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress);
assert(jresult != NULL);

evmc_bytes32* result_ptr = (struct evmc_bytes32*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
evmc_bytes32 result = *result_ptr; // copy here
evmc_bytes32 result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(evmc_bytes32));

(*jenv)->ReleaseByteArrayElements(jenv, jaddress, (jbyte*)address, 0);

Expand Down Expand Up @@ -312,10 +325,9 @@ static struct evmc_result call_fn(struct evmc_host_context* context, const struc
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jmsg);
assert(jresult != NULL);

struct evmc_result* result_ptr =
(struct evmc_result*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
return *result_ptr; // copy here
struct evmc_result result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(struct evmc_result));
return result;
}

static struct evmc_tx_context get_tx_context_fn(struct evmc_host_context* context)
Expand All @@ -339,10 +351,9 @@ static struct evmc_tx_context get_tx_context_fn(struct evmc_host_context* contex
jobject jresult = (*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index);
assert(jresult != NULL);

struct evmc_tx_context* result_ptr =
(struct evmc_tx_context*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
return *result_ptr; // copy here
struct evmc_tx_context result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(struct evmc_tx_context));
return result;
}

static evmc_bytes32 get_block_hash_fn(struct evmc_host_context* context, int64_t number)
Expand All @@ -367,9 +378,9 @@ static evmc_bytes32 get_block_hash_fn(struct evmc_host_context* context, int64_t
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, (jlong)number);
assert(jresult != NULL);

evmc_bytes32* result_ptr = (struct evmc_bytes32*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result_ptr != NULL);
return *result_ptr; // copy here
evmc_bytes32 result;
CopyFromByteBuffer(jenv, jresult, &result, sizeof(evmc_bytes32));
return result;
}

static void emit_log_fn(struct evmc_host_context* context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public boolean accountExists(byte[] address) {

@Override
public ByteBuffer getStorage(byte[] address, byte[] key) {
return ByteBuffer.allocateDirect(64).put(new byte[64]);
return ByteBuffer.allocateDirect(32).put(new byte[32]);
}

@Override
Expand All @@ -23,7 +23,7 @@ public int setStorage(byte[] address, byte[] key, byte[] value) {

@Override
public ByteBuffer getBalance(byte[] address) {
return ByteBuffer.allocateDirect(64).put(new byte[64]);
return ByteBuffer.allocateDirect(32).put(new byte[32]);
}

@Override
Expand All @@ -33,7 +33,7 @@ public int getCodeSize(byte[] address) {

@Override
public ByteBuffer getCodeHash(byte[] address) {
return ByteBuffer.allocateDirect(64).put(new byte[64]);
return ByteBuffer.allocateDirect(32).put(new byte[32]);
}

@Override
Expand All @@ -51,12 +51,12 @@ public ByteBuffer call(ByteBuffer msg) {

@Override
public ByteBuffer getTxContext() {
return ByteBuffer.allocateDirect(64).put(new byte[64]);
return ByteBuffer.allocateDirect(160).put(new byte[160]);
}

@Override
public ByteBuffer getBlockHash(long number) {
return ByteBuffer.allocateDirect(64).put(new byte[64]);
return ByteBuffer.allocateDirect(32).put(new byte[32]);
}

@Override
Expand Down

0 comments on commit 57bbfb5

Please sign in to comment.