WebAssembly in Android apps — Do WASMs dream of JNI?
keiji_ariyamaC-LIS CO., LTD.
For a long time, JNI (Java Native Interface) was the standard choice when utilizing existing assets written in C/C++ (native libraries) in Android apps. However, in huge and complex libraries like image codecs, once a vulnerability is exploited, it carries serious security risks that involve the app and even the entire system. In this session, through a development case study of "jp2k-decoder-android", a library ported for Android from the JPEG2000 decoder (OpenJPEG), we will explore the possibilities of WASM (WebAssembly) as a new alternative to JNI. How did we build a secure and realistic architecture that does not "completely eliminate vulnerabilities" but rather "does not expose the app to threats even if vulnerabilities exist" by running WASM in an isolated sandbox environment using Jetpack JavaScript Engine? And we will talk about the performance troubles faced through WASM conversion, such as the barrier of Inter-Process Communication (IPC) and the transfer of huge image data (Base64 vs byte array), as well as their optimization strategies. ## Agenda 1. Why WASM on Android? * Security risks and limitations of JNI in image codecs * WASM as a practical solution 2. Architecture * WASM sandbox utilizing Jetpack JavaScript Engine * Creating the "mechanism" to compile a C library (OpenJPEG) to WASM and integrate it into an app 3. Challenges and Performance Optimization * Execution speed of WASM itself and bottlenecks caused by Inter-Process Communication (IPC) with JS Engine * Serialization costs in large data transfers ## Out of Scope 1. Detailed specifications of the JPEG2000 image format 2. Explanation of C/C++ and OpenJPEG at the source code level 3. Details of internal implementations of Jetpack JavaScript Engine ## Key Takeaways * Risks of native code execution via JNI and security enhancement approaches using sandboxing with WASM * Implementation image for practically running WASM within Android apps using Jetpack JavaScript Engine * Performance optimization when exchanging large amounts of data between WASM/JS Sandbox (Translated by the DroidKaigi Committee)
Intended audience
* Developers who handle or plan to handle native code (NDK/JNI) such as C/C++ in Android apps * Developers who feel challenged by security and vulnerability risks of third-party libraries * Developers interested in the practical use of WASM (WebAssembly) and Jetpack JavaScript Engine in the Android environment