tflite: Fix native handle deallocation size mismatch The default new/delete memory management will act based on the given type. However, since native_handle_t contains zero-length (dynamic size) array, new/delete cannot handle properly. Thus, use `OwnedNativeHandle` defined in `android/native_handle_util` and manually malloc the memory to solve the issue. BUG=b:443074562 TEST=CQ Change-Id: I06e533441072e0945b4bb050e6b32dd2f8613691 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tflite/+/6934338 Tested-by: Tommy Chiang <ototot@google.com> Commit-Queue: Shik Chen <shik@chromium.org> Reviewed-by: Shik Chen <shik@chromium.org> Commit-Queue: Tommy Chiang <ototot@google.com> Auto-Submit: Tommy Chiang <ototot@google.com>
diff --git a/android/BUILD.bazel b/android/BUILD.bazel index fe2f540..248d153 100644 --- a/android/BUILD.bazel +++ b/android/BUILD.bazel
@@ -56,6 +56,7 @@ ], deps = [ ":hardware_buffer", + ":native_handle_util", "//common:log", "@com_google_absl//absl/strings", "@com_google_googletest//:gtest",
diff --git a/android/hardware_buffer_test.cc b/android/hardware_buffer_test.cc index a31d30f..d0aa34d 100644 --- a/android/hardware_buffer_test.cc +++ b/android/hardware_buffer_test.cc
@@ -9,6 +9,7 @@ #include <gtest/gtest.h> #include <algorithm> +#include <cstdlib> #include <fstream> #include <memory> #include <string> @@ -16,6 +17,7 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_split.h" +#include "android/native_handle_util.h" #include "common/log.h" namespace {} // namespace @@ -152,9 +154,8 @@ AHardwareBuffer_release(original); // Construct a new handle with the fd. - auto handle = std::unique_ptr<native_handle_t>( - static_cast<native_handle_t*>(operator new(sizeof(native_handle_t) + - sizeof(int)))); + auto handle = tflite::cros::OwnedNativeHandle(static_cast<native_handle_t*>( + std::malloc(sizeof(native_handle_t) + sizeof(int)))); *handle = { .version = sizeof(native_handle_t), .numFds = 1,