Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DWARF support in text-to-binary #1632

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ hashbrown = { version = "0.14.3", default-features = false, features = ['ahash']
ahash = { version = "0.8.11", default-features = false }
termcolor = "1.2.0"
indoc = "2.0.5"
gimli = "0.29.0"

wasm-compose = { version = "0.211.1", path = "crates/wasm-compose" }
wasm-encoder = { version = "0.211.1", path = "crates/wasm-encoder" }
Expand All @@ -108,7 +109,7 @@ log = { workspace = true }
clap = { workspace = true }
clap_complete = { workspace = true, optional = true }
tempfile = "3.2.0"
wat = { workspace = true }
wat = { workspace = true, features = ['dwarf'] }
termcolor = { workspace = true }

# Dependencies of `validate`
Expand Down Expand Up @@ -156,7 +157,7 @@ wit-smith = { workspace = true, features = ["clap"], optional = true }

# Dependencies of `addr2line`
addr2line = { version = "0.22.0", optional = true }
gimli = { version = "0.29.0", optional = true }
gimli = { workspace = true, optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
is_executable = { version = "1.0.1", optional = true }
Expand Down Expand Up @@ -200,7 +201,7 @@ default = [
]

# Each subcommand is gated behind a feature and lists the dependencies it needs
validate = ['dep:wasmparser', 'rayon']
validate = ['dep:wasmparser', 'rayon', 'dep:addr2line', 'dep:gimli']
print = []
parse = []
smith = ['wasm-smith', 'arbitrary', 'dep:serde', 'dep:serde_derive', 'dep:serde_json']
Expand Down
10 changes: 10 additions & 0 deletions crates/wast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Customizable Rust parsers for the WebAssembly Text formats WAT and WAST
"""
rust-version.workspace = true

[package.metadata.docs.rs]
all-features = true

[lints]
workspace = true

Expand All @@ -22,12 +25,14 @@ unicode-width = "0.1.9"
memchr = "2.4.1"
wasm-encoder = { workspace = true }
bumpalo = "3.14.0"
gimli = { workspace = true, optional = true }

[dev-dependencies]
anyhow = { workspace = true }
libtest-mimic = { workspace = true }
wasmparser = { path = "../wasmparser" }
wat = { path = "../wat" }
rand = { workspace = true }

[features]
default = ['wasm-module']
Expand All @@ -40,6 +45,11 @@ default = ['wasm-module']
# This feature is turned on by default.
wasm-module = []

# Off-by-default feature to support emitting DWARF debugging information in
# parsed binaries pointing back to source locations in the original `*.wat`
# source.
dwarf = ["dep:gimli"]

[[test]]
name = "parse-fail"
harness = false
2 changes: 1 addition & 1 deletion crates/wast/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Types and support for parsing the component model text format.

mod alias;
mod binary;
pub(crate) mod binary;
mod component;
mod custom;
mod expand;
Expand Down
17 changes: 10 additions & 7 deletions crates/wast/src/component/binary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::component::*;
use crate::core;
use crate::core::EncodeOptions;
use crate::token::{Id, Index, NameAnnotation, Span};
use wasm_encoder::{
CanonicalFunctionSection, ComponentAliasSection, ComponentDefinedTypeEncoder,
Expand All @@ -9,10 +10,10 @@ use wasm_encoder::{
NestedComponentSection, RawSection, SectionId,
};

pub fn encode(component: &Component<'_>) -> Vec<u8> {
pub fn encode(component: &Component<'_>, options: &EncodeOptions) -> Vec<u8> {
match &component.kind {
ComponentKind::Text(fields) => {
encode_fields(&component.id, &component.name, fields).finish()
encode_fields(&component.id, &component.name, fields, options).finish()
}
ComponentKind::Binary(bytes) => bytes.iter().flat_map(|b| b.iter().copied()).collect(),
}
Expand All @@ -23,15 +24,16 @@ fn encode_fields(
component_id: &Option<Id<'_>>,
component_name: &Option<NameAnnotation<'_>>,
fields: &[ComponentField<'_>],
options: &EncodeOptions,
) -> wasm_encoder::Component {
let mut e = Encoder::default();

for field in fields {
match field {
ComponentField::CoreModule(m) => e.encode_core_module(m),
ComponentField::CoreModule(m) => e.encode_core_module(m, options),
ComponentField::CoreInstance(i) => e.encode_core_instance(i),
ComponentField::CoreType(t) => e.encode_core_type(t),
ComponentField::Component(c) => e.encode_component(c),
ComponentField::Component(c) => e.encode_component(c, options),
ComponentField::Instance(i) => e.encode_instance(i),
ComponentField::Alias(a) => e.encode_alias(a),
ComponentField::Type(t) => e.encode_type(t),
Expand Down Expand Up @@ -191,7 +193,7 @@ impl<'a> Encoder<'a> {
})
}

fn encode_core_module(&mut self, module: &CoreModule<'a>) {
fn encode_core_module(&mut self, module: &CoreModule<'a>, options: &EncodeOptions) {
// Flush any in-progress section before encoding the module
self.flush(None);

Expand All @@ -202,7 +204,7 @@ impl<'a> Encoder<'a> {
CoreModuleKind::Import { .. } => unreachable!("should be expanded already"),
CoreModuleKind::Inline { fields } => {
// TODO: replace this with a wasm-encoder based encoding (should return `wasm_encoder::Module`)
let data = crate::core::binary::encode(&module.id, &module.name, fields);
let data = crate::core::binary::encode(&module.id, &module.name, fields, options);
self.component.section(&RawSection {
id: ComponentSectionId::CoreModule.into(),
data: &data,
Expand Down Expand Up @@ -238,7 +240,7 @@ impl<'a> Encoder<'a> {
self.flush(Some(self.core_types.id()));
}

fn encode_component(&mut self, component: &NestedComponent<'a>) {
fn encode_component(&mut self, component: &NestedComponent<'a>, options: &EncodeOptions) {
self.component_names
.push(get_name(&component.id, &component.name));
// Flush any in-progress section before encoding the component
Expand All @@ -252,6 +254,7 @@ impl<'a> Encoder<'a> {
&component.id,
&component.name,
fields,
options,
)));
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/wast/src/component/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl<'a> Component<'a> {
/// This function can return an error for name resolution errors and other
/// expansion-related errors.
pub fn encode(&mut self) -> std::result::Result<Vec<u8>, crate::Error> {
self.resolve()?;
Ok(crate::component::binary::encode(self))
crate::core::EncodeOptions::default().encode_component(self)
}

pub(crate) fn validate(&self, parser: Parser<'_>) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions crates/wast/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod table;
mod tag;
mod types;
mod wast;
pub use self::binary::{EncodeOptions, GenerateDwarf};
pub use self::custom::*;
pub use self::export::*;
pub use self::expr::*;
Expand Down
Loading