Fix alignment_hint < sizeof(void*) in BuiltinDataAllocator::Allocate

Handle the case when `alignof(T)` return value `< sizeof(void*)` and causes
the fail of `aligned_alloc()`. Fix by using `sizeof(void*)` as `alignment_hint`
in this case.

PiperOrigin-RevId: 311219237
Change-Id: Ib5d9c194ac00f17f4f3a47bf98cba0afbdce5840
This commit is contained in:
A. Unique TensorFlower 2020-05-12 15:55:20 -07:00 committed by TensorFlower Gardener
parent ce11d03f84
commit 8788846283
2 changed files with 11 additions and 1 deletions

View File

@ -253,6 +253,7 @@ cc_library(
"//tensorflow/lite/core/api",
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
"//tensorflow/lite/experimental/resource",
"//tensorflow/lite/kernels/internal:compatibility",
"//tensorflow/lite/nnapi:nnapi_implementation",
"//tensorflow/lite/schema:schema_fbs",
] + select({

View File

@ -26,6 +26,7 @@ limitations under the License.
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/core/api/flatbuffer_conversions.h"
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/util.h"
#include "tensorflow/lite/version.h"
@ -209,7 +210,15 @@ class MallocDataAllocator : public BuiltinDataAllocator {
public:
void* Allocate(size_t size, size_t alignment_hint) override {
#ifdef TFLITE_USE_STD_ALIGNED_ALLOC
return aligned_alloc(alignment_hint, size);
// Ensure that alignment is a power of two and a multiple of sizeof(void *)
// and that size is an integral multiple of alignment.
size_t used_alignment = std::max(alignment_hint, sizeof(void*));
size_t used_size =
((size + used_alignment - 1) / used_alignment) * used_alignment;
TFLITE_DCHECK(
(used_alignment != 0) &&
((used_alignment & (used_alignment - 1)) == 0)); // is power-of-two
return aligned_alloc(used_alignment, used_size);
#else
return malloc(size);
#endif