summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Lib/Emscripten/EmscriptenPrivate.h
blob: b7f8fdf0d7990b35f7b1829a47c20b3b98a6b4ca (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#pragma once

#include <atomic>
#include <vector>
#include "EmscriptenABI.h"
#include "WAVM/Emscripten/Emscripten.h"
#include "WAVM/IR/Value.h"
#include "WAVM/Inline/IndexMap.h"
#include "WAVM/Inline/IntrusiveSharedPtr.h"
#include "WAVM/Inline/Time.h"
#include "WAVM/Platform/Mutex.h"
#include "WAVM/Platform/Thread.h"
#include "WAVM/Runtime/Intrinsics.h"
#include "WAVM/Runtime/Linker.h"
#include "WAVM/Runtime/Runtime.h"

namespace WAVM { namespace IR {
	struct Module;
}}

namespace WAVM { namespace VFS {
	struct VFD;
}}

namespace WAVM { namespace Emscripten {

	// Metadata from the emscripten_metadata user section of a module emitted by Emscripten.
	struct EmscriptenModuleMetadata
	{
		U32 metadataVersionMajor;
		U32 metadataVersionMinor;

		U32 abiVersionMajor;
		U32 abiVersionMinor;

		U32 backendID;
		U32 numMemoryPages;
		U32 numTableElems;
		U32 globalBaseAddress;
		U32 dynamicBaseAddress;
		U32 dynamicTopAddressAddress;
		U32 tempDoubleAddress;
		U32 standaloneWASM;
	};

	// Keeps track of the entry function used by a running WebAssembly-spawned thread.
	// Used to find garbage collection roots.
	struct Thread
	{
		Emscripten::Process* process;
		emabi::pthread_t id = 0;
		std::atomic<Uptr> numRefs{0};

		Platform::Thread* platformThread = nullptr;
		Runtime::GCPointer<Runtime::Context> context;
		Runtime::GCPointer<Runtime::Function> threadFunc;

		emabi::Address stackAddress;
		emabi::Address numStackBytes;

		std::atomic<U32> exitCode{0};

		I32 argument;

		HashMap<emabi::pthread_key_t, emabi::Address> pthreadSpecific;

		Thread(Emscripten::Process* inProcess,
			   Runtime::Context* inContext,
			   Runtime::Function* inEntryFunction,
			   I32 inArgument)
		: process(inProcess), context(inContext), threadFunc(inEntryFunction), argument(inArgument)
		{
		}

		void addRef(Uptr delta = 1) { numRefs += delta; }
		void removeRef()
		{
			if(--numRefs == 0) { delete this; }
		}
	};

	struct Process : Runtime::Resolver
	{
		Runtime::GCPointer<Runtime::Compartment> compartment;

		Runtime::GCPointer<Runtime::Instance> wasi_snapshot_preview1;
		Runtime::GCPointer<Runtime::Instance> env;
		Runtime::GCPointer<Runtime::Instance> asm2wasm;
		Runtime::GCPointer<Runtime::Instance> global;

		IntrusiveSharedPtr<Thread> mainThread;

		Runtime::GCPointer<Runtime::Instance> instance;
		Runtime::GCPointer<Runtime::Memory> memory;
		Runtime::GCPointer<Runtime::Table> table;

		Runtime::GCPointer<Runtime::Function> malloc;
		Runtime::GCPointer<Runtime::Function> free;
		Runtime::GCPointer<Runtime::Function> stackAlloc;
		Runtime::GCPointer<Runtime::Function> stackSave;
		Runtime::GCPointer<Runtime::Function> stackRestore;
		Runtime::GCPointer<Runtime::Function> errnoLocation;

		// A global list of running threads created by WebAssembly code.
		Platform::Mutex threadsMutex;
		IndexMap<emabi::pthread_t, IntrusiveSharedPtr<Thread>> threads{1, UINT32_MAX};

		std::atomic<emabi::pthread_key_t> pthreadSpecificNextKey{0};

		std::atomic<emabi::Address> currentLocale;

		std::vector<std::string> args;
		std::vector<std::string> envs;

		VFS::VFD* stdIn{nullptr};
		VFS::VFD* stdOut{nullptr};
		VFS::VFD* stdErr{nullptr};

		Time processClockOrigin;

		~Process();

		bool resolve(const std::string& moduleName,
					 const std::string& exportName,
					 IR::ExternType type,
					 Runtime::Object*& outObject) override;
	};

	struct ExitException
	{
		U32 exitCode;
	};

	struct ExitThreadException
	{
		U32 exitCode;
	};

	inline emabi::Address coerce32bitAddress(Runtime::Memory* memory, Uptr address)
	{
		if(address >= emabi::addressMax)
		{
			Runtime::throwException(Runtime::ExceptionTypes::outOfBoundsMemoryAccess,
									{Runtime::asObject(memory), U64(address)});
		}
		return (U32)address;
	}

	inline emabi::Result coerce32bitAddressResult(Runtime::Memory* memory, Iptr address)
	{
		if(address >= emabi::resultMax)
		{
			Runtime::throwException(Runtime::ExceptionTypes::outOfBoundsMemoryAccess,
									{Runtime::asObject(memory), U64(address)});
		}
		return (emabi::Result)address;
	}

	inline Emscripten::Process* getProcess(Runtime::ContextRuntimeData* contextRuntimeData)
	{
		auto process = (Emscripten::Process*)getUserData(
			getCompartmentFromContextRuntimeData(contextRuntimeData));
		WAVM_ASSERT(process);
		WAVM_ASSERT(process->memory);
		return process;
	}

	inline Emscripten::Thread* getEmscriptenThread(Runtime::ContextRuntimeData* contextRuntimeData)
	{
		auto thread
			= (Emscripten::Thread*)getUserData(getContextFromRuntimeData(contextRuntimeData));
		WAVM_ASSERT(thread);
		WAVM_ASSERT(thread->process);
		return thread;
	}

	emabi::Address dynamicAlloc(Emscripten::Process* process,
								Runtime::Context* context,
								emabi::Size numBytes);
	void initThreadLocals(Thread* thread);

	WAVM_DECLARE_INTRINSIC_MODULE(envThreads)
}}