ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.96k stars 2.55k forks source link

error.Unexpected from fs.createFileAbsolute #16378

Open n4skx opened 1 year ago

n4skx commented 1 year ago

Zig Version

0.11.0-dev.3944+2e424e019

Steps to Reproduce and Observed Behavior

Compile flags:

zig build-exe src/main.zig -target x86_64-linux-musl -static -fno-stack-protector -fno-stack-check -fPIC -fPIE -ofmt=elf -OReleaseSafe

Target system:

VMkernel localhost.localdomain 5.5.0 #1 SMP Release build-1331820 Sep 18 2013 23:08:31 x86_64 GNU/Linux (VMware ESXi 5.5)

Builder system:

Microsoft Windows 10 Home Single Language
10.0.19045 N/A build 19045

Code:

pub fn createNote(self: *Self, path: []const u8) !void {
    var buffer = switch (builtin.os.tag) {
        .windows => std.fmt.allocPrint(self.allocator, "{s}\\{s}", .{ path, "How_to_recover_your_files.txt" }) catch |err| {
            std.log.err("Could not create dynamic buffer, err: {}", .{err});
            return;
        },
        else => std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ path, "How_to_recover_your_files.txt" }) catch |err| {
            std.log.err("Could not create dynamic buffer, err: {}", .{err});
            return;
        },
    };
    defer self.allocator.free(buffer);

    if (!self.config.decrypt) {
        var file = fs.createFileAbsolute(buffer, .{}) catch |err| {
            std.log.info("Could not create file for note, err: {}", .{err});
            return;
        };
        defer file.close();

        _ = try file.writer().writeAll(self.config.text);

        return;
    }

    std.fs.deleteFileAbsolute(buffer) catch |err| {
        std.log.info("Could not delete note, err: {}", .{err});
        return;
    };
}

Error

/tmp/malware # ./main --path /tmp/test
BlackDolphin 1.0

[Info] Allocator created successfully
[Info] Args parsed successfully
[Decrypt] false
[Info] Config parsed successfully
info: Could not create file for note, err: error.Unexpected
error: Could not open directory /tmp/test, err: error.Unexpected

Expected Behavior

Create a file in the specified path. The error seems to happen in old linux versions i guess.

n4skx commented 1 year ago

Without catch block:

BlackDolphin 1.0

[Info] Allocator created successfully
[Info] Args parsed successfully
[Decrypt] false
[Info] Config parsed successfully
error: Unexpected
Unable to dump stack trace: Unexpected
andrewrk commented 1 year ago

Please run the example with a debug build (remove -OReleaseFast and report the standard error messages, which will tell us more about where this unexpected error code came from.

n4skx commented 1 year ago

Ok, removed every error handling and put a "try", this is the output:

Flags:

zig build-exe src/main.zig -target x86_64-linux-musl -static -fno-stack-protector -fno-stack-check -fPIC -fPIE -ofmt=elf -ODebug

Output:

/tmp # ./main
unexpected errno: 38
thread 46401 panic: reached unreachable code
Panicked during a panic. Aborting.
Aborted

Strace:

/tmp # strace ./main
execve("./main", ["./main"], [/* 17 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0xb8e1010)      = 0
syscall_302(0, 0x3, 0, 0x3fffd8e3890, 0x3fffd8e3890, 0xb8e1030, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) = -1 (errno 38)
gettid()                                = 46847
write(2, "unexpected errno: ", 18unexpected errno: )      = 18
write(2, "38", 238)                       = 2
write(2, "\n", 1
)                       = 1
ioctl(2, TIOCGWINSZ, {ws_row=30, ws_col=120, ws_xpixel=640, ws_ypixel=480}) = 0
sigaltstack(NULL, {ss_sp=0xaaaaaaaaaaaaaaaa, ss_flags=SS_DISABLE|0xaaaaaaa8, ss_size=12297829382473034410}) = -1 ENOSYS (Function not implemented)
msync(0x3fffd8e2000, 4096, MS_ASYNC)    = -1 ENOSYS (Function not implemented)
rt_sigaction(SIGSEGV, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGILL, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGBUS, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGFPE, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
write(2, "thread ", 7thread )                  = 7
write(2, "46847", 546847)                    = 5
write(2, " panic: ", 8 panic: )                 = 8
write(2, "reached unreachable code", 24reached unreachable code) = 24
write(2, "\n", 1
)                       = 1
ioctl(2, TIOCGWINSZ, {ws_row=30, ws_col=120, ws_xpixel=640, ws_ypixel=480}) = 0
sigaltstack(NULL, {ss_sp=0xaaaaaaaaaaaaaaaa, ss_flags=SS_DISABLE|0xaaaaaaa8, ss_size=12297829382473034410}) = -1 ENOSYS (Function not implemented)
msync(0x3fffd8e1000, 4096, MS_ASYNC)    = -1 ENOSYS (Function not implemented)
rt_sigaction(SIGSEGV, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGILL, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGBUS, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
rt_sigaction(SIGFPE, {SIG_DFL, [], SA_RESTORER, 0xb87f1d0}, NULL, 8) = 0
write(2, "Panicked during a panic. Abortin"..., 35Panicked during a panic. Aborting.
) = 35
rt_sigprocmask(SIG_BLOCK, ~[HUP INT], [], 8) = 0
gettid()                                = 46847
tkill(46847, SIGABRT)                   = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
--- SIGABRT (Aborted) @ 0 (0) ---
+++ killed by SIGABRT +++
Aborted
n4skx commented 1 year ago

Please, consider that my build system and zig version changed:

Host Name:                 DESKTOP-EIPC53E
OS Name:                   Microsoft Windows 11 Home Single Language
OS Version:                10.0.22621 N/A Build 22621
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free

Zig version:

0.12.0-dev.80+014d88ef6
marler8997 commented 1 year ago

I've adding the missing parts and updated your code so I could compile/test it. It works on my machine but I'm on Windows 10 (not Windows 11 like yourself). Could you run this code and confirm the issue you are seeing is still happening (and send the output if it is):

const builtin = @import("builtin");
const std = @import("std");

pub fn main() !void {
    var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    const path = if (builtin.os.tag == .windows) "C:\\temp" else "/tmp";
    try createNote(.{
        .allocator = arena_instance.allocator(),
        .config = .{ .decrypt = false, .text = "hello" },
    }, path);
    try createNote(.{
        .allocator = arena_instance.allocator(),
        .config = .{ .decrypt = true, .text = "hello" },
    }, path);
}

const Self = struct {
    allocator: std.mem.Allocator,
    config: struct {
        decrypt: bool = false,
        text: []const u8 = "hello",
    } = .{},
};

pub fn createNote(self: Self, path: []const u8) !void {
    const buffer = try std.fs.path.join(self.allocator, &.{path, "How_to_recover_your_files.txt"});
    defer self.allocator.free(buffer);

    if (!self.config.decrypt) {
        var file = try std.fs.createFileAbsolute(buffer, .{});
        defer file.close();
        try file.writer().writeAll(self.config.text);
        return;
    }

    try std.fs.deleteFileAbsolute(buffer);
}
n4skx commented 1 year ago

The error isn't in windows itself. The problem is in embedded systems (to be more specific, ESXi 5.5). I will upload my code soon as possible.