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 1d2c6f4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 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
2 changes: 2 additions & 0 deletions include/evmc/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ enum evmc_loader_error_code
*/
evmc_create_fn evmc_load(const char* filename, enum evmc_loader_error_code* error_code);

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
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 1d2c6f4

Please sign in to comment.