Commit aee1824a authored by titzer's avatar titzer Committed by Commit bot

[wasm] Fold bounds checks during graph building.

R=ahaas@chromium.org,clemensh@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/1962553002
Cr-Commit-Position: refs/heads/master@{#36100}
parent 38ec29b4
......@@ -2571,23 +2571,34 @@ Node* WasmGraphBuilder::StoreGlobal(uint32_t index, Node* val) {
void WasmGraphBuilder::BoundsCheckMem(MachineType memtype, Node* index,
uint32_t offset,
wasm::WasmCodePosition position) {
// TODO(turbofan): fold bounds checks for constant indexes.
DCHECK(module_ && module_->instance);
size_t size = module_->instance->mem_size;
byte memsize = wasm::WasmOpcodes::MemSize(memtype);
Node* cond;
if (offset >= size || (static_cast<uint64_t>(offset) + memsize) > size) {
// The access will always throw.
cond = jsgraph()->Int32Constant(0);
} else {
// Check against the limit.
size_t limit = size - offset - memsize;
CHECK(limit <= kMaxUInt32);
cond = graph()->NewNode(
jsgraph()->machine()->Uint32LessThanOrEqual(), index,
jsgraph()->Int32Constant(static_cast<uint32_t>(limit)));
// The access will always throw (unless memory is grown).
Node* cond = jsgraph()->Int32Constant(0);
trap_->AddTrapIfFalse(wasm::kTrapMemOutOfBounds, cond, position);
return;
}
// Check against the effective size.
size_t effective_size = size - offset - memsize;
CHECK(effective_size <= kMaxUInt32);
Uint32Matcher m(index);
if (m.HasValue()) {
uint32_t value = m.Value();
if (value <= effective_size) {
// The bounds check will always succeed.
return;
}
}
Node* cond = graph()->NewNode(
jsgraph()->machine()->Uint32LessThanOrEqual(), index,
jsgraph()->Int32Constant(static_cast<uint32_t>(effective_size)));
trap_->AddTrapIfFalse(wasm::kTrapMemOutOfBounds, cond, position);
}
......
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