Runtime.cpp 5.7 KB
Newer Older
D
Daniel Larimer 已提交
1 2 3 4 5 6
#include "Inline/BasicTypes.h"
#include "Platform/Platform.h"
#include "Logging/Logging.h"
#include "Runtime.h"
#include "RuntimePrivate.h"

7 8
#include <iostream>

D
Daniel Larimer 已提交
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
namespace Runtime
{
	void init()
	{
		LLVMJIT::init();
		initWAVMIntrinsics();
	}
	
	// Returns a vector of strings, each element describing a frame of the call stack.
	// If the frame is a JITed function, use the JIT's information about the function
	// to describe it, otherwise fallback to whatever platform-specific symbol resolution
	// is available.
	std::vector<std::string> describeCallStack(const Platform::CallStack& callStack)
	{
		std::vector<std::string> frameDescriptions;
		for(auto frame : callStack.stackFrames)
		{
			std::string frameDescription;
			if(	LLVMJIT::describeInstructionPointer(frame.ip,frameDescription)
			||	Platform::describeInstructionPointer(frame.ip,frameDescription))
			{
				frameDescriptions.push_back(frameDescription);
			}
			else { frameDescriptions.push_back("<unknown function>"); }
		}
		return frameDescriptions;
	}

	[[noreturn]] void causeException(Exception::Cause cause)
	{
		auto callStack = Platform::captureCallStack();
		throw Exception {cause,describeCallStack(callStack)};
	}

	bool isA(ObjectInstance* object,const ObjectType& type)
	{
		if(type.kind != object->kind) { return false; }

		switch(type.kind)
		{
		case ObjectKind::function: return asFunctionType(type) == asFunction(object)->type;
		case ObjectKind::global: return asGlobalType(type) == asGlobal(object)->type;
		case ObjectKind::table: return isSubset(asTableType(type),asTable(object)->type);
		case ObjectKind::memory: return isSubset(asMemoryType(type),asMemory(object)->type);
		default: Errors::unreachable();
		}
	}

	[[noreturn]] void handleHardwareTrap(Platform::HardwareTrapType trapType,Platform::CallStack&& trapCallStack,Uptr trapOperand)
	{
59
    std::cerr << "handle hadrware trap \n";
D
Daniel Larimer 已提交
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
		std::vector<std::string> callStackDescription = describeCallStack(trapCallStack);

		switch(trapType)
		{
		case Platform::HardwareTrapType::accessViolation:
		{
			// If the access violation occured in a Table's reserved pages, treat it as an undefined table element runtime error.
			if(isAddressOwnedByTable(reinterpret_cast<U8*>(trapOperand))) { throw Exception { Exception::Cause::undefinedTableElement, callStackDescription }; }
			// If the access violation occured in a Memory's reserved pages, treat it as an access violation runtime error.
			else if(isAddressOwnedByMemory(reinterpret_cast<U8*>(trapOperand))) { throw Exception { Exception::Cause::accessViolation, callStackDescription }; }
			else
			{
				// If the access violation occured outside of a Table or Memory, treat it as a bug (possibly a security hole)
				// rather than a runtime error in the WebAssembly code.
				Log::printf(Log::Category::error,"Access violation outside of table or memory reserved addresses. Call stack:\n");
				for(auto calledFunction : callStackDescription) { Log::printf(Log::Category::error,"  %s\n",calledFunction.c_str()); }
				Errors::fatalf("unsandboxed access violation");
			}
		}
		case Platform::HardwareTrapType::stackOverflow: throw Exception { Exception::Cause::stackOverflow, callStackDescription };
		case Platform::HardwareTrapType::intDivideByZeroOrOverflow: throw Exception { Exception::Cause::integerDivideByZeroOrIntegerOverflow, callStackDescription };
		default: Errors::unreachable();
		};
	}

85

D
Daniel Larimer 已提交
86 87 88 89 90 91
	Result invokeFunction(FunctionInstance* function,const std::vector<Value>& parameters)
	{
		const FunctionType* functionType = function->type;
		
		// Check that the parameter types match the function, and copy them into a memory block that stores each as a 64-bit value.
		if(parameters.size() != functionType->parameters.size())
92 93 94
		{ 
       throw Exception {Exception::Cause::invokeSignatureMismatch}; 
    }
D
Daniel Larimer 已提交
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

		U64* thunkMemory = (U64*)alloca((functionType->parameters.size() + getArity(functionType->ret)) * sizeof(U64));
		for(Uptr parameterIndex = 0;parameterIndex < functionType->parameters.size();++parameterIndex)
		{
			if(functionType->parameters[parameterIndex] != parameters[parameterIndex].type)
			{
				throw Exception {Exception::Cause::invokeSignatureMismatch};
			}

			thunkMemory[parameterIndex] = parameters[parameterIndex].i64;
		}
		
		// Get the invoke thunk for this function type.
		LLVMJIT::InvokeFunctionPointer invokeFunctionPointer = LLVMJIT::getInvokeThunk(functionType);

		// Catch platform-specific runtime exceptions and turn them into Runtime::Values.
		Result result;
		Platform::HardwareTrapType trapType;
		Platform::CallStack trapCallStack;
		Uptr trapOperand;
		trapType = Platform::catchHardwareTraps(trapCallStack,trapOperand,
			[&]
			{
				// Call the invoke thunk.
				(*invokeFunctionPointer)(function->nativeFunction,thunkMemory);

				// Read the return value out of the thunk memory block.
				if(functionType->ret != ResultType::none)
				{
					result.type = functionType->ret;
					result.i64 = thunkMemory[functionType->parameters.size()];
				}
			});

		// If there was no hardware trap, just return the result.
		if(trapType == Platform::HardwareTrapType::none) { return result; }
		else { handleHardwareTrap(trapType,std::move(trapCallStack),trapOperand); }
	}

	const FunctionType* getFunctionType(FunctionInstance* function)
	{
		return function->type;
	}

	GlobalInstance* createGlobal(GlobalType type,Value initialValue)
	{
		return new GlobalInstance(type,initialValue);
	}

	Value getGlobalValue(GlobalInstance* global)
	{
		return Value(global->type.valueType,global->value);
	}

	Value setGlobalValue(GlobalInstance* global,Value newValue)
	{
		assert(newValue.type == global->type.valueType);
		assert(global->type.isMutable);
		const Value previousValue = Value(global->type.valueType,global->value);
		global->value = newValue;
		return previousValue;
	}
157
}