Skip to content

Commit

Permalink
Parse DataCount section
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmstill committed Sep 4, 2022
1 parent 609675c commit e1c5bdf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub const Module = struct {
local_types: ArrayList(LocalType),
br_table_indices: ArrayList(u32),
function_index_start: ?usize,
dataCount: ?u32 = null,

pub fn init(alloc: mem.Allocator, module: []const u8) Module {
return Module{
Expand Down Expand Up @@ -160,6 +161,7 @@ pub const Module = struct {
.Element => try self.decodeElementSection(),
.Code => try self.decodeCodeSection(),
.Data => try self.decodeDataSection(),
.DataCount => try self.decodeDataCountSection(size),
}

const section_end = rd.context.pos;
Expand Down Expand Up @@ -652,6 +654,17 @@ pub const Module = struct {
}
}

fn decodeDataCountSection(self: *Module, size: u32) !void {
const rd = self.buf.reader();

if (size == 0) return;

self.dataCount = leb.readULEB128(u32, rd) catch |err| switch (err) {
error.EndOfStream => return error.UnexpectedEndOfInput,
else => return err,
};
}

fn decodeCodeSection(self: *Module) !void {
const rd = self.buf.reader();

Expand Down Expand Up @@ -731,6 +744,11 @@ pub const Module = struct {
error.EndOfStream => return error.UnexpectedEndOfInput,
else => return err,
};

if (self.dataCount) |dataCount| {
if (count != dataCount) return error.DataCountSectionDataSectionCountMismatch;
}

self.datas.count = count;

var i: usize = 0;
Expand Down Expand Up @@ -940,6 +958,7 @@ const SectionType = enum(u8) {
Element = 0x09,
Code = 0x0a,
Data = 0x0b,
DataCount = 0x0c,
};

const testing = std.testing;
Expand Down
7 changes: 7 additions & 0 deletions test/src/testrunner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ pub fn main() anyerror!void {
}
}

if (mem.eql(u8, trap, "data count and data section have inconsistent lengths")) {
switch (err) {
error.DataCountSectionDataSectionCountMismatch => continue,
else => {},
}
}

if (mem.eql(u8, trap, "integer representation too long")) {
switch (err) {
error.InvalidValue => continue,
Expand Down

0 comments on commit e1c5bdf

Please sign in to comment.