Skip to content

Commit ee6fab5

Browse files
Quickfix for sosy-lab/java-smt#347
1 parent 8701a55 commit ee6fab5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: src/api/java/jni/api_utilities.h

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cvc5/cvc5_parser.h>
2020
#include <jni.h>
2121

22+
#include <mutex>
2223
#include <string>
2324
#include <vector>
2425

@@ -149,8 +150,15 @@ jobject getBooleanObject(JNIEnv* env, bool value);
149150
* a map from solver pointers to global references that need to be freed when
150151
* the java Solver.deletePointer method is called
151152
*/
153+
152154
inline std::map<jlong, std::vector<jobject> > globalReferences;
153155

156+
/**
157+
* a lock to protect globalReferences
158+
*/
159+
160+
inline std::mutex globalLock;
161+
154162
/**
155163
* @param env jni environment
156164
* @param solverRef a global reference to java Solver object

Diff for: src/api/java/jni/solver.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ JNIEXPORT void JNICALL Java_io_github_cvc5_Solver_deletePointer(JNIEnv* env,
4141
jobject,
4242
jlong pointer)
4343
{
44+
std::lock_guard<std::mutex> guard(globalLock);
4445
const std::vector<jobject>& refs = globalReferences[pointer];
4546
for (jobject ref : refs)
4647
{
@@ -2437,6 +2438,7 @@ Java_io_github_cvc5_Solver_declareOracleFun(JNIEnv* env,
24372438
jobject oracle)
24382439
{
24392440
CVC5_JAVA_API_TRY_CATCH_BEGIN;
2441+
std::lock_guard<std::mutex> guard(globalLock);
24402442
jobject oracleReference = env->NewGlobalRef(oracle);
24412443
globalReferences[pointer].push_back(oracleReference);
24422444
Solver* solver = reinterpret_cast<Solver*>(pointer);
@@ -2446,6 +2448,13 @@ Java_io_github_cvc5_Solver_declareOracleFun(JNIEnv* env,
24462448
std::vector<Sort> sorts = getObjectsFromPointers<Sort>(env, sortPointers);
24472449
std::function<Term(std::vector<Term>)> fn =
24482450
[env, oracleReference](std::vector<Term> input) {
2451+
// FIXME:
2452+
// This is likely still broken if multiple threads are
2453+
// used as the captured JEnv ("env") is only valid on the
2454+
// current thread. However, the callback might come from
2455+
// any thread that encouters a term for the oracle.
2456+
//
2457+
// See https://github.com/sosy-lab/java-smt/pull/345
24492458
Term term = applyOracle(env, oracleReference, input);
24502459
return term;
24512460
};

0 commit comments

Comments
 (0)