|
| 1 | +// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#include "Luau/CodeGen.h" |
| 5 | +#include "Luau/Common.h" |
| 6 | +#include "Luau/NativeProtoExecData.h" |
| 7 | + |
| 8 | +#include <array> |
| 9 | +#include <atomic> |
| 10 | +#include <memory> |
| 11 | +#include <mutex> |
| 12 | +#include <optional> |
| 13 | +#include <stdint.h> |
| 14 | +#include <unordered_map> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace Luau |
| 18 | +{ |
| 19 | +namespace CodeGen |
| 20 | +{ |
| 21 | + |
| 22 | +// SharedCodeAllocator is a native executable code allocator that provides |
| 23 | +// shared ownership of the native code. Code is allocated on a per-module |
| 24 | +// basis. Each module is uniquely identifiable via an id, which may be a hash |
| 25 | +// or other unique value. Each module may contain multiple natively compiled |
| 26 | +// functions (protos). |
| 27 | +// |
| 28 | +// The module is the unit of shared ownership (i.e., it is where the reference |
| 29 | +// count is maintained). |
| 30 | + |
| 31 | + |
| 32 | +struct CodeAllocator; |
| 33 | +class NativeModule; |
| 34 | +class NativeModuleRef; |
| 35 | +class SharedCodeAllocator; |
| 36 | + |
| 37 | + |
| 38 | +// A NativeModule represents a single natively-compiled module (script). It is |
| 39 | +// the unit of shared ownership and is thus where the reference count is |
| 40 | +// maintained. It owns a set of NativeProtos, with associated native exec data, |
| 41 | +// and the allocated native data and code. |
| 42 | +class NativeModule |
| 43 | +{ |
| 44 | +public: |
| 45 | + NativeModule(SharedCodeAllocator* allocator, const std::optional<ModuleId>& moduleId, const uint8_t* moduleBaseAddress, |
| 46 | + std::vector<NativeProtoExecDataPtr> nativeProtos) noexcept; |
| 47 | + |
| 48 | + NativeModule(const NativeModule&) = delete; |
| 49 | + NativeModule(NativeModule&&) = delete; |
| 50 | + NativeModule& operator=(const NativeModule&) = delete; |
| 51 | + NativeModule& operator=(NativeModule&&) = delete; |
| 52 | + |
| 53 | + // The NativeModule must not be destroyed if there are any outstanding |
| 54 | + // references. It should thus only be destroyed by a call to release() |
| 55 | + // that releases the last reference. |
| 56 | + ~NativeModule() noexcept; |
| 57 | + |
| 58 | + size_t addRef() const noexcept; |
| 59 | + size_t addRefs(size_t count) const noexcept; |
| 60 | + size_t release() const noexcept; |
| 61 | + [[nodiscard]] size_t getRefcount() const noexcept; |
| 62 | + |
| 63 | + [[nodiscard]] const std::optional<ModuleId>& getModuleId() const noexcept; |
| 64 | + |
| 65 | + // Gets the base address of the executable native code for the module. |
| 66 | + [[nodiscard]] const uint8_t* getModuleBaseAddress() const noexcept; |
| 67 | + |
| 68 | + // Attempts to find the NativeProto with the given bytecode id. If no |
| 69 | + // NativeProto for that bytecode id exists, a null pointer is returned. |
| 70 | + [[nodiscard]] const uint32_t* tryGetNativeProto(uint32_t bytecodeId) const noexcept; |
| 71 | + |
| 72 | + [[nodiscard]] const std::vector<NativeProtoExecDataPtr>& getNativeProtos() const noexcept; |
| 73 | + |
| 74 | +private: |
| 75 | + mutable std::atomic<size_t> refcount = 0; |
| 76 | + |
| 77 | + SharedCodeAllocator* allocator = nullptr; |
| 78 | + std::optional<ModuleId> moduleId = {}; |
| 79 | + const uint8_t* moduleBaseAddress = nullptr; |
| 80 | + |
| 81 | + std::vector<NativeProtoExecDataPtr> nativeProtos = {}; |
| 82 | +}; |
| 83 | + |
| 84 | +// A NativeModuleRef is an owning reference to a NativeModule. (Note: We do |
| 85 | +// not use shared_ptr, to avoid complex state management in the Luau GC Proto |
| 86 | +// object.) |
| 87 | +class NativeModuleRef |
| 88 | +{ |
| 89 | +public: |
| 90 | + NativeModuleRef() noexcept = default; |
| 91 | + NativeModuleRef(const NativeModule* nativeModule) noexcept; |
| 92 | + |
| 93 | + NativeModuleRef(const NativeModuleRef& other) noexcept; |
| 94 | + NativeModuleRef(NativeModuleRef&& other) noexcept; |
| 95 | + NativeModuleRef& operator=(NativeModuleRef other) noexcept; |
| 96 | + |
| 97 | + ~NativeModuleRef() noexcept; |
| 98 | + |
| 99 | + void reset() noexcept; |
| 100 | + void swap(NativeModuleRef& other) noexcept; |
| 101 | + |
| 102 | + [[nodiscard]] bool empty() const noexcept; |
| 103 | + explicit operator bool() const noexcept; |
| 104 | + |
| 105 | + [[nodiscard]] const NativeModule* get() const noexcept; |
| 106 | + [[nodiscard]] const NativeModule* operator->() const noexcept; |
| 107 | + [[nodiscard]] const NativeModule& operator*() const noexcept; |
| 108 | + |
| 109 | +private: |
| 110 | + const NativeModule* nativeModule = nullptr; |
| 111 | +}; |
| 112 | + |
| 113 | +class SharedCodeAllocator |
| 114 | +{ |
| 115 | +public: |
| 116 | + SharedCodeAllocator(CodeAllocator* codeAllocator) noexcept; |
| 117 | + |
| 118 | + SharedCodeAllocator(const SharedCodeAllocator&) = delete; |
| 119 | + SharedCodeAllocator(SharedCodeAllocator&&) = delete; |
| 120 | + SharedCodeAllocator& operator=(const SharedCodeAllocator&) = delete; |
| 121 | + SharedCodeAllocator& operator=(SharedCodeAllocator&&) = delete; |
| 122 | + |
| 123 | + ~SharedCodeAllocator() noexcept; |
| 124 | + |
| 125 | + // If we have a NativeModule for the given ModuleId, an owning reference to |
| 126 | + // it is returned. Otherwise, an empty NativeModuleRef is returned. |
| 127 | + [[nodiscard]] NativeModuleRef tryGetNativeModule(const ModuleId& moduleId) const noexcept; |
| 128 | + |
| 129 | + // If we have a NativeModule for the given ModuleId, an owning reference to |
| 130 | + // it is returned. Otherwise, a new NativeModule is created for that ModuleId |
| 131 | + // using the provided NativeProtos, data, and code (space is allocated for the |
| 132 | + // data and code such that it can be executed). Like std::map::insert, the |
| 133 | + // bool result is true if a new module was created; false if an existing |
| 134 | + // module is being returned. |
| 135 | + std::pair<NativeModuleRef, bool> getOrInsertNativeModule(const ModuleId& moduleId, std::vector<NativeProtoExecDataPtr> nativeProtos, |
| 136 | + const uint8_t* data, size_t dataSize, const uint8_t* code, size_t codeSize); |
| 137 | + |
| 138 | + NativeModuleRef insertAnonymousNativeModule( |
| 139 | + std::vector<NativeProtoExecDataPtr> nativeProtos, const uint8_t* data, size_t dataSize, const uint8_t* code, size_t codeSize); |
| 140 | + |
| 141 | + // If a NativeModule exists for the given ModuleId and that NativeModule |
| 142 | + // is no longer referenced, the NativeModule is destroyed. This should |
| 143 | + // usually only be called by NativeModule::release() when the reference |
| 144 | + // count becomes zero |
| 145 | + void eraseNativeModuleIfUnreferenced(const NativeModule& nativeModule); |
| 146 | + |
| 147 | +private: |
| 148 | + struct ModuleIdHash |
| 149 | + { |
| 150 | + [[nodiscard]] size_t operator()(const ModuleId& moduleId) const noexcept; |
| 151 | + }; |
| 152 | + |
| 153 | + [[nodiscard]] NativeModuleRef tryGetNativeModuleWithLockHeld(const ModuleId& moduleId) const noexcept; |
| 154 | + |
| 155 | + mutable std::mutex mutex; |
| 156 | + |
| 157 | + std::unordered_map<ModuleId, std::unique_ptr<NativeModule>, ModuleIdHash, std::equal_to<>> identifiedModules; |
| 158 | + |
| 159 | + std::atomic<size_t> anonymousModuleCount = 0; |
| 160 | + |
| 161 | + CodeAllocator* codeAllocator = nullptr; |
| 162 | +}; |
| 163 | + |
| 164 | +} // namespace CodeGen |
| 165 | +} // namespace Luau |
0 commit comments