-
Notifications
You must be signed in to change notification settings - Fork 968
Expand file tree
/
Copy pathXNNHeader.cpp
More file actions
123 lines (101 loc) · 3.9 KB
/
XNNHeader.cpp
File metadata and controls
123 lines (101 loc) · 3.9 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/xnnpack/runtime/XNNHeader.h>
#include <cinttypes>
#include <cstring>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>
#pragma clang diagnostic ignored "-Wdeprecated"
namespace executorch {
namespace backends {
namespace xnnpack {
namespace delegate {
using executorch::runtime::Error;
using executorch::runtime::Result;
namespace {
/// Interprets the 8 bytes at `data` as a little-endian uint64_t.
uint64_t GetUInt64LE(const uint8_t* data) {
return (uint64_t)data[0] | ((uint64_t)data[1] << 8) |
((uint64_t)data[2] << 16) | ((uint64_t)data[3] << 24) |
((uint64_t)data[4] << 32) | ((uint64_t)data[5] << 40) |
((uint64_t)data[6] << 48) | ((uint64_t)data[7] << 56);
}
/// Interprets the 4 bytes at `data` as a little-endian uint32_t.
uint32_t GetUInt32LE(const uint8_t* data) {
return (uint32_t)data[0] | ((uint32_t)data[1] << 8) |
((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24);
}
} // namespace
Result<XNNHeader> XNNHeader::Parse(const void* data, size_t size) {
const uint8_t* header_data = (const uint8_t*)data;
if (size < XNNHeader::kMinSize) {
return Error::InvalidArgument;
}
const uint8_t* magic_start = header_data + XNNHeader::kMagicOffset;
if (std::memcmp(magic_start, XNNHeader::kMagic, XNNHeader::kMagicSize) != 0) {
return Error::NotFound;
}
uint32_t flatbuffer_offset =
GetUInt32LE(header_data + XNNHeader::kFlatbufferDataOffsetOffset);
uint32_t flatbuffer_size =
GetUInt32LE(header_data + XNNHeader::kFlatbufferDataSizeOffset);
uint32_t constant_data_offset =
GetUInt32LE(header_data + XNNHeader::kConstantDataOffsetOffset);
uint64_t constant_data_size =
GetUInt64LE(header_data + XNNHeader::kConstantDataSizeOffset);
// Validate min flatbuffer size.
constexpr size_t kMinFlatbufferSize =
sizeof(uint32_t) + 4; // root offset + identifier
ET_CHECK_OR_RETURN_ERROR(
flatbuffer_size >= kMinFlatbufferSize,
InvalidArgument,
"flatbuffer_size %" PRIu32 " is too small (minimum %zu)",
flatbuffer_size,
kMinFlatbufferSize);
// Validate that flatbuffer region does not overflow or exceed the buffer.
ET_CHECK_OR_RETURN_ERROR(
flatbuffer_offset <= size && flatbuffer_size <= size - flatbuffer_offset,
InvalidArgument,
"flatbuffer_offset: %" PRIu32 " and flatbuffer_size: %" PRIu32
" are invalid for buffer of size: %zu",
flatbuffer_offset,
flatbuffer_size,
size);
// Validate that constant data region does not overflow or exceed the buffer.
ET_CHECK_OR_RETURN_ERROR(
constant_data_offset <= size &&
constant_data_size <= size - constant_data_offset,
InvalidArgument,
"constant_data_offset: %" PRIu32 " and constant_data_size: %" PRIu64
" are invalid for buffer of size: %zu",
constant_data_offset,
constant_data_size,
size);
// Validate that constant data region does not overlap with flatbuffer region.
// flatbuffer should come before constant data.
ET_CHECK_OR_RETURN_ERROR(
constant_data_offset >= flatbuffer_offset &&
constant_data_offset - flatbuffer_offset >= flatbuffer_size,
InvalidArgument,
"constant_data_offset: %" PRIu32 " and flatbuffer_offset: %" PRIu32
" with flatbuffer_size: %" PRIu32 " are overlapping.",
constant_data_offset,
flatbuffer_offset,
flatbuffer_size);
return XNNHeader{
flatbuffer_offset,
flatbuffer_size,
constant_data_offset,
constant_data_size};
}
// Define storage for the static.
constexpr char XNNHeader::kMagic[kMagicSize];
} // namespace delegate
} // namespace xnnpack
} // namespace backends
} // namespace executorch