Test for missing object slot information leading to memory leaks.

Bug: 7377851
Change-Id: I542ae45482b3644bb055c0bc70b58b3037edccbf
diff --git a/tests/res/raw/leak.bc b/tests/res/raw/leak.bc
new file mode 100644
index 0000000..327f25c
--- /dev/null
+++ b/tests/res/raw/leak.bc
Binary files differ
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/LeakTest.java b/tests/tests/renderscript/src/android/renderscript/cts/LeakTest.java
new file mode 100644
index 0000000..7662ffd
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/LeakTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.renderscript.cts;
+
+import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.Type;
+import android.util.Log;
+
+/*
+// -target-api 11
+
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+rs_allocation a;
+
+void print() {
+    rsDebug("unused", rsGetElementAt_int(a, 0, 0));
+}
+*/
+
+/**
+ * Test for memory leaks due to missing object slot information.
+ *
+ * The code to generate leak.bc is in the previous comment block.
+ * Note that you need to modify llvm-rs-cc to skip emitting the
+ * .rs.dtor() function, since it will also do the proper cleanup
+ * (but not trigger the original bug). Old HC code can trigger this
+ * bug, since it may have been compiled without .rs.dtor() support.
+ */
+public class LeakTest extends RSBaseCompute {
+    private final static int x = 16 * 1024 * 1024;
+    private static final String TAG = "LeakTest";
+
+    public void testForLeaks() {
+        for (int i = 0; i < 100; i++) {
+            Log.w(TAG, "Leak test iteration " + i);
+            ScriptC_leak leak = new ScriptC_leak(mRS);
+            Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(x).create();
+            Allocation A = Allocation.createTyped(mRS, t);
+            leak.set_a(A);
+            A = null;
+            System.gc();
+            leak.destroy();
+            mRS.finish();
+        }
+        mRS.finish();
+        checkForErrors();
+    }
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/ScriptC_leak.java b/tests/tests/renderscript/src/android/renderscript/cts/ScriptC_leak.java
new file mode 100644
index 0000000..c026755
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/ScriptC_leak.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2011-2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * This file is auto-generated. DO NOT MODIFY!
+ * The source Renderscript file: leak.rs
+ */
+package android.renderscript.cts;
+
+import android.renderscript.*;
+import android.content.res.Resources;
+
+/**
+ * @hide
+ */
+public class ScriptC_leak extends ScriptC {
+    private static final String __rs_resource_name = "leak";
+    // Constructor
+    public  ScriptC_leak(RenderScript rs) {
+        this(rs,
+             rs.getApplicationContext().getResources(),
+             rs.getApplicationContext().getResources().getIdentifier(
+                 __rs_resource_name, "raw",
+                 rs.getApplicationContext().getPackageName()));
+    }
+
+    public  ScriptC_leak(RenderScript rs, Resources resources, int id) {
+        super(rs, resources, id);
+    }
+
+    private FieldPacker __rs_fp_ALLOCATION;
+    private final static int mExportVarIdx_a = 0;
+    private Allocation mExportVar_a;
+    public synchronized void set_a(Allocation v) {
+        setVar(mExportVarIdx_a, v);
+        mExportVar_a = v;
+    }
+
+    public Allocation get_a() {
+        return mExportVar_a;
+    }
+
+    private final static int mExportFuncIdx_print = 0;
+    public void invoke_print() {
+        invoke(mExportFuncIdx_print);
+    }
+
+}
+