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

Fix recursive array type cache load #2121

Merged
merged 2 commits into from
Nov 7, 2024
Merged
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
19 changes: 14 additions & 5 deletions src/main/java/soot/ArrayTypeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import heros.solver.Pair;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import soot.Singletons.Global;
Expand All @@ -36,7 +36,7 @@
* @author Marc Miltenberger
*/
public class ArrayTypeCache {
private final Map<Pair<Type, Integer>, ArrayType> cache = new ConcurrentHashMap<>();
private final Map<Pair<Type, Integer>, ArrayType> cache = new HashMap<>();

private final Function<Pair<Type, Integer>, ArrayType> mapping = new Function<Pair<Type, Integer>, ArrayType>() {

Expand All @@ -51,7 +51,7 @@ public ArrayType apply(Pair<Type, Integer> t) {
if (ret == null) {
int n = orgDimensions - numDimensions + 1;
if (n != orgDimensions) {
ret = cache.computeIfAbsent(new Pair<>(baseType, n), mapping);
ret = getArrayType(baseType, n);
} else {
ret = new ArrayType(baseType, n);
}
Expand All @@ -76,10 +76,19 @@ public ArrayTypeCache(Global g) {
* @param numDimensions the number of dimensions
* @return the array type
*/
public ArrayType getArrayType(Type baseType, int numDimensions) {
//We are doing this synchronized now to ensure correctness:
//Already creating a new ArrayType adds it to the type numberer, so we must not create
//the same array type twice. Furthermore, the ConcurrentHashMap's computeIfAbsent
//method does not allow the update of other keys in while a value is computed.
public synchronized ArrayType getArrayType(Type baseType, int numDimensions) {
Pair<Type, Integer> pairSearch = new Pair<>(baseType, numDimensions);
ArrayType res = cache.get(pairSearch);
if (res == null) {
res = mapping.apply(pairSearch);
cache.put(pairSearch, res);
}

return cache.computeIfAbsent(pairSearch, mapping);
return res;

}

Expand Down
Loading