Merge branch 'dev/11/fp3/security-aosp-rvc-release' into int/11/fp3

* dev/11/fp3/security-aosp-rvc-release:
  Add additional bounds checks to NNAPI FMQ deserialize utility functions

Change-Id: Ie2205d57d1f2653b8e1045aabcb18f95f2e6f0f8
diff --git a/nn/runtime/test/Android.bp b/nn/runtime/test/Android.bp
index 65d9de6..df0c588 100644
--- a/nn/runtime/test/Android.bp
+++ b/nn/runtime/test/Android.bp
@@ -193,28 +193,28 @@
     ],
 }
 
-cc_fuzz {
-    name: "libneuralnetworks_fuzzer",
-    defaults: ["NeuralNetworksTest_default_libs"],
-    owner: "google",
-    fuzz_config: {
-        cc: ["butlermichael@google.com"],
-    },
-    srcs: [
-        "android_fuzzing/Converter.cpp",
-        "android_fuzzing/FuzzTest.cpp",
-        "android_fuzzing/StaticAssert.cpp",
-    ],
-    corpus: ["android_fuzzing/corpus/*"],
-    shared_libs: ["libprotobuf-cpp-full"],
-    static_libs: [
-        "libneuralnetworks_common",
-        "libneuralnetworks_fuzzer_proto",
-        "libneuralnetworks_generated_test_harness",
-        "libneuralnetworks_static",
-        "libprotobuf-mutator",
-    ],
-}
+//cc_fuzz {
+//    name: "libneuralnetworks_fuzzer",
+//    defaults: [
+//        "NeuralNetworksTest_default_libs",
+//        "libneuralnetworks_fuzzer_defaults",
+//    ],
+//    owner: "google",
+//    fuzz_config: {
+//        cc: ["butlermichael@google.com"],
+//        // Temporarily disabled due to b/151102177.
+//        fuzz_on_haiku_host: false,
+//        fuzz_on_haiku_device: false,
+//    },
+//    srcs: [
+//        "android_fuzzing/FuzzTest.cpp",
+//    ],
+//    static_libs: [
+//        "libneuralnetworks_common",
+//        "libneuralnetworks_generated_test_harness",
+//        "libneuralnetworks_static",
+//    ],
+//}
 
 // Temporarily disabled due to b/139889855.
 cc_test {
diff --git a/nn/runtime/test/GeneratedTestUtils.h b/nn/runtime/test/GeneratedTestUtils.h
index 3ac5cba..1354f67 100644
--- a/nn/runtime/test/GeneratedTestUtils.h
+++ b/nn/runtime/test/GeneratedTestUtils.h
@@ -20,6 +20,7 @@
 #include <gtest/gtest.h>
 
 #include <memory>
+#include <string>
 #include <utility>
 #include <vector>
 
@@ -31,6 +32,7 @@
 class GeneratedTestBase
     : public ::testing::TestWithParam<test_helper::TestModelManager::TestParam> {
    protected:
+    const std::string& kTestName = GetParam().first;
     const test_helper::TestModel& testModel = *GetParam().second;
 };
 
diff --git a/nn/runtime/test/TestGenerated.cpp b/nn/runtime/test/TestGenerated.cpp
index a76aecd..70b0e6f 100644
--- a/nn/runtime/test/TestGenerated.cpp
+++ b/nn/runtime/test/TestGenerated.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <android-base/logging.h>
+#include <android-base/properties.h>
 #include <ftw.h>
 #include <gtest/gtest.h>
 #include <unistd.h>
@@ -26,6 +27,7 @@
 #include <iostream>
 #include <map>
 #include <memory>
+#include <set>
 #include <string>
 #include <thread>
 #include <utility>
@@ -60,6 +62,8 @@
     void SetUp() override;
     void TearDown() override;
 
+    bool shouldSkipTest();
+
     std::optional<Compilation> compileModel(const Model& model);
     void executeWithCompilation(const Compilation& compilation, const TestModel& testModel);
     void executeOnce(const Model& model, const TestModel& testModel);
@@ -68,6 +72,9 @@
     // Test driver for those generated from ml/nn/runtime/test/spec
     void execute(const TestModel& testModel);
 
+    // VNDK version of the device under test.
+    static int mVndkVersion;
+
     std::string mCacheDir;
     std::vector<uint8_t> mToken;
     bool mTestCompilationCaching = false;
@@ -77,6 +84,8 @@
     bool mTestDeviceMemory = false;
 };
 
+int GeneratedTests::mVndkVersion = __ANDROID_API_FUTURE__;
+
 // Tag for the dynamic output shape tests
 class DynamicOutputShapeTest : public GeneratedTests {
    protected:
@@ -328,8 +337,35 @@
     }
 }
 
+bool GeneratedTests::shouldSkipTest() {
+    // A map of {min VNDK version -> tests that should be skipped with earlier VNDK versions}.
+    // The listed tests are added in a later release, but exercising old APIs. They should be
+    // skipped if the device has a mixed build of system and vendor partitions.
+    static const std::map<int, std::set<std::string>> kMapOfMinVndkVersionToTests = {
+            {
+                    __ANDROID_API_R__,
+                    {
+                            "add_broadcast_quant8_all_inputs_as_internal",
+                    },
+            },
+    };
+    for (const auto& [minVersion, names] : kMapOfMinVndkVersionToTests) {
+        if (mVndkVersion < minVersion && names.count(kTestName) > 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
 void GeneratedTests::SetUp() {
     GeneratedTestBase::SetUp();
+
+    mVndkVersion = ::android::base::GetIntProperty("ro.vndk.version", __ANDROID_API_FUTURE__);
+    if (shouldSkipTest()) {
+        GTEST_SKIP();
+        return;
+    }
+
     char cacheDirTemp[] = "/data/local/tmp/TestCompilationCachingXXXXXX";
     char* cacheDir = mkdtemp(cacheDirTemp);
     ASSERT_NE(cacheDir, nullptr);
diff --git a/nn/runtime/test/fuzzing/TestRandomGraph.cpp b/nn/runtime/test/fuzzing/TestRandomGraph.cpp
index 2c8024a..fc73bc2 100644
--- a/nn/runtime/test/fuzzing/TestRandomGraph.cpp
+++ b/nn/runtime/test/fuzzing/TestRandomGraph.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <android-base/properties.h>
 #include <gtest/gtest.h>
 
 #include <algorithm>
@@ -31,7 +32,6 @@
 #include "fuzzing/RandomGraphGeneratorUtils.h"
 
 #ifndef NNTEST_CTS
-#include <android-base/properties.h>
 #include <memunreachable/memunreachable.h>
 
 #include <vector>
@@ -152,6 +152,7 @@
         mSyntheticDevices.push_back(makeTestDevice<TestDriverV1_1>());
         mSyntheticDevices.push_back(makeTestDevice<TestDriverV1_0>());
 #endif
+        mVndkVersion = ::android::base::GetIntProperty("ro.vndk.version", __ANDROID_API_FUTURE__);
 
         // Get all the devices and device names.
         mStandardDevicesFeatureLevel = __ANDROID_API_FUTURE__;
@@ -225,6 +226,11 @@
                 featureLevel <= __ANDROID_API_Q__) {
                 return true;
             }
+            // Skip the following operations when the VNDK version is earlier than R.
+            if (mVndkVersion < __ANDROID_API_R__ &&
+                op.type == TestOperationType::HEATMAP_MAX_KEYPOINT) {
+                return true;
+            }
         }
         return false;
     }
@@ -433,6 +439,7 @@
     // A vector of {name, output_results}.
     std::vector<std::pair<std::string, std::vector<TestBuffer>>> mResults;
 
+    static int mVndkVersion;
     static int64_t mStandardDevicesFeatureLevel;  // minimum across all devices
 #ifndef NNTEST_CTS
     static std::vector<std::shared_ptr<Device>> mStandardDevices;
@@ -445,6 +452,7 @@
 bool RandomGraphTest::mDetectMemoryLeak = false;
 std::map<std::string, ANeuralNetworksDevice*> RandomGraphTest::mDevices;
 
+int RandomGraphTest::mVndkVersion = __ANDROID_API_FUTURE__;
 int64_t RandomGraphTest::mStandardDevicesFeatureLevel;
 #ifndef NNTEST_CTS
 std::vector<std::shared_ptr<Device>> RandomGraphTest::mStandardDevices;