From 85969fb913c06e08e6481ebce55b1eba5a9f3292 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 6 Sep 2020 15:55:56 -0700 Subject: [PATCH] seed random at start of jni, not of first game opened Noticed the same emulator would always generate the same MQTT id, even after a factory reset. That's because I was seeding rand() in that jni *game* init code, not the (called-earlier) init of the whole jni world. MQTT id generation happens on app launch before any game can be opened so was using an unseeded rand(). --- .../java/org/eehouse/android/xw4/jni/XwJNI.java | 13 ++++++++----- xwords4/android/jni/xwjni.c | 15 ++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java index 00a95a2bf..b7ddf995a 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java @@ -121,7 +121,11 @@ public class XwJNI { private long m_ptrGlobals; private XwJNI() { - m_ptrGlobals = initGlobals( new DUtilCtxt(), JNIUtilsImpl.get() ); + long seed = Utils.nextRandomInt(); + seed <<= 32; + seed |= Utils.nextRandomInt(); + seed ^= System.currentTimeMillis(); + m_ptrGlobals = globalsInit( new DUtilCtxt(), JNIUtilsImpl.get(), seed ); } public static void cleanGlobalsEmu() @@ -217,8 +221,7 @@ public class XwJNI { // Game methods private static GamePtr initGameJNI( long rowid ) { - int seed = Utils.nextRandomInt(); - long ptr = initGameJNI( getJNI().m_ptrGlobals, seed ); + long ptr = gameJNIInit( getJNI().m_ptrGlobals ); GamePtr result = 0 == ptr ? null : new GamePtr( ptr, rowid ); return result; } @@ -659,7 +662,7 @@ public class XwJNI { private static native int[] di_getIndices( long closure ); // Private methods -- called only here - private static native long initGlobals( DUtilCtxt dutil, JNIUtils jniu ); + private static native long globalsInit( DUtilCtxt dutil, JNIUtils jniu, long seed ); private static native String dvc_getMQTTDevID( long jniState, String[] topic ); private static native byte[] dvc_makeMQTTInvite( long jniState, NetLaunchInfo nli, String[] addrToTopic ); @@ -675,7 +678,7 @@ public class XwJNI { byte[] stream ); private static native byte[] nli_to_stream( long jniState, NetLaunchInfo nli ); private static native NetLaunchInfo nli_from_stream( long jniState, byte[] stream ); - private static native long initGameJNI( long jniState, int seed ); + private static native long gameJNIInit( long jniState ); private static native void envDone( long globals ); private static native long dict_make( long jniState, byte[] dict, String name, String path ); private static native void dict_ref( long dictPtr ); diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index d10c84eb2..51a9f01ca 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -367,13 +367,17 @@ getState( JNIEnv* env, GamePtrType gamePtr, const char* func ) #endif JNIEXPORT jlong JNICALL -Java_org_eehouse_android_xw4_jni_XwJNI_initGlobals -( JNIEnv* env, jclass C, jobject jdutil, jobject jniu ) +Java_org_eehouse_android_xw4_jni_XwJNI_globalsInit +( JNIEnv* env, jclass C, jobject jdutil, jobject jniu, jlong jseed ) { #ifdef MEM_DEBUG MemPoolCtx* mpool = mpool_make( NULL ); XP_LOGF( "%s(): ptr size: %zu", __func__, sizeof(mpool) ); #endif + int seed = (int)jseed; + XP_LOGFF( "calling srandom(seed %d)", seed ); + srandom( seed ); + JNIGlobalState* globalState = (JNIGlobalState*)XP_CALLOC( mpool, sizeof(*globalState) ); map_init( MPPARM(mpool) &globalState->ti, env ); @@ -1227,8 +1231,8 @@ struct _JNIState { } \ JNIEXPORT jlong JNICALL -Java_org_eehouse_android_xw4_jni_XwJNI_initGameJNI -( JNIEnv* env, jclass C, jlong jniGlobalPtr, jint seed ) +Java_org_eehouse_android_xw4_jni_XwJNI_gameJNIInit +( JNIEnv* env, jclass C, jlong jniGlobalPtr ) { #ifdef MEM_DEBUG MemPoolCtx* mpool = ((JNIGlobalState*)jniGlobalPtr)->mpool; @@ -1245,9 +1249,6 @@ Java_org_eehouse_android_xw4_jni_XwJNI_initGameJNI MPASSIGN( state->mpool, mpool ); globals->vtMgr = make_vtablemgr(MPPARM_NOCOMMA(mpool)); - /* XP_LOGF( "%s: initing srand with %d", __func__, seed ); */ - srandom( seed ); - /* LOG_RETURNF( "%p", state ); */ return (jlong) state; }