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().
This commit is contained in:
Eric House 2020-09-06 15:55:56 -07:00
parent 786bb39941
commit 85969fb913
2 changed files with 16 additions and 12 deletions

View file

@ -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 );

View file

@ -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;
}