This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly][Object] Remove requirement that objects must have code sections
ClosedPublic

Authored by dschuff on Jun 17 2022, 1:15 PM.

Details

Summary

When parsing name and linking sections, we currently require that the object
must have a code section (it seems that this was intended to verify section
ordering). However it can be useful for binaries to have their code sections
stripped out (e.g. if we just want the debug info). In that case we need
the rest of the known sections (so e.g. we know how many functions there
are, to verify the name section) but not the actual code.

I've removed the restriction completely. I think this is OK because the
section-parsing code already checks function and global indices in many
places for validity and will return appropriate errors if the relevant sections
are missing. Also we can't just replace the requirement of seeing a code section
with a requirement that we see a function or global section, because a binary
may just not have any functions or globals.
But there's only an problem if the name or linking section tries to name a
nonexistent function.

Part of a fix for https://github.com/emscripten-core/emscripten/issues/13084

Diff Detail

Event Timeline

dschuff created this revision.Jun 17 2022, 1:15 PM
Herald added a reviewer: MaskRay. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Herald added subscribers: pmatos, asb, wingo and 5 others. · View Herald Transcript
dschuff requested review of this revision.Jun 17 2022, 1:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2022, 1:15 PM
sbc100 added inline comments.Jun 17 2022, 1:29 PM
llvm/lib/Object/WasmObjectFile.cpp
526

What happens if the linking metdata comes before the code section now? Is that OK?

llvm/test/tools/llvm-objdump/wasm/no-codesec.test
27

This is no longer a valid wasm file though right? Since it refers to a function that doesn't have a body. I guess that is maybe ok as long as we produce valid wasm files by default?

dschuff added inline comments.Jun 17 2022, 1:51 PM
llvm/lib/Object/WasmObjectFile.cpp
526

The code section is not actually needed to check the linking metadata; only the Function, Global, and Import sections IIUC. So yes it's OK.

llvm/test/tools/llvm-objdump/wasm/no-codesec.test
27

Correct; I think a function without a body would not validate. The use case for this is to have a wasm file with just debug info in it (e.g. to take a debug-info-containing wasm file and ship a stripped version, and archive the debug info separately).

27

and I think that's ok for low-level tools like objcopy and objdump.

aheejin accepted this revision.Jun 17 2022, 6:07 PM

(Also we can't just always require the presence of e.g. the
function or global sections, because a binary may just have any functions.
There's only an problem if the name or linking section tries to name a
nonexistent function).

Code LGTM, but not sure what this sentence means.. Can you provide a little more context?

This revision is now accepted and ready to land.Jun 17 2022, 6:07 PM
dschuff added a comment.EditedJun 21 2022, 5:29 PM

(Also we can't just always require the presence of e.g. the
function or global sections, because a binary may just have any functions.
There's only an problem if the name or linking section tries to name a
nonexistent function).

Code LGTM, but not sure what this sentence means.. Can you provide a little more context?

Oops, that should say "a binary may just *not* have any functions." (or globals). In other words, we could try replacing the requirement that we've seen the code section with a requirement that we've seen the Function section; but there still could be perfectly valid wasm binaries with no function section (or maybe we want to be able to parse binaries that only have custom sections and no functions, and we don't care about validation).

dschuff edited the summary of this revision. (Show Details)Jun 23 2022, 1:48 PM
dschuff edited the summary of this revision. (Show Details)
This revision was landed with ongoing or failed builds.Jun 23 2022, 1:56 PM
This revision was automatically updated to reflect the committed changes.