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

Fix bug with parsing prefixed opcodes #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 10 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();

const lib = b.addStaticLibrary("wasmparser", "src/lib.zig");
lib.setBuildMode(mode);
lib.install();

var main_tests = b.addTest("tests/test.zig");
main_tests.addPackagePath("wasmparser", "src/lib.zig");
main_tests.setBuildMode(mode);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
b.addModule(.{
.name = "wasmparser",
.source_file = .{ .path = "src/lib.zig" },
});

var tests = b.addTest(.{
.root_source_file = .{ .path = "tests/test.zig" },
});
tests.addModule("wasmparser", b.modules.get("wasmparser").?);
b.step("test", "Run library tests").dependOn(&tests.step);
}
14 changes: 9 additions & 5 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn Parser(comptime ReaderType: type) type {
var instructions = std.ArrayList(wasm.Instruction).init(gpa);
defer instructions.deinit();

while (readEnum(std.wasm.Opcode, code_reader)) |opcode| {
while (readOpcode(code_reader)) |opcode| {
const instr = try buildInstruction(opcode, gpa, code_reader);
try instructions.append(instr);
} else |err| switch (err) {
Expand All @@ -271,7 +271,7 @@ fn Parser(comptime ReaderType: type) type {
else => |e| return e,
}

code.body = instructions.toOwnedSlice();
code.body = try instructions.toOwnedSlice();
}
try assertEnd(code_reader);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ fn Parser(comptime ReaderType: type) type {
error.EndOfStream => {},
else => |e| return e,
}
module.custom = custom_sections.toOwnedSlice();
module.custom = try custom_sections.toOwnedSlice();
return module;
}
};
Expand Down Expand Up @@ -341,6 +341,10 @@ fn readEnum(comptime T: type, reader: anytype) !T {
}
}

fn readOpcode(reader: anytype) !std.wasm.Opcode {
return @intToEnum(std.wasm.Opcode, try reader.readByte());
}

fn readLimits(reader: anytype) !wasm.Limits {
const flags = try readLeb(u1, reader);
const min = try readLeb(u32, reader);
Expand All @@ -358,7 +362,7 @@ fn readInit(reader: anytype) !wasm.InitExpression {
else => unreachable,
};

if ((try readEnum(std.wasm.Opcode, reader)) != .end) return error.MissingEndForExpression;
if ((try readOpcode(reader)) != .end) return error.MissingEndForExpression;
return init;
}

Expand Down Expand Up @@ -445,7 +449,7 @@ fn buildInstruction(opcode: std.wasm.Opcode, gpa: Allocator, reader: anytype) !w
}
break :blk .{ .multi_valtype = .{ .data = list.ptr, .len = len } };
},
wasm.need_secondary => @as(wasm.Instruction.InstrValue, blk: {
.prefixed => @as(wasm.Instruction.InstrValue, blk: {
const secondary = try readEnum(wasm.SecondaryOpcode, reader);
instr.secondary = secondary;
switch (secondary) {
Expand Down
2 changes: 0 additions & 2 deletions src/wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ fn MergedEnum(comptime T: type, comptime fields: []const TypeInfo.EnumField) typ
std.mem.copy(TypeInfo.EnumField, new_fields[old_fields.len..], fields);

return @Type(.{ .Enum = .{
.layout = .Auto,
.tag_type = u8,
.fields = &new_fields,
.decls = &.{},
Expand Down Expand Up @@ -301,6 +300,5 @@ pub const SecondaryOpcode = enum(u8) {
_,
};

pub const need_secondary = @intToEnum(wasm.Opcode, 0xFC);
pub const table_get = @intToEnum(wasm.Opcode, 0x25);
pub const table_set = @intToEnum(wasm.Opcode, 0x26);
20 changes: 4 additions & 16 deletions tests/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ fn testForOptions(content: []const u8, options: Options) !void {
try testing.expectEqual(len, module.functions.data.len);
}

for (options.export_names) |name, i| {
for (options.export_names, 0..) |name, i| {
try testing.expectEqualStrings(name, module.exports.data[i].name);
}

for (options.locals) |local, i| {
for (options.locals, 0..) |local, i| {
for (module.code.data[i].locals) |code_local| {
if (local.local == code_local.valtype) {
try testing.expectEqual(local.count, code_local.count);
Expand Down Expand Up @@ -67,19 +67,7 @@ test "tests/call_indirect.wasm" {
});
}

test "tests/wasi_hello_world.wasm except code" {
const file = @embedFile("wasi_hello_world.wasm");
var stream = std.io.fixedBufferStream(file);
var options : wasmparser.parser.Options = .{};
// skip code section
options.skip_section[@enumToInt(std.wasm.Section.code)] = true;
var result = try wasmparser.parser.parseWithOptions(ally, stream.reader(), options);
defer result.deinit(ally);
}

test "tests/wasi_hello_world.wasm" {
// This is not working, seems something not supported by parser in code section
const file = @embedFile("wasi_hello_world.wasm");
try testForOptions(file, .{
});
}
try testForOptions(file, .{});
}