Handle older structure sizes by zeroing new fields

Bug 19734267
Bug 19866850

Prior fix handled only rsForEach calls from a script.  It is not
sufficient, as launch options in support library also will also use old
structures.  Apps compiled for support library still run in native mode
when available, thus necessitating this fix.

This fix copies and extends the input in ScriptC::runForEach instead of
rsrForEach.

Change-Id: I3bb2527eadcbcdb85a76a1f5568269bbfdee972b
(cherry-pick of 9479e5bf0152ecff022cd374e2e80905f88b1a5d from AOSP)
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index bc5c892..c728ca7 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -439,9 +439,8 @@
                                              const void *usr,
                                              const rs_script_call *call) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    const Script *sc = RsdCpuReference::getTlsScript();
     rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p,
-               (Allocation *)out.p, usr, 0, (RsScriptCall *)call, sc);
+               (Allocation *)out.p, usr, 0, (RsScriptCall *)call);
 }
 
 void __attribute__((overloadable)) rsForEach(::rs_script script,
@@ -449,18 +448,16 @@
                                              ::rs_allocation out,
                                              const void *usr) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    const Script *sc = RsdCpuReference::getTlsScript();
     rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
-               usr, 0, nullptr, sc);
+               usr, 0, nullptr);
 }
 
 void __attribute__((overloadable)) rsForEach(::rs_script script,
                                              ::rs_allocation in,
                                              ::rs_allocation out) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    const Script *sc = RsdCpuReference::getTlsScript();
     rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
-               nullptr, 0, nullptr, sc);
+               nullptr, 0, nullptr);
 }
 
 // These functions are only supported in 32-bit.
@@ -471,9 +468,8 @@
                                              const void *usr,
                                              uint32_t usrLen) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    const Script *sc = RsdCpuReference::getTlsScript();
     rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
-               usr, usrLen, nullptr, sc);
+               usr, usrLen, nullptr);
 }
 
 void __attribute__((overloadable)) rsForEach(::rs_script script,
@@ -483,9 +479,8 @@
                                              uint32_t usrLen,
                                              const rs_script_call *call) {
     Context *rsc = RsdCpuReference::getTlsContext();
-    const Script *sc = RsdCpuReference::getTlsScript();
     rsrForEach(rsc, (Script *)script.p, (Allocation *)in.p, (Allocation *)out.p,
-               usr, usrLen, (RsScriptCall *)call, sc);
+               usr, usrLen, (RsScriptCall *)call);
 }
 #endif
 
diff --git a/rsRuntime.h b/rsRuntime.h
index de6ede3..5a05883 100644
--- a/rsRuntime.h
+++ b/rsRuntime.h
@@ -159,8 +159,7 @@
                 Allocation *out,
                 const void *usr,
                 uint32_t usrBytes,
-                const RsScriptCall *call,
-                const Script *callingScript);
+                const RsScriptCall *call);
 
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index e1685b9..a24334e 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -192,6 +192,15 @@
                          const void * usr,
                          size_t usrBytes,
                          const RsScriptCall *sc) {
+    // Make a copy of RsScriptCall and zero out extra fields that are absent
+    // in API levels below 23.
+    RsScriptCall sc_copy;
+    if (sc != nullptr && getApiLevel() < 23) {
+        memset(&sc_copy, 0, sizeof(sc_copy));
+        memcpy(&sc_copy, sc, 7*4);
+        sc = &sc_copy;
+    }
+
     // Trace this function call.
     // To avoid overhead we only build the string if tracing is actually
     // enabled.
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index cc93021..c404bde 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -238,30 +238,17 @@
                 Script *target,
                 Allocation *in, Allocation *out,
                 const void *usr, uint32_t usrBytes,
-                const RsScriptCall *call,
-                const Script *callingScript) {
-
-    RsScriptCall c, *cptr = nullptr;
-    memset(&c, 0, sizeof(c));
-    if (call != nullptr) {
-        cptr = &c;
-        if (callingScript->getApiLevel() < 23) {
-            // Up to API 23, the structure was smaller and we need to zero extend
-            memcpy(&c, call, 7*4);
-        } else {
-            c = *call;
-        }
-    }
+                const RsScriptCall *call) {
 
     if (in == nullptr) {
         target->runForEach(rsc, /* root slot */ 0, nullptr, 0, out, usr,
-                           usrBytes, cptr);
+                           usrBytes, call);
 
     } else {
         const Allocation *ins[1] = {in};
         target->runForEach(rsc, /* root slot */ 0, ins,
                            sizeof(ins) / sizeof(RsAllocation), out, usr,
-                           usrBytes, cptr);
+                           usrBytes, call);
     }
 }