Skip to content

Commit

Permalink
loader: Expose DLL load error in hacky way
Browse files Browse the repository at this point in the history
Only works on Linux and macos, otherwise returns null.
  • Loading branch information
chfast committed Apr 3, 2019
1 parent 4408da2 commit e8067fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
8 changes: 7 additions & 1 deletion bindings/go/evmc/evmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ func Load(filename string) (instance *Instance, err error) {
case C.EVMC_LOADER_SUCCESS:
instance = &Instance{handle}
case C.EVMC_LOADER_CANNOT_OPEN:
err = fmt.Errorf("evmc loader: cannot open %s", filename)
optionalErrMsg := C.evmc_cannot_open_error()
if optionalErrMsg != nil {
msg := C.GoString(optionalErrMsg)
err = fmt.Errorf("evmc loader: %s", msg)
} else {
err = fmt.Errorf("evmc loader: cannot open %s", filename)
}
case C.EVMC_LOADER_SYMBOL_NOT_FOUND:
err = fmt.Errorf("evmc loader: the EVMC create function not found in %s", filename)
case C.EVMC_LOADER_INVALID_ARGUMENT:
Expand Down
13 changes: 11 additions & 2 deletions include/evmc/loader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/

/**
Expand Down Expand Up @@ -87,6 +87,15 @@ enum evmc_loader_error_code
*/
evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* error_code);

/**
* Exposes additional information about ::EVMC_LOADER_CANNOT_OPEN error.
*
* This function is not thread-safe and not supported on all operating systems.
*
* @return Error message or NULL if no additional information is available.
*/
const char* evmc_cannot_open_error();

/**
* Dynamically loads the VM DLL and creates the VM instance.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* erro
return create_fn;
}

const char* evmc_cannot_open_error()
{
#if !defined(EVMC_LOADER_MOCK) && !_WIN32
const char* error = dlerror();
if (error)
return error;
#endif
return NULL;
}

struct evmc_instance* evmc_load_and_create(const char* filename,
enum evmc_loader_error_code* error_code)
{
Expand Down
2 changes: 1 addition & 1 deletion test/vmtester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ add_test(NAME vmtester/help COMMAND evmc-vmtester --version --help)
set_tests_properties(vmtester/help PROPERTIES PASS_REGULAR_EXPRESSION "Usage:")

add_test(NAME vmtester/nonexistingvm COMMAND evmc-vmtester nonexistingvm)
set_tests_properties(vmtester/nonexistingvm PROPERTIES PASS_REGULAR_EXPRESSION "Cannot open")
set_tests_properties(vmtester/nonexistingvm PROPERTIES PASS_REGULAR_EXPRESSION "[Cc]annot open")

add_test(NAME vmtester/noarg COMMAND evmc-vmtester)
set_tests_properties(vmtester/noarg PROPERTIES PASS_REGULAR_EXPRESSION "is required")
Expand Down
8 changes: 7 additions & 1 deletion test/vmtester/vmtester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ int main(int argc, char* argv[])
case EVMC_LOADER_SUCCESS:
break;
case EVMC_LOADER_CANNOT_OPEN:
std::cerr << "Cannot open " << evmc_module << "\n";
{
const auto error = evmc_cannot_open_error();
if (error)
std::cerr << error << "\n";
else
std::cerr << "Cannot open " << evmc_module << "\n";
return static_cast<int>(ec);
}
case EVMC_LOADER_SYMBOL_NOT_FOUND:
std::cerr << "EVMC create function not found in " << evmc_module << "\n";
return static_cast<int>(ec);
Expand Down

0 comments on commit e8067fd

Please sign in to comment.