/src/WasmEdge/include/loader/aot_section.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/loader/aot_section.h - AOT Section definition ------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the AOTSection, which contains the |
12 | | /// logic for loading an AOT section. |
13 | | /// |
14 | | //===----------------------------------------------------------------------===// |
15 | | #pragma once |
16 | | |
17 | | #include "ast/section.h" |
18 | | #include "common/executable.h" |
19 | | #include "common/filesystem.h" |
20 | | #include "system/winapi.h" |
21 | | |
22 | | #include <cstdint> |
23 | | #include <memory> |
24 | | #include <vector> |
25 | | |
26 | | namespace WasmEdge { |
27 | | namespace Loader { |
28 | | |
29 | | /// Holder class for a library handle. |
30 | | class AOTSection : public Executable { |
31 | | public: |
32 | 0 | AOTSection() noexcept = default; |
33 | 0 | ~AOTSection() noexcept override { unload(); } |
34 | | Expect<void> load(const AST::AOTSection &AOTSec) noexcept; |
35 | | void unload() noexcept; |
36 | | |
37 | 0 | Symbol<const IntrinsicsTable *> getIntrinsics() noexcept override { |
38 | 0 | if (Binary) { |
39 | 0 | if (auto *const Pointer = getPointer<const IntrinsicsTable *>( |
40 | 0 | IntrinsicsAddress, sizeof(const IntrinsicsTable *))) { |
41 | 0 | return createSymbol<const IntrinsicsTable *>(Pointer); |
42 | 0 | } |
43 | 0 | } |
44 | 0 | return {}; |
45 | 0 | } |
46 | | |
47 | 0 | std::vector<Symbol<Wrapper>> getTypes(size_t) noexcept override { |
48 | 0 | std::vector<Symbol<Wrapper>> Result; |
49 | 0 | if (Binary) { |
50 | 0 | Result.reserve(TypesAddress.size()); |
51 | 0 | for (const auto Address : TypesAddress) { |
52 | 0 | auto *const Pointer = getPointer<Wrapper>(Address, 1); |
53 | 0 | if (!Pointer) { |
54 | 0 | return {}; |
55 | 0 | } |
56 | 0 | Result.push_back(createSymbol<Wrapper>(Pointer)); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | return Result; |
60 | 0 | } |
61 | | |
62 | 0 | std::vector<Symbol<void>> getCodes(size_t, size_t) noexcept override { |
63 | 0 | std::vector<Symbol<void>> Result; |
64 | 0 | if (Binary) { |
65 | 0 | Result.reserve(CodesAddress.size()); |
66 | 0 | for (const auto Address : CodesAddress) { |
67 | 0 | auto *const Pointer = getPointer<void>(Address, 1); |
68 | 0 | if (!Pointer) { |
69 | 0 | return {}; |
70 | 0 | } |
71 | 0 | Result.push_back(createSymbol<void>(Pointer)); |
72 | 0 | } |
73 | 0 | } |
74 | 0 | return Result; |
75 | 0 | } |
76 | | |
77 | | private: |
78 | 0 | uintptr_t getOffset() const noexcept { |
79 | 0 | return reinterpret_cast<uintptr_t>(Binary); |
80 | 0 | } |
81 | | |
82 | | /// Check that [Offset, Offset + Length) stays inside the mapped binary. |
83 | 0 | bool checkAccessBound(uint64_t Offset, uint64_t Length) const noexcept { |
84 | 0 | return Offset <= BinarySize && Length <= BinarySize - Offset; |
85 | 0 | } |
86 | | |
87 | | template <typename T> |
88 | 0 | T *getPointer(uint64_t Address, uint64_t Size) const noexcept { |
89 | 0 | if (!checkAccessBound(Address, Size)) { |
90 | 0 | return nullptr; |
91 | 0 | } |
92 | 0 | return reinterpret_cast<T *>(getOffset() + Address); |
93 | 0 | } Unexecuted instantiation: void* const (**WasmEdge::Loader::AOTSection::getPointer<void* const (*) [38]>(unsigned long, unsigned long) const) [38] Unexecuted instantiation: void (*WasmEdge::Loader::AOTSection::getPointer<void (void*, void*, void*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>*)>(unsigned long, unsigned long) const)(void*, void*, void*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant> const*, WasmEdge::Variant<unsigned int, int, unsigned long, long, float, double, unsigned __int128, __int128, unsigned long __vector(2), long __vector(2), unsigned int __vector(4), int __vector(4), unsigned short __vector(8), short __vector(8), unsigned char __vector(16), signed char __vector(16), float __vector(4), double __vector(2), WasmEdge::RefVariant>*) Unexecuted instantiation: void* WasmEdge::Loader::AOTSection::getPointer<void>(unsigned long, unsigned long) const |
94 | | |
95 | | uint8_t *Binary = nullptr; |
96 | | uint64_t BinarySize = 0; |
97 | | uint64_t IntrinsicsAddress = 0; |
98 | | std::vector<uintptr_t> TypesAddress; |
99 | | std::vector<uintptr_t> CodesAddress; |
100 | | #if WASMEDGE_OS_LINUX |
101 | | void *EHFrameAddress = nullptr; |
102 | | #elif WASMEDGE_OS_MACOS |
103 | | uint8_t *EHFrameAddress = nullptr; |
104 | | uint32_t EHFrameSize = 0; |
105 | | #elif WASMEDGE_OS_WINDOWS |
106 | | void *PDataAddress = nullptr; |
107 | | uint32_t PDataSize = 0; |
108 | | #endif |
109 | | }; |
110 | | |
111 | | } // namespace Loader |
112 | | } // namespace WasmEdge |