Skip to content

Undefined struct fields are initialized to 0 instead of 0xAA in debug mode (with llvm backend) #20095

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

Open
IntegratedQuantum opened this issue May 28, 2024 · 1 comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@IntegratedQuantum
Copy link
Contributor

IntegratedQuantum commented May 28, 2024

Zig Version

0.13.0-dev.75+5c9eb4081

Steps to Reproduce and Observed Behavior

const std = @import("std");

pub const Biome = struct {
	val: u32 = undefined,
};

pub fn main() !void {
	var biome1: Biome = .{};
	std.log.err("{any}", .{std.mem.asBytes(&biome1)});
	var biome2: Biome = .{.val = undefined};
	std.log.err("{any}", .{std.mem.asBytes(&biome2)});
	var biome3: Biome = undefined;
	std.log.err("{any}", .{std.mem.asBytes(&biome3)});
}

Note how only the last variant actually contains the desired 0xAA (170) bytes:

$ zig run test.zig
error: { 0, 0, 0, 0 }
error: { 0, 0, 0, 0 }
error: { 170, 170, 170, 170 }

Expected Behavior

Setting things to undefined should set them to 0xAA in Debug mode, like it does with the x86 backend.
This makes some bugs easier to identify.

Consider for example an ArrayListUnmanaged that gets initialized to undefined, like in #20085.
Since it gets currently set to 0 in debug mode, the deinit() does not produce any errors, making it only possible to identify the issue in release.
With 0xAA it would produce a runtime crash when attempting to free it and according to zig zen:

  • Runtime crashes are better than bugs.
@IntegratedQuantum IntegratedQuantum added the bug Observed behavior contradicts documented or intended behavior label May 28, 2024
@IntegratedQuantum IntegratedQuantum changed the title Undefined struct fields are initialized to 0 instead of 0xAA in debug mode Undefined struct fields are initialized to 0 instead of 0xAA in debug mode (with llvm backend) May 28, 2024
@Vexu Vexu added the backend-llvm The LLVM backend outputs an LLVM IR Module. label May 29, 2024
@Vexu Vexu added this to the 0.14.0 milestone May 29, 2024
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 10, 2025
@FnControlOption
Copy link
Contributor

Relevant: https://reviews.llvm.org/D58188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

4 participants