xref: /aosp_15_r20/external/executorch/schema/extended_header.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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 #pragma once
10 
11 #include <executorch/runtime/core/result.h>
12 
13 namespace executorch {
14 namespace runtime {
15 
16 /**
17  * An extended, ExecuTorch-specific header that may be embedded in the
18  * serialized Program data header.
19  *
20  * For details see //executorch/docs/source/pte-file-format.md
21  */
22 struct ExtendedHeader {
23   /**
24    * To find the header, callers should provide at least this many bytes of the
25    * head of the serialized Program data.
26    */
27   static constexpr size_t kNumHeadBytes = 64;
28 
29   /**
30    * The offset into the Program serialized program data where the extended
31    * header should begin.
32    */
33   static constexpr size_t kHeaderOffset = 8;
34 
35   /**
36    * The magic bytes that identify the header.
37    *
38    * This is the canonical definition of the expected value. If the header
39    * layout ever changes in a compatibility-breaking way, increment the digits
40    * in the magic. But, doing so will prevent older binaries from recognizing
41    * the presence of the header. The compatibility-preserving way to make
42    * changes is to increase the header's length field and add new fields at the
43    * end.
44    */
45   static constexpr size_t kMagicSize = 4;
46   static constexpr char kMagic[kMagicSize] = {'e', 'h', '0', '0'};
47 
48   /**
49    * Look for and parse an ExtendedHeader in the provided data.
50    *
51    * @param[in] data The contents of the beginning of the serialized binary
52    *     Program data, starting at offset 0 (i.e., the head of the file).
53    * @param[in] size Length of `data` in bytes. Must be >= kNumHeadBytes or this
54    *     call will fail.
55    *
56    * @returns an ExtendedHeader if the header was found and is valid. Returns an
57    *     error if size was too short, if the header was not found, or if the
58    *     header appeared to be corrupt.
59    */
60   static Result<ExtendedHeader> Parse(const void* data, size_t size);
61 
62   /**
63    * The size in bytes of the Program flatbuffer data, starting from offset
64    * zero.
65    */
66   uint64_t program_size;
67 
68   /**
69    * The offset in bytes of the first segment, if present. Zero if no segment
70    * is present.
71    */
72   uint64_t segment_base_offset;
73 };
74 
75 } // namespace runtime
76 } // namespace executorch
77