Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions extension/data_loader/shared_ptr_data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/platform/log.h>
#include <cstring>
#include <memory>

namespace executorch {
Expand Down Expand Up @@ -51,6 +52,24 @@ class SharedPtrDataLoader final : public executorch::runtime::DataLoader {
return size_;
}

ET_NODISCARD executorch::runtime::Error load_into(
size_t offset,
size_t size,
ET_UNUSED const SegmentInfo& segment_info,
void* buffer) const override {
ET_CHECK_OR_RETURN_ERROR(
buffer != nullptr,
InvalidArgument,
"Destination buffer cannot be null");

auto result = load(offset, size, segment_info);
if (!result.ok()) {
return result.error();
}
std::memcpy(buffer, result->data(), size);
return executorch::runtime::Error::Ok;
}

private:
const std::shared_ptr<void> data_;
const size_t size_;
Expand Down
57 changes: 57 additions & 0 deletions extension/data_loader/test/shared_ptr_data_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,60 @@ TEST_F(SharedPtrDataLoaderTest, OutOfBoundsLoadFails) {
EXPECT_NE(fb.error(), Error::Ok);
}
}

TEST_F(SharedPtrDataLoaderTest, LoadIntoNullDstFails) {
std::shared_ptr<uint8_t[]> data(
new uint8_t[256](), std::default_delete<uint8_t[]>());
SharedPtrDataLoader edl(data, 256);

EXPECT_EQ(
edl.load_into(
/*offset=*/0,
/*size=*/1,
/*segment_info=*/
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
nullptr),
Error::InvalidArgument);

EXPECT_EQ(
edl.load_into(
/*offset=*/0,
/*size=*/0,
/*segment_info=*/
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
nullptr),
Error::InvalidArgument);
}

TEST_F(SharedPtrDataLoaderTest, LoadIntoCopiesRequestedData) {
constexpr size_t kDataSize = 256;
std::shared_ptr<uint8_t[]> data(
new uint8_t[kDataSize], std::default_delete<uint8_t[]>());
for (size_t i = 0; i < kDataSize; ++i) {
data[i] = static_cast<uint8_t>(i);
}
SharedPtrDataLoader edl(data, kDataSize);
uint8_t buffer[3] = {};

EXPECT_EQ(
edl.load_into(
/*offset=*/kDataSize - sizeof(buffer),
/*size=*/sizeof(buffer),
/*segment_info=*/
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
buffer),
Error::Ok);
EXPECT_EQ(
0,
std::memcmp(
buffer, data.get() + kDataSize - sizeof(buffer), sizeof(buffer)));

EXPECT_EQ(
edl.load_into(
/*offset=*/0,
/*size=*/kDataSize + 1,
/*segment_info=*/
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program),
buffer),
Error::InvalidArgument);
}
Loading