Commit e0e8b2a2 authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Remove remaining occurences of the context in wasm code

The only remaining allowed occurence of a context is in WASM_TO_JS
code, which is regenerated for each instance.
This CL removes all the rest, to avoid subtle bugs where we might
forget to patch it. By renaming the BuildCallToRuntime method, we make
sure that noone accidentially calls the version which embeds a context.
For consistency, I even remove it from the WasmRunInterpreter stub,
which is never reused for new instantiations.

R=titzer@chromium.org

Review-Url: https://codereview.chromium.org/2708743003
Cr-Commit-Position: refs/heads/master@{#43409}
parent 5b8f1f84
...@@ -65,10 +65,12 @@ void MergeControlToEnd(JSGraph* jsgraph, Node* node) { ...@@ -65,10 +65,12 @@ void MergeControlToEnd(JSGraph* jsgraph, Node* node) {
} }
} }
Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, // Only call this function for code which is not reused across instantiations,
Handle<Context> context, Node** parameters, // as we do not patch the embedded context.
int parameter_count, Node** effect_ptr, Node* BuildCallToRuntimeWithContext(Runtime::FunctionId f, JSGraph* jsgraph,
Node* control) { Node* context, Node** parameters,
int parameter_count, Node** effect_ptr,
Node* control) {
const Runtime::Function* fun = Runtime::FunctionForId(f); const Runtime::Function* fun = Runtime::FunctionForId(f);
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor(
jsgraph->zone(), f, fun->nargs, Operator::kNoProperties, jsgraph->zone(), f, fun->nargs, Operator::kNoProperties,
...@@ -76,7 +78,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, ...@@ -76,7 +78,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph,
// CEntryStubConstant nodes have to be created and cached in the main // CEntryStubConstant nodes have to be created and cached in the main
// thread. At the moment this is only done for CEntryStubConstant(1). // thread. At the moment this is only done for CEntryStubConstant(1).
DCHECK_EQ(1, fun->result_size); DCHECK_EQ(1, fun->result_size);
// At the moment we only allow 2 parameters. If more parameters are needed, // At the moment we only allow 3 parameters. If more parameters are needed,
// increase this constant accordingly. // increase this constant accordingly.
static const int kMaxParams = 3; static const int kMaxParams = 3;
DCHECK_GE(kMaxParams, parameter_count); DCHECK_GE(kMaxParams, parameter_count);
...@@ -89,9 +91,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, ...@@ -89,9 +91,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph,
inputs[count++] = jsgraph->ExternalConstant( inputs[count++] = jsgraph->ExternalConstant(
ExternalReference(f, jsgraph->isolate())); // ref ExternalReference(f, jsgraph->isolate())); // ref
inputs[count++] = jsgraph->Int32Constant(fun->nargs); // arity inputs[count++] = jsgraph->Int32Constant(fun->nargs); // arity
inputs[count++] = context.is_null() inputs[count++] = context; // context
? jsgraph->SmiConstant(0)
: jsgraph->HeapConstant(context); // context
inputs[count++] = *effect_ptr; inputs[count++] = *effect_ptr;
inputs[count++] = control; inputs[count++] = control;
...@@ -101,6 +101,14 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, ...@@ -101,6 +101,14 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph,
return node; return node;
} }
Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph,
Node** parameters, int parameter_count,
Node** effect_ptr, Node* control) {
return BuildCallToRuntimeWithContext(f, jsgraph, jsgraph->NoContextConstant(),
parameters, parameter_count, effect_ptr,
control);
}
} // namespace } // namespace
// TODO(eholk): Support trap handlers on other platforms. // TODO(eholk): Support trap handlers on other platforms.
...@@ -327,8 +335,7 @@ class WasmTrapHelper : public ZoneObject { ...@@ -327,8 +335,7 @@ class WasmTrapHelper : public ZoneObject {
if (module && !module->instance->context.is_null()) { if (module && !module->instance->context.is_null()) {
Node* parameters[] = {trap_reason_smi, // message id Node* parameters[] = {trap_reason_smi, // message id
trap_position_smi}; // byte position trap_position_smi}; // byte position
BuildCallToRuntime(Runtime::kThrowWasmError, jsgraph(), BuildCallToRuntime(Runtime::kThrowWasmError, jsgraph(), parameters,
Handle<Context>::null(), parameters,
arraysize(parameters), effect_ptr, *control_ptr); arraysize(parameters), effect_ptr, *control_ptr);
} }
if (false) { if (false) {
...@@ -499,7 +506,7 @@ void WasmGraphBuilder::StackCheck(wasm::WasmCodePosition position, ...@@ -499,7 +506,7 @@ void WasmGraphBuilder::StackCheck(wasm::WasmCodePosition position,
CallDescriptor::kNoFlags, Operator::kNoProperties); CallDescriptor::kNoFlags, Operator::kNoProperties);
Node* stub_code = jsgraph()->HeapConstant(code); Node* stub_code = jsgraph()->HeapConstant(code);
Node* context = jsgraph()->SmiConstant(0); Node* context = jsgraph()->NoContextConstant();
Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code,
context, *effect, stack_check.if_false); context, *effect, stack_check.if_false);
...@@ -1790,30 +1797,18 @@ Node* WasmGraphBuilder::GrowMemory(Node* input) { ...@@ -1790,30 +1797,18 @@ Node* WasmGraphBuilder::GrowMemory(Node* input) {
check_input_range.Chain(*control_); check_input_range.Chain(*control_);
Runtime::FunctionId function_id = Runtime::kWasmGrowMemory; Node* parameters[] = {BuildChangeUint32ToSmi(input)};
const Runtime::Function* function = Runtime::FunctionForId(function_id); Node* old_effect = *effect_;
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( Node* call = BuildCallToRuntime(Runtime::kWasmGrowMemory, jsgraph(),
jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow, parameters, arraysize(parameters), effect_,
CallDescriptor::kNoFlags); check_input_range.if_true);
wasm::ModuleEnv* module = module_;
input = BuildChangeUint32ToSmi(input);
Node* inputs[] = {
jsgraph()->CEntryStubConstant(function->result_size), input, // C entry
jsgraph()->ExternalConstant(
ExternalReference(function_id, jsgraph()->isolate())), // ref
jsgraph()->Int32Constant(function->nargs), // arity
jsgraph()->HeapConstant(module->instance->context), // context
*effect_,
check_input_range.if_true};
Node* call = graph()->NewNode(jsgraph()->common()->Call(desc),
static_cast<int>(arraysize(inputs)), inputs);
Node* result = BuildChangeSmiToInt32(call); Node* result = BuildChangeSmiToInt32(call);
result = check_input_range.Phi(MachineRepresentation::kWord32, result, result = check_input_range.Phi(MachineRepresentation::kWord32, result,
jsgraph()->Int32Constant(-1)); jsgraph()->Int32Constant(-1));
*effect_ = graph()->NewNode(jsgraph()->common()->EffectPhi(2), call, *effect_, *effect_ = graph()->NewNode(jsgraph()->common()->EffectPhi(2), call,
check_input_range.merge); old_effect, check_input_range.merge);
*control_ = check_input_range.merge; *control_ = check_input_range.merge;
return result; return result;
} }
...@@ -1836,8 +1831,7 @@ Node* WasmGraphBuilder::Throw(Node* input) { ...@@ -1836,8 +1831,7 @@ Node* WasmGraphBuilder::Throw(Node* input) {
graph()->NewNode(machine->Word32And(), input, Int32Constant(0xFFFFu))); graph()->NewNode(machine->Word32And(), input, Int32Constant(0xFFFFu)));
Node* parameters[] = {lower, upper}; // thrown value Node* parameters[] = {lower, upper}; // thrown value
return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), parameters,
module_->instance->context, parameters,
arraysize(parameters), effect_, *control_); arraysize(parameters), effect_, *control_);
} }
...@@ -1847,8 +1841,7 @@ Node* WasmGraphBuilder::Catch(Node* input, wasm::WasmCodePosition position) { ...@@ -1847,8 +1841,7 @@ Node* WasmGraphBuilder::Catch(Node* input, wasm::WasmCodePosition position) {
Node* parameters[] = {input}; // caught value Node* parameters[] = {input}; // caught value
Node* value = Node* value =
BuildCallToRuntime(Runtime::kWasmGetCaughtExceptionValue, jsgraph(), BuildCallToRuntime(Runtime::kWasmGetCaughtExceptionValue, jsgraph(),
module_->instance->context, parameters, parameters, arraysize(parameters), effect_, *control_);
arraysize(parameters), effect_, *control_);
Node* is_smi; Node* is_smi;
Node* is_heap; Node* is_heap;
...@@ -2769,12 +2762,18 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code, ...@@ -2769,12 +2762,18 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code,
*control_ = start; *control_ = start;
*effect_ = start; *effect_ = start;
// Create the context parameter
Node* context = graph()->NewNode(
jsgraph()->common()->Parameter(
Linkage::GetJSCallContextParamIndex(wasm_count + 1), "%context"),
graph()->start());
if (!HasJSCompatibleSignature(sig_)) { if (!HasJSCompatibleSignature(sig_)) {
// Throw a TypeError. The native context is good enough here because we // Throw a TypeError. Use the context of the calling javascript function
// only throw a TypeError. // (passed as a parameter), such that the generated code is context
BuildCallToRuntime(Runtime::kWasmThrowTypeError, jsgraph(), // independent.
jsgraph()->isolate()->native_context(), nullptr, 0, BuildCallToRuntimeWithContext(Runtime::kWasmThrowTypeError, jsgraph(),
effect_, *control_); context, nullptr, 0, effect_, *control_);
// Add a dummy call to the wasm function so that the generated wrapper // Add a dummy call to the wasm function so that the generated wrapper
// contains a reference to the wrapped wasm function. Without this reference // contains a reference to the wrapped wasm function. Without this reference
...@@ -2793,12 +2792,6 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code, ...@@ -2793,12 +2792,6 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code,
return; return;
} }
// Create the context parameter
Node* context = graph()->NewNode(
jsgraph()->common()->Parameter(
Linkage::GetJSCallContextParamIndex(wasm_count + 1), "%context"),
graph()->start());
int pos = 0; int pos = 0;
args[pos++] = HeapConstant(wasm_code); args[pos++] = HeapConstant(wasm_code);
...@@ -2849,11 +2842,13 @@ void WasmGraphBuilder::BuildWasmToJSWrapper(Handle<JSReceiver> target, ...@@ -2849,11 +2842,13 @@ void WasmGraphBuilder::BuildWasmToJSWrapper(Handle<JSReceiver> target,
*control_ = start; *control_ = start;
if (!HasJSCompatibleSignature(sig_)) { if (!HasJSCompatibleSignature(sig_)) {
// Throw a TypeError. The native context is good enough here because we // Throw a TypeError. Embedding the context is ok here, since this code is
// only throw a TypeError. // regenerated at instantiation time.
Return(BuildCallToRuntime(Runtime::kWasmThrowTypeError, jsgraph(), Node* context =
jsgraph()->isolate()->native_context(), nullptr, jsgraph()->HeapConstant(jsgraph()->isolate()->native_context());
0, effect_, *control_)); Return(BuildCallToRuntimeWithContext(Runtime::kWasmThrowTypeError,
jsgraph(), context, nullptr, 0,
effect_, *control_));
return; return;
} }
...@@ -3005,8 +3000,7 @@ void WasmGraphBuilder::BuildWasmInterpreterEntry( ...@@ -3005,8 +3000,7 @@ void WasmGraphBuilder::BuildWasmInterpreterEntry(
jsgraph()->SmiConstant(function_index), // function index jsgraph()->SmiConstant(function_index), // function index
arg_buffer, // argument buffer arg_buffer, // argument buffer
}; };
BuildCallToRuntime(Runtime::kWasmRunInterpreter, jsgraph(), BuildCallToRuntime(Runtime::kWasmRunInterpreter, jsgraph(), parameters,
instance->compiled_module()->native_context(), parameters,
arraysize(parameters), effect_, *control_); arraysize(parameters), effect_, *control_);
// Read back the return value. // Read back the return value.
......
...@@ -55,6 +55,11 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { ...@@ -55,6 +55,11 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
CONVERT_UINT32_ARG_CHECKED(delta_pages, 0); CONVERT_UINT32_ARG_CHECKED(delta_pages, 0);
Handle<WasmInstanceObject> instance(GetWasmInstanceOnStackTop(isolate), Handle<WasmInstanceObject> instance(GetWasmInstanceOnStackTop(isolate),
isolate); isolate);
// Set the current isolate's context.
DCHECK_NULL(isolate->context());
isolate->set_context(instance->compiled_module()->ptr_to_native_context());
return *isolate->factory()->NewNumberFromInt( return *isolate->factory()->NewNumberFromInt(
wasm::GrowMemory(isolate, instance, delta_pages)); wasm::GrowMemory(isolate, instance, delta_pages));
} }
...@@ -145,6 +150,10 @@ RUNTIME_FUNCTION(Runtime_WasmThrow) { ...@@ -145,6 +150,10 @@ RUNTIME_FUNCTION(Runtime_WasmThrow) {
const int32_t thrown_value = (upper << 16) | lower; const int32_t thrown_value = (upper << 16) | lower;
// Set the current isolate's context.
DCHECK_NULL(isolate->context());
isolate->set_context(GetWasmContextOnStackTop(isolate));
return isolate->Throw(*isolate->factory()->NewNumberFromInt(thrown_value)); return isolate->Throw(*isolate->factory()->NewNumberFromInt(thrown_value));
} }
...@@ -176,8 +185,9 @@ RUNTIME_FUNCTION(Runtime_WasmRunInterpreter) { ...@@ -176,8 +185,9 @@ RUNTIME_FUNCTION(Runtime_WasmRunInterpreter) {
CHECK(arg_buffer_obj->IsSmi()); CHECK(arg_buffer_obj->IsSmi());
uint8_t* arg_buffer = reinterpret_cast<uint8_t*>(*arg_buffer_obj); uint8_t* arg_buffer = reinterpret_cast<uint8_t*>(*arg_buffer_obj);
DCHECK_EQ(isolate->context(), // Set the current isolate's context.
instance->compiled_module()->ptr_to_native_context()); DCHECK_NULL(isolate->context());
isolate->set_context(instance->compiled_module()->ptr_to_native_context());
instance->debug_info()->RunInterpreter(func_index, arg_buffer); instance->debug_info()->RunInterpreter(func_index, arg_buffer);
return isolate->heap()->undefined_value(); return isolate->heap()->undefined_value();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment