Skip to content

Commit

Permalink
Update to 0.12.0 (#42)
Browse files Browse the repository at this point in the history
* Updated build.zig file
* Major move from var -> const
* Updated mem.copy and reading and writing int slices
* Fixing abs and nan
* Fixing wasm test executables
* Fixing std.mem.rem calls
* Fix wasi.zig for std.os -> std.posix changes
* Adding build.zig.zon file
* Changing to addModule to make bytebox module public
* Fixed memory leak related to not freeing datas
* Updating ci to 0.12.0
* Fixing stable_array init
* Fixed misinterpretation of i32 as i64 related to mem64 changes

---------

Co-authored-by: Shem Sedrick <ssedrick@gmail.com>
  • Loading branch information
Southporter and Shem Sedrick authored May 24, 2024
1 parent 1e4fe3f commit a3e9dba
Show file tree
Hide file tree
Showing 19 changed files with 1,044 additions and 939 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on:
on:
push:
branches:
- main
Expand All @@ -20,7 +20,7 @@ jobs:

- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.12.0

# The current default version of clang on macos runners is 14, which doesn't support the wasm64-freestanding target.
- name: Install LLVM and Clang
Expand All @@ -40,7 +40,7 @@ jobs:
run: python3 -m pip install -r requirements.txt

# Ideally we would use this but it seems to be broken
# - name: Setup wasm-tools
# - name: Setup wasm-tools
# uses: jcbhmr/setup-wasm-tools@v2
# with:
# wasm-tools-version: 1.207
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Bytebox is a WebAssembly VM.

# Getting started

## Requirements
Bytebox currently builds with [Zig 0.11.x](https://ziglang.org/download) to avoid churn on zig master.
### Requirements
Bytebox currently builds with [Zig 0.12.x](https://ziglang.org/download) to avoid churn on zig master.

To run the tests:
* `wasm-tools` is required to run the wasm testsuite. You can install it via the rust toolchain `cargo install wasm-tools` or directly from the [release page](https://github.com/bytecodealliance/wasm-tools/releases).
Expand Down
16 changes: 10 additions & 6 deletions bench/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ const bytebox = @import("bytebox");
const Val = bytebox.Val;
const Timer = std.time.Timer;

pub const std_options: std.Options = .{
.log_level = .info,
};

const Benchmark = struct {
name: []const u8,
filename: []const u8,
param: i32,
};

fn elapsedMilliseconds(timer: *std.time.Timer) f64 {
var ns_elapsed: f64 = @as(f64, @floatFromInt(timer.read()));
const ns_elapsed: f64 = @as(f64, @floatFromInt(timer.read()));
const ms_elapsed = ns_elapsed / 1000000.0;
return ms_elapsed;
}

fn run(allocator: std.mem.Allocator, benchmark: Benchmark) !void {
var cwd = std.fs.cwd();
var wasm_data: []u8 = try cwd.readFileAlloc(allocator, benchmark.filename, 1024 * 64); // Our wasm programs aren't very large
const wasm_data: []u8 = try cwd.readFileAlloc(allocator, benchmark.filename, 1024 * 64); // Our wasm programs aren't very large

var timer = try Timer.start();

Expand All @@ -40,19 +44,19 @@ fn run(allocator: std.mem.Allocator, benchmark: Benchmark) !void {

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator: std.mem.Allocator = gpa.allocator();
const allocator: std.mem.Allocator = gpa.allocator();

const benchmarks = [_]Benchmark{ .{
.name = "add-one",
.filename = "zig-out/lib/add-one.wasm",
.filename = "zig-out/bin/add-one.wasm",
.param = 123456789,
}, .{
.name = "fibonacci",
.filename = "zig-out/lib/fibonacci.wasm",
.filename = "zig-out/bin/fibonacci.wasm",
.param = 20,
}, .{
.name = "mandelbrot",
.filename = "zig-out/lib/mandelbrot.wasm",
.filename = "zig-out/bin/mandelbrot.wasm",
.param = 20,
} };

Expand Down
4 changes: 2 additions & 2 deletions bench/samples/fibonacci.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export fn run(n: i32) i32 {
if (n < 2) {
return 1;
} else {
var a = run(n - 1);
var b = run(n - 2);
const a = run(n - 1);
const b = run(n - 2);
return a + b;
}
}
48 changes: 24 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const std = @import("std");

const Build = std.Build;
const CrossTarget = std.zig.CrossTarget;
const Builder = std.build.Builder;
const CompileStep = std.build.CompileStep;
const InstallFileStep = std.build.InstallFileStep;
const CompileStep = std.Build.Step.Compile;

const ExeOpts = struct {
exe_name: []const u8,
Expand All @@ -19,15 +17,15 @@ pub fn build(b: *Build) void {
const should_emit_asm = b.option(bool, "asm", "Emit asm for the bytebox binaries") orelse false;
const no_clang = b.option(bool, "noclang", "Pass this if clang isn't in the PATH") orelse false;

var bench_add_one_step: *CompileStep = buildWasmLib(b, "bench/samples/add-one.zig");
var bench_fibonacci_step: *CompileStep = buildWasmLib(b, "bench/samples/fibonacci.zig");
var bench_mandelbrot_step: *CompileStep = buildWasmLib(b, "bench/samples/mandelbrot.zig");

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var bench_add_one_step: *CompileStep = buildWasmExe(b, "bench/samples/add-one.zig");
var bench_fibonacci_step: *CompileStep = buildWasmExe(b, "bench/samples/fibonacci.zig");
var bench_mandelbrot_step: *CompileStep = buildWasmExe(b, "bench/samples/mandelbrot.zig");

const bytebox_module: *Build.Module = b.addModule("bytebox", .{
.source_file = Build.LazyPath.relative("src/core.zig"),
.root_source_file = b.path("src/core.zig"),
});

_ = buildExeWithRunStep(b, target, optimize, bytebox_module, .{
Expand All @@ -53,16 +51,16 @@ pub fn build(b: *Build) void {

const lib_bytebox = b.addStaticLibrary(.{
.name = "bytebox",
.root_source_file = .{ .path = "src/cffi.zig" },
.root_source_file = b.path("src/cffi.zig"),
.target = target,
.optimize = optimize,
});
lib_bytebox.installHeader("src/bytebox.h", "bytebox.h");
lib_bytebox.installHeader(b.path("src/bytebox.h"), "bytebox.h");
b.installArtifact(lib_bytebox);

// Unit tests
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/tests.zig" },
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
Expand All @@ -71,7 +69,7 @@ pub fn build(b: *Build) void {
unit_test_step.dependOn(&run_unit_tests.step);

// wasm tests
var wasm_testsuite_step = buildExeWithRunStep(b, target, optimize, bytebox_module, .{
const wasm_testsuite_step = buildExeWithRunStep(b, target, optimize, bytebox_module, .{
.exe_name = "test-wasm",
.root_src = "test/wasm/main.zig",
.step_name = "test-wasm",
Expand Down Expand Up @@ -100,7 +98,7 @@ pub fn build(b: *Build) void {
compile_memtest.addArg("-Wl,--export-dynamic");
compile_memtest.addArg("-o");
compile_memtest.addArg("test/mem64/memtest.wasm");
compile_memtest.addFileArg(.{ .path = "test/mem64/memtest.c" });
compile_memtest.addFileArg(b.path("test/mem64/memtest.c"));
compile_memtest.has_side_effects = true;

b.getInstallStep().dependOn(&compile_memtest.step);
Expand All @@ -123,15 +121,15 @@ pub fn build(b: *Build) void {
}
}

fn buildExeWithRunStep(b: *Build, target: CrossTarget, optimize: std.builtin.Mode, bytebox_module: *Build.Module, opts: ExeOpts) *Build.Step {
fn buildExeWithRunStep(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Mode, bytebox_module: *Build.Module, opts: ExeOpts) *Build.Step {
const exe = b.addExecutable(.{
.name = opts.exe_name,
.root_source_file = Build.LazyPath.relative(opts.root_src),
.root_source_file = b.path(opts.root_src),
.target = target,
.optimize = optimize,
});

exe.addModule("bytebox", bytebox_module);
exe.root_module.addImport("bytebox", bytebox_module);

// exe.emit_asm = if (opts.should_emit_asm) .emit else .default;
b.installArtifact(exe);
Expand All @@ -154,21 +152,23 @@ fn buildExeWithRunStep(b: *Build, target: CrossTarget, optimize: std.builtin.Mod
return step;
}

fn buildWasmLib(b: *Build, filepath: []const u8) *CompileStep {
fn buildWasmExe(b: *Build, filepath: []const u8) *CompileStep {
var filename: []const u8 = std.fs.path.basename(filepath);
var filename_no_extension: []const u8 = filename[0 .. filename.len - 4];
const filename_no_extension: []const u8 = filename[0 .. filename.len - 4];

const lib = b.addSharedLibrary(.{
var exe = b.addExecutable(.{
.name = filename_no_extension,
.root_source_file = Build.LazyPath.relative(filepath),
.target = CrossTarget{
.root_source_file = b.path(filepath),
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
},
}),
.optimize = .ReleaseSmall,
});
exe.rdynamic = true;
exe.entry = .disabled;

b.installArtifact(lib);
b.installArtifact(exe);

return lib;
return exe;
}
20 changes: 17 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
.{
.name = "bytebox",
.version = "0.0.1",
.dependencies = .{},
.name = "bytebox",
.version = "0.0.1",
.minimum_zig_version = "0.12.0",
.paths = .{
"src",
"test/mem64",
"test/wasi/run.py",
"test/wasi/bytebox_adapter.py",
"test/wasm/main.zig",
"bench",
"run",
"build.zig",
"build.zig.zon",
"LICENSE",
"README.md",
},
.dependencies = .{},
}
30 changes: 15 additions & 15 deletions run/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ fn parseCmdOpts(args: [][]const u8, env_buffer: *std.ArrayList([]const u8), dir_

var arg_index: usize = 1;
while (arg_index < args.len) {
var arg = args[arg_index];
const arg = args[arg_index];

if (arg_index == 1 and !isArgvOption(arg)) {
opts.filename = arg;
opts.wasm_argv = args[1..2];
} else if (arg_index == 2 and !isArgvOption(arg)) {
var wasm_argv_begin: usize = arg_index - 1; // include wasm filename
const wasm_argv_begin: usize = arg_index - 1; // include wasm filename
var wasm_argv_end: usize = arg_index;
while (wasm_argv_end + 1 < args.len and !isArgvOption(args[wasm_argv_end + 1])) {
wasm_argv_end += 1;
Expand Down Expand Up @@ -136,7 +136,7 @@ const version_string = "bytebox v0.0.1";
fn printHelp(args: [][]const u8) void {
const usage_string: []const u8 =
\\Usage: {s} <FILE> [WASM_ARGS]... [OPTION]...
\\
\\
\\ Options:
\\
\\ -h, --help
Expand All @@ -148,7 +148,7 @@ fn printHelp(args: [][]const u8) void {
\\ --dump
\\ Prints the given module definition's imports and exports. Imports are qualified
\\ with the import module name.
\\
\\
\\ -i, --invoke <FUNCTION> [ARGS]...
\\ Call an exported, named function with arguments. The arguments are automatically
\\ translated from string inputs to the function's native types. If the conversion
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator: std.mem.Allocator = gpa.allocator();

var args = try std.process.argsAlloc(allocator);
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var env_buffer = std.ArrayList([]const u8).init(allocator);
Expand Down Expand Up @@ -220,7 +220,7 @@ pub fn main() !void {
std.debug.assert(opts.filename != null);

var cwd = std.fs.cwd();
var wasm_data: []u8 = cwd.readFileAlloc(allocator, opts.filename.?, 1024 * 1024 * 128) catch |e| {
const wasm_data: []u8 = cwd.readFileAlloc(allocator, opts.filename.?, 1024 * 1024 * 128) catch |e| {
std.log.err("Failed to read file '{s}' into memory: {}", .{ opts.filename.?, e });
return RunErrors.IoError;
};
Expand Down Expand Up @@ -256,7 +256,7 @@ pub fn main() !void {
}, allocator);
defer wasi.deinitImports(&imports_wasi);

var instantiate_opts = bytebox.ModuleInstantiateOpts{
const instantiate_opts = bytebox.ModuleInstantiateOpts{
.imports = &[_]bytebox.ModuleImportPackage{imports_wasi},
.log = log,
};
Expand Down Expand Up @@ -302,28 +302,28 @@ pub fn main() !void {
const arg: []const u8 = invoke_args[i];
switch (valtype) {
.I32 => {
var parsed: i32 = std.fmt.parseInt(i32, arg, 0) catch |e| {
const parsed: i32 = std.fmt.parseInt(i32, arg, 0) catch |e| {
std.log.err("Failed to parse arg at index {} ('{s}') as an i32: {}", .{ i, arg, e });
return RunErrors.BadFunctionParam;
};
params.items[i] = Val{ .I32 = parsed };
},
.I64 => {
var parsed: i64 = std.fmt.parseInt(i64, arg, 0) catch |e| {
const parsed: i64 = std.fmt.parseInt(i64, arg, 0) catch |e| {
std.log.err("Failed to parse arg at index {} ('{s}') as an i64: {}", .{ i, arg, e });
return RunErrors.BadFunctionParam;
};
params.items[i] = Val{ .I64 = parsed };
},
.F32 => {
var parsed: f32 = std.fmt.parseFloat(f32, arg) catch |e| {
const parsed: f32 = std.fmt.parseFloat(f32, arg) catch |e| {
std.log.err("Failed to parse arg at index {} ('{s}') as a f32: {}", .{ i, arg, e });
return RunErrors.BadFunctionParam;
};
params.items[i] = Val{ .F32 = parsed };
},
.F64 => {
var parsed: f64 = std.fmt.parseFloat(f64, arg) catch |e| {
const parsed: f64 = std.fmt.parseFloat(f64, arg) catch |e| {
std.log.err("Failed to parse arg at index {} ('{s}') as a f64: {}", .{ i, arg, e });
return RunErrors.BadFunctionParam;
};
Expand Down Expand Up @@ -357,7 +357,7 @@ pub fn main() !void {
{
var strbuf = std.ArrayList(u8).init(allocator);
defer strbuf.deinit();
var writer = strbuf.writer();
const writer = strbuf.writer();

if (returns.items.len > 0) {
const return_types = func_export.returns;
Expand All @@ -382,13 +382,13 @@ pub fn main() !void {
}

fn writeSignature(strbuf: *std.ArrayList(u8), info: *const bytebox.FunctionExport) !void {
var writer = strbuf.writer();
const writer = strbuf.writer();
if (info.params.len == 0) {
try std.fmt.format(writer, " params: none\n", .{});
} else {
try std.fmt.format(writer, " params:\n", .{});
for (info.params) |valtype| {
var name: []const u8 = valtypeToString(valtype);
const name: []const u8 = valtypeToString(valtype);
try std.fmt.format(writer, " {s}\n", .{name});
}
}
Expand All @@ -398,7 +398,7 @@ fn writeSignature(strbuf: *std.ArrayList(u8), info: *const bytebox.FunctionExpor
} else {
try std.fmt.format(writer, " returns:\n", .{});
for (info.returns) |valtype| {
var name: []const u8 = valtypeToString(valtype);
const name: []const u8 = valtypeToString(valtype);
try std.fmt.format(writer, " {s}\n", .{name});
}
}
Expand Down
Loading

0 comments on commit a3e9dba

Please sign in to comment.