Skip to content
Permalink
Browse files

Android glue: don’t zero-init non-null function pointers

Fixes:

```rust
error: the type `android_injected_glue::ffi::android_app` does not permit zero-initialization
   --> ports/libsimpleservo/jniapi/src/lib.rs:511:46
    |
511 |     let mut app: ffi::android_app = unsafe { std::mem::zeroed() };
    |                                              ^^^^^^^^^^^^^^^^^^
    |                                              |
    |                                              this code causes undefined behavior when executed
    |                                              help: use `MaybeUninit<T>` instead
    |
    = note: `-D invalid-value` implied by `-D warnings`
note: Function pointers must be non-null (in this struct field)
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/android_injected_glue-0.2.3/src/ffi.rs:23:5
    |
23  |     pub onAppCmd: extern fn(*mut android_app, i32),
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
```
  • Loading branch information...
SimonSapin committed Aug 18, 2019
1 parent 98e4a53 commit 2130e6966a29c0201717f9e495166441917df010
Showing with 43 additions and 10 deletions.
  1. +43 −10 ports/libsimpleservo/jniapi/src/lib.rs
@@ -16,6 +16,7 @@ use log::Level;
use simpleservo::{self, gl_glue, ServoGlue, SERVO};
use simpleservo::{Coordinates, EventLoopWaker, HostTrait, InitOptions, VRInitOptions};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr::{null, null_mut};
use std::sync::Arc;
use std::thread;

@@ -508,19 +509,51 @@ fn initialize_android_glue(env: &JNIEnv, activity: JObject) {

// From jni-rs to android_injected_glue

let mut app: ffi::android_app = unsafe { std::mem::zeroed() };
let mut native_activity: ffi::ANativeActivity = unsafe { std::mem::zeroed() };

let clazz = Box::into_raw(Box::new(env.new_global_ref(activity).unwrap()));
native_activity.clazz = unsafe { (*clazz).as_obj().into_inner() as *mut c_void };

let vm = env.get_java_vm().unwrap().get_java_vm_pointer();
native_activity.vm = vm as *mut ffi::_JavaVM;
let clazz = Box::leak(Box::new(env.new_global_ref(activity).unwrap()));

let activity = Box::into_raw(Box::new(ffi::ANativeActivity {
clazz: clazz.as_obj().into_inner() as *mut c_void,
vm: env.get_java_vm().unwrap().get_java_vm_pointer() as *mut ffi::_JavaVM,

callbacks: null_mut(),
env: null_mut(),
internalDataPath: null(),
externalDataPath: null(),
sdkVersion: 0,
instance: null_mut(),
assetManager: null_mut(),
obbPath: null(),
}));

extern "C" fn on_app_cmd(_: *mut ffi::android_app, _: i32) {}
extern "C" fn on_input_event(_: *mut ffi::android_app, _: *const c_void) -> i32 {
0
}

app.activity = Box::into_raw(Box::new(native_activity));
let app = Box::into_raw(Box::new(ffi::android_app {
activity,
onAppCmd: on_app_cmd,
onInputEvent: on_input_event,

userData: null_mut(),
config: null(),
savedState: null_mut(),
savedStateSize: 0,
looper: null_mut(),
inputQueue: null(),
window: null_mut(),
contentRect: ffi::ARect {
left: 0,
top: 0,
right: 0,
bottom: 0,
},
activityState: 0,
destroyRequested: 0,
}));

unsafe {
ANDROID_APP = Box::into_raw(Box::new(app));
ANDROID_APP = app;
}
}

0 comments on commit 2130e69

Please sign in to comment.
You can’t perform that action at this time.