Skip to content
Snippets Groups Projects
Commit b9ef411d authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi
Browse files

[res] Better native pointer tracking in Java

Make sure we clear the native pointers when freeing them,
so any race condition that calls into it gets a null instead
of calling into already freed object

Bug: 197260547
Test: build + unit tests
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:7c2f195cfc8c02403d61a394213398d13584a5de)
Merged-In: I817dc2f5ef24e1fafeba3ce4c1c440abe70ad3ed
Change-Id: I817dc2f5ef24e1fafeba3ce4c1c440abe70ad3ed
parent 82883c24
No related branches found
No related tags found
No related merge requests found
......@@ -528,6 +528,10 @@ public final class AssetManager implements AutoCloseable {
if (!mOpen) {
throw new RuntimeException("AssetManager has been closed");
}
// Let's still check if the native object exists, given all the memory corruptions.
if (mObject == 0) {
throw new RuntimeException("AssetManager is open but the native object is gone");
}
}
/**
......@@ -1153,6 +1157,7 @@ public final class AssetManager implements AutoCloseable {
int[] getAttributeResolutionStack(long themePtr, @AttrRes int defStyleAttr,
@StyleRes int defStyleRes, @StyleRes int xmlStyle) {
synchronized (this) {
ensureValidLocked();
return nativeAttributeResolutionStack(
mObject, themePtr, xmlStyle, defStyleAttr, defStyleRes);
}
......
......@@ -62,7 +62,7 @@ public final class StringBlock implements Closeable {
private static final String TAG = "AssetManager";
private static final boolean localLOGV = false;
private final long mNative;
private long mNative; // final, but gets modified when closed
private final boolean mUseSparse;
private final boolean mOwnsNative;
......@@ -207,6 +207,7 @@ public final class StringBlock implements Closeable {
if (mOwnsNative) {
nativeDestroy(mNative);
}
mNative = 0;
}
}
}
......
......@@ -73,7 +73,9 @@ public final class XmlBlock implements AutoCloseable {
private void decOpenCountLocked() {
mOpenCount--;
if (mOpenCount == 0) {
mStrings.close();
nativeDestroy(mNative);
mNative = 0;
if (mAssets != null) {
mAssets.xmlBlockGone(hashCode());
}
......@@ -621,7 +623,7 @@ public final class XmlBlock implements AutoCloseable {
}
private @Nullable final AssetManager mAssets;
private final long mNative;
private long mNative; // final, but gets reset on close
/*package*/ final StringBlock mStrings;
private boolean mOpen = true;
private int mOpenCount = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment