summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Lib/Runtime/Invoke.cpp
blob: 8ab4381f3872f821bafcb04501c2f692ddb2ccd1 (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
#include <string.h>
#include <memory>
#include <vector>
#include "RuntimePrivate.h"
#include "WAVM/IR/Types.h"
#include "WAVM/IR/Value.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/LLVMJIT/LLVMJIT.h"
#include "WAVM/Logging/Logging.h"
#include "WAVM/Runtime/Runtime.h"
#include "WAVM/RuntimeABI/RuntimeABI.h"

using namespace WAVM;
using namespace WAVM::IR;
using namespace WAVM::Runtime;

void Runtime::invokeFunction(Context* context,
							 const Function* function,
							 FunctionType invokeSig,
							 const UntaggedValue arguments[],
							 UntaggedValue outResults[])
{
	FunctionType functionType{function->encodedType};

	// Verify that the invoke signature matches the function being invoked.
	if(invokeSig != functionType && !isSubtype(functionType, invokeSig))
	{
		if(Log::isCategoryEnabled(Log::debug))
		{
			Log::printf(
				Log::debug,
				"Invoke signature mismatch:\n  Invoke signature: %s\n  Invoked function type: %s\n",
				asString(invokeSig).c_str(),
				asString(getFunctionType(function)).c_str());
		}
		throwException(ExceptionTypes::invokeSignatureMismatch);
	}

	// Assert that the function, the context, and any reference arguments are all in the same
	// compartment.
	if(false)
	{
		WAVM_ASSERT(isInCompartment(asObject(function), context->compartment));
		for(Uptr argumentIndex = 0; argumentIndex < invokeSig.params().size(); ++argumentIndex)
		{
			const ValueType argType = invokeSig.params()[argumentIndex];
			const UntaggedValue& arg = arguments[argumentIndex];
			WAVM_ASSERT(!isReferenceType(argType) || !arg.object
						|| isInCompartment(arg.object, context->compartment));
		}
	}

	// Get the invoke thunk for this function type. Cache it in the function's FunctionMutableData
	// to avoid the global lock implied by LLVMJIT::getInvokeThunk.
	InvokeThunkPointer invokeThunk
		= function->mutableData->invokeThunk.load(std::memory_order_acquire);
	if(WAVM_UNLIKELY(!invokeThunk))
	{
		invokeThunk = LLVMJIT::getInvokeThunk(functionType);

		// Replace the cached thunk pointer, but since LLVMJIT::getInvokeThunk is guaranteed to
		// return the same thunk when called with the same FunctionType, we can assume that any
		// other writes this might race with were are writing the same value.
		function->mutableData->invokeThunk.store(invokeThunk, std::memory_order_release);
	}
	WAVM_ASSERT(invokeThunk);

	// MacOS std::function is a little more pessimistic about heap allocating captures, and without
	// wrapping these captured variables into a single reference, does a heap allocation for the
	// thunk passed to unwindSignalsAsExceptions below.
	struct InvokeContext
	{
		Context* context;
		const Function* function;
		const UntaggedValue* arguments;
		UntaggedValue* outResults;
		InvokeThunkPointer invokeThunk;
	};
	InvokeContext invokeContext;
	invokeContext.context = context;
	invokeContext.function = function;
	invokeContext.arguments = arguments;
	invokeContext.outResults = outResults;
	invokeContext.invokeThunk = invokeThunk;

	// Use unwindSignalsAsExceptions to ensure that any signal that occurs in WebAssembly code calls
	// C++ destructors on the stack between here and where it is caught.
	unwindSignalsAsExceptions([&invokeContext] {
		ContextRuntimeData* contextRuntimeData = getContextRuntimeData(invokeContext.context);

		// Call the invoke thunk.
		(*invokeContext.invokeThunk)(invokeContext.function,
									 contextRuntimeData,
									 invokeContext.arguments,
									 invokeContext.outResults);
	});
}