Skip to content

WASM - elem/data segment validation - #2174

Merged
chakrabot merged 4 commits into
chakra-core:release/1.4from
Cellule:wasm/elem
Dec 12, 2016
Merged

chakrabot merged 4 commits into
chakra-core:release/1.4from
Cellule:wasm/elem

Conversation

@Cellule

@Cellule Cellule commented Dec 8, 2016 •

Copy link
Copy Markdown
Contributor

Check if a table is present when reading an element section
Validate the init_expr for element and data segments

Disallow internal global in init_expr
The issue WebAssembly/spec#367 made me realize we were doing the same thing the spec interpreter was which was not according to the design documents

Check the type of init_expr for globals


This change is Reviewable

@Cellule

Cellule commented Dec 9, 2016

Copy link
Copy Markdown
Contributor Author

Hold off a moment to review this, I want to include another fix related to this

@Cellule

Cellule commented Dec 9, 2016

Copy link
Copy Markdown
Contributor Author

Should be okay to review now

}

void
WebAssemblyModule::ValidateInitExporForOffset(const Wasm::WasmNode& initExpr) const

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spelling (ValidateInitExprForOffset)

throw Wasm::WasmCompilationException(_u("Invalid init_expr for element offset"));
}
// Should have been checked at compile time, but let's just make sure one more time
ValidateInitExporForOffset(initExpr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then maybe we should put under #if DBG and assert?

{
uint32 index = LEB128(length); // Table id
if (index != 0)
if (index != 0 || !(m_module->HasTable() || m_module->HasTableImport()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spec seemed unclear to me about this. it looked like it might be legal to have no table or table import and have element section, as long as it does nothing (e.g. any segments have length 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this bug using the spec invalid tests.
I think it does make sense to be a compile error since the index will be able to refer to different table, we should tell ahead of time that the elements are not using a valid table

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, but the design says that every module has one default memory/table (not up to one). there is no mention what the state is if there is no explicit table section. so if this is how the spec interpreter behaves then i'm fine deferring to it. just one of those corners that will needed to be worked out in the formal spec

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to allow to set data or elements in a memory/table that hasn't been defined.
In our current implementation, we would throw a linking error when treating the data/element segments.
This is only moving the error to compile time effectively changing from WebAssembly.RuntimeError to WebAssembly.CompileError which is more in line with the spec interpreter.
In the end, it really doesn't matter, but early error should be favorable if we have to read the data anyway

if (initExpr.op != Wasm::wbI32Const && initExpr.op != Wasm::wbGetGlobal)
try
{
ValidateInitExportForOffset(initExpr);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep the runtime check even on release builds, so I wrapped it in a try-catch with an assert to help us find errors.
I am not totally convinced by the end result though, what do you think ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit strange, but seems ok to me.

@Cellule

Cellule commented Dec 12, 2016

Copy link
Copy Markdown
Contributor Author

ping

if (initExpr.op != Wasm::wbI32Const && initExpr.op != Wasm::wbGetGlobal)
try
{
ValidateInitExportForOffset(initExpr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit strange, but seems ok to me.

Validate the init_expr for element and data segments
Update memory.wast from the spec repo
@chakrabot
chakrabot merged commit 5b6798b into chakra-core:release/1.4 Dec 12, 2016
chakrabot pushed a commit that referenced this pull request Dec 12, 2016
Merge pull request #2174 from Cellule:wasm/elem

Check if a table is present when reading an element section
Validate the init_expr for element and data segments

Disallow internal global in init_expr
The issue WebAssembly/spec#367 made me realize we were doing the same thing the spec interpreter was which was not according to the design documents

Check the type of init_expr for globals
@Cellule
Cellule deleted the wasm/elem branch December 12, 2016 23:07
chakrabot pushed a commit that referenced this pull request Dec 12, 2016
Merge pull request #2174 from Cellule:wasm/elem

Check if a table is present when reading an element section
Validate the init_expr for element and data segments

Disallow internal global in init_expr
The issue WebAssembly/spec#367 made me realize we were doing the same thing the spec interpreter was which was not according to the design documents

Check the type of init_expr for globals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants