aboutsummaryrefslogtreecommitdiff
path: root/test/link/wasm/shared-memory/build.zig
blob: 1cb26deb5471401377711eb3996732a0680c9fe8 (plain)
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
const std = @import("std");

pub fn build(b: *std.Build) void {
    const test_step = b.step("test", "Test");
    b.default_step = test_step;

    add(b, test_step, .Debug);
    add(b, test_step, .ReleaseFast);
}

fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode) void {
    const exe = b.addExecutable(.{
        .name = "lib",
        .root_module = b.createModule(.{
            .root_source_file = b.path("lib.zig"),
            .target = b.resolveTargetQuery(.{
                .cpu_arch = .wasm32,
                .cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
                .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory }),
                .os_tag = .freestanding,
            }),
            .optimize = optimize_mode,
            .strip = false,
            .single_threaded = false,
        }),
    });
    exe.entry = .disabled;
    exe.use_lld = false;
    exe.import_memory = true;
    exe.export_memory = true;
    exe.shared_memory = true;
    exe.max_memory = 67108864;
    exe.root_module.export_symbol_names = &.{"foo"};
    // Don't pull in ubsan, since we're just expecting a very minimal executable.
    exe.bundle_ubsan_rt = false;

    const check_exe = exe.checkObject();

    check_exe.checkInHeaders();
    check_exe.checkExact("Section import");
    check_exe.checkExact("entries 1");
    check_exe.checkExact("module env");
    check_exe.checkExact("name memory"); // ensure we are importing memory

    check_exe.checkInHeaders();
    check_exe.checkExact("Section export");
    check_exe.checkExact("entries 2");
    check_exe.checkExact("name foo");
    check_exe.checkExact("name memory"); // ensure we also export memory again

    // This section *must* be emit as the start function is set to the index
    // of __wasm_init_memory
    // release modes will have the TLS segment optimized out in our test-case.
    // This means we won't have __wasm_init_memory in such case, and therefore
    // should also not have a section "start"
    if (optimize_mode == .Debug) {
        check_exe.checkInHeaders();
        check_exe.checkExact("Section start");
    }

    // This section is only and *must* be emit when shared-memory is enabled
    // release modes will have the TLS segment optimized out in our test-case.
    if (optimize_mode == .Debug) {
        check_exe.checkInHeaders();
        check_exe.checkExact("Section data_count");
        check_exe.checkExact("count 1");
    }

    check_exe.checkInHeaders();
    check_exe.checkExact("Section custom");
    check_exe.checkExact("name name");
    check_exe.checkExact("type function");
    if (optimize_mode == .Debug) {
        check_exe.checkExact("name __wasm_init_memory");
        check_exe.checkExact("name __wasm_init_tls");
    }
    check_exe.checkExact("type global");

    // In debug mode the symbol __tls_base is resolved to an undefined symbol
    // from the object file, hence its placement differs than in release modes
    // where the entire tls segment is optimized away, and tls_base will have
    // its original position.
    if (optimize_mode == .Debug) {
        check_exe.checkExact("name __tls_base");
        check_exe.checkExact("name __tls_size");
        check_exe.checkExact("name __tls_align");

        check_exe.checkExact("type data_segment");
        check_exe.checkExact("names 1");
        check_exe.checkExact("index 0");
        check_exe.checkExact("name .tdata");
    } else {
        check_exe.checkNotPresent("name __tls_base");
        check_exe.checkNotPresent("name __tls_size");
        check_exe.checkNotPresent("name __tls_align");
    }

    test_step.dependOn(&check_exe.step);
}