Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip/kw/detect parser misuse #11137

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 71 additions & 21 deletions lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,42 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public final class Parser implements AutoCloseable {
private final static class OstensiblySuperfluousMutex {
private final Map<Thread, StackTraceElement[]> mutators = new HashMap<>();

private <R> R run(Supplier<R> action) {
var thisThread = Thread.currentThread();
synchronized (mutators) {
mutators.put(thisThread, thisThread.getStackTrace());
if (mutators.size() != 1) {
System.err.println("THREAD CONFLICT:");
var index = 0;
for (var entry : mutators.entrySet()) {
System.err.println("Thread " + index + ": " + entry.getKey());
System.err.println(Arrays.toString(entry.getValue()));
index += 1;
}
}
}
R result;
synchronized (this) {
result = action.get();
}
synchronized (mutators) {
mutators.remove(thisThread);
}
return result;
}
}

private final OstensiblySuperfluousMutex mutex = new OstensiblySuperfluousMutex();

private static void initializeLibraries() {
try {
System.loadLibrary("enso_parser");
Expand Down Expand Up @@ -75,10 +109,10 @@ private static boolean searchFromDirToTop(Throwable chain, File root, String...
return false;
}

private long state;
private long stateUnlessClosed;

private Parser(long stateIn) {
state = stateIn;
stateUnlessClosed = stateIn;
}

private static native long allocState();
Expand Down Expand Up @@ -108,30 +142,46 @@ public static Parser create() {
}

public long isIdentOrOperator(CharSequence input) {
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
return mutex.run(() -> {
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);

return isIdentOrOperator(inputBuf);
});
}

return isIdentOrOperator(inputBuf);
private long getState() {
if (stateUnlessClosed != 0) {
return stateUnlessClosed;
} else {
throw new IllegalStateException("Parser used after close()");
}
}

public ByteBuffer parseInputLazy(CharSequence input) {
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
return parseTreeLazy(state, inputBuf);
return mutex.run(() -> {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
return parseTreeLazy(state, inputBuf);
});
}

public Tree parse(CharSequence input) {
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
var serializedTree = parseTree(state, inputBuf);
var base = getLastInputBase(state);
var metadata = getMetadata(state);
serializedTree.order(ByteOrder.LITTLE_ENDIAN);
var message = new Message(serializedTree, input, base, metadata);
return Tree.deserialize(message);
return mutex.run(() -> {
var state = getState();
byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8);
ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length);
inputBuf.put(inputBytes);
var serializedTree = parseTree(state, inputBuf);
var base = getLastInputBase(state);
var metadata = getMetadata(state);
serializedTree.order(ByteOrder.LITTLE_ENDIAN);
var message = new Message(serializedTree, input, base, metadata);
return Tree.deserialize(message);
});
}

public static String getWarningMessage(Warning warning) {
Expand All @@ -140,7 +190,7 @@ public static String getWarningMessage(Warning warning) {

@Override
public void close() {
freeState(state);
state = 0;
freeState(stateUnlessClosed);
stateUnlessClosed = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Value getType(String moduleName, String typeName) {
var ex =
new NullPointerException(
"Cannot get type for " + moduleName + " type: " + typeName + " at " + module);
ex.initCause(ex);
ex.initCause(e);
throw ex;
}
}
Expand Down