File size: 2,856 Bytes
8cb4813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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;

// Simple data loader: read file into memory
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");

  // Small method allocator: 32KB — enough for values_ (2 * EValue)
  // but NOT enough for evalp_list (50000 * 8 = 400KB) → nullptr
  static uint8_t method_pool[32 * 1024];
  MemoryAllocator method_alloc(sizeof(method_pool), method_pool);

  // Empty planned memory
  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;
}