ziglang / zig

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

x86_64 backend cant make HTTPS requests #18126

Open Beyley opened 10 months ago

Beyley commented 10 months ago

Zig Version

0.12.0-dev.1726+8b1097083

Steps to Reproduce and Observed Behavior

Clone this repo at commit 2900ecbcf8ca3b6e6827c0a82164f22c33668e89 Compile with zig build -Duse_llvm=false run on a Linux host, with a file located at ~/.config/fresh_presence_config.ini with the contents

instance_url = https://lbp.littlebigrefresh.com/
username = Beyley
close_upon_game_exit = false

App crashes on startup when it attempts to ask the server for instance information

steps [2/6] zig build-exe FreshPresence Debug native... Semantic Analysis [5081] ... info: Reading config from /home/beyley/.config/fresh_presence_config.ini
General protection exception (no address available)
/usr/lib/zig/lib/std/crypto/tls/Client.zig:1286:5: 0x8277b1d in prepareCiphertextRecord (main.zig)
    return switch (native_endian) {
    ^
Unwind error at address `:0x8277b1d` (error.AddressOutOfRange), trace may be incomplete

/usr/lib/zig/lib/std/crypto/tls/Client.zig:740:43: 0x80ded03 in writeEnd__anon_14231 (main.zig)
    var prepared = prepareCiphertextRecord(c, &iovecs_buf, &ciphertext_buf, bytes, .application_data);
                                          ^
/usr/lib/zig/lib/std/crypto/tls/Client.zig:710:20: 0x827f9b3 in write__anon_15793 (main.zig)
    return writeEnd(c, stream, bytes, false);
                   ^
/usr/lib/zig/lib/std/crypto/tls/Client.zig:717:29: 0x827fb18 in writeAll__anon_15792 (main.zig)
        index += try c.write(stream, bytes[index..]);
                            ^
/usr/lib/zig/lib/std/http/Client.zig:302:40: 0x827fcbe in writeAllDirectTls (main.zig)
        return conn.tls_client.writeAll(conn.stream, buffer) catch |err| switch (err) {
                                       ^
/usr/lib/zig/lib/std/http/Client.zig:312:42: 0x80e2b0a in writeAllDirect (main.zig)
            return conn.writeAllDirectTls(buffer);
                                         ^
/usr/lib/zig/lib/std/http/Client.zig:340:32: 0x80b0915 in flush (main.zig)
        try conn.writeAllDirect(conn.write_buf[0..conn.write_end]);
                               ^
/usr/lib/zig/lib/std/http/Client.zig:730:35: 0x8083013 in send (main.zig)
        try req.connection.?.flush();
                                  ^
/home/beyley/.cache/zig/p/1220e05e6e1d110e9b2e4511358e621a2b9e7b86e8f33bd7201d21e30f1992073bc4/src/api.zig:307:21: 0x802ccf9 in makeRequest__anon_10053 (main.zig)
    try request.send(.{});
                    ^
/home/beyley/.cache/zig/p/1220e05e6e1d110e9b2e4511358e621a2b9e7b86e8f33bd7201d21e30f1992073bc4/src/api.zig:393:34: 0x8016fd7 in getInstanceInformation (main.zig)
    var request = try makeRequest(
                                 ^
/home/beyley/Projects/PROGRAMMING/Zig/FreshPresence/src/main.zig:76:64: 0x80079b8 in runApp (main.zig)
    var instance_info_response = try Api.getInstanceInformation(allocator, uri);
                                                               ^
/home/beyley/Projects/PROGRAMMING/Zig/FreshPresence/src/main.zig:43:11: 0x800ec06 in main (main.zig)
    runApp(allocator) catch |err| {
          ^
/usr/lib/zig/lib/std/start.zig:585:37: 0x800f39a in main (main.zig)
            const result = root.main() catch |err| {
                                    ^

Expected Behavior

App runs as it does on the LLVM backend, with the logs of

┌─[beyley@arch] - [~/Projects/PROGRAMMING/Zig/FreshPresence] - [2023-11-25 06:08:37]
└─[1] <git:(master 2900ecb) > zig build run
info: Reading config from /home/beyley/.config/fresh_presence_config.ini
info: Connected to instance LittleBigRefresh
info: Found user Beyley with id 64d761a52878d52b63a53ae0
akhildevelops commented 10 months ago

I too face the same issue for below simple code on (0.12.0-dev.1751+7fbbeae61):

const std = @import("std");

pub fn main() !void {
    var gp = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gp.allocator();
    var client = std.http.Client{ .allocator = allocator };
    client.deinit();
    var resp = try client.fetch(allocator, .{ .location = .{ .url = "https://dummyjson.com/products/1" } });
    resp.deinit();
}
akhildevelops commented 10 months ago

I too face the same issue for below simple code on (0.12.0-dev.1751+7fbbeae61):

const std = @import("std");

pub fn main() !void {
    var gp = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gp.allocator();
    var client = std.http.Client{ .allocator = allocator };
    client.deinit();
    var resp = try client.fetch(allocator, .{ .location = .{ .url = "https://dummyjson.com/products/1" } });
    resp.deinit();
}

My Bad, I'm de-initializing the memory without defer, now it's working for me:

const std = @import("std");

pub fn main() !void {
    var gp = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gp.allocator();
    var client = std.http.Client{ .allocator = allocator };
    defer    client.deinit();
    var resp = try client.fetch(allocator, .{ .location = .{ .url = "https://dummyjson.com/products/1" } });
    defer resp.deinit();
}