Skip to content

Commit

Permalink
Handle failure in ffi_closure_alloc
Browse files Browse the repository at this point in the history
`ffi_closure_alloc` may fail and return `NULL` if, for instance, we're
running in a locked-down operating system that forbids FFI from
allocating executable pages of memory in any of the ways that it tries.
Today we pass this `NULL` on to `ffi_prep_closure_loc` which triggers a
segmentation fault that takes down the whole JVM. With this change we
check for a failure in this call and turn it into an
`UnsupportedOperationException` so that the caller can handle it more
gracefully.

Relates elastic/elasticsearch#73309
Relates elastic/elasticsearch#18272
  • Loading branch information
DaveCTurner committed Aug 30, 2021
1 parent 030411b commit c9df91f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -11,6 +11,7 @@ Features

Bug Fixes
---------
* [#TBD](https://github.com/java-native-access/jna/pull/TBD): Handle failure in `ffi_closure_alloc` - [@davecturner](https://github.com/davecturner).


Release 5.9.0
Expand Down
5 changes: 5 additions & 0 deletions native/dispatch.c
Expand Up @@ -3466,6 +3466,11 @@ Java_com_sun_jna_Native_registerMethod(JNIEnv *env, jclass UNUSED(ncls),
}

closure = ffi_closure_alloc(sizeof(ffi_closure), &code);
if (closure == NULL) {
throwByName(env, EUnsupportedOperation, "Failed to allocate closure");
status = FFI_BAD_ABI;
goto cleanup;
}
status = ffi_prep_closure_loc(closure, closure_cif, dispatch_direct, data, code);
if (status != FFI_OK) {
throwByName(env, EError, "Native method linkage failed");
Expand Down

0 comments on commit c9df91f

Please sign in to comment.