| 68 | } // namespace |
| 69 | |
| 70 | TEST(WasmBpfTest, SimpleRingbuf) { |
| 71 | using namespace std::string_view_literals; |
| 72 | // Test loading and attaching a BPF program and polling a buffer. |
| 73 | auto module = dynamic_cast<WasmEdge::Host::WasmBpfModule *>(createModule()); |
| 74 | ASSERT_NE(module, nullptr); |
| 75 | |
| 76 | // Create the calling frame with memory instance. |
| 77 | WasmEdge::Runtime::Instance::ModuleInstance moduleInst(""); |
| 78 | // moduleInst.addHostFunc() |
| 79 | moduleInst.addHostMemory( |
| 80 | "memory", std::make_unique<WasmEdge::Runtime::Instance::MemoryInstance>( |
| 81 | WasmEdge::AST::MemoryType(1))); |
| 82 | auto *memoryInst = moduleInst.findMemoryExports("memory"); |
| 83 | ASSERT_NE(memoryInst, nullptr); |
| 84 | auto &memoryInstRef = *memoryInst; |
| 85 | WasmEdge::Executor::Executor executor((WasmEdge::Configure())); |
| 86 | WasmEdge::Runtime::CallingFrame CallFrame(&executor, &moduleInst); |
| 87 | |
| 88 | namespace fs = std::filesystem; |
| 89 | auto bpfObject = getAssertsPath() / "simple_ringbuf.bpf.o"; |
| 90 | |
| 91 | // Ensure the BPF object we need exists. |
| 92 | ASSERT_TRUE(fs::exists(bpfObject)); |
| 93 | |
| 94 | // Read the BPF object into Wasm memory. |
| 95 | std::ifstream bpfObjStream(bpfObject); |
| 96 | ASSERT_TRUE(bpfObjStream.is_open()); |
| 97 | ASSERT_TRUE(bpfObjStream.good()); |
| 98 | std::vector<char> bpfObjectBytes( |
| 99 | (std::istreambuf_iterator<char>(bpfObjStream)), |
| 100 | std::istreambuf_iterator<char>()); |
| 101 | ASSERT_FALSE(bpfObjectBytes.empty()); |
| 102 | // Offset used to place data in memory. |
| 103 | uint32_t nextOffset = 1; |
| 104 | |
| 105 | // Put the BPF object in memory. |
| 106 | const uint32_t bpfObjectMemoryOffset = nextOffset; |
| 107 | fillMemContent(memoryInstRef, bpfObjectMemoryOffset, bpfObjectBytes); |
| 108 | nextOffset += static_cast<uint32_t>(bpfObjectBytes.size()); |
| 109 | |
| 110 | // Write the strings to memory. |
| 111 | std::array<const char *, 3> strings = { |
| 112 | "rb", // Map name |
| 113 | "handle_exec", // Program names |
| 114 | "" // An empty string |
| 115 | }; |
| 116 | std::array<uint32_t, 3> stringOffsets; |
| 117 | |
| 118 | for (size_t i = 0; i < strings.size(); i++) { |
| 119 | std::string currString(strings[i]); |
| 120 | std::vector<char> bytes(currString.begin(), currString.end()); |
| 121 | // Ensure that strings are zero-terminated |
| 122 | bytes.push_back('\0'); |
| 123 | fillMemContent(memoryInstRef, nextOffset, bytes); |
| 124 | stringOffsets[i] = nextOffset; |
| 125 | nextOffset += static_cast<uint32_t>(bytes.size()); |
| 126 | } |
| 127 |
nothing calls this directly
no test coverage detected