<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[213465] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/213465">213465</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2017-03-06 13:44:20 -0800 (Mon, 06 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly: implement init_expr for Element
https://bugs.webkit.org/show_bug.cgi?id=165888
&lt;rdar://problem/29760199&gt;

Reviewed by Keith Miller.

JSTests:

* wasm/Builder_WebAssemblyBinary.js:
(const.emitters.Element):
* wasm/assert.js:
* wasm/js-api/element.js:
(assert.throws):
(badInstantiation.makeModule):
(badInstantiation.test):
(badInstantiation):
* wasm/js-api/global-error.js:

Source/JavaScriptCore:

This patch fixes a few bugs. The main change is allowing init_expr
for the Element's offset. To do this, I had to fix a couple of
other bugs:
        
- I removed our invalid early module-parse-time invalidation
of out of bound Element sections. This is not in the spec because
it can't be validated in the general case when the offset is a
get_global.
        
- Our get_global validation inside our init_expr parsing code was simply wrong.
It thought that the index operand to get_global went into the pool of imports,
but it does not. It indexes into the pool of globals. I changed the code to
refer to the global pool instead.

* wasm/WasmFormat.h:
(JSC::Wasm::Element::Element):
* wasm/WasmModuleParser.cpp:
* wasm/js/WebAssemblyModuleRecord.cpp:
(JSC::WebAssemblyModuleRecord::evaluate):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkJSTestswasmBuilder_WebAssemblyBinaryjs">trunk/JSTests/wasm/Builder_WebAssemblyBinary.js</a></li>
<li><a href="#trunkJSTestswasmassertjs">trunk/JSTests/wasm/assert.js</a></li>
<li><a href="#trunkJSTestswasmjsapielementjs">trunk/JSTests/wasm/js-api/element.js</a></li>
<li><a href="#trunkJSTestswasmjsapiglobalerrorjs">trunk/JSTests/wasm/js-api/global-error.js</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmFormath">trunk/Source/JavaScriptCore/wasm/WasmFormat.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmModuleParsercpp">trunk/Source/JavaScriptCore/wasm/WasmModuleParser.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyModuleRecordcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/JSTests/ChangeLog        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2017-03-06  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: implement init_expr for Element
+        https://bugs.webkit.org/show_bug.cgi?id=165888
+        &lt;rdar://problem/29760199&gt;
+
+        Reviewed by Keith Miller.
+
+        * wasm/Builder_WebAssemblyBinary.js:
+        (const.emitters.Element):
+        * wasm/assert.js:
+        * wasm/js-api/element.js:
+        (assert.throws):
+        (badInstantiation.makeModule):
+        (badInstantiation.test):
+        (badInstantiation):
+        * wasm/js-api/global-error.js:
+
</ins><span class="cx"> 2017-03-06  Carlos Alberto Lopez Perez  &lt;clopez@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] [Linux] Test stress/spread-forward-call-varargs-stack-overflow.js fails
</span></span></pre></div>
<a id="trunkJSTestswasmBuilder_WebAssemblyBinaryjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/Builder_WebAssemblyBinary.js (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/Builder_WebAssemblyBinary.js        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/JSTests/wasm/Builder_WebAssemblyBinary.js        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -195,11 +195,12 @@
</span><span class="cx">         for (const {tableIndex, offset, functionIndices} of data) {
</span><span class="cx">             put(bin, &quot;varuint32&quot;, tableIndex);
</span><span class="cx"> 
</span><del>-            // FIXME allow complex init_expr here. https://bugs.webkit.org/show_bug.cgi?id=165700
-            // For now we only handle i32.const as offset.
-            put(bin, &quot;uint8&quot;, WASM.description.opcode[&quot;i32.const&quot;].value);
-            put(bin, WASM.description.opcode[&quot;i32.const&quot;].immediate[0].type, offset);
-            put(bin, &quot;uint8&quot;, WASM.description.opcode[&quot;end&quot;].value);
</del><ins>+            let initExpr;
+            if (typeof offset === &quot;number&quot;)
+                initExpr = {op: &quot;i32.const&quot;, initValue: offset};
+            else
+                initExpr = offset;
+            putInitExpr(bin, initExpr);
</ins><span class="cx"> 
</span><span class="cx">             put(bin, &quot;varuint32&quot;, functionIndices.length);
</span><span class="cx">             for (const functionIndex of functionIndices)
</span></span></pre></div>
<a id="trunkJSTestswasmassertjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/assert.js (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/assert.js        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/JSTests/wasm/assert.js        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -112,7 +112,7 @@
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> // Ignore source information at the end of the error message if the expected message didn't specify that information. Sometimes it changes, or it's tricky to get just right.
</span><del>-const _sourceRe = new RegExp(/ \(evaluating '.*'\)/);
</del><ins>+const _sourceRe = new RegExp(/( \(evaluating '.*'\))|( \(In .*\))/);
</ins><span class="cx"> 
</span><span class="cx"> const _throws = (func, type, message, ...args) =&gt; {
</span><span class="cx">     try {
</span></span></pre></div>
<a id="trunkJSTestswasmjsapielementjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/element.js (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/element.js        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/JSTests/wasm/js-api/element.js        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -53,7 +53,7 @@
</span><span class="cx">             .Table({element: &quot;anyfunc&quot;, initial: 20, maximum: 20})
</span><span class="cx">         .End()
</span><span class="cx">         .Element()
</span><del>-            .Element({offset: 19, functionIndices: [0, 1]})
</del><ins>+            .Element({offset: 19, functionIndices: [0, 0]})
</ins><span class="cx">         .End()
</span><span class="cx">         .Code()
</span><span class="cx">             .Function(&quot;foo&quot;, {params: [&quot;i32&quot;], ret: &quot;i32&quot;})
</span><span class="lines">@@ -64,7 +64,8 @@
</span><span class="cx">             .End()
</span><span class="cx">         .End();
</span><span class="cx"> 
</span><del>-    assert.throws(() =&gt; new WebAssembly.Module(builder.WebAssembly().get()), WebAssembly.CompileError, &quot;WebAssembly.Module doesn't parse at byte 35 / 49: Element section's 0th element writes to index 20 which exceeds the maximum 20 (evaluating 'new WebAssembly.Module(builder.WebAssembly().get())')&quot;);
</del><ins>+    const module = new WebAssembly.Module(builder.WebAssembly().get());
+    assert.throws(() =&gt; new WebAssembly.Instance(module), WebAssembly.LinkError, &quot;Element is trying to set an out of bounds table index&quot;);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> {
</span><span class="lines">@@ -87,7 +88,8 @@
</span><span class="cx">             .End()
</span><span class="cx">         .End();
</span><span class="cx"> 
</span><del>-    assert.throws(() =&gt; new WebAssembly.Module(builder.WebAssembly().get()), WebAssembly.CompileError, &quot;WebAssembly.Module doesn't parse at byte 35 / 48: Element section's 0th element writes to index 20 which exceeds the maximum 20 (evaluating 'new WebAssembly.Module(builder.WebAssembly().get())')&quot;);
</del><ins>+    const module = new WebAssembly.Module(builder.WebAssembly().get());
+    assert.throws(() =&gt; new WebAssembly.Instance(module), WebAssembly.LinkError, &quot;Element is trying to set an out of bounds table index&quot;);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> {
</span><span class="lines">@@ -144,3 +146,74 @@
</span><span class="cx">         badInstantiation(table, WebAssembly.LinkError, &quot;Element is trying to set an out of bounds table index (evaluating 'new WebAssembly.Instance(module, {imp: {table: actualTable}})')&quot;);
</span><span class="cx">     }
</span><span class="cx"> }
</span><ins>+
+{
+    function makeModule() {
+        const builder = new Builder()
+            .Type().End()
+            .Import()
+                .Table(&quot;imp&quot;, &quot;table&quot;, {element: &quot;anyfunc&quot;, initial: 19}) // unspecified maximum.
+                .Global().I32(&quot;imp&quot;, &quot;global&quot;, &quot;immutable&quot;).End()
+            .End()
+            .Function().End()
+            .Element()
+                .Element({offset: {op: &quot;get_global&quot;, initValue: 0}, functionIndices: [0]})
+            .End()
+            .Code()
+                .Function(&quot;foo&quot;, {params: [&quot;i32&quot;], ret: &quot;i32&quot;})
+                    .GetLocal(0)
+                    .I32Const(42)
+                    .I32Add()
+                    .Return()
+                .End()
+            .End();
+
+        const bin = builder.WebAssembly().get();
+        return new WebAssembly.Module(bin);
+    }
+
+    function test(i) {
+        const table = new WebAssembly.Table({element: &quot;anyfunc&quot;, initial: 19});
+        const global = i;
+        const module = makeModule();
+        const instance = new WebAssembly.Instance(module, {imp: {table, global}});
+        for (let j = 0; j &lt; 19; j++) {
+            if (j === i)
+                assert.eq(table.get(j)(i*2), i*2 + 42);
+            else
+                assert.throws(() =&gt; table.get(j)(i*2), TypeError, &quot;table.get(j) is not a function.&quot;);
+        }
+    }
+    for (let i = 0; i &lt; 19; i++)
+        test(i);
+
+    assert.throws(() =&gt; test(19), Error, &quot;Element is trying to set an out of bounds table index&quot;);
+}
+
+{
+    function badModule() {
+        const builder = new Builder()
+            .Type().End()
+            .Import()
+                .Table(&quot;imp&quot;, &quot;table&quot;, {element: &quot;anyfunc&quot;, initial: 19}) // unspecified maximum.
+                .Global().F32(&quot;imp&quot;, &quot;global&quot;, &quot;immutable&quot;).End()
+            .End()
+            .Function().End()
+            .Element()
+                .Element({offset: {op: &quot;get_global&quot;, initValue: 0}, functionIndices: [0]})
+            .End()
+            .Code()
+                .Function(&quot;foo&quot;, {params: [&quot;i32&quot;], ret: &quot;i32&quot;})
+                    .GetLocal(0)
+                    .I32Const(42)
+                    .I32Add()
+                    .Return()
+                .End()
+            .End();
+
+        const bin = builder.WebAssembly().get();
+        return new WebAssembly.Module(bin);
+    }
+
+    assert.throws(() =&gt; badModule(), WebAssembly.CompileError, &quot;WebAssembly.Module doesn't parse at byte 58 / 72: 0th Element init_expr must produce an i32&quot;);
+}
</ins></span></pre></div>
<a id="trunkJSTestswasmjsapiglobalerrorjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/global-error.js (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/global-error.js        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/JSTests/wasm/js-api/global-error.js        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -23,7 +23,7 @@
</span><span class="cx">     const bin = builder.WebAssembly();
</span><span class="cx">     bin.trim();
</span><span class="cx"> 
</span><del>-    assert.throws(() =&gt; new WebAssembly.Module(bin.get()), WebAssembly.CompileError, &quot;WebAssembly.Module doesn't parse at byte 26 / 59: get_global's index 0 exceeds the number of imports 0 (evaluating 'new WebAssembly.Module(bin.get())')&quot;);
</del><ins>+    assert.throws(() =&gt; new WebAssembly.Module(bin.get()), WebAssembly.CompileError, &quot;WebAssembly.Module doesn't parse at byte 26 / 59: get_global's index 0 exceeds the number of globals 0 (evaluating 'new WebAssembly.Module(bin.get())')&quot;);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -1,3 +1,31 @@
</span><ins>+2017-03-06  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: implement init_expr for Element
+        https://bugs.webkit.org/show_bug.cgi?id=165888
+        &lt;rdar://problem/29760199&gt;
+
+        Reviewed by Keith Miller.
+
+        This patch fixes a few bugs. The main change is allowing init_expr
+        for the Element's offset. To do this, I had to fix a couple of
+        other bugs:
+        
+        - I removed our invalid early module-parse-time invalidation
+        of out of bound Element sections. This is not in the spec because
+        it can't be validated in the general case when the offset is a
+        get_global.
+        
+        - Our get_global validation inside our init_expr parsing code was simply wrong.
+        It thought that the index operand to get_global went into the pool of imports,
+        but it does not. It indexes into the pool of globals. I changed the code to
+        refer to the global pool instead.
+
+        * wasm/WasmFormat.h:
+        (JSC::Wasm::Element::Element):
+        * wasm/WasmModuleParser.cpp:
+        * wasm/js/WebAssemblyModuleRecord.cpp:
+        (JSC::WebAssemblyModuleRecord::evaluate):
+
</ins><span class="cx"> 2017-03-06  Yusuke Suzuki  &lt;utatane.tea@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] Allow indexed module namespace object fields
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmFormath"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmFormat.h (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmFormat.h        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/Source/JavaScriptCore/wasm/WasmFormat.h        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -189,7 +189,11 @@
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> struct Element {
</span><del>-    uint32_t offset;
</del><ins>+    Element(I32InitExpr offset)
+        : offset(offset)
+    { }
+
+    I32InitExpr offset;
</ins><span class="cx">     Vector&lt;uint32_t&gt; functionIndices;
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmModuleParsercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmModuleParser.cpp (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmModuleParser.cpp        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/Source/JavaScriptCore/wasm/WasmModuleParser.cpp        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -431,7 +431,7 @@
</span><span class="cx">     WASM_PARSER_FAIL_IF(!m_result.module-&gt;elements.tryReserveCapacity(elementCount), &quot;can't allocate memory for &quot;, elementCount, &quot; Elements&quot;);
</span><span class="cx">     for (unsigned elementNum = 0; elementNum &lt; elementCount; ++elementNum) {
</span><span class="cx">         uint32_t tableIndex;
</span><del>-        uint64_t offset;
</del><ins>+        uint64_t initExprBits;
</ins><span class="cx">         uint8_t initOpcode;
</span><span class="cx">         uint32_t indexCount;
</span><span class="cx"> 
</span><span class="lines">@@ -438,29 +438,16 @@
</span><span class="cx">         WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), &quot;can't get &quot;, elementNum, &quot;th Element table index&quot;);
</span><span class="cx">         WASM_PARSER_FAIL_IF(tableIndex, &quot;Element section can only have one Table for now&quot;);
</span><span class="cx">         Type initExprType;
</span><del>-        WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, offset, initExprType));
-        WASM_PARSER_FAIL_IF(initOpcode != OpType::I32Const, &quot;Element section doesn't support non-i32 init_expr opcode for now, got &quot;, initOpcode);
</del><ins>+        WASM_FAIL_IF_HELPER_FAILS(parseInitExpr(initOpcode, initExprBits, initExprType));
+        WASM_PARSER_FAIL_IF(initExprType != I32, &quot;Element init_expr must produce an i32&quot;);
</ins><span class="cx">         WASM_PARSER_FAIL_IF(!parseVarUInt32(indexCount), &quot;can't get &quot;, elementNum, &quot;th index count for Element section&quot;);
</span><span class="cx">         WASM_PARSER_FAIL_IF(indexCount == std::numeric_limits&lt;uint32_t&gt;::max(), &quot;Element section's &quot;, elementNum, &quot;th index count is too big &quot;, indexCount);
</span><span class="cx"> 
</span><span class="cx">         ASSERT(!!m_result.module-&gt;tableInformation);
</span><del>-        if (std::optional&lt;uint32_t&gt; maximum = m_result.module-&gt;tableInformation.maximum()) {
-            // FIXME: should indexCount being zero be a validation error?
-            // https://bugs.webkit.org/show_bug.cgi?id=165826
-            if (indexCount) {
-                // FIXME: right now, provably out of bounds writes are validation errors.
-                // Should they be though?
-                // https://bugs.webkit.org/show_bug.cgi?id=165827
-                uint64_t lastWrittenIndex = static_cast&lt;uint64_t&gt;(indexCount) + static_cast&lt;uint64_t&gt;(offset) - 1;
-                WASM_PARSER_FAIL_IF(lastWrittenIndex &gt;= static_cast&lt;uint64_t&gt;(*maximum), &quot;Element section's &quot;, elementNum, &quot;th element writes to index &quot;, lastWrittenIndex, &quot; which exceeds the maximum &quot;, *maximum);
-            }
-        }
</del><span class="cx"> 
</span><del>-        Element element;
</del><ins>+        Element element(makeI32InitExpr(initOpcode, initExprBits));
</ins><span class="cx">         WASM_PARSER_FAIL_IF(!element.functionIndices.tryReserveCapacity(indexCount), &quot;can't allocate memory for &quot;, indexCount, &quot; Element indices&quot;);
</span><span class="cx"> 
</span><del>-        element.offset = offset;
-
</del><span class="cx">         for (unsigned index = 0; index &lt; indexCount; ++index) {
</span><span class="cx">             uint32_t functionIndex;
</span><span class="cx">             WASM_PARSER_FAIL_IF(!parseVarUInt32(functionIndex), &quot;can't get Element section's &quot;, elementNum, &quot;th element's &quot;, index, &quot;th index&quot;);
</span><span class="lines">@@ -536,12 +523,11 @@
</span><span class="cx">     case GetGlobal: {
</span><span class="cx">         uint32_t index;
</span><span class="cx">         WASM_PARSER_FAIL_IF(!parseVarUInt32(index), &quot;can't get get_global's index&quot;);
</span><del>-        WASM_PARSER_FAIL_IF(index &gt;= m_result.module-&gt;imports.size(), &quot;get_global's index &quot;, index, &quot; exceeds the number of imports &quot;, m_result.module-&gt;imports.size());
-        const Import&amp; import = m_result.module-&gt;imports[index];
-        WASM_PARSER_FAIL_IF(m_result.module-&gt;imports[index].kind != ExternalKind::Global, &quot;get_global's import kind is &quot;, m_result.module-&gt;imports[index].kind, &quot; should be global&quot;);
-        WASM_PARSER_FAIL_IF(import.kindIndex &gt;= m_result.module-&gt;firstInternalGlobal, &quot;get_global import kind index &quot;, import.kindIndex, &quot; exceeds the first internal global &quot;, m_result.module-&gt;firstInternalGlobal);
</del><span class="cx"> 
</span><del>-        ASSERT(m_result.module-&gt;globals[import.kindIndex].mutability == Global::Immutable);
</del><ins>+        WASM_PARSER_FAIL_IF(index &gt;= m_result.module-&gt;globals.size(), &quot;get_global's index &quot;, index, &quot; exceeds the number of globals &quot;, m_result.module-&gt;globals.size());
+        WASM_PARSER_FAIL_IF(index &gt;= m_result.module-&gt;firstInternalGlobal, &quot;get_global import kind index &quot;, index, &quot; exceeds the first internal global &quot;, m_result.module-&gt;firstInternalGlobal);
+
+        ASSERT(m_result.module-&gt;globals[index].mutability == Global::Immutable);
</ins><span class="cx">         resultType = m_result.module-&gt;globals[index].type;
</span><span class="cx">         bitsOrImportNumber = index;
</span><span class="cx">         break;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleRecordcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp (213464 => 213465)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp        2017-03-06 21:40:31 UTC (rev 213464)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp        2017-03-06 21:44:20 UTC (rev 213465)
</span><span class="lines">@@ -224,7 +224,13 @@
</span><span class="cx">             if (!element.functionIndices.size())
</span><span class="cx">                 continue;
</span><span class="cx"> 
</span><del>-            uint32_t tableIndex = element.offset;
</del><ins>+            uint32_t tableIndex;
+
+            if (element.offset.isGlobalImport())
+                tableIndex = static_cast&lt;uint32_t&gt;(m_instance-&gt;loadI32Global(element.offset.globalImportIndex()));
+            else
+                tableIndex = element.offset.constValue();
+
</ins><span class="cx">             uint64_t lastWrittenIndex = static_cast&lt;uint64_t&gt;(tableIndex) + static_cast&lt;uint64_t&gt;(element.functionIndices.size()) - 1;
</span><span class="cx">             if (lastWrittenIndex &gt;= table-&gt;size())
</span><span class="cx">                 return throwException(state, scope, createJSWebAssemblyLinkError(state, vm, ASCIILiteral(&quot;Element is trying to set an out of bounds table index&quot;)));
</span></span></pre>
</div>
</div>

</body>
</html>