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

Integer overflow when building shared library with version number on Windows using build.zig #8928

Closed
niklas-ourmachinery opened this issue May 28, 2021 · 0 comments · Fixed by #9016
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows zig build system
Milestone

Comments

@niklas-ourmachinery
Copy link

niklas-ourmachinery commented May 28, 2021

Building a shared library for Windows using this code:

const lib = b.addSharedLibrary("tm_" ++ name, null, b.version(1,0,0));
lib.setOutputDir("zigbuild");
lib.setBuildMode(mode);
lib.setTarget(foundation.target);
lib.linkLibC();
lib.install();

Gives an Integer overflow:

thread 15944 panic: integer overflow
C:\Users\nikla\zig\lib\zig\std\heap\arena_allocator.zig:56:36: 0x7ff6afdcd4ae in std.heap.arena_allocator.ArenaAllocator::std.heap.arena_allocator.ArenaAllocator.createNode (build.obj)
        const len = big_enough_len + big_enough_len / 2;
                                   ^
C:\Users\nikla\zig\lib\zig\std\heap\arena_allocator.zig:90:51: 0x7ff6afdc0eb4 in std.heap.arena_allocator.ArenaAllocator::std.heap.arena_allocator.ArenaAllocator.alloc (build.obj)
                    cur_node = try self.createNode(cur_buf.len, n + ptr_align);
                                                  ^
C:\Users\nikla\zig\lib\zig\std\mem\Allocator.zig:290:40: 0x7ff6afda2156 in std.mem.Allocator.allocAdvancedWithRetAddr (build.obj)
    const byte_slice = try self.allocFn(self, byte_count, a, len_align, return_address);
                                       ^
C:\Users\nikla\zig\lib\zig\std\mem\Allocator.zig:182:41: 0x7ff6afd9c289 in std.mem.Allocator.alloc (build.obj)
    return self.allocAdvancedWithRetAddr(T, null, n, .exact, @returnAddress());
                                        ^
C:\Users\nikla\zig\lib\zig\std\mem\Allocator.zig:458:40: 0x7ff6afda1d78 in std.mem.Allocator.dupe (build.obj)
    const new_buf = try allocator.alloc(T, m.len);
                                       ^
C:\Users\nikla\zig\lib\zig\std\build.zig:336:35: 0x7ff6afdb545c in std.build.Builder::std.build.Builder.dupe (build.obj)
        return self.allocator.dupe(u8, bytes) catch unreachable;
                                  ^
C:\Users\nikla\zig\lib\zig\std\build.zig:3028:33: 0x7ff6afe1cc8e in std.build.InstalledFile::std.build.InstalledFile.dupe (build.obj)
            .path = builder.dupe(self.path),
                                ^
C:\Users\nikla\zig\lib\zig\std\build.zig:1006:46: 0x7ff6afe043e0 in std.build.Builder::std.build.Builder.pushInstalledFile (build.obj)
        self.installed_files.append(file.dupe(self)) catch unreachable;
                                             ^
C:\Users\nikla\zig\lib\zig\std\build.zig:2703:42: 0x7ff6afdf9b9b in std.build.InstallArtifactStep::std.build.InstallArtifactStep.create (build.obj)
                builder.pushInstalledFile(.Lib, artifact.major_only_filename);
                                         ^
C:\Users\nikla\zig\lib\zig\std\build.zig:936:42: 0x7ff6afdea4de in std.build.Builder::std.build.Builder.addInstallArtifact (build.obj)
        return InstallArtifactStep.create(self, artifact);
                                         ^
C:\Users\nikla\zig\lib\zig\std\build.zig:932:64: 0x7ff6afddcdaf in std.build.Builder::std.build.Builder.installArtifact (build.obj)
        self.getInstallStep().dependOn(&self.addInstallArtifact(artifact).step);
                                                               ^
C:\Users\nikla\zig\lib\zig\std\build.zig:1602:37: 0x7ff6afdd3d4e in std.build.LibExeObjStep::std.build.LibExeObjStep.install (build.obj)
        self.builder.installArtifact(self);
                                    ^
C:\work\themachinery\build.zig:142:16: 0x7ff6afdd3e25 in .@build.buildPlugin (build.obj)
    lib.install();
               ^
C:\work\themachinery\build.zig:146:16: 0x7ff6afdc5ac2 in .@build.buildPlugins (build.obj)
    buildPlugin(b, "animation", foundation);
               ^
C:\work\themachinery\build.zig:177:17: 0x7ff6afdb7e13 in .@build.build (build.obj)
    buildPlugins(b, foundation);
                ^
C:\Users\nikla\zig\lib\zig\std\special\build_runner.zig:157:28: 0x7ff6afdadcd6 in runBuild (build.obj)
        .Void => root.build(builder),
                           ^
C:\Users\nikla\zig\lib\zig\std\special\build_runner.zig:139:17: 0x7ff6afd94820 in main (build.obj)
    try runBuild(builder);
                ^
C:\Users\nikla\zig\lib\zig\std\start.zig:420:37: 0x7ff6afd92720 in std.start.callMain (build.obj)
            const result = root.main() catch |err| {
                                    ^
C:\Users\nikla\zig\lib\zig\std\start.zig:249:65: 0x7ff6afd92077 in std.start.WinStartup (build.obj)
    std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());

From Specs_guy on the Zig Discord Server:

This looks like a bug, major_only_filename is not set on windows but is still used by the install step. The workaround (I think) is >to pass .unversioned as the version. Could you please open an issue on GH for this?

For the issue, here's where it's not initialized:

zig/lib/std/build.zig

Lines 1596 to 1598 in c0aa929

} else if (target.os.tag == .windows) {
self.out_lib_filename = self.builder.fmt("{s}.lib", .{self.name});
} else {

And here's where it's used:

zig/lib/std/build.zig

Lines 2749 to 2752 in c0aa929

if (self.artifact.version != null) {
builder.pushInstalledFile(.Lib, artifact.major_only_filename);
builder.pushInstalledFile(.Lib, artifact.name_only_filename);
}

Confirmed that using .unversioned works.

@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. zig build system os-windows labels May 28, 2021
LemonBoy added a commit to LemonBoy/zig that referenced this issue Jun 6, 2021
andrewrk pushed a commit that referenced this issue Jun 6, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.8.1 Jun 6, 2021
andrewrk pushed a commit that referenced this issue Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows zig build system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants