Skip to content

aro_translate_c: undefined triggers illegal behavior later in ast rendering #22601

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

Closed
jacobly0 opened this issue Jan 25, 2025 · 0 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@jacobly0
Copy link
Member

diff --git a/lib/compiler/aro_translate_c/ast.zig b/lib/compiler/aro_translate_c/ast.zig
index b92db7862f..4c74f272d7 100644
--- a/lib/compiler/aro_translate_c/ast.zig
+++ b/lib/compiler/aro_translate_c/ast.zig
@@ -776,8 +776,8 @@ pub fn render(gpa: Allocator, nodes: []const Node) !std.zig.Ast {
         .tag = .root,
         .main_token = 0,
         .data = .{
-            .lhs = undefined,
-            .rhs = undefined,
+            .lhs = std.math.maxInt(u32),
+            .rhs = std.math.maxInt(u32),
         },
     });
 
@@ -993,7 +993,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
             .main_token = try c.addToken(.keyword_continue, "continue"),
             .data = .{
                 .lhs = 0,
-                .rhs = undefined,
+                .rhs = std.math.maxInt(u32),
             },
         }),
         .return_void => return c.addNode(.{
@@ -1001,7 +1001,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
             .main_token = try c.addToken(.keyword_return, "return"),
             .data = .{
                 .lhs = 0,
-                .rhs = undefined,
+                .rhs = std.math.maxInt(u32),
             },
         }),
         .@"break" => return c.addNode(.{
@@ -1035,7 +1035,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = try c.addToken(.keyword_return, "return"),
                 .data = .{
                     .lhs = try renderNode(c, payload),
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
         },
@@ -1046,7 +1046,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = try c.addToken(.keyword_comptime, "comptime"),
                 .data = .{
                     .lhs = try renderNode(c, payload),
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
         },
@@ -1056,7 +1056,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .tag = .@"defer",
                 .main_token = try c.addToken(.keyword_defer, "defer"),
                 .data = .{
-                    .lhs = undefined,
+                    .lhs = std.math.maxInt(u32),
                     .rhs = try renderNode(c, payload),
                 },
             });
@@ -1106,7 +1106,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = tok,
                 .data = .{
                     .lhs = arg,
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
         },
@@ -1326,7 +1326,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .tag = .deref,
                 .data = .{
                     .lhs = try renderNodeGrouped(c, payload.init),
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
                 .main_token = try c.addToken(.period_asterisk, ".*"),
             });
@@ -1507,7 +1507,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = ampersand,
                 .data = .{
                     .lhs = base,
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
         },
@@ -1520,7 +1520,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = deref_tok,
                 .data = .{
                     .lhs = operand,
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
         },
@@ -1796,7 +1796,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
                 .main_token = try c.addToken(.bang, "!"),
                 .data = .{
                     .lhs = try renderNodeGrouped(c, payload),
-                    .rhs = undefined,
+                    .rhs = std.math.maxInt(u32),
                 },
             });
             _ = try c.addToken(.r_paren, ")");
@@ -2541,7 +2541,7 @@ fn renderPrefixOp(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_tag: T
         .main_token = try c.addToken(tok_tag, bytes),
         .data = .{
             .lhs = try renderNodeGrouped(c, payload),
-            .rhs = undefined,
+            .rhs = std.math.maxInt(u32),
         },
     });
 }
$ cat repro.h
void foo(void) {
    for (;;) {
        continue;
    }
}
$ zig translate-c -lc repro.h
thread 230209 panic: index out of bounds: index 4294967295, len 2050
Unwind error at address `:0x22e8e03` (error.AddressOutOfRange), trace may be incomplete

lib/std/zig/render.zig:350:22: 0x229d60b in renderExpression (std.zig)
    switch (node_tags[node]) {
                     ^
lib/std/zig/render.zig:705:37: 0x22a356b in renderExpression (std.zig)
                try renderExpression(r, target, space);
                                    ^
lib/std/zig/render.zig:1969:41: 0x2d1afc6 in finishRenderBlock (std.zig)
            else => try renderExpression(r, stmt, .semicolon),
                                        ^
lib/std/zig/render.zig:1947:29: 0x27cfc95 in renderBlock (std.zig)
    return finishRenderBlock(r, block_node, statements, space);
                            ^
lib/std/zig/render.zig:402:31: 0x22a2161 in renderExpression (std.zig)
            return renderBlock(r, node, statements, space);
                              ^
lib/std/zig/render.zig:1390:33: 0x2d1efb1 in renderThenElse (std.zig)
            try renderExpression(r, then_expr, space);
                                ^
lib/std/zig/render.zig:1322:23: 0x27e6c07 in renderWhile (std.zig)
    try renderThenElse(
                      ^
lib/std/zig/render.zig:871:30: 0x22a4b84 in renderExpression (std.zig)
        => return renderWhile(r, tree.fullWhile(node).?, space),
                             ^
lib/std/zig/render.zig:1969:41: 0x2d1afc6 in finishRenderBlock (std.zig)
            else => try renderExpression(r, stmt, .semicolon),
                                        ^
lib/std/zig/render.zig:1947:29: 0x27cfc95 in renderBlock (std.zig)
    return finishRenderBlock(r, block_node, statements, space);
                            ^
lib/std/zig/render.zig:402:31: 0x22a2161 in renderExpression (std.zig)
            return renderBlock(r, node, statements, space);
                              ^
lib/std/zig/render.zig:249:40: 0x27ed534 in renderMember (std.zig)
                return renderExpression(r, body_node, space);
                                       ^
lib/std/zig/render.zig:128:25: 0x22a7c86 in renderMembers (std.zig)
        try renderMember(r, container, member, .newline);
                        ^
lib/std/zig/render.zig:110:26: 0x1ed50ce in renderTree (std.zig)
        try renderMembers(&r, tree.rootDecls());
                         ^
lib/std/zig/Ast.zig:127:46: 0x1c26e8f in renderToArrayList (std.zig)
    return @import("./render.zig").renderTree(buffer, tree, fixups);
                                             ^
lib/std/zig/Ast.zig:120:31: 0x1beed37 in render (std.zig)
    try tree.renderToArrayList(&buffer, .{});
                              ^
src/main.zig:4604:55: 0x1a89a63 in cmdTranslateC (main.zig)
                break :f .{ .success = try tree.render(arena) };
                                                      ^
src/main.zig:3669:33: 0x1aaf607 in buildOutputType (main.zig)
            return cmdTranslateC(comp, arena, null, null, root_prog_node);
                                ^
src/main.zig:315:31: 0x1af3274 in mainArgs (main.zig)
        return buildOutputType(gpa, arena, args, .translate_c);
                              ^
src/main.zig:214:20: 0x19b9bf4 in main (main.zig)
    return mainArgs(gpa, arena, args);
                   ^
lib/std/start.zig:656:37: 0x19b7f89 in main (std.zig)
            const result = root.main() catch |err| {
                                    ^
Aborted (core dumped)

See #20095 for why safety is broken with the llvm backend.

@jacobly0 jacobly0 added bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport) labels Jan 25, 2025
@jacobly0 jacobly0 added this to the 0.16.0 milestone Jan 25, 2025
Techatrix added a commit to Techatrix/zig that referenced this issue Feb 6, 2025
Vexu added a commit to ziglang/translate-c that referenced this issue Feb 7, 2025
Techatrix added a commit to Techatrix/zig that referenced this issue Feb 7, 2025
Techatrix added a commit to Techatrix/zig that referenced this issue Feb 9, 2025
Techatrix added a commit to Techatrix/zig that referenced this issue Feb 26, 2025
@mlugg mlugg closed this as completed in de9c889 Mar 12, 2025
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 translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

1 participant