Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/include/loader/filemgr.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
//===-- wasmedge/loader/filemgr.h - File Manager definition ---------------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the declaration of the FileMgr class, which controls flow
12
/// of WASM loading.
13
///
14
//===----------------------------------------------------------------------===//
15
#pragma once
16
17
#include "common/errcode.h"
18
#include "common/types.h"
19
#include "system/mmap.h"
20
21
#include <algorithm>
22
#include <cstddef>
23
#include <cstdint>
24
#include <optional>
25
#include <string>
26
#include <vector>
27
28
namespace WasmEdge {
29
30
/// File manager interface.
31
class FileMgr {
32
public:
33
  enum class FileHeader : uint8_t {
34
    // WASM or universal WASM.
35
    Wasm,
36
    // AOT compiled WASM as Linux ELF.
37
    ELF,
38
    // AOT compiled WASM as MacOS Mach_O 32-bit.
39
    MachO_32,
40
    // AOT compiled WASM as MacOS Mach_O 64-bit.
41
    MachO_64,
42
    // AOT compiled WASM as Windows DLL.
43
    DLL,
44
    // Unknown file header.
45
    Unknown
46
  };
47
48
  /// Set the file path.
49
  Expect<void> setPath(const std::filesystem::path &FilePath);
50
51
  /// Set the binary data.
52
  Expect<void> setCode(Span<const Byte> CodeData);
53
54
  /// Set the binary data.
55
  Expect<void> setCode(std::vector<Byte> CodeData);
56
57
  /// Read one byte.
58
  Expect<Byte> readByte();
59
60
  /// Read number of bytes into a vector.
61
  Expect<std::vector<Byte>> readBytes(size_t SizeToRead);
62
63
  /// Read an unsigned int.
64
  Expect<uint32_t> readU32();
65
66
  /// Read an unsigned long long int.
67
  Expect<uint64_t> readU64();
68
69
  template <typename Ret, size_t N> Expect<Ret> readSN();
70
71
  /// Read a signed int.
72
  Expect<int32_t> readS32();
73
74
  /// Read a S33.
75
  Expect<int64_t> readS33();
76
77
  /// Read a signed long long int.
78
  Expect<int64_t> readS64();
79
80
  /// Read a float.
81
  Expect<float> readF32();
82
83
  /// Read a double.
84
  Expect<double> readF64();
85
86
  /// Read a string, which is size(unsigned int) + bytes.
87
  Expect<std::string> readName();
88
89
  /// Peek one byte.
90
  Expect<Byte> peekByte();
91
92
  /// Get the file header type.
93
  FileHeader getHeaderType();
94
95
  /// Get current offset.
96
9.33M
  uint64_t getOffset() const noexcept { return Pos; }
97
98
  /// Get last succeeded read offset.
99
4.68k
  uint64_t getLastOffset() const noexcept { return LastPos; }
100
101
  /// Get remain size.
102
14.4M
  uint64_t getRemainSize() const noexcept { return Size - Pos; }
103
104
  /// Jump the content with size (size + content).
105
  Expect<void> jumpContent();
106
107
  /// Change the access position of the file.
108
103k
  void seek(uint64_t NewPos) {
109
103k
    if (Status != ErrCode::Value::IllegalPath) {
110
103k
      Pos = std::min(NewPos, Size);
111
103k
      LastPos = Pos;
112
103k
      Status = ErrCode::Value::Success;
113
103k
    }
114
103k
  }
115
116
  /// Reset status
117
9.30k
  void reset() {
118
9.30k
    Status = ErrCode::Value::UnexpectedEnd;
119
9.30k
    LastPos = 0;
120
9.30k
    Pos = 0;
121
9.30k
    Size = 0;
122
9.30k
    Data = nullptr;
123
9.30k
    FileMap.reset();
124
9.30k
    DataHolder.reset();
125
9.30k
  }
126
127
private:
128
  /// Helper function for reading number of bytes into a vector.
129
  Expect<void> readBytes(Span<Byte> Buffer);
130
131
  /// Helper function for checking boundary.
132
  Expect<void> testRead(uint64_t Read);
133
134
  /// File manager status.
135
  ErrCode::Value Status = ErrCode::Value::UnexpectedEnd;
136
137
  /// Last succeeded read start or read failed offset.
138
  /// Will be set to the read error or EOF offset when read failed, or set to
139
  /// the u32, u64, s32, s64, f32, f64, name, or bytes start offset when read
140
  /// succeeded or syntax error.
141
  uint64_t LastPos;
142
143
  /// Current read offset.
144
  uint64_t Pos;
145
146
  /// File or vector size.
147
  uint64_t Size;
148
149
  /// File or data management.
150
  const Byte *Data;
151
  std::optional<MMap> FileMap;
152
  std::optional<std::vector<Byte>> DataHolder;
153
};
154
155
} // namespace WasmEdge