MH2 | Check that subhal index is in range

For each HalProxy method that takes in a sensorHandle, the code now
checks that the subhal index byte (first byte) of the handle is the
range of subhals in list before indexing into mSubHalList so that are
crash does not occur and returns Result::BAD_VALUE if that is the case.
Add unit tests to assert that error is returned. Also change the methods
that accept sensorHandles to int32_t type since that is the actual type
defined in the HAL spec.

Bug: 136511617
Test: Passing new unit tests.

Change-Id: I5489e999bc5eaef1a21698bdbc0a0341bc88194c
diff --git a/sensors/2.0/multihal/include/HalProxy.h b/sensors/2.0/multihal/include/HalProxy.h
index 26bc644..10fce43 100644
--- a/sensors/2.0/multihal/include/HalProxy.h
+++ b/sensors/2.0/multihal/include/HalProxy.h
@@ -124,7 +124,7 @@
      *
      * @return The sensor info object in the mapping.
      */
-    const SensorInfo& getSensorInfo(uint32_t sensorHandle) { return mSensors[sensorHandle]; }
+    const SensorInfo& getSensorInfo(int32_t sensorHandle) { return mSensors[sensorHandle]; }
 
     bool areThreadsRunning() { return mThreadsRun.load(); }
 
@@ -177,10 +177,10 @@
      * The subhal index is encoded in the first byte of the sensor handle and the remaining
      * bytes are generated by the subhal to identify the sensor.
      */
-    std::map<uint32_t, SensorInfo> mSensors;
+    std::map<int32_t, SensorInfo> mSensors;
 
     //! Map of the dynamic sensors that have been added to halproxy.
-    std::map<uint32_t, SensorInfo> mDynamicSensors;
+    std::map<int32_t, SensorInfo> mDynamicSensors;
 
     //! The current operation mode for all subhals.
     OperationMode mCurrentOperationMode = OperationMode::NORMAL;
@@ -192,7 +192,7 @@
     static const int64_t kPendingWriteTimeoutNs = 5 * INT64_C(1000000000) /* 5 seconds */;
 
     //! The bit mask used to get the subhal index from a sensor handle.
-    static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
+    static constexpr int32_t kSensorHandleSubHalIndexMask = 0xFF000000;
 
     /**
      * A FIFO queue of pairs of vector of events and the number of wakeup events in that vector
@@ -319,7 +319,16 @@
      *
      * @param sensorHandle The handle used to identify a sensor in one of the subhals.
      */
-    ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle);
+    ISensorsSubHal* getSubHalForSensorHandle(int32_t sensorHandle);
+
+    /**
+     * Checks that sensorHandle's subhal index byte is within bounds of mSubHalList.
+     *
+     * @param sensorHandle The sensor handle to check.
+     *
+     * @return true if sensorHandles's subhal index byte is valid.
+     */
+    bool isSubHalIndexValid(int32_t sensorHandle);
 
     /**
      * Count the number of wakeup events in the first n events of the vector.
@@ -338,14 +347,14 @@
      *
      * @return The modified version of the sensor handle.
      */
-    static uint32_t clearSubHalIndex(uint32_t sensorHandle);
+    static int32_t clearSubHalIndex(int32_t sensorHandle);
 
     /**
      * @param sensorHandle The sensor handle to modify.
      *
      * @return true if subHalIndex byte of sensorHandle is zeroed.
      */
-    static bool subHalIndexIsClear(uint32_t sensorHandle);
+    static bool subHalIndexIsClear(int32_t sensorHandle);
 };
 
 /**