Skip to content

Commit

Permalink
java: throw proper exception on EVMC loader failure
Browse files Browse the repository at this point in the history
Introduce specific EvmcLoaderException exception.
  • Loading branch information
axic committed Oct 9, 2020
1 parent 0a38524 commit 57cccf6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions bindings/java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_jar(evmc-jar
OUTPUT_NAME evmc-java
VERSION ${PROJECT_VERSION}
SOURCES
java/src/main/java/org/ethereum/evmc/EvmcLoaderException.java
java/src/main/java/org/ethereum/evmc/EvmcVm.java
java/src/main/java/org/ethereum/evmc/Host.java
java/src/main/java/org/ethereum/evmc/HostContext.java
Expand Down
10 changes: 3 additions & 7 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
#include <stdint.h>
#include <stdlib.h>

static void throw_java_assert(JNIEnv* jenv, const char* msg)
{
jclass jcls = (*jenv)->FindClass(jenv, "java/lang/AssertionError");
(*jenv)->ThrowNew(jenv, jcls, msg);
}

JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_load_1and_1create(JNIEnv* jenv,
jclass jcls,
jstring jfilename)
Expand All @@ -36,7 +30,9 @@ JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_load_1and_1create(JNIEnv
if (loader_error != EVMC_LOADER_SUCCESS)
{
const char* error_msg = evmc_last_error_msg();
throw_java_assert(jenv, error_msg ? error_msg : "Loading EVMC VM failed");
jclass exception_class = (*jenv)->FindClass(jenv, "org/ethereum/evmc/EvmcLoaderException");
assert(exception_class != NULL);
(*jenv)->ThrowNew(jenv, exception_class, error_msg ? error_msg : "Loading EVMC VM failed");
}
jobject jresult = (*jenv)->NewDirectByteBuffer(jenv, (void*)evm, sizeof(struct evmc_vm));
assert(jresult != NULL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2019-2020 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.
package org.ethereum.evmc;

public class EvmcLoaderException extends Exception {
public EvmcLoaderException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the Apache License, Version 2.0.
package org.ethereum.evmc;

import org.ethereum.evmc.EvmcLoaderException;
import static org.ethereum.evmc.Host.addContext;
import static org.ethereum.evmc.Host.removeContext;

Expand All @@ -22,8 +23,9 @@ public final class EvmcVm implements AutoCloseable {
* This method loads the specified evm shared library and loads/initializes the jni bindings.
*
* @param filename /path/filename of the evm shared object
* @throws EvmcLoaderException
*/
public static EvmcVm create(String filename) {
public static EvmcVm create(String filename) throws EvmcLoaderException {
if (!EvmcVm.isEvmcLibraryLoaded) {
try {
// load so containing the jni bindings to evmc
Expand All @@ -40,7 +42,7 @@ public static EvmcVm create(String filename) {
return evmcVm;
}

private EvmcVm(String filename) {
private EvmcVm(String filename) throws EvmcLoaderException {
nativeVm = load_and_create(filename);
}

Expand All @@ -49,8 +51,9 @@ private EvmcVm(String filename) {
*
* @param Path to the dynamic object representing the EVM implementation.
* @return Internal object pointer.
* @throws EvmcLoaderException
*/
private static native ByteBuffer load_and_create(String filename);
private static native ByteBuffer load_and_create(String filename) throws EvmcLoaderException;

/**
* EVMC ABI version implemented by the VM instance.
Expand Down

0 comments on commit 57cccf6

Please sign in to comment.