Skip to content

Code Metadata - #1840

Merged
sbc100 merged 8 commits into
WebAssembly:mainfrom
yuri91:metadata
Feb 25, 2022
Merged

sbc100 merged 8 commits into
WebAssembly:mainfrom
yuri91:metadata

Conversation

@yuri91

@yuri91 yuri91 commented Feb 24, 2022

Copy link
Copy Markdown
Contributor

This PR adds support for Code Metadata.

See https://github.com/WebAssembly/tool-conventions/blob/main/CodeMetadata.md for the specification.

In particular this pr implements the following:

  • Parsing code metadata sections in BinaryReader, providing appropriate callbacks that a BinaryReaderDelegate can implement:
    • BinaryReaderObjdump: show the sections in a human-readable form
    • BinaryReaderIr: add code metadata in the IR as expressions
  • Parsing code metadata annotations in text format, adding them in the IR like the BinaryReaderIR does
  • Writing the code metadata present in the IR in the proper sections when converting IR to binary
  • Support in wasm-decompiler for showing code metadata as comments in the pseudo-code

All the features have corresponding tests.

Support for code metadata is gated through the --enable-code-metadata feature. For reading/writing in the text format, --enable-annotations is also required.

Missing features:

Support for function-level code metadata (offset 0)
Extensive validation in validator.cc (like making sure that all metadata instances are at the same code offset of an instruction)

Added parsing from binary and general callbacks for generic code
metadata sections.
Basic logging and info in objdump.
The annotations are shown in the line before the referenced instruction,
as a C-style line comment.
@yuri91 yuri91 mentioned this pull request Feb 24, 2022

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking good!

Comment thread src/binary-reader-ir.cc Resolved
Comment thread src/binary-reader-ir.cc

Result BinaryReaderIR::BeginFunctionBody(Index index, Offset size) {
current_func_ = module_->funcs[index];
current_func_->loc = GetLocation();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems odd that this was not needed before.. do you know why this is needed now?

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 am not sure why it wasn't used before, and maybe I am using it as a hack?

Anyway, It is now needed by CodeMetadataExprQueue: the offsets are always absolute but code metadata need relative offsets from the beginning of the function. So it seemed natural to fill this existing field here and use it to get relative offsets later

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, actually you are adding this new loc field to struct Func. I guess that makes sense for this use case and we already track this for other things such as expressions, and I guess the location a function is different from the location of its first expression due to param declaration?

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.

Sorry, I made this change some time ago and I didn't remember that I added the field!

But yes, it felt natural, since like you say it is there for similar things.

And also yes, the difference with the location of the first expression is that there are the locals in between.

Comment thread src/binary-reader-ir.cc Resolved
Comment thread src/binary-reader-logging.cc Resolved
Comment thread src/binary-reader.cc
CHECK_RESULT(ReadCount(&function_index, "function index"));
ERROR_UNLESS(function_index >= num_func_imports_,
"function import can't have metadata (got %" PRIindex ")",
function_index);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this is a custom section I guess we should not completely fail if its malformed?

(Update: I see tests below where it looks like there are just warnings... I guess that downgrade from error to warning ins somewhere in the custom section handling code?)

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.

Yes, the PrintError method automatically converts to warning if it is invoked when parsing a custom section.

Comment thread src/binary-reader.cc
} else if (section_name == WABT_BINARY_SECTION_LINKING) {
CHECK_RESULT(ReadLinkingSection(section_size));
} else if (options_.features.code_metadata_enabled() &&
section_name.find(WABT_BINARY_SECTION_CODE_METADATA) == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there no startswith for C++ strings?

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 seems that they added it in c++20. But wabt uses c++17 currently.

Comment thread src/common.h Resolved
@sbc100
sbc100 enabled auto-merge (squash) February 25, 2022 15:25
@sbc100
sbc100 merged commit 1f59b65 into WebAssembly:main Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants