Skip to content

Commit 3f5e937

Browse files
committed
Handle failure in ffi_closure_alloc
`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
1 parent 030411b commit 3f5e937

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111

1212
Bug Fixes
1313
---------
14+
* [#1378](https://github.com/java-native-access/jna/pull/1378): Handle failure in `ffi_closure_alloc` - [@davecturner](https://github.com/davecturner).
1415

1516

1617
Release 5.9.0

native/dispatch.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,11 @@ Java_com_sun_jna_Native_registerMethod(JNIEnv *env, jclass UNUSED(ncls),
34663466
}
34673467

34683468
closure = ffi_closure_alloc(sizeof(ffi_closure), &code);
3469+
if (closure == NULL) {
3470+
throwByName(env, EUnsupportedOperation, "Failed to allocate closure");
3471+
status = FFI_BAD_ABI;
3472+
goto cleanup;
3473+
}
34693474
status = ffi_prep_closure_loc(closure, closure_cif, dispatch_direct, data, code);
34703475
if (status != FFI_OK) {
34713476
throwByName(env, EError, "Native method linkage failed");
@@ -3514,16 +3519,31 @@ Java_com_sun_jna_Native_ffi_1prep_1closure(JNIEnv *env, jclass UNUSED(cls), jlon
35143519
ffi_status s;
35153520

35163521
if ((*env)->GetJavaVM(env, &cb->vm) != JNI_OK) {
3522+
free(cb);
35173523
throwByName(env, EUnsatisfiedLink, "Can't get Java VM");
35183524
return 0;
35193525
}
35203526

35213527
cb->object = (*env)->NewWeakGlobalRef(env, obj);
3528+
if (cb->object == NULL) {
3529+
// either obj was null or an OutOfMemoryError has been thrown
3530+
free(cb);
3531+
return 0;
3532+
}
35223533
cb->closure = ffi_closure_alloc(sizeof(ffi_closure), L2A(&cb->x_closure));
3534+
if (cb->closure == NULL) {
3535+
(*env)->DeleteWeakGlobalRef(env, cb->object);
3536+
free(cb);
3537+
throwByName(env, EUnsupportedOperation, "Failed to allocate closure");
3538+
return 0;
3539+
}
35233540

35243541
s = ffi_prep_closure_loc(cb->closure, L2A(cif), &closure_handler,
35253542
cb, cb->x_closure);
35263543
if (ffi_error(env, "ffi_prep_cif", s)) {
3544+
ffi_closure_free(cb->closure);
3545+
(*env)->DeleteWeakGlobalRef(env, cb->object);
3546+
free(cb);
35273547
return 0;
35283548
}
35293549
return A2L(cb);

0 commit comments

Comments
 (0)