From b018c32f9d0ba963560fa08da84802c23c41d89d Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 28 Aug 2020 18:23:47 +0100 Subject: Fix crashes in swig_connect_director during director class construction. Occurs when using the director class from multiple threads - a race condition initialising block scope static variables. Block scope static variables are guaranteed to be thread safe in C++11, so the fix is guaranteed when using C++11. However, most modern compilers also fix it when using C++03/C++98. Closes #1862 --- CHANGES.current | 6 ++++++ Lib/java/director.swg | 14 ++++++++++++++ Source/Modules/java.cxx | 31 ++++++++++--------------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 2eb67c929..e557ec9ad 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2020-08-28: wsfulton + [Java] #1862 Fix crashes in swig_connect_director during director class construction + when using the director class from multiple threads - a race condition initialising + block scope static variables. The fix is guaranteed when using C++11, but most + compilers also fix it when using C++03/C++98. + 2020-08-16: wsfulton [Python] Add missing initializer for member ‘_heaptypeobject::ht_module’ when using -builtin to complete Python 3.9 support. diff --git a/Lib/java/director.swg b/Lib/java/director.swg index d3bd162ec..e911a3da7 100644 --- a/Lib/java/director.swg +++ b/Lib/java/director.swg @@ -215,6 +215,15 @@ namespace Swig { } }; + struct SwigDirectorMethod { + const char *name; + const char *desc; + jmethodID methid; + SwigDirectorMethod(JNIEnv *jenv, jclass baseclass, const char *name, const char *desc) : name(name), desc(desc) { + methid = jenv->GetMethodID(baseclass, name, desc); + } + }; + /* Java object wrapper */ JObjectWrapper swig_self_; @@ -238,6 +247,11 @@ namespace Swig { } } + jclass swig_new_global_ref(JNIEnv *jenv, const char *classname) { + jclass clz = jenv->FindClass(classname); + return clz ? (jclass)jenv->NewGlobalRef(clz) : 0; + } + public: Director(JNIEnv *jenv) : swig_jvm_((JavaVM *) NULL), swig_self_() { /* Acquire the Java VM pointer */ diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index 7734c6471..231c6c0cb 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -4835,34 +4835,27 @@ public: // .'s to delimit namespaces, so we need to replace those with /'s Replace(internal_classname, NSPACE_SEPARATOR, "/", DOH_REPLACE_ANY); - Wrapper_add_localv(w, "baseclass", "static jclass baseclass", "= 0", NIL); Printf(w->def, "void %s::swig_connect_director(JNIEnv *jenv, jobject jself, jclass jcls, bool swig_mem_own, bool weak_global) {", director_classname); + Printf(w->def, "static jclass baseclass = swig_new_global_ref(jenv, \"%s\");\n", internal_classname); + Printf(w->def, "if (!baseclass) return;\n"); + if (first_class_dmethod != curr_class_dmethod) { - Printf(w->def, "static struct {\n"); - Printf(w->def, "const char *mname;\n"); - Printf(w->def, "const char *mdesc;\n"); - Printf(w->def, "jmethodID base_methid;\n"); - Printf(w->def, "} methods[] = {\n"); + Printf(w->def, "static SwigDirectorMethod methods[] = {\n"); for (int i = first_class_dmethod; i < curr_class_dmethod; ++i) { UpcallData *udata = Getitem(dmethods_seq, i); - Printf(w->def, "{ \"%s\", \"%s\", NULL }", Getattr(udata, "method"), Getattr(udata, "fdesc")); + Printf(w->def, "SwigDirectorMethod(jenv, baseclass, \"%s\", \"%s\")", Getattr(udata, "method"), Getattr(udata, "fdesc")); if (i != curr_class_dmethod - 1) Putc(',', w->def); Putc('\n', w->def); } - Printf(w->def, "};\n"); + Printf(w->def, "};"); } Printf(w->code, "if (swig_set_self(jenv, jself, swig_mem_own, weak_global)) {\n"); - Printf(w->code, "if (!baseclass) {\n"); - Printf(w->code, "baseclass = jenv->FindClass(\"%s\");\n", internal_classname); - Printf(w->code, "if (!baseclass) return;\n"); - Printf(w->code, "baseclass = (jclass) jenv->NewGlobalRef(baseclass);\n"); - Printf(w->code, "}\n"); int n_methods = curr_class_dmethod - first_class_dmethod; @@ -4877,12 +4870,8 @@ public: /* Emit the code to look up the class's methods, initialize the override array */ - Printf(w->code, "bool derived = (jenv->IsSameObject(baseclass, jcls) ? false : true);\n"); - Printf(w->code, "for (int i = 0; i < %d; ++i) {\n", n_methods); - Printf(w->code, " if (!methods[i].base_methid) {\n"); - Printf(w->code, " methods[i].base_methid = jenv->GetMethodID(baseclass, methods[i].mname, methods[i].mdesc);\n"); - Printf(w->code, " if (!methods[i].base_methid) return;\n"); - Printf(w->code, " }\n"); + Printf(w->code, " bool derived = (jenv->IsSameObject(baseclass, jcls) ? false : true);\n"); + Printf(w->code, " for (int i = 0; i < %d; ++i) {\n", n_methods); // Generally, derived classes have a mix of overridden and // non-overridden methods and it is worth making a GetMethodID // check during initialization to determine if each method is @@ -4902,8 +4891,8 @@ public: } else { Printf(w->code, " swig_override[i] = false;\n"); Printf(w->code, " if (derived) {\n"); - Printf(w->code, " jmethodID methid = jenv->GetMethodID(jcls, methods[i].mname, methods[i].mdesc);\n"); - Printf(w->code, " swig_override[i] = (methid != methods[i].base_methid);\n"); + Printf(w->code, " jmethodID methid = jenv->GetMethodID(jcls, methods[i].name, methods[i].desc);\n"); + Printf(w->code, " swig_override[i] = methods[i].methid && (methid != methods[i].methid);\n"); Printf(w->code, " jenv->ExceptionClear();\n"); Printf(w->code, " }\n"); } -- cgit v1.2.1