Skip to content

Commit 7a906bf

Browse files
author
lind
committed
Revert "[AOSP] add file descriptor support to file_data_loader (#6611)"
This reverts commit 3a1f8d2.
1 parent cd565b5 commit 7a906bf

File tree

4 files changed

+16
-205
lines changed

4 files changed

+16
-205
lines changed

examples/portable/executor_runner/executor_runner.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
#include <gflags/gflags.h>
2424

25-
#include <fcntl.h>
26-
#include <unistd.h>
27-
2825
#include <executorch/extension/data_loader/file_data_loader.h>
2926
#include <executorch/extension/evalue_util/print_evalue.h>
3027
#include <executorch/extension/runner_util/inputs.h>
@@ -39,10 +36,6 @@ DEFINE_string(
3936
model_path,
4037
"model.pte",
4138
"Model serialized in flatbuffer format.");
42-
DEFINE_bool(
43-
is_fd_uri,
44-
false,
45-
"True if the model_path passed is a file descriptor with the prefix \"fd:///\".");
4639

4740
using executorch::extension::FileDataLoader;
4841
using executorch::runtime::Error;
@@ -73,12 +66,7 @@ int main(int argc, char** argv) {
7366
// DataLoaders that use mmap() or point to data that's already in memory, and
7467
// users can create their own DataLoaders to load from arbitrary sources.
7568
const char* model_path = FLAGS_model_path.c_str();
76-
const bool is_fd_uri = FLAGS_is_fd_uri;
77-
78-
Result<FileDataLoader> loader = is_fd_uri
79-
? FileDataLoader::fromFileDescriptorUri(model_path)
80-
: FileDataLoader::from(model_path);
81-
69+
Result<FileDataLoader> loader = FileDataLoader::from(model_path);
8270
ET_CHECK_MSG(
8371
loader.ok(),
8472
"FileDataLoader::from() failed: 0x%" PRIx32,

extension/data_loader/file_data_loader.cpp

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ namespace extension {
4343

4444
namespace {
4545

46-
static constexpr char kFdFilesystemPrefix[] = "fd:///";
47-
4846
/**
4947
* Returns true if the value is an integer power of 2.
5048
*/
@@ -76,36 +74,25 @@ FileDataLoader::~FileDataLoader() {
7674
::close(fd_);
7775
}
7876

79-
Result<int> getFDFromUri(const char* file_descriptor_uri) {
80-
// check if the uri starts with the prefix "fd://"
77+
Result<FileDataLoader> FileDataLoader::from(
78+
const char* file_name,
79+
size_t alignment) {
8180
ET_CHECK_OR_RETURN_ERROR(
82-
strncmp(
83-
file_descriptor_uri,
84-
kFdFilesystemPrefix,
85-
strlen(kFdFilesystemPrefix)) == 0,
81+
is_power_of_2(alignment),
8682
InvalidArgument,
87-
"File descriptor uri (%s) does not start with %s",
88-
file_descriptor_uri,
89-
kFdFilesystemPrefix);
90-
91-
// strip "fd:///" from the uri
92-
int fd_len = strlen(file_descriptor_uri) - strlen(kFdFilesystemPrefix);
93-
char fd_without_prefix[fd_len + 1];
94-
memcpy(
95-
fd_without_prefix,
96-
&file_descriptor_uri[strlen(kFdFilesystemPrefix)],
97-
fd_len);
98-
fd_without_prefix[fd_len] = '\0';
83+
"Alignment %zu is not a power of 2",
84+
alignment);
9985

100-
// check if remaining fd string is a valid integer
101-
int fd = ::atoi(fd_without_prefix);
102-
return fd;
103-
}
86+
// Use open() instead of fopen() to avoid the layer of buffering that
87+
// fopen() does. We will be reading large portions of the file in one shot,
88+
// so buffering does not help.
89+
int fd = ::open(file_name, O_RDONLY);
90+
if (fd < 0) {
91+
ET_LOG(
92+
Error, "Failed to open %s: %s (%d)", file_name, strerror(errno), errno);
93+
return Error::AccessFailed;
94+
}
10495

105-
Result<FileDataLoader> FileDataLoader::fromFileDescriptor(
106-
const char* file_name,
107-
const int fd,
108-
size_t alignment) {
10996
// Cache the file size.
11097
struct stat st;
11198
int err = ::fstat(fd, &st);
@@ -132,47 +119,6 @@ Result<FileDataLoader> FileDataLoader::fromFileDescriptor(
132119
return FileDataLoader(fd, file_size, alignment, file_name_copy);
133120
}
134121

135-
Result<FileDataLoader> FileDataLoader::fromFileDescriptorUri(
136-
const char* file_descriptor_uri,
137-
size_t alignment) {
138-
ET_CHECK_OR_RETURN_ERROR(
139-
is_power_of_2(alignment),
140-
InvalidArgument,
141-
"Alignment %zu is not a power of 2",
142-
alignment);
143-
144-
auto parsed_fd = getFDFromUri(file_descriptor_uri);
145-
if (!parsed_fd.ok()) {
146-
return parsed_fd.error();
147-
}
148-
149-
int fd = parsed_fd.get();
150-
151-
return fromFileDescriptor(file_descriptor_uri, fd, alignment);
152-
}
153-
154-
Result<FileDataLoader> FileDataLoader::from(
155-
const char* file_name,
156-
size_t alignment) {
157-
ET_CHECK_OR_RETURN_ERROR(
158-
is_power_of_2(alignment),
159-
InvalidArgument,
160-
"Alignment %zu is not a power of 2",
161-
alignment);
162-
163-
// Use open() instead of fopen() to avoid the layer of buffering that
164-
// fopen() does. We will be reading large portions of the file in one shot,
165-
// so buffering does not help.
166-
int fd = ::open(file_name, O_RDONLY);
167-
if (fd < 0) {
168-
ET_LOG(
169-
Error, "Failed to open %s: %s (%d)", file_name, strerror(errno), errno);
170-
return Error::AccessFailed;
171-
}
172-
173-
return fromFileDescriptor(file_name, fd, alignment);
174-
}
175-
176122
namespace {
177123
/**
178124
* FreeableBuffer::FreeFn-compatible callback.

extension/data_loader/file_data_loader.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@ namespace extension {
2626
*/
2727
class FileDataLoader final : public executorch::runtime::DataLoader {
2828
public:
29-
/**
30-
* Creates a new FileDataLoader that wraps the named file descriptor, and the
31-
* ownership of the file descriptor is passed. This helper is used when ET is
32-
* running in a process that does not have access to the filesystem, and the
33-
* caller is able to open the file and pass the file descriptor.
34-
*
35-
* @param[in] file_descriptor_uri File descriptor with the prefix "fd:///",
36-
* followed by the file descriptor number.
37-
* @param[in] alignment Alignment in bytes of pointers returned by this
38-
* instance. Must be a power of two.
39-
*
40-
* @returns A new FileDataLoader on success.
41-
* @retval Error::InvalidArgument `alignment` is not a power of two.
42-
* @retval Error::AccessFailed `file_name` could not be opened, or its size
43-
* could not be found.
44-
* @retval Error::MemoryAllocationFailed Internal memory allocation failure.
45-
*/
46-
static executorch::runtime::Result<FileDataLoader> fromFileDescriptorUri(
47-
const char* file_descriptor_uri,
48-
size_t alignment = alignof(std::max_align_t));
49-
5029
/**
5130
* Creates a new FileDataLoader that wraps the named file.
5231
*
@@ -100,11 +79,6 @@ class FileDataLoader final : public executorch::runtime::DataLoader {
10079
void* buffer) const override;
10180

10281
private:
103-
static executorch::runtime::Result<FileDataLoader> fromFileDescriptor(
104-
const char* file_name,
105-
const int fd,
106-
size_t alignment = alignof(std::max_align_t));
107-
10882
FileDataLoader(
10983
int fd,
11084
size_t file_size,

extension/data_loader/test/file_data_loader_test.cpp

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -40,103 +40,6 @@ class FileDataLoaderTest : public ::testing::TestWithParam<size_t> {
4040
}
4141
};
4242

43-
TEST_P(FileDataLoaderTest, InBoundsFileDescriptorLoadsSucceed) {
44-
// Write some heterogeneous data to a file.
45-
uint8_t data[256];
46-
for (int i = 0; i < sizeof(data); ++i) {
47-
data[i] = i;
48-
}
49-
TempFile tf(data, sizeof(data));
50-
51-
int fd = ::open(tf.path().c_str(), O_RDONLY);
52-
53-
// Wrap it in a loader.
54-
Result<FileDataLoader> fdl = FileDataLoader::fromFileDescriptorUri(
55-
("fd:///" + std::to_string(fd)).c_str(), alignment());
56-
ASSERT_EQ(fdl.error(), Error::Ok);
57-
58-
// size() should succeed and reflect the total size.
59-
Result<size_t> size = fdl->size();
60-
ASSERT_EQ(size.error(), Error::Ok);
61-
EXPECT_EQ(*size, sizeof(data));
62-
63-
// Load the first bytes of the data.
64-
{
65-
Result<FreeableBuffer> fb = fdl->load(
66-
/*offset=*/0,
67-
/*size=*/8,
68-
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program));
69-
ASSERT_EQ(fb.error(), Error::Ok);
70-
EXPECT_ALIGNED(fb->data(), alignment());
71-
EXPECT_EQ(fb->size(), 8);
72-
EXPECT_EQ(
73-
0,
74-
std::memcmp(
75-
fb->data(),
76-
"\x00\x01\x02\x03"
77-
"\x04\x05\x06\x07",
78-
fb->size()));
79-
80-
// Freeing should release the buffer and clear out the segment.
81-
fb->Free();
82-
EXPECT_EQ(fb->size(), 0);
83-
EXPECT_EQ(fb->data(), nullptr);
84-
85-
// Safe to call multiple times.
86-
fb->Free();
87-
}
88-
89-
// Load the last few bytes of the data, a different size than the first time.
90-
{
91-
Result<FreeableBuffer> fb = fdl->load(
92-
/*offset=*/sizeof(data) - 3,
93-
/*size=*/3,
94-
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program));
95-
ASSERT_EQ(fb.error(), Error::Ok);
96-
EXPECT_ALIGNED(fb->data(), alignment());
97-
EXPECT_EQ(fb->size(), 3);
98-
EXPECT_EQ(0, std::memcmp(fb->data(), "\xfd\xfe\xff", fb->size()));
99-
}
100-
101-
// Loading all of the data succeeds.
102-
{
103-
Result<FreeableBuffer> fb = fdl->load(
104-
/*offset=*/0,
105-
/*size=*/sizeof(data),
106-
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program));
107-
ASSERT_EQ(fb.error(), Error::Ok);
108-
EXPECT_ALIGNED(fb->data(), alignment());
109-
EXPECT_EQ(fb->size(), sizeof(data));
110-
EXPECT_EQ(0, std::memcmp(fb->data(), data, fb->size()));
111-
}
112-
113-
// Loading zero-sized data succeeds, even at the end of the data.
114-
{
115-
Result<FreeableBuffer> fb = fdl->load(
116-
/*offset=*/sizeof(data),
117-
/*size=*/0,
118-
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program));
119-
ASSERT_EQ(fb.error(), Error::Ok);
120-
EXPECT_EQ(fb->size(), 0);
121-
}
122-
}
123-
124-
TEST_P(FileDataLoaderTest, FileDescriptorLoadPrefixFail) {
125-
// Write some heterogeneous data to a file.
126-
uint8_t data[256];
127-
for (int i = 0; i < sizeof(data); ++i) {
128-
data[i] = i;
129-
}
130-
TempFile tf(data, sizeof(data));
131-
132-
int fd = ::open(tf.path().c_str(), O_RDONLY);
133-
134-
// Wrap it in a loader.
135-
Result<FileDataLoader> fdl = FileDataLoader::fromFileDescriptorUri(
136-
std::to_string(fd).c_str(), alignment());
137-
ASSERT_EQ(fdl.error(), Error::InvalidArgument);
138-
}
139-
14043
TEST_P(FileDataLoaderTest, InBoundsLoadsSucceed) {
14144
// Write some heterogeneous data to a file.
14245
uint8_t data[256];

0 commit comments

Comments
 (0)