Fix allocation of low-alignment memory

posix_memalign() returns EINVAL if the alignment argument is not a
multiple of sizeof(void*). Use regular malloc() for those cases.
malloc() is specified to allocate memory which is "suitably aligned for
any built-in type".

Bug b/128618202

Change-Id: Ibee07be89f2a5bd6be770c35710b20b0c752dfd1
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/27228
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Common/Memory.cpp b/src/Common/Memory.cpp
index f44fc9e..779d068 100644
--- a/src/Common/Memory.cpp
+++ b/src/Common/Memory.cpp
@@ -54,14 +54,21 @@
 	ASSERT((alignment & (alignment - 1)) == 0);   // Power of 2 alignment.
 
 	#if defined(LINUX_ENABLE_NAMED_MMAP)
-		void *allocation;
-		int result = posix_memalign(&allocation, alignment, bytes);
-		if(result != 0)
+		if(alignment < sizeof(void*))
 		{
-			errno = result;
-			allocation = nullptr;
+			return malloc(bytes);
 		}
-		return allocation;
+		else
+		{
+			void *allocation;
+			int result = posix_memalign(&allocation, alignment, bytes);
+			if(result != 0)
+			{
+				errno = result;
+				allocation = nullptr;
+			}
+			return allocation;
+		}
 	#else
 		unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
 		unsigned char *aligned = nullptr;
diff --git a/src/Reactor/ExecutableMemory.cpp b/src/Reactor/ExecutableMemory.cpp
index 67d84ea..bbde2ab 100644
--- a/src/Reactor/ExecutableMemory.cpp
+++ b/src/Reactor/ExecutableMemory.cpp
@@ -57,14 +57,21 @@
 	ASSERT((alignment & (alignment - 1)) == 0);   // Power of 2 alignment.
 
 	#if defined(LINUX_ENABLE_NAMED_MMAP)
-		void *allocation;
-		int result = posix_memalign(&allocation, alignment, bytes);
-		if(result != 0)
+		if(alignment < sizeof(void*))
 		{
-			errno = result;
-			allocation = nullptr;
+			return malloc(bytes);
 		}
-		return allocation;
+		else
+		{
+			void *allocation;
+			int result = posix_memalign(&allocation, alignment, bytes);
+			if(result != 0)
+			{
+				errno = result;
+				allocation = nullptr;
+			}
+			return allocation;
+		}
 	#else
 		unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
 		unsigned char *aligned = nullptr;
diff --git a/src/System/Memory.cpp b/src/System/Memory.cpp
index 0e73891..ffaab71 100644
--- a/src/System/Memory.cpp
+++ b/src/System/Memory.cpp
@@ -54,14 +54,21 @@
 	ASSERT((alignment & (alignment - 1)) == 0);   // Power of 2 alignment.
 
 	#if defined(LINUX_ENABLE_NAMED_MMAP)
-		void *allocation;
-		int result = posix_memalign(&allocation, alignment, bytes);
-		if(result != 0)
+		if(alignment < sizeof(void*))
 		{
-			errno = result;
-			allocation = nullptr;
+			return malloc(bytes);
 		}
-		return allocation;
+		else
+		{
+			void *allocation;
+			int result = posix_memalign(&allocation, alignment, bytes);
+			if(result != 0)
+			{
+				errno = result;
+				allocation = nullptr;
+			}
+			return allocation;
+		}
 	#else
 		unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
 		unsigned char *aligned = nullptr;