| #include <executorch/runtime/platform/platform.h> |
| #include <cstdio> |
| #include <cstdlib> |
| #include <cstring> |
| #include <cstdint> |
| #include <executorch/runtime/executor/program.h> |
| #include <executorch/runtime/core/memory_allocator.h> |
| #include <executorch/runtime/core/hierarchical_allocator.h> |
| #include <executorch/runtime/executor/method.h> |
|
|
| using executorch::runtime::Program; |
| using executorch::runtime::MemoryAllocator; |
| using executorch::runtime::HierarchicalAllocator; |
| using executorch::runtime::MemoryManager; |
| using executorch::runtime::Method; |
| using executorch::runtime::DataLoader; |
| using executorch::runtime::FreeableBuffer; |
| using executorch::runtime::Result; |
| using executorch::runtime::Error; |
| using executorch::runtime::Span; |
|
|
| |
| class SimpleLoader : public DataLoader { |
| public: |
| SimpleLoader(const uint8_t* data, size_t size) : data_(data), size_(size) {} |
| Result<FreeableBuffer> load( |
| size_t offset, size_t size, |
| __ET_UNUSED const DataLoader::SegmentInfo& info) const override { |
| if (offset + size > size_) { |
| return Error::InvalidArgument; |
| } |
| return FreeableBuffer(data_ + offset, size, nullptr); |
| } |
| Result<size_t> size() const override { return size_; } |
| private: |
| const uint8_t* data_; |
| size_t size_; |
| }; |
|
|
| int main(int argc, char** argv) { |
| const char* path = argc > 1 ? argv[1] : "poc_bug11.pte"; |
| FILE* f = fopen(path, "rb"); |
| if (!f) { fprintf(stderr, "cannot open %s\n", path); return 1; } |
| fseek(f, 0, SEEK_END); |
| size_t fsize = ftell(f); |
| fseek(f, 0, SEEK_SET); |
| uint8_t* buf = (uint8_t*)malloc(fsize); |
| fread(buf, 1, fsize, f); |
| fclose(f); |
| fprintf(stderr, "[harness] loaded %zu bytes from %s\n", fsize, path); |
|
|
| et_pal_init(); |
| SimpleLoader loader(buf, fsize); |
| Result<Program> program = Program::load(&loader); |
| if (!program.ok()) { |
| fprintf(stderr, "[harness] Program::load failed: 0x%x\n", |
| (unsigned)program.error()); |
| free(buf); |
| return 2; |
| } |
| fprintf(stderr, "[harness] Program loaded OK\n"); |
|
|
| |
| |
| static uint8_t method_pool[32 * 1024]; |
| MemoryAllocator method_alloc(sizeof(method_pool), method_pool); |
|
|
| |
| Span<uint8_t> planned_span((uint8_t*)nullptr, (size_t)0); |
| HierarchicalAllocator planned({&planned_span, 1}); |
|
|
| MemoryManager memory_manager(&method_alloc, &planned); |
|
|
| fprintf(stderr, "[harness] calling load_method('forward') with 32KB allocator\n"); |
| Result<Method> method = program->load_method("forward", &memory_manager); |
| if (method.ok()) { |
| fprintf(stderr, "[harness] method loaded (unexpected)\n"); |
| } else { |
| fprintf(stderr, "[harness] load_method error: 0x%x\n", |
| (unsigned)method.error()); |
| } |
|
|
| free(buf); |
| return 0; |
| } |
|
|