Skip to content
Prev Previous commit
Next Next commit
Avoid blocking to log from a non-PG thread
There isn't a perfect method available (yet) to inquire "could I
doInPG() or log() at this moment without blocking?", so this check
may unnecessarily send the message off to System.err on occasions
when log() would have worked fine. But it's better than blocking.

In passing, add a permission to read another property that the
Java runtime started using in Java 14 but forgot to give itself
permission to read. It's at the false-alarm level of severity, as
the runtime behaves gracefully when unable to read the property,
but again, if using TrialPolicy, it kind of spams the log.
  • Loading branch information
jcflack committed Nov 23, 2020
commit d2d03a320f149fcf3bd61073be2d94e821bda4a4
5 changes: 5 additions & 0 deletions pljava-packaging/src/main/resources/pljava.policy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ grant {
//
permission java.util.PropertyPermission
"jdk.lang.ref.disableClearBeforeEnqueue", "read";

// Something similar happened in Java 14 (not yet fixed in 15).
//
permission java.util.PropertyPermission
"java.util.concurrent.ForkJoinPool.common.maximumSpares", "read";
};


Expand Down
19 changes: 16 additions & 3 deletions pljava/src/main/java/org/postgresql/pljava/policy/TrialPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static org.postgresql.pljava.elog.ELogHandler.LOG_LOG;
import static org.postgresql.pljava.internal.Backend.log;
import static org.postgresql.pljava.internal.Backend.threadMayEnterPG;
import static org.postgresql.pljava.internal.Privilege.doPrivileged;

/**
Expand Down Expand Up @@ -203,7 +204,8 @@ public boolean implies(
/*
* Construct a string representation of the trace.
*/
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(
"POLICY DENIES/TRIAL POLICY ALLOWS: " + permission + '\n');
Iterator<StackTraceElement> it = stack.iterator();
int i = 0;
for ( ;; )
Expand All @@ -222,8 +224,19 @@ public boolean implies(
sb.append('\n');
}

log(LOG_LOG,
"POLICY DENIES/TRIAL POLICY ALLOWS: " + permission + '\n' + sb);
/*
* This is not the best way to avoid blocking on log(); in some flavors
* of pljava.java_thread_pg_entry, threadMayEnterPG can return false
* simply because it's not /known/ that PG could be entered right now,
* and this could send the message off to System.err at times even if
* log() would have completed with no blocking. But the always accurate
* "could I enter PG right now without blocking?" method isn't provided
* yet.
*/
if ( threadMayEnterPG() )
log(LOG_LOG, sb.toString());
else
System.err.println(sb);

return true;
}
Expand Down