Reactor: Assert that array indexing is in bounds

For compile time indices, we can do some basic sanity checking.

Change-Id: I9ae4ea5bbf106c4653cbf9bbe6776a5a188b4289
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/33470
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Reactor/Reactor.cpp b/src/Reactor/Reactor.cpp
index 61a5c1f..de39d4a 100644
--- a/src/Reactor/Reactor.cpp
+++ b/src/Reactor/Reactor.cpp
@@ -26,7 +26,7 @@
 	// Set of variables that do not have a stack location yet.
 	std::unordered_set<Variable*> Variable::unmaterializedVariables;
 
-	Variable::Variable(Type *type, int arraySize) : type(type), arraySize(arraySize)
+	Variable::Variable(Type *type, int arraySize) : arraySize(arraySize), type(type)
 	{
 		#if REACTOR_MATERIALIZE_LVALUES_ON_DEFINITION
 			materialize();
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index 317efc7..d9419a8 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -132,6 +132,8 @@
 
 		~Variable();
 
+		const int arraySize;
+
 	private:
 		static void materializeAll();
 		static void killUnmaterialized();
@@ -139,7 +141,6 @@
 		static std::unordered_set<Variable*> unmaterializedVariables;
 
 		Type *const type;
-		const int arraySize;
 		mutable Value *rvalue = nullptr;
 		mutable Value *address = nullptr;
 	};
@@ -2915,6 +2916,7 @@
 	template<class T, int S>
 	Reference<T> Array<T, S>::operator[](int index)
 	{
+		assert(index < this->arraySize);
 		Value *element = LValue<T>::getElementPointer(Nucleus::createConstantInt(index), false);
 
 		return Reference<T>(element);
@@ -2923,6 +2925,7 @@
 	template<class T, int S>
 	Reference<T> Array<T, S>::operator[](unsigned int index)
 	{
+		assert(index < static_cast<unsigned int>(this->arraySize));
 		Value *element = LValue<T>::getElementPointer(Nucleus::createConstantInt(index), true);
 
 		return Reference<T>(element);