Debian Codegraph @

crates/wasm-metadata/src/producers.rs

raw ◀ ▶
1 Debian patch touch this file
1 use anyhow::Result;
2 use indexmap::{IndexMap, map::Entry};
3 use wasm_encoder::Encode;
4 use wasmparser::{BinaryReader, KnownCustom, Parser, ProducersSectionReader};
5
6 use crate::{AddMetadata, rewrite_wasm};
7 /// A representation of a WebAssembly producers section.
8 ///
9 /// Spec: <https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md>
10 #[derive(Debug)]
11 #[cfg_attr(feature = "serde", derive(serde_derive::Serialize))]
12 pub struct Producers(
13 #[cfg_attr(
14 feature = "serde",
15 serde(serialize_with = "indexmap::map::serde_seq::serialize")
16 )]
17 IndexMap<String, IndexMap<String, String>>,
18 );
19
20 impl Default for Producers {
21 fn default() -> Self {
22 Self::empty()
23 }
24 }
25
26 impl Producers {
27 /// Creates an empty producers section
28 pub fn empty() -> Self {
29 Producers(IndexMap::new())
30 }
31
32 /// Indicates if section is empty
33 pub fn is_empty(&self) -> bool {
34 self.0.is_empty()
35 }
36
37 /// Read the producers section from a Wasm binary. Supports both core
38 /// Modules and Components. In the component case, only returns the
39 /// producers section in the outer component, ignoring all interior
40 /// components and modules.
41 pub fn from_wasm(bytes: &[u8]) -> Result<Option<Self>> {
42 let mut depth = 0;
43 for payload in Parser::new(0).parse_all(bytes) {
44 let payload = payload?;
45 use wasmparser::Payload::*;
46 match payload {
47 ModuleSection { .. } | ComponentSection { .. } => depth += 1,
48 End { .. } => depth -= 1,
49 CustomSection(c) if depth == 0 => {
50 if let KnownCustom::Producers(_) = c.as_known() {
51 let producers = Self::from_bytes(c.data(), c.data_offset())?;
52 return Ok(Some(producers));
53 }
54 }
55 _ => {}
56 }
57 }
58 Ok(None)
59 }
60 /// Read the producers section from a Wasm binary.
61 pub fn from_bytes(bytes: &[u8], offset: usize) -> Result<Self> {
62 let reader = BinaryReader::new(bytes, offset);
63 let section = ProducersSectionReader::new(reader)?;
64 let mut fields = IndexMap::new();
65 for field in section.into_iter() {
66 let field = field?;
67 let mut values = IndexMap::new();
68 for value in field.values.into_iter() {
69 let value = value?;
70 values.insert(value.name.to_owned(), value.version.to_owned());
71 }
72 fields.insert(field.name.to_owned(), values);
73 }
74 Ok(Producers(fields))
75 }
76 /// Add a name & version value to a field.
77 ///
78 /// The spec says expected field names are "language", "processed-by", and "sdk".
79 /// The version value should be left blank for languages.
80 pub fn add(&mut self, field: &str, name: &str, version: &str) {
81 match self.0.entry(field.to_string()) {
82 Entry::Occupied(e) => {
83 e.into_mut().insert(name.to_owned(), version.to_owned());
84 }
85 Entry::Vacant(e) => {
86 let mut m = IndexMap::new();
87 m.insert(name.to_owned(), version.to_owned());
88 e.insert(m);
89 }
90 }
91 }
92
93 /// Add all values found in another `Producers` section. Values in `other` take
94 /// precedence.
95 pub fn merge(&mut self, other: &Self) {
96 for (field, values) in other.iter() {
97 for (name, version) in values.iter() {
98 self.add(field, name, version);
99 }
100 }
101 }
102
103 /// Get the contents of a field
104 pub fn get<'a>(&'a self, field: &str) -> Option<ProducersField<'a>> {
105 self.0.get(&field.to_owned()).map(ProducersField)
106 }
107
108 /// Iterate through all fields
109 pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a String, ProducersField<'a>)> + 'a {
110 self.0
111 .iter()
112 .map(|(name, field)| (name, ProducersField(field)))
113 }
114
115 /// Construct the fields specified by [`AddMetadata`]
116 pub(crate) fn from_meta(add: &AddMetadata) -> Self {
117 let mut s = Self::empty();
118 for (lang, version) in add.language.iter() {
119 s.add("language", &lang, &version);
120 }
121 for (name, version) in add.processed_by.iter() {
122 s.add("processed-by", &name, &version);
123 }
124 for (name, version) in add.sdk.iter() {
125 s.add("sdk", &name, &version);
126 }
127 s
128 }
129
130 /// Serialize into [`wasm_encoder::ProducersSection`].
131 pub(crate) fn section(&self) -> wasm_encoder::ProducersSection {
132 let mut section = wasm_encoder::ProducersSection::new();
133 for (fieldname, fieldvalues) in self.0.iter() {
134 let mut field = wasm_encoder::ProducersField::new();
135 for (name, version) in fieldvalues {
136 field.value(&name, &version);
137 }
138 section.field(&fieldname, &field);
139 }
140 section
141 }
142
143 /// Serialize into the raw bytes of a wasm custom section.
144 pub fn raw_custom_section(&self) -> Vec<u8> {
145 let mut ret = Vec::new();
146 self.section().encode(&mut ret);
147 ret
148 }
149
150 /// Merge into an existing wasm module. Rewrites the module with this producers section
151 /// merged into its existing one, or adds this producers section if none is present.
152 pub fn add_to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
153 rewrite_wasm(&Default::default(), self, input)
154 }
155 }
156
157 /// Contents of a producers field
158 #[derive(Debug)]
159 pub struct ProducersField<'a>(&'a IndexMap<String, String>);
160
161 impl<'a> ProducersField<'a> {
162 /// Get the version associated with a name in the field
163 pub fn get(&self, name: &str) -> Option<&'a String> {
164 self.0.get(&name.to_owned())
165 }
166 /// Iterate through all name-version pairs in the field
167 pub fn iter(&self) -> impl Iterator<Item = (&'a String, &'a String)> + 'a {
168 self.0.iter()
169 }
170 }
171
172 #[cfg(test)]
•173 #[cfg(feature = "oci")]
174 mod test {
175 use super::*;
176 use crate::{Metadata, Payload};
177 use wasm_encoder::Module;
178
179 #[test]
180 fn producers_empty_module() {
181 let module = Module::new().finish();
182 let mut producers = Producers::empty();
183 producers.add("language", "bar", "");
184 producers.add("processed-by", "baz", "1.0");
185
186 let module = producers.add_to_wasm(&module).unwrap();
187
188 match Payload::from_binary(&module).unwrap() {
189 Payload::Module(Metadata {
190 name, producers, ..
191 }) => {
192 assert_eq!(name, None);
193 let producers = producers.expect("some producers");
194 assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
195 assert_eq!(
196 producers.get("processed-by").unwrap().get("baz").unwrap(),
197 "1.0"
198 );
199 }
200 _ => panic!("metadata should be module"),
201 }
202 }
203
204 #[test]
205 fn producers_add_another_field() {
206 let module = Module::new().finish();
207 let mut producers = Producers::empty();
208 producers.add("language", "bar", "");
209 producers.add("processed-by", "baz", "1.0");
210 let module = producers.add_to_wasm(&module).unwrap();
211
212 let mut producers = Producers::empty();
213 producers.add("language", "waaat", "");
214 let module = producers.add_to_wasm(&module).unwrap();
215
216 match Payload::from_binary(&module).unwrap() {
217 Payload::Module(Metadata {
218 name, producers, ..
219 }) => {
220 assert_eq!(name, None);
221 let producers = producers.expect("some producers");
222 assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
223 assert_eq!(producers.get("language").unwrap().get("waaat").unwrap(), "");
224 assert_eq!(
225 producers.get("processed-by").unwrap().get("baz").unwrap(),
226 "1.0"
227 );
228 }
229 _ => panic!("metadata should be module"),
230 }
231 }
232
233 #[test]
234 fn producers_overwrite_field() {
235 let module = Module::new().finish();
236 let mut producers = Producers::empty();
237 producers.add("processed-by", "baz", "1.0");
238 let module = producers.add_to_wasm(&module).unwrap();
239
240 let mut producers = Producers::empty();
241 producers.add("processed-by", "baz", "420");
242 let module = producers.add_to_wasm(&module).unwrap();
243
244 match Payload::from_binary(&module).unwrap() {
245 Payload::Module(Metadata { producers, .. }) => {
246 let producers = producers.expect("some producers");
247 assert_eq!(
248 producers.get("processed-by").unwrap().get("baz").unwrap(),
249 "420"
250 );
251 }
252 _ => panic!("metadata should be module"),
253 }
254 }
255 }