herumi / bls

288 stars 132 forks source link

Crash after invoke blsInit method - Android #96

Closed magtamas closed 1 year ago

magtamas commented 1 year ago

Hi!

After i import bls library into my android application. Some android device is crash after i invoke blsInit function.

ERROR:

2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A Build fingerprint: 'samsung/a13venseea/a13ve:12/SP1A.210812.016/A137FXXS1AVJ1:user/release-keys' 2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A Revision: '2' 2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A ABI: 'arm' 2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A Processor: '0' 2023-01-13 10:57:14.729 10953 DEBUG pid-10953 A Timestamp: 2023-01-13 10:57:13.700245253+0100 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A Process uptime: 2s 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A Cmdline: com.magtamas.blstest 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A pid: 10904, tid: 10904, name: agtamas.blstest >>> com.magtamas.blstest <<< 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A uid: 10279 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x6e70fb64 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A r0 6e70fb64 r1 6e70fb64 r2 9bb17cea r3 8cf11047 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A r4 56855da5 r5 56855da5 r6 ffab0970 r7 ffab07b0 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A r8 00000000 r9 f7a5de00 r10 6e70fb74 r11 ec33d17c 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A ip ffab0768 sp ffab06f4 lr c6a15637 pc c698e728 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A backtrace: 2023-01-13 10:57:14.730 10953 DEBUG pid-10953 A #00 pc 00076728 /data/app/~~RyyOV8uQ13FRWCS1f9Ajsw==/com.magtamas.blstest-0_RMWw2isnOkUxpPDK2DJw==/base.apk!libbls384_256.so (mclb_sub6+56) (BuildId: 037f4ade1a4e5f927b4075dffabc88dfc2282639) 2023-01-13 10:57:14.799 600 tombstoned pid-600 E Tombstone written to: tombstone_15

DEVICES:

Samsung Galaxy A13 (SM-A137F) Samsung Galaxy A52S 5G

On A52 if i update the os version to Android 13, my app is running well, the crash is gone, but on A13 this method was not helping.

SOME CODE

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Example of a call to a native method
    binding.sampleText.text = stringFromJNI()

    Hiver.init()
}

class Hiver {

companion object {
    private var isInitialized = false

    fun init() {
        if (Hiver.isInitialized) {
            return
        }
        Bls.init(BLS12_381)
        isInitialized = true
    }
    }

extern "C" JNIEXPORT jint JNICALL Java_com_magtamas_blstest_bls_BlsJNI_init(JNIEnv *env, jclass clazz, jint jarg1) { int arg1; (void) env; (void) clazz; arg1 = (int) jarg1; __android_log_print(ANDROID_LOG_VERBOSE, "Tag", "COMPILED_TIME_VAR: ", MCLBN_COMPILED_TIME_VAR); try { return (jint)blsInit(arg1, MCLBN_COMPILED_TIME_VAR); } catch (std::exception &_e) { __android_log_print(ANDROID_LOG_VERBOSE, "Tag", "CATH ERROR"); return 0; } }

Thanks if you can help me.

herumi commented 1 year ago

Samsung Galaxy A13 seems a new device, but is it not AArch64 but armv7?

magtamas commented 1 year ago

i attach cpu informations from A13

A13 CPU Info (same device, 2 device info app):

aida64

deviceInfo

Additional infomation: For build .so files (arm64-v8a, armeabi-v7a, x86_64) i follow the instructions from https://github.com/herumi/bls-eth-go-binary Android section

herumi commented 1 year ago

Could you try the old bls v1.20 ?

herumi commented 1 year ago

I found a mistake to build bint$(BIT).ll in Android.mk for armv7. Could you try the latest version in dev branch? https://github.com/herumi/bls/commit/64de88cde07deb8ba8f9c98fe4fba359ba42dffb

magtamas commented 1 year ago

I will try it, thanks. Just a question: I not sure i was used this Android.mk. I was mention it above, i ran the make commands from the Android section, that i found in https://github.com/herumi/bls-eth-go-binary. Can you pls confirm your changes will effect that commands? Or i need to use other way to build .so files?

herumi commented 1 year ago

After you can verify the modified Android.mk, I'll update the submodule bls of bls-eth-go-binary, then you can build it by yourself by make android in bls-eth-go-binary.

magtamas commented 1 year ago

I build .so files with Android.mk, but blsInit return with -1 in all device, because initPairing(&b, *cp); return false

if (!b) return -1;

int blsInit(int curve, int compiledTimeVar)
{
    if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) {
        return -(compiledTimeVar + (MCLBN_COMPILED_TIME_VAR * 1000));
    }
    const mcl::CurveParam* cp = mcl::getCurveParam(curve);
    if (cp == 0) return -1;
    bool b;
    initPairing(&b, *cp);
#ifdef __wasm__
//  G2::setMulArrayGLV(0);
#endif
    if (!b) return -1;
    g_curveType = curve;
magtamas commented 1 year ago

I call blsInit with BLS12_381 curveType (value = 5) and MCLBN_COMPILED_TIME_VAR (value = 46) which calculated from

#define MCLBN_FP_UNIT_SIZE 6
#define MCLBN_FR_UNIT_SIZE 4
herumi commented 1 year ago

Please define BLS_ETH before including bls/bls384_256.h?

herumi commented 1 year ago

cf. https://github.com/herumi/bls/#header-files

magtamas commented 1 year ago

I want to use bls without eth mode. (Sorry if i miss this information)

herumi commented 1 year ago

Then, could you build the library in the https://github.com/herumi/bls-go-binary with the modified Android.mk?

zsoltpete commented 1 year ago

@magtamas cannot answer that right now, but he tried and still not working

magtamas commented 1 year ago

I have successfully build your library with the changes. We need to run tests on some device, but your fix seems good

herumi commented 1 year ago

Thanks for the review. I've merged the fix into the master branch.