"Fossies" - the Fresh Open Source Software Archive

Member "qt-everywhere-src-6.3.1/qtwebengine/src/3rdparty/chromium/third_party/tflite-support/patches/0001-remove-unsupported-memory-map-from-file-handler.patch" (8 Jun 2022, 9539 Bytes) of package /linux/misc/qt-everywhere-src-6.3.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Diff source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 From 8c5a37f7324b4a03f123c6faecedb4abc8eb0066 Mon Sep 17 00:00:00 2001
    2 From: mcrouse <mcrouse@google.com>
    3 Date: Fri, 5 Feb 2021 15:30:25 +0000
    4 Subject: [PATCH] remove unsupported memory map from file handler
    5 
    6 ---
    7  .../cc/task/core/external_file_handler.cc     | 126 +-----------------
    8  .../cc/task/core/external_file_handler.h      |   7 -
    9  .../cc/task/core/tflite_engine.cc             |   2 -
   10  .../cc/task/core/tflite_engine.h              |   2 -
   11  4 files changed, 6 insertions(+), 131 deletions(-)
   12 
   13 diff --git a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
   14 index ee689e41c6e5..55b662f0926f 100644
   15 --- a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
   16 +++ b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
   17 @@ -18,9 +18,6 @@ limitations under the License.
   18  #include <errno.h>
   19  #include <fcntl.h>
   20  #include <stddef.h>
   21 -#include <sys/mman.h>
   22 -#include <unistd.h>
   23 -
   24  #include <memory>
   25  #include <string>
   26  
   27 @@ -40,18 +37,6 @@ using ::tflite::support::CreateStatusWithPayload;
   28  using ::tflite::support::StatusOr;
   29  using ::tflite::support::TfLiteSupportStatus;
   30  
   31 -// Gets the offset aligned to page size for mapping given files into memory by
   32 -// file descriptor correctly, as according to mmap(2), the offset used in mmap
   33 -// must be a multiple of sysconf(_SC_PAGE_SIZE).
   34 -int64 GetPageSizeAlignedOffset(int64 offset) {
   35 -  int64 aligned_offset = offset;
   36 -  int64 page_size = sysconf(_SC_PAGE_SIZE);
   37 -  if (offset % page_size != 0) {
   38 -    aligned_offset = offset / page_size * page_size;
   39 -  }
   40 -  return aligned_offset;
   41 -}
   42 -
   43  }  // namespace
   44  
   45  /* static */
   46 @@ -71,103 +56,11 @@ absl::Status ExternalFileHandler::MapExternalFile() {
   47    if (!external_file_.file_content().empty()) {
   48      return absl::OkStatus();
   49    }
   50 -  if (external_file_.file_name().empty() &&
   51 -      !external_file_.has_file_descriptor_meta()) {
   52 -    return CreateStatusWithPayload(
   53 -        StatusCode::kInvalidArgument,
   54 -        "ExternalFile must specify at least one of 'file_content', file_name' "
   55 -        "or 'file_descriptor_meta'.",
   56 -        TfLiteSupportStatus::kInvalidArgumentError);
   57 -  }
   58 -  // Obtain file descriptor, offset and size.
   59 -  int fd = -1;
   60 -  if (!external_file_.file_name().empty()) {
   61 -    owned_fd_ = open(external_file_.file_name().c_str(), O_RDONLY);
   62 -    if (owned_fd_ < 0) {
   63 -      const std::string error_message = absl::StrFormat(
   64 -          "Unable to open file at %s", external_file_.file_name());
   65 -      switch (errno) {
   66 -        case ENOENT:
   67 -          return CreateStatusWithPayload(
   68 -              StatusCode::kNotFound, error_message,
   69 -              TfLiteSupportStatus::kFileNotFoundError);
   70 -        case EACCES:
   71 -        case EPERM:
   72 -          return CreateStatusWithPayload(
   73 -              StatusCode::kPermissionDenied, error_message,
   74 -              TfLiteSupportStatus::kFilePermissionDeniedError);
   75 -        case EINTR:
   76 -          return CreateStatusWithPayload(StatusCode::kUnavailable,
   77 -                                         error_message,
   78 -                                         TfLiteSupportStatus::kFileReadError);
   79 -        case EBADF:
   80 -          return CreateStatusWithPayload(StatusCode::kFailedPrecondition,
   81 -                                         error_message,
   82 -                                         TfLiteSupportStatus::kFileReadError);
   83 -        default:
   84 -          return CreateStatusWithPayload(
   85 -              StatusCode::kUnknown,
   86 -              absl::StrFormat("%s, errno=%d", error_message, errno),
   87 -              TfLiteSupportStatus::kFileReadError);
   88 -      }
   89 -    }
   90 -    fd = owned_fd_;
   91 -  } else {
   92 -    fd = external_file_.file_descriptor_meta().fd();
   93 -    if (fd < 0) {
   94 -      return CreateStatusWithPayload(
   95 -          StatusCode::kInvalidArgument,
   96 -          absl::StrFormat("Provided file descriptor is invalid: %d < 0", fd),
   97 -          TfLiteSupportStatus::kInvalidArgumentError);
   98 -    }
   99 -    buffer_offset_ = external_file_.file_descriptor_meta().offset();
  100 -    buffer_size_ = external_file_.file_descriptor_meta().length();
  101 -  }
  102 -  // Get actual file size. Always use 0 as offset to lseek(2) to get the actual
  103 -  // file size, as SEEK_END returns the size of the file *plus* offset.
  104 -  size_t file_size = lseek(fd, /*offset=*/0, SEEK_END);
  105 -  if (file_size <= 0) {
  106 -    return CreateStatusWithPayload(
  107 -        StatusCode::kUnknown,
  108 -        absl::StrFormat("Unable to get file size, errno=%d", errno),
  109 -        TfLiteSupportStatus::kFileReadError);
  110 -  }
  111 -  // Deduce buffer size if not explicitly provided through file descriptor.
  112 -  if (buffer_size_ <= 0) {
  113 -    buffer_size_ = file_size - buffer_offset_;
  114 -  }
  115 -  // Check for out of range issues.
  116 -  if (file_size <= buffer_offset_) {
  117 -    return CreateStatusWithPayload(
  118 -        StatusCode::kInvalidArgument,
  119 -        absl::StrFormat("Provided file offset (%d) exceeds or matches actual "
  120 -                        "file length (%d)",
  121 -                        buffer_offset_, file_size),
  122 -        TfLiteSupportStatus::kInvalidArgumentError);
  123 -  }
  124 -  if (file_size < buffer_size_ + buffer_offset_) {
  125 -    return CreateStatusWithPayload(
  126 -        StatusCode::kInvalidArgument,
  127 -        absl::StrFormat("Provided file length + offset (%d) exceeds actual "
  128 -                        "file length (%d)",
  129 -                        buffer_size_ + buffer_offset_, file_size),
  130 -        TfLiteSupportStatus::kInvalidArgumentError);
  131 -  }
  132 -  // If buffer_offset_ is not multiple of sysconf(_SC_PAGE_SIZE), align with
  133 -  // extra leading bytes and adjust buffer_size_ to account for the extra
  134 -  // leading bytes.
  135 -  buffer_aligned_offset_ = GetPageSizeAlignedOffset(buffer_offset_);
  136 -  buffer_aligned_size_ = buffer_size_ + buffer_offset_ - buffer_aligned_offset_;
  137 -  // Map into memory.
  138 -  buffer_ = mmap(/*addr=*/nullptr, buffer_aligned_size_, PROT_READ, MAP_SHARED,
  139 -                 fd, buffer_aligned_offset_);
  140 -  if (buffer_ == MAP_FAILED) {
  141 -    return CreateStatusWithPayload(
  142 -        StatusCode::kUnknown,
  143 -        absl::StrFormat("Unable to map file to memory buffer, errno=%d", errno),
  144 -        TfLiteSupportStatus::kFileMmapError);
  145 -  }
  146 -  return absl::OkStatus();
  147 +  return CreateStatusWithPayload(
  148 +      StatusCode::kInvalidArgument,
  149 +      "ExternalFile must have 'file_content' set, loading from"
  150 +      "'file_name' is not supported.",
  151 +      TfLiteSupportStatus::kInvalidArgumentError);
  152  }
  153  
  154  absl::string_view ExternalFileHandler::GetFileContent() {
  155 @@ -180,14 +73,7 @@ absl::string_view ExternalFileHandler::GetFileContent() {
  156    }
  157  }
  158  
  159 -ExternalFileHandler::~ExternalFileHandler() {
  160 -  if (buffer_ != MAP_FAILED) {
  161 -    munmap(buffer_, buffer_aligned_size_);
  162 -  }
  163 -  if (owned_fd_ >= 0) {
  164 -    close(owned_fd_);
  165 -  }
  166 -}
  167 +ExternalFileHandler::~ExternalFileHandler() = default;
  168  
  169  }  // namespace core
  170  }  // namespace task
  171 diff --git a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
  172 index 236d90347698..ad292dcc3702 100644
  173 --- a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
  174 +++ b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
  175 @@ -65,10 +65,6 @@ class ExternalFileHandler {
  176    // Reference to the input ExternalFile.
  177    const ExternalFile& external_file_;
  178  
  179 -  // The file descriptor of the ExternalFile if provided by path, as it is
  180 -  // opened and owned by this class. Set to -1 otherwise.
  181 -  int owned_fd_{-1};
  182 -
  183    // Points to the memory buffer mapped from the file descriptor of the
  184    // ExternalFile, if provided by path or file descriptor.
  185    void* buffer_{};
  186 @@ -82,9 +78,6 @@ class ExternalFileHandler {
  187  
  188    // The aligned mapped memory buffer offset, if any.
  189    int64 buffer_aligned_offset_{};
  190 -  // The aligned mapped memory buffer size in bytes taking into account the
  191 -  // offset shift introduced by buffer_aligned_memory_offset_, if any.
  192 -  int64 buffer_aligned_size_{};
  193  };
  194  
  195  }  // namespace core
  196 diff --git a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
  197 index 0317d5f8ea34..6230e5c645c0 100644
  198 --- a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
  199 +++ b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
  200 @@ -15,8 +15,6 @@ limitations under the License.
  201  
  202  #include "tensorflow_lite_support/cc/task/core/tflite_engine.h"
  203  
  204 -#include <unistd.h>
  205 -
  206  #include "absl/strings/match.h"
  207  #include "absl/strings/str_cat.h"
  208  #include "tensorflow/lite/builtin_ops.h"
  209 diff --git a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
  210 index 6a7e97dd264e..bc55f6b0fe72 100644
  211 --- a/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
  212 +++ b/third_party/tflite-support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
  213 @@ -16,8 +16,6 @@ limitations under the License.
  214  #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
  215  #define TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
  216  
  217 -#include <sys/mman.h>
  218 -
  219  #include <memory>
  220  
  221  #include "absl/memory/memory.h"
  222 -- 
  223 2.30.0.365.g02bc693789-goog
  224