authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-01-01 14:50:19+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-01-02 18:36:18+01:00
log3e32a189566b191f1cfe01e911fed941e3e38603
treed202a3004f688b984151b7deb68fccbed36e78d0
parent3ca3fe94f454688f05415cf0a39954a0379b4c84
signaturelock-open Commit is signed but in an unrecognized format.

test/link: add test case for function table

Adds 3 linker tests to verify the indirect function table functionality for importing, exporting and its regular definitions.

3 files changed, 75 insertions(+), 0 deletions(-)

test/link.zig+5
...@@ -58,6 +58,11 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {...@@ -58,6 +58,11 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
58 .requires_stage2 = true,58 .requires_stage2 = true,
59 });59 });
6060
61 cases.addBuildFile("test/link/wasm/function-table/build.zig", .{
62 .build_modes = true,
63 .requires_stage2 = true,
64 });
65
61 cases.addBuildFile("test/link/wasm/infer-features/build.zig", .{66 cases.addBuildFile("test/link/wasm/infer-features/build.zig", .{
62 .requires_stage2 = true,67 .requires_stage2 = true,
63 });68 });
test/link/wasm/function-table/build.zig created+63
...@@ -0,0 +1,63 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const test_step = b.step("test", "Test");
8 test_step.dependOn(b.getInstallStep());
9
10 const import_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
11 import_table.setBuildMode(mode);
12 import_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
13 import_table.use_llvm = false;
14 import_table.use_lld = false;
15 import_table.import_table = true;
16
17 const export_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
18 export_table.setBuildMode(mode);
19 export_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
20 export_table.use_llvm = false;
21 export_table.use_lld = false;
22 export_table.export_table = true;
23
24 const regular_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
25 regular_table.setBuildMode(mode);
26 regular_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
27 regular_table.use_llvm = false;
28 regular_table.use_lld = false;
29
30 const check_import = import_table.checkObject(.wasm);
31 const check_export = export_table.checkObject(.wasm);
32 const check_regular = regular_table.checkObject(.wasm);
33
34 check_import.checkStart("Section import");
35 check_import.checkNext("entries 1");
36 check_import.checkNext("module env");
37 check_import.checkNext("name __indirect_function_table");
38 check_import.checkNext("kind table");
39 check_import.checkNext("type funcref");
40 check_import.checkNext("min 1"); // 1 function pointer
41 check_import.checkNotPresent("max"); // when importing, we do not provide a max
42 check_import.checkNotPresent("Section table"); // we're importing it
43
44 check_export.checkStart("Section export");
45 check_export.checkNext("entries 2");
46 check_export.checkNext("name __indirect_function_table"); // as per linker specification
47 check_export.checkNext("kind table");
48
49 check_regular.checkStart("Section table");
50 check_regular.checkNext("entries 1");
51 check_regular.checkNext("type funcref");
52 check_regular.checkNext("min 2"); // index starts at 1 & 1 function pointer = 2.
53 check_regular.checkNext("max 2");
54 check_regular.checkStart("Section element");
55 check_regular.checkNext("entries 1");
56 check_regular.checkNext("table index 0");
57 check_regular.checkNext("i32.const 1"); // we want to start function indexes at 1
58 check_regular.checkNext("indexes 1"); // 1 function pointer
59
60 test_step.dependOn(&check_import.step);
61 test_step.dependOn(&check_export.step);
62 test_step.dependOn(&check_regular.step);
63}
test/link/wasm/function-table/lib.zig created+7
...@@ -0,0 +1,7 @@
1var func: *const fn () void = &bar;
2
3export fn foo() void {
4 func();
5}
6
7fn bar() void {}