wasm-micro-runtime: aarch64 AOT module fails to load on android

When trying to load an aarch64 AOT file on android, the module fails to load due to an overflow check failing, AOT module load failed: target address out of range..

Expected behaviour

aarch64 AOT module loads without error

Actual behaviour

aarch64 AOT module fails to load

Steps to reproduce

I’ve attached the example android studio project which reproduces the issue consistently on an arm64 emulator: WasmAOT.zip

C++ code to load the AOT module:

#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <jni.h>

#include <cstring>
#include <string>
#include <vector>

#include "wasm_export.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_wasmaot_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from wasm aot example";
    return env->NewStringUTF(hello.c_str());
}

extern "C" JNIEXPORT jint JNICALL
Java_com_example_wasmaot_MainActivity_loadWasmAOTModule(
        JNIEnv* env,
        jobject /* this */,
        jobject jasset_manager) {
    auto asset_manager = AAssetManager_fromJava(env, jasset_manager);

    auto aot_file_name = std::string("wamr_aot_test_") + AOT_FILE_SUFFIX + std::string(".aot");

    __android_log_print(ANDROID_LOG_INFO,"example.wasmaot", "%s", ("Loading AOT file " + aot_file_name).c_str());

    auto asset = AAssetManager_open(asset_manager, aot_file_name.c_str(), AASSET_MODE_BUFFER);

    if (!asset) {
        __android_log_print(ANDROID_LOG_ERROR,"example.wasmaot", "Failed to open AOT asset");
        return 1;
    }

    auto size = AAsset_getLength(asset);
    auto asset_buffer = AAsset_getBuffer(asset);

    std::vector<uint8_t> buf;
    buf.reserve(size);
    buf.assign(static_cast<const uint8_t*>(asset_buffer), static_cast<const uint8_t*>(asset_buffer) + size);

    AAsset_close(asset);

    wasm_runtime_init();

    char error_buf[128] = {};

    auto module = wasm_runtime_load(buf.data(), size, error_buf, sizeof(error_buf));

    if (module) {
        wasm_runtime_unload(module);
    } else {
        __android_log_print(ANDROID_LOG_ERROR,"example.wasmaot" ,"%s", error_buf);
        return 1;
    }

    wasm_runtime_destroy();

    return 0;
}

This is the rust code for the AOT module:

use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn main(_argv: *const *const c_char, _argc: i32) -> i32 {
    let mut a = 0;
    for i in 0..10 {
        a += i;
    }

    let mut buffer = Vec::new();

    buffer.extend([1, 2, 3]);

    println!("Hello world {:?}", buffer);
    println!("Hello world {:?}", a);

    0
}

which was compiled via cargo build --target=wasm32-wasi and

wamrc \
    --disable-simd \
    --target=aarch64 \
    --size-level=3 \
    -o "wamr_aot_test_aarch64.aot" \
    "hello_world.wasm"

The same rust code, when compiled for armv7 loads without issue on an armv7 android build.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Glad to know that WAMR is tested in nearly 1million devices, it is cool!