|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/webgpu/runtime/WebGPUDelegateHeader.h> |
| 10 | + |
| 11 | +#include <cstring> |
| 12 | + |
| 13 | +#include <executorch/runtime/core/error.h> |
| 14 | +#include <executorch/runtime/core/result.h> |
| 15 | + |
| 16 | +namespace executorch { |
| 17 | +namespace backends { |
| 18 | +namespace webgpu { |
| 19 | + |
| 20 | +using executorch::runtime::Error; |
| 21 | +using executorch::runtime::Result; |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +struct ByteSlice { |
| 26 | + size_t offset; |
| 27 | + size_t size; |
| 28 | +}; |
| 29 | + |
| 30 | +constexpr size_t kExpectedSize = 30; |
| 31 | +constexpr char kExpectedMagic[4] = {'V', 'H', '0', '0'}; |
| 32 | + |
| 33 | +constexpr ByteSlice kMagic = {4, 4}; |
| 34 | +constexpr ByteSlice kHeaderSize = {8, 2}; |
| 35 | +constexpr ByteSlice kFlatbufferOffset = {10, 4}; |
| 36 | +constexpr ByteSlice kFlatbufferSize = {14, 4}; |
| 37 | +constexpr ByteSlice kBytesOffset = {18, 4}; |
| 38 | +constexpr ByteSlice kBytesSize = {22, 8}; |
| 39 | + |
| 40 | +uint64_t getUInt64LE(const uint8_t* data) { |
| 41 | + return (uint64_t)data[0] | ((uint64_t)data[1] << 8) | |
| 42 | + ((uint64_t)data[2] << 16) | ((uint64_t)data[3] << 24) | |
| 43 | + ((uint64_t)data[4] << 32) | ((uint64_t)data[5] << 40) | |
| 44 | + ((uint64_t)data[6] << 48) | ((uint64_t)data[7] << 56); |
| 45 | +} |
| 46 | + |
| 47 | +uint32_t getUInt32LE(const uint8_t* data) { |
| 48 | + return (uint32_t)data[0] | ((uint32_t)data[1] << 8) | |
| 49 | + ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24); |
| 50 | +} |
| 51 | + |
| 52 | +uint32_t getUInt16LE(const uint8_t* data) { |
| 53 | + return (uint32_t)data[0] | ((uint32_t)data[1] << 8); |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +bool WebGPUDelegateHeader::is_valid() const { |
| 59 | + if (header_size < kExpectedSize) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + if (flatbuffer_offset < header_size) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + if (flatbuffer_size == 0) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + if (bytes_offset < flatbuffer_offset + flatbuffer_size) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +Result<WebGPUDelegateHeader> WebGPUDelegateHeader::parse(const void* data) { |
| 75 | + const uint8_t* header_data = (const uint8_t*)data; |
| 76 | + |
| 77 | + const uint8_t* magic_start = header_data + kMagic.offset; |
| 78 | + if (std::memcmp(magic_start, kExpectedMagic, kMagic.size) != 0) { |
| 79 | + return Error::NotFound; |
| 80 | + } |
| 81 | + |
| 82 | + WebGPUDelegateHeader header = WebGPUDelegateHeader{ |
| 83 | + getUInt16LE(header_data + kHeaderSize.offset), |
| 84 | + getUInt32LE(header_data + kFlatbufferOffset.offset), |
| 85 | + getUInt32LE(header_data + kFlatbufferSize.offset), |
| 86 | + getUInt32LE(header_data + kBytesOffset.offset), |
| 87 | + getUInt64LE(header_data + kBytesSize.offset), |
| 88 | + }; |
| 89 | + |
| 90 | + if (!header.is_valid()) { |
| 91 | + return Error::InvalidArgument; |
| 92 | + } |
| 93 | + |
| 94 | + return header; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace webgpu |
| 98 | +} // namespace backends |
| 99 | +} // namespace executorch |
0 commit comments