Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java: remove code_size argument from execute() #552

Merged
merged 2 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,21 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
jint jrev,
jobject jmsg,
jobject jcode,
jint jcode_size,
jobject jresult)
{
(void)jcls;
struct evmc_message* msg = (struct evmc_message*)(*jenv)->GetDirectBufferAddress(jenv, jmsg);
assert(msg != NULL);
const uint8_t* code = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jcode);
assert(code != NULL);
size_t code_size;
const uint8_t* code = GetDirectBuffer(jenv, jcode, &code_size);
struct evmc_host_context context = {jcontext_index};
struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm);
assert(evm != NULL);
const struct evmc_host_interface* host = evmc_java_get_host_interface();
struct evmc_result* result =
(struct evmc_result*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result != NULL);
*result =
evmc_execute(evm, host, &context, (enum evmc_revision)jrev, msg, code, (size_t)jcode_size);
*result = evmc_execute(evm, host, &context, (enum evmc_revision)jrev, msg, code, code_size);
}

JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_get_1capabilities(JNIEnv* jenv,
Expand Down
5 changes: 2 additions & 3 deletions bindings/java/c/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ static jbyteArray CopyDataToJava(JNIEnv* jenv, const void* ptr, size_t size)

static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size)
{
size_t src_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, src);
size_t src_size;
const void* ptr = GetDirectBuffer(jenv, src, &src_size);
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);
}

Expand Down
12 changes: 12 additions & 0 deletions bindings/java/c/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the Apache License, Version 2.0.
*/
#include "evmc/evmc.h"
#include <assert.h>
#include <jni.h>

#ifndef _Included_org_ethereum_evmc_Host
Expand All @@ -19,6 +20,17 @@ struct evmc_host_context
int evmc_java_set_jvm(JNIEnv*);
const struct evmc_host_interface* evmc_java_get_host_interface();

static inline void* GetDirectBuffer(JNIEnv* jenv, jobject buf, size_t* size)
{
void* ret = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, buf);
assert(ret != NULL);
jlong buf_size = (*jenv)->GetDirectBufferCapacity(jenv, buf);
assert(buf_size != -1);
if (size)
*size = (size_t)buf_size;
return ret;
}

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ private static native void execute(
int rev,
ByteBuffer msg,
ByteBuffer code,
int size,
ByteBuffer result);

/**
Expand All @@ -116,11 +115,11 @@ private static native void execute(
* <p>This allows the context to managed in one method
*/
public synchronized ByteBuffer execute(
HostContext context, int rev, ByteBuffer msg, ByteBuffer code, int size) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is a breaking change, but the Java binding we have is not stable enough and more changes are coming.

Everything merged so far were non-breaking changes.

HostContext context, int rev, ByteBuffer msg, ByteBuffer code) {
int context_index = addContext(context);
int resultSize = get_result_size();
ByteBuffer result = ByteBuffer.allocateDirect(resultSize);
execute(nativeVm, context_index, rev, msg, code, size, result);
execute(nativeVm, context_index, rev, msg, code, result);
removeContext(context_index);
return result;
}
Expand Down
12 changes: 6 additions & 6 deletions bindings/java/java/src/test/java/org/ethereum/evmc/EvmcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void testExecute_returnAddress() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down Expand Up @@ -95,7 +95,7 @@ void testExecute_counter() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down Expand Up @@ -125,7 +125,7 @@ void testExecute_returnBlockNumber() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down Expand Up @@ -157,7 +157,7 @@ void testExecute_saveReturnBlockNumber() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down Expand Up @@ -196,7 +196,7 @@ void testExecute_makeCall() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down Expand Up @@ -224,7 +224,7 @@ void testExecute_EVMC_CREATE() throws Exception {
ByteBuffer bbcode = ByteBuffer.allocateDirect(code.length).put(code);

ByteBuffer result =
vm.execute(context, BYZANTIUM, msg, bbcode, code.length).order(ByteOrder.nativeOrder());
vm.execute(context, BYZANTIUM, msg, bbcode).order(ByteOrder.nativeOrder());
int statusCode = result.getInt();
result.getInt(); // padding
long gasLeft = result.getLong();
Expand Down