Merge "Revert "Reorganize the header files in a way that's easier to document.""
diff --git a/cpu_ref/rsCpuExecutable.cpp b/cpu_ref/rsCpuExecutable.cpp
index 86e7294..98f9ef8 100644
--- a/cpu_ref/rsCpuExecutable.cpp
+++ b/cpu_ref/rsCpuExecutable.cpp
@@ -11,7 +11,6 @@
 #include <unistd.h>
 #else
 #include "bcc/Config/Config.h"
-#include <sys/wait.h>
 #endif
 
 #include <dlfcn.h>
@@ -132,42 +131,8 @@
         nullptr
     };
 
-    std::unique_ptr<const char> joined(
-        rsuJoinStrings(args.size()-1, args.data()));
-    std::string cmdLineStr (joined.get());
+    return rsuExecuteCommand(LD_EXE_PATH, args.size()-1, args.data());
 
-    pid_t pid = fork();
-
-    switch (pid) {
-    case -1: {  // Error occurred (we attempt no recovery)
-        ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
-        return false;
-    }
-    case 0: {  // Child process
-        ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
-        execv(LD_EXE_PATH, (char* const*) args.data());
-
-        ALOGE("execv() failed: %s", strerror(errno));
-        abort();
-        return false;
-    }
-    default: {  // Parent process (actual driver)
-        // Wait on child process to finish compiling the source.
-        int status = 0;
-        pid_t w = waitpid(pid, &status, 0);
-        if (w == -1) {
-            ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
-            return false;
-        }
-
-        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-            return true;
-        }
-
-        ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
-        return false;
-    }
-    }
 }
 
 #endif  // RS_COMPATIBILITY_LIB
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index 481c54d..6099cf4 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -23,6 +23,8 @@
     #include <sys/stat.h>
     #include <unistd.h>
 #else
+    #include "rsCppUtils.h"
+
     #include <bcc/BCCContext.h>
     #include <bcc/Config/Config.h>
     #include <bcc/Renderscript/RSCompilerDriver.h>
@@ -32,7 +34,6 @@
     #include <zlib.h>
     #include <sys/file.h>
     #include <sys/types.h>
-    #include <sys/wait.h>
     #include <unistd.h>
 
     #include <string>
@@ -122,8 +123,7 @@
 static bool compileBitcode(const std::string &bcFileName,
                            const char *bitcode,
                            size_t bitcodeSize,
-                           const char **compileArguments,
-                           const char *compileCommandLine) {
+                           std::vector<const char *> &compileArguments) {
     rsAssert(bitcode && bitcodeSize);
 
     FILE *bcfile = fopen(bcFileName.c_str(), "w");
@@ -139,39 +139,9 @@
         return false;
     }
 
-    pid_t pid = fork();
-
-    switch (pid) {
-    case -1: {  // Error occurred (we attempt no recovery)
-        ALOGE("Couldn't fork for bcc compiler execution");
-        return false;
-    }
-    case 0: {  // Child process
-        ALOGV("Invoking BCC with: %s", compileCommandLine);
-        execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
-              (char* const*)compileArguments);
-
-        ALOGE("execv() failed: %s", strerror(errno));
-        abort();
-        return false;
-    }
-    default: {  // Parent process (actual driver)
-        // Wait on child process to finish compiling the source.
-        int status = 0;
-        pid_t w = waitpid(pid, &status, 0);
-        if (w == -1) {
-            ALOGE("Could not wait for bcc compiler");
-            return false;
-        }
-
-        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-            return true;
-        }
-
-        ALOGE("bcc compiler terminated unexpectedly");
-        return false;
-    }
-    }
+    return android::renderscript::rsuExecuteCommand(
+                   android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
+                   compileArguments.size()-1, compileArguments.data());
 }
 
 bool isChecksumNeeded() {
@@ -200,7 +170,7 @@
             break;
     }
 
-    if (close(FD) != 0) {
+    if (TEMP_FAILURE_RETRY(close(FD)) != 0) {
         ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
         return false;
     }
@@ -360,16 +330,17 @@
     setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
                         useRSDebugContext, bccPluginName);
 
-    // The last argument of compileArguments is a nullptr, so remove 1 from the
-    // size.
-    std::unique_ptr<const char> compileCommandLine(
-        rsuJoinStrings(compileArguments.size() - 1, compileArguments.data()));
-
     mChecksumNeeded = isChecksumNeeded();
     if (mChecksumNeeded) {
         std::vector<const char *> bccFiles = { BCC_EXE_PATH,
                                                core_lib,
                                              };
+
+        // The last argument of compileArguments is a nullptr, so remove 1 from
+        // the size.
+        std::unique_ptr<const char> compileCommandLine(
+            rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
+
         mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
                                                 compileCommandLine.get(),
                                                 bccFiles);
@@ -393,10 +364,6 @@
     compileArguments.push_back(mBuildChecksum);
     compileArguments.push_back(nullptr);
 
-    // recompute compileCommandLine with the extra arguments
-    compileCommandLine.reset(
-        rsuJoinStrings(compileArguments.size() - 1, compileArguments.data()));
-
     if (!is_force_recompile() && !useRSDebugContext) {
         mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
 
@@ -411,7 +378,7 @@
     // again.
     if (mScriptSO == nullptr) {
         if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
-                            compileArguments.data(), compileCommandLine.get()))
+                            compileArguments))
         {
             ALOGE("bcc: FAILS to compile '%s'", resName);
             mCtx->unlockMutex();
diff --git a/cpu_ref/rsCpuScriptGroup2.cpp b/cpu_ref/rsCpuScriptGroup2.cpp
index 2e50ecb..3a50221 100644
--- a/cpu_ref/rsCpuScriptGroup2.cpp
+++ b/cpu_ref/rsCpuScriptGroup2.cpp
@@ -12,7 +12,6 @@
 
 #ifndef RS_COMPATIBILITY_LIB
 #include "bcc/Config/Config.h"
-#include <sys/wait.h>
 #endif
 
 #include "cpu_ref/rsCpuCore.h"
@@ -264,40 +263,6 @@
     args->push_back(nullptr);
 }
 
-bool fuseAndCompile(const char** arguments,
-                    const string& commandLine) {
-    const pid_t pid = fork();
-
-    if (pid == -1) {
-        ALOGE("Couldn't fork for bcc execution");
-        return false;
-    }
-
-    if (pid == 0) {
-        // Child process
-        ALOGV("Invoking BCC with: %s", commandLine.c_str());
-        execv(RsdCpuScriptImpl::BCC_EXE_PATH, (char* const*)arguments);
-
-        ALOGE("execv() failed: %s", strerror(errno));
-        abort();
-        return false;
-    }
-
-    // Parent process
-    int status = 0;
-    const pid_t w = waitpid(pid, &status, 0);
-    if (w == -1) {
-        return false;
-    }
-
-    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0 ) {
-        ALOGE("bcc terminated unexpectedly");
-        return false;
-    }
-
-    return true;
-}
-
 void generateSourceSlot(const Closure& closure,
                         const std::vector<std::string>& inputs,
                         std::stringstream& ss) {
@@ -384,13 +349,14 @@
     const string& coreLibPath = getCoreLibPath(getCpuRefImpl()->getContext(),
                                                &coreLibRelaxedPath);
     vector<const char*> arguments;
-    setupCompileArguments(inputs, kernelBatches, invokeBatches, cacheDir,
+    string output_dir(cacheDir);
+    setupCompileArguments(inputs, kernelBatches, invokeBatches, output_dir,
                           outputFileName, coreLibPath, coreLibRelaxedPath, &arguments);
-    std::unique_ptr<const char> joined(
-        rsuJoinStrings(arguments.size() - 1, arguments.data()));
-    string commandLine (joined.get());
 
-    if (!fuseAndCompile(arguments.data(), commandLine)) {
+    bool compiled = rsuExecuteCommand(RsdCpuScriptImpl::BCC_EXE_PATH,
+                                     arguments.size()-1,
+                                     arguments.data());
+    if (!compiled) {
         unlink(objFilePath.c_str());
         return;
     }
diff --git a/driver/rsdAllocation.cpp b/driver/rsdAllocation.cpp
index eb57760..3467086 100644
--- a/driver/rsdAllocation.cpp
+++ b/driver/rsdAllocation.cpp
@@ -482,9 +482,6 @@
         return;
     }
 
-    uint8_t * ptrA = (uint8_t *)base->getPointerUnchecked(alloc->mHal.state.originX, alloc->mHal.state.originY);
-    uint8_t * ptrB = (uint8_t *)base->getPointerUnchecked(0, 0);
-
     //ALOGE("rsdAllocationAdapterOffset  %p  %p", ptrA, ptrB);
     //ALOGE("rsdAllocationAdapterOffset  lodCount %i", alloc->mHal.drvState.lodCount);
 
@@ -492,9 +489,9 @@
     uint32_t lodCount = rsMax(alloc->mHal.drvState.lodCount, (uint32_t)1);
     for (uint32_t lod=0; lod < lodCount; lod++) {
         alloc->mHal.drvState.lod[lod] = base->mHal.drvState.lod[lod + lodBias];
-        alloc->mHal.drvState.lod[lod].mallocPtr =
-                ((uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr + (ptrA - ptrB));
-        //ALOGE("rsdAllocationAdapterOffset  lod  %p  %i %i", alloc->mHal.drvState.lod[lod].mallocPtr, alloc->mHal.drvState.lod[lod].dimX, alloc->mHal.drvState.lod[lod].dimY);
+        alloc->mHal.drvState.lod[lod].mallocPtr = GetOffsetPtr(alloc,
+                      alloc->mHal.state.originX, alloc->mHal.state.originY, alloc->mHal.state.originZ,
+                      lodBias, (RsAllocationCubemapFace)alloc->mHal.state.originFace);
     }
 }
 
diff --git a/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java b/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
index e2de83a..504ed64 100644
--- a/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
+++ b/java/tests/RsTest/src/com/android/rs/test/RSTestCore.java
@@ -66,7 +66,7 @@
         unitTests.add(new UT_kernel(this, mRes, mCtx));
         unitTests.add(new UT_kernel_struct(this, mRes, mCtx));
         unitTests.add(new UT_kernel2d(this, mRes, mCtx));
-//      unitTests.add(new UT_kernel3d(this, mRes, mCtx)); // does not pass reliably yet
+        unitTests.add(new UT_kernel3d(this, mRes, mCtx));
         unitTests.add(new UT_kernel2d_oldstyle(this, mRes, mCtx));
         unitTests.add(new UT_ctxt_default(this, mRes, mCtx));
         unitTests.add(new UT_bug_char(this, mRes, mCtx));
@@ -85,6 +85,8 @@
         unitTests.add(new UT_foreach(this, mRes, mCtx));
         unitTests.add(new UT_foreach_bounds(this, mRes, mCtx));
         unitTests.add(new UT_noroot(this, mRes, mCtx));
+        unitTests.add(new UT_script_group2_pointwise(this, mRes, mCtx));
+        unitTests.add(new UT_script_group2_gatherscatter(this, mRes, mCtx));
         unitTests.add(new UT_atomic(this, mRes, mCtx));
         unitTests.add(new UT_struct(this, mRes, mCtx));
         unitTests.add(new UT_math(this, mRes, mCtx));
diff --git a/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_gatherscatter.java b/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_gatherscatter.java
new file mode 100644
index 0000000..1e74e14
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_gatherscatter.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2015 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 com.android.rs.test;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.util.Log;
+import java.lang.Thread;
+import java.util.HashMap;
+
+public class UT_script_group2_gatherscatter extends UnitTest {
+    private Resources mRes;
+
+    private static final int ARRAY_SIZE = 256;
+
+    private static final String TAG = "ScriptGroup2 (GatherScatter)";
+
+    int[] mArray;
+
+    protected UT_script_group2_gatherscatter(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, TAG, ctx);
+        mRes = res;
+    }
+
+    public void initializeGlobals(RenderScript RS, ScriptC_addup s) {
+        mArray = new int[ARRAY_SIZE * 4];
+
+        for (int i = 0; i < ARRAY_SIZE; i++) {
+            mArray[i*4] = i * 7;
+            mArray[i*4 + 1] = i * 7 + 1;
+            mArray[i*4 + 2] = i * 7 + 2;
+            mArray[i*4 + 3] = i * 7 + 3;
+        }
+    }
+
+    // This test tests ScriptGroup2 API for handling gather scatter operations
+    // on global allocations that are passed across kernels in a script group.
+    // The test sums up all elements in the input int4 array of size ARRAY_SIZE.
+    // To do so, it adds up the second half of the array to its first half using
+    // kernel function add() in addsup.rs, and then repeatedly applies the same
+    // kernel function to the shrinking result arrays until the result is a
+    // single int4 value.
+    // These steps are created as a script group by repeatedly adding the
+    // same kernel function, with the input of one kernel being the output of
+    // the previous added kernel function.
+    // Since the kernel function relies on rsGetElementAt to access the counterpart
+    // of the current element in the second half of the array, the compiler cannot
+    // fuse it with the other kernel that it dependes on.
+    // This test verifies an ScriptGroup2 implementation correctly handles such
+    // a case.
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_addup s = new ScriptC_addup(pRS);
+        pRS.setMessageHandler(mRsMessage);
+        initializeGlobals(pRS, s);
+
+        Allocation input = Allocation.createSized(pRS, Element.I32_4(pRS), ARRAY_SIZE);
+        input.copyFrom(mArray);
+
+        ScriptGroup2.Builder builder = new ScriptGroup2.Builder(pRS);
+
+        HashMap<Script.FieldID, Object> map = new HashMap<Script.FieldID, Object>();
+
+        ScriptGroup2.UnboundValue unbound = builder.addInput();
+
+        ScriptGroup2.Closure c = null;
+        ScriptGroup2.Future f = null;
+        int stride;
+        for (stride = ARRAY_SIZE / 2; stride >= 1; stride >>= 1) {
+            map.put(s.getFieldID_reduction_stride(), new Integer(stride));
+            if (f == null) {
+                map.put(s.getFieldID_a_in(), unbound);
+            } else {
+                map.put(s.getFieldID_a_in(), f);
+            }
+            c = builder.addKernel(s.getKernelID_add(),
+                                  Type.createX(pRS, Element.I32_4(pRS), stride),
+                                  new Object[0],
+                                  map);
+            f = c.getReturn();
+        }
+
+        ScriptGroup2 group = builder.create(c.getReturn());
+
+        if (c == null) {
+            return;
+        }
+
+        int[] a = new int[4];
+        ((Allocation)group.execute(input)[0]).copyTo(a);
+
+        pRS.finish();
+        pRS.destroy();
+
+        boolean failed = false;
+        for (int i = 0; i < 4; i++) {
+            if (failed == false &&
+                a[i] != ARRAY_SIZE * (ARRAY_SIZE - 1) * 7 / 2 + i * ARRAY_SIZE) {
+                Log.e(TAG, "a["+i+"]="+a[i]+", should be "+
+                      (ARRAY_SIZE * (ARRAY_SIZE - 1) * 7 / 2 + i * ARRAY_SIZE));
+                failed = true;
+            }
+        }
+        if (failed) {
+            failTest();
+            return;
+        }
+        passTest();
+    }
+}
diff --git a/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_pointwise.java b/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_pointwise.java
new file mode 100644
index 0000000..c02ae20
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/UT_script_group2_pointwise.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2015 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 com.android.rs.test;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+import android.util.Log;
+import java.lang.Thread;
+import java.util.HashMap;
+
+public class UT_script_group2_pointwise extends UnitTest {
+    private Resources mRes;
+
+    private static final int ARRAY_SIZE = 256;
+
+    private static final String TAG = "ScritGroup2 (Pointwise)";
+
+    protected UT_script_group2_pointwise(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, TAG, ctx);
+        mRes = res;
+    }
+
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_increment s_inc = new ScriptC_increment(pRS);
+        ScriptC_double s_double = new ScriptC_double(pRS);
+        pRS.setMessageHandler(mRsMessage);
+
+        int[] array = new int[ARRAY_SIZE * 4];
+
+        for (int i = 0; i < ARRAY_SIZE * 4; i++) {
+            array[i] = i;
+        }
+
+        Allocation input = Allocation.createSized(pRS, Element.I32_4(pRS), ARRAY_SIZE);
+        input.copyFrom(array);
+
+        ScriptGroup2.Builder builder = new ScriptGroup2.Builder(pRS);
+
+        HashMap<Script.FieldID, Object> map = new HashMap<Script.FieldID, Object>();
+
+        ScriptGroup2.UnboundValue unbound = builder.addInput();
+
+        ScriptGroup2.Closure c0 =
+                builder.addKernel(s_inc.getKernelID_increment(),
+                                  Type.createX(pRS, Element.I32_4(pRS), ARRAY_SIZE),
+                                  new Object[]{unbound}, map);
+
+        ScriptGroup2.Closure c1 =
+                builder.addKernel(s_double.getKernelID_doubleKernel(),
+                                  Type.createX(pRS, Element.I32_4(pRS), ARRAY_SIZE),
+                                  new Object[]{c0.getReturn()}, map);
+
+        ScriptGroup2 group = builder.create(c1.getReturn());
+
+        int[] a = new int[ARRAY_SIZE * 4];
+        ((Allocation)group.execute(input)[0]).copyTo(a);
+
+        pRS.finish();
+        pRS.destroy();
+
+        boolean failed = false;
+        for (int i = 0; i < ARRAY_SIZE * 4; i++) {
+            if (a[i] != (i+1) * 2) {
+                Log.e(TAG, "a["+i+"]="+a[i]+", should be "+ ((i+1) * 2));
+                failed = true;
+            }
+        }
+        if (failed) {
+            failTest();
+            return;
+        }
+        passTest();
+    }
+}
diff --git a/java/tests/RsTest/src/com/android/rs/test/addup.rs b/java/tests/RsTest/src/com/android/rs/test/addup.rs
new file mode 100644
index 0000000..afc6a08
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/addup.rs
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#pragma version(1)
+#pragma rs java_package_name(com.android.rs.test)
+
+rs_allocation a_in;
+int reduction_stride;
+
+int4 RS_KERNEL add(uint x)
+{
+    return rsGetElementAt_int4(a_in, x) + rsGetElementAt_int4(a_in, x + reduction_stride);
+}
diff --git a/java/tests/RsTest/src/com/android/rs/test/double.rs b/java/tests/RsTest/src/com/android/rs/test/double.rs
new file mode 100644
index 0000000..a1b7097
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/double.rs
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#pragma version(1)
+#pragma rs java_package_name(com.android.rs.test)
+
+int4 RS_KERNEL doubleKernel(int4 in)
+{
+    return in * 2;
+}
\ No newline at end of file
diff --git a/java/tests/RsTest/src/com/android/rs/test/increment.rs b/java/tests/RsTest/src/com/android/rs/test/increment.rs
new file mode 100644
index 0000000..0c5de01
--- /dev/null
+++ b/java/tests/RsTest/src/com/android/rs/test/increment.rs
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#pragma version(1)
+#pragma rs java_package_name(com.android.rs.test)
+
+int4 RS_KERNEL increment(int4 in)
+{
+    return in + 1;
+}
diff --git a/rsContext.cpp b/rsContext.cpp
index 75f927e..233926c 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -459,6 +459,7 @@
     mForceCpu = false;
     mContextType = RS_CONTEXT_TYPE_NORMAL;
     mSynchronous = false;
+    mFatalErrorOccured = false;
 }
 
 Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
@@ -725,6 +726,13 @@
 
 void Context::setError(RsError e, const char *msg) const {
     mError = e;
+
+    if (mError >= RS_ERROR_FATAL_DEBUG) {
+        // If a FATAL error occurred, set the flag to indicate the process
+        // will be goign down
+        mFatalErrorOccured = true;
+    }
+
     sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
 }
 
diff --git a/rsContext.h b/rsContext.h
index 459550c..1c6fc58 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -246,6 +246,12 @@
     RsContextType getContextType() const { return mContextType; }
     void setContextType(RsContextType ct) { mContextType = ct; }
 
+    // Check for Fatal errors
+    // Should be used to prevent work from being launched
+    // which could take the process down.  Maximizes the chance
+    // the process lives long enough to get the error to the developer
+    bool hadFatalError() {return mFatalErrorOccured;}
+
     Device *mDev;
 
 #ifdef RS_COMPATIBILITY_LIB
@@ -273,8 +279,10 @@
     bool mRunning;
     bool mExit;
     bool mPaused;
+    mutable bool mFatalErrorOccured;
     mutable RsError mError;
 
+
     pthread_t mThreadId;
     pid_t mNativeThreadId;
 
diff --git a/rsCppUtils.cpp b/rsCppUtils.cpp
index c9a19c2..b55f9e6 100644
--- a/rsCppUtils.cpp
+++ b/rsCppUtils.cpp
@@ -21,6 +21,10 @@
 
 #include <string.h>
 
+#ifndef RS_COMPATIBILITY_LIB
+#include <sys/wait.h>
+#endif
+
 namespace android {
 namespace renderscript {
 
@@ -46,5 +50,45 @@
     return strndup(tmp.c_str(), tmp.size());
 }
 
+#ifndef RS_COMPATIBILITY_LIB
+bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args) {
+    std::unique_ptr<const char> joined(rsuJoinStrings(nArgs, args));
+
+    pid_t pid = fork();
+
+    switch (pid) {
+    case -1: {  // Error occurred (we attempt no recovery)
+        ALOGE("Fork of \"%s\" failed with error %s", exe, strerror(errno));
+        return false;
+    }
+    case 0: {  // Child process
+        ALOGV("Invoking %s with args '%s'", exe, joined.get());
+        execv(exe, (char * const *)args);
+
+        ALOGE("execv() failed: %s", strerror(errno));
+        abort();
+        return false;
+    }
+    default: {  // Parent process (actual driver)
+        // Wait on child process to finish execution.
+        int status = 0;
+        pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
+        if (w == -1) {
+            ALOGE("Waitpid of \"%s\" failed with error %s", exe,
+                  strerror(errno));
+            return false;
+        }
+
+        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+            return true;
+        }
+
+        ALOGE("Child process \"%s\" terminated with status %d", exe, status);
+        return false;
+    }
+    }
+}
+#endif // RS_COMPATIBILITY_LIB
+
 }
 }
diff --git a/rsCppUtils.h b/rsCppUtils.h
index cc6d6cf..7377a44 100644
--- a/rsCppUtils.h
+++ b/rsCppUtils.h
@@ -283,6 +283,15 @@
 
 const char* rsuJoinStrings(int n, const char* const* strs);
 
+#ifndef RS_COMPATIBILITY_LIB
+// Utility to fork/exec a command.
+//     exe - Command to execute
+//     nArgs - Number of arguments (excluding the trailing nullptr in args)
+//     args - Arguments to the command
+bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args);
+#endif
+
+
 }
 }
 
diff --git a/rsScript.cpp b/rsScript.cpp
index b89c96e..cb611af 100644
--- a/rsScript.cpp
+++ b/rsScript.cpp
@@ -48,6 +48,8 @@
         return;
     }
 
+    if (mRSC->hadFatalError()) return;
+
     mSlots[slot].set(a);
     mHasObjectSlots = true;
     mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
@@ -59,6 +61,8 @@
         ALOGE("Script::setVar unable to set allocation, invalid slot index");
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
 }
 
@@ -69,6 +73,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
 }
 
@@ -79,6 +85,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
             (void *)val, len, e, dims, dimLen);
 }
@@ -90,6 +98,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mHasObjectSlots = true;
     mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
 }
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index 4b204d3..4d791f7 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -204,6 +204,7 @@
     }
     ATRACE_NAME(String);
     (void)String;
+    if (mRSC->hadFatalError()) return;
 
     Context::PushState ps(rsc);
 
@@ -235,6 +236,8 @@
         rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     setupScript(rsc);
 
     if (rsc->props.mLogScripts) {