1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Table elements within a wasm module.

use crate::emit::{Emit, EmitContext, Section};
use crate::ir::Value;
use crate::parse::IndicesToIds;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
use crate::{FunctionId, InitExpr, Module, Result, TableKind, ValType};
use failure::{bail, ResultExt};

/// A passive element segment identifier
pub type ElementId = Id<Element>;

/// A passive segment which contains a list of functions
#[derive(Debug)]
pub struct Element {
    id: Id<Element>,

    /// The function members of this passive elements segment.
    pub members: Vec<FunctionId>,
}

impl Element {
    /// Get this segment's id
    pub fn id(&self) -> Id<Element> {
        self.id
    }
}

impl Tombstone for Element {
    fn on_delete(&mut self) {
        self.members = Vec::new();
    }
}

/// All element segments of a wasm module, used to initialize `anyfunc` tables,
/// used as function pointers.
#[derive(Debug, Default)]
pub struct ModuleElements {
    arena: TombstoneArena<Element>,
}

impl ModuleElements {
    /// Get an element associated with an ID
    pub fn get(&self, id: ElementId) -> &Element {
        &self.arena[id]
    }

    /// Get an element associated with an ID
    pub fn get_mut(&mut self, id: ElementId) -> &mut Element {
        &mut self.arena[id]
    }

    /// Delete an elements entry from this module.
    ///
    /// It is up to you to ensure that all references to this deleted element
    /// are removed.
    pub fn delete(&mut self, id: ElementId) {
        self.arena.delete(id);
    }

    /// Get a shared reference to this module's passive elements.
    pub fn iter(&self) -> impl Iterator<Item = &Element> {
        self.arena.iter().map(|(_, f)| f)
    }
}

impl Module {
    /// Parses a raw was section into a fully-formed `ModuleElements` instance.
    pub(crate) fn parse_elements(
        &mut self,
        section: wasmparser::ElementSectionReader,
        ids: &mut IndicesToIds,
    ) -> Result<()> {
        log::debug!("parse element section");
        for (i, segment) in section.into_iter().enumerate() {
            let segment = segment?;

            match segment.kind {
                wasmparser::ElementKind::Passive(_) => {
                    bail!("passive element segments not supported yet");
                }
                wasmparser::ElementKind::Active {
                    table_index,
                    init_expr,
                } => {
                    let table = ids.get_table(table_index)?;
                    let table = match &mut self.tables.get_mut(table).kind {
                        TableKind::Function(t) => t,
                        TableKind::Anyref(_) => {
                            bail!("active anyref segments not supported yet");
                        }
                    };

                    let offset = InitExpr::eval(&init_expr, ids)
                        .with_context(|_e| format!("in segment {}", i))?;
                    let functions = segment.items.get_items_reader()?.into_iter().map(|func| {
                        let func = func?;
                        ids.get_func(func)
                    });

                    match offset {
                        InitExpr::Value(Value::I32(n)) => {
                            let offset = n as usize;
                            for (i, id) in functions.enumerate() {
                                while i + offset + 1 > table.elements.len() {
                                    table.elements.push(None);
                                }
                                table.elements[i + offset] = Some(id?);
                            }
                        }
                        InitExpr::Global(global) if self.globals.get(global).ty == ValType::I32 => {
                            let list = functions.collect::<Result<_>>()?;
                            table.relative_elements.push((global, list));
                        }
                        _ => bail!("non-i32 constant in segment {}", i),
                    }
                }
            }
        }
        Ok(())
    }
}

impl Emit for ModuleElements {
    fn emit(&self, cx: &mut EmitContext) {
        log::debug!("emit element section");
        // Sort table ids for a deterministic emission for now, eventually we
        // may want some sort of sorting heuristic here.
        let mut active = cx
            .module
            .tables
            .iter()
            .filter_map(|t| match &t.kind {
                TableKind::Function(list) => Some((t.id(), list)),
                TableKind::Anyref(_) => None,
            })
            .collect::<Vec<_>>();
        active.sort_by_key(|pair| pair.0);

        // Append segments as we find them for all table initializers. We can
        // skip initializers for unused tables, and otherwise we just want to
        // create an initializer for each contiguous chunk of function indices.
        let mut chunks = Vec::new();
        for (table_id, table) in active.iter() {
            let mut offset = 0;
            let mut len = 0;
            for (i, item) in table.elements.iter().enumerate() {
                if item.is_some() {
                    if len == 0 {
                        offset = i;
                    }
                    len += 1;
                } else {
                    if len > 0 {
                        chunks.push((table_id, table, offset, len));
                    }
                    len = 0;
                }
            }

            if len > 0 {
                chunks.push((table_id, table, offset, len));
            }
        }

        let passive = self.iter().count();
        let relative = active
            .iter()
            .map(|(_, table)| table.relative_elements.len())
            .sum::<usize>();
        let total = passive + relative + chunks.len();

        if total == 0 {
            return;
        }
        let mut cx = cx.start_section(Section::Element);
        cx.encoder.usize(total);

        // Emits the leading data for describing a table's index
        //
        // Note that much of this is in accordance with the
        // currently-in-progress bulk-memory proposal for WebAssembly.
        let active_table_header = |cx: &mut EmitContext, index: u32| {
            if index == 0 {
                cx.encoder.byte(0x00);
            } else {
                cx.encoder.byte(0x02);
                cx.encoder.u32(index);
            }
        };

        // Emit all contiguous chunks of functions pointers that are located at
        // constant offsets
        for (&id, table, offset, len) in chunks {
            let table_index = cx.indices.get_table_index(id);
            active_table_header(&mut cx, table_index);
            InitExpr::Value(Value::I32(offset as i32)).emit(&mut cx);
            cx.encoder.usize(len);
            for item in table.elements[offset..][..len].iter() {
                let index = cx.indices.get_func_index(item.unwrap());
                cx.encoder.u32(index);
            }
        }

        // Emit all chunks of function pointers that are located at relative
        // global offsets.
        for (id, table) in active.iter() {
            let table_index = cx.indices.get_table_index(*id);
            for (global, list) in table.relative_elements.iter() {
                active_table_header(&mut cx, table_index);
                InitExpr::Global(*global).emit(&mut cx);
                cx.encoder.usize(list.len());
                for func in list {
                    let index = cx.indices.get_func_index(*func);
                    cx.encoder.u32(index);
                }
            }
        }

        // After all the active segments are added add passive segments next. We
        // may want to sort this more intelligently in the future. Otherwise
        // emitting a segment here is in general much simpler than above as we
        // know there are no holes.
        for (id, _) in self.arena.iter() {
            cx.indices.push_element(id);
            // TODO: sync this with the upstream spec
            panic!(
                "encoding a passive element segment requires either \
                 `ref.null` or `ref.func` encodings, which aren't \
                 currently implemented"
            );
        }
    }
}