xwords/xwords4/android/jni/jniutlswrapper.c
Eric House cbb683fc74 fix env-mapping crash in jni due to race condition
As reported to google, dict iterator destruction was crashing due to a
race condition if it happened after a game using the same dict had been
closed since it needed a mapped env that the game closure would
remove. Fixed in two ways, one by adding the mapping prior to the code
that uses it (a common pattern: add happens many times, whenver it might
be needed, but remove only once), and second by passing env into the
code that was crashing.

The mapping stuff remains inherently racy and I'm not sure now how to
fix that. It depends on there being a place to unmap after which it's
guaranteed the mapped value won't be needed again. When two
objects (game and dict_iter in this case) map the same env/thread combo
there's a race.
2018-01-22 07:45:43 -08:00

124 lines
3.9 KiB
C

/* -*-mode: C; compile-command: "cd ..; ../scripts/ndkbuild.sh -j3"; -*- */
/*
* Copyright 2001-2010 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "strutils.h"
#include "jniutlswrapper.h"
#include "andutils.h"
struct JNIUtilCtxt {
EnvThreadInfo* ti;
jobject jjniutil;
MPSLOT;
};
JNIUtilCtxt*
makeJNIUtil( MPFORMAL JNIEnv* env, EnvThreadInfo* ti, jobject jniutls )
{
JNIUtilCtxt* ctxt = (JNIUtilCtxt*)XP_CALLOC( mpool, sizeof( *ctxt ) );
ctxt->ti = ti;
ctxt->jjniutil = (*env)->NewGlobalRef( env, jniutls );
MPASSIGN( ctxt->mpool, mpool );
return ctxt;
}
void
destroyJNIUtil( JNIEnv* env, JNIUtilCtxt** ctxtp )
{
JNIUtilCtxt* ctxt = *ctxtp;
if ( !!ctxt ) {
(*env)->DeleteGlobalRef( env, ctxt->jjniutil );
XP_FREE( ctxt->mpool, ctxt );
*ctxtp = NULL;
}
}
/* These are called from anddict.c, not via vtable */
#ifndef DROP_BITMAPS
jobject
and_util_makeJBitmap( JNIUtilCtxt* jniutil, int nCols, int nRows,
const jboolean* colors )
{
jobject bitmap;
JNIEnv* env = ENVFORME( jniutil->ti );
jmethodID mid
= getMethodID( env, jniutil->jjniutil, "makeBitmap",
"(II[Z)Landroid/graphics/drawable/BitmapDrawable;" );
jbooleanArray jcolors = makeBooleanArray( env, nCols*nRows, colors );
bitmap = (*env)->CallObjectMethod( env, jniutil->jjniutil, mid,
nCols, nRows, jcolors );
deleteLocalRef( env, jcolors );
return bitmap;
}
#endif
jobject
and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len,
XP_Bool isUTF8 )
{
jobject strarray = NULL;
JNIEnv* env = ENVFORME( jniutil->ti );
jmethodID mid
= getMethodID( env, jniutil->jjniutil, "splitFaces",
"([BZ)[[Ljava/lang/String;" );
jbyteArray jbytes = makeByteArray( env, len, (jbyte*)bytes );
strarray =
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jbytes, isUTF8 );
deleteLocalRef( env, jbytes );
return strarray;
}
jstring
and_util_getMD5SumForDict( JNIUtilCtxt* jniutil, const XP_UCHAR* name,
const XP_U8* bytes, jsize len )
{
JNIEnv* env = ENVFORME( jniutil->ti );
jmethodID mid = getMethodID( env, jniutil->jjniutil, "getMD5SumFor",
"(Ljava/lang/String;[B)Ljava/lang/String;" );
jstring jname = (*env)->NewStringUTF( env, name );
jbyteArray jbytes = NULL == bytes? NULL
: makeByteArray( env, len, (jbyte*)bytes );
jstring result =
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jname, jbytes );
deleteLocalRefs( env, jname, jbytes, DELETE_NO_REF );
return result;
}
#ifdef COMMS_CHECKSUM
jstring
and_util_getMD5SumForBytes( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len )
{
JNIEnv* env = ENVFORME( jniutil->ti );
jmethodID mid = getMethodID( env, jniutil->jjniutil, "getMD5SumFor",
"([B)Ljava/lang/String;" );
jbyteArray jbytes = NULL == bytes? NULL
: makeByteArray( env, len, (jbyte*)bytes );
jstring result =
(*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jbytes );
deleteLocalRef( env, jbytes );
return result;
}
#endif