-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Missing mask of bits in implicit cast #2850
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
Comments
Maybe related to #1512 (comment) ? |
Simpler test case: const std = @import("std");
fn hashFunc(out: []u8, in: []const u8) void {
for (out) |*p, i| {
p.* = 0xff;
}
}
fn printUsize(x: usize) usize {
std.debug.warn("inside function call: {}\n", x);
return x;
}
pub fn main() void {
const Index = std.math.IntFittingRange(0, 1024 - 1);
var K_th_bit: Index = undefined;
hashFunc(std.mem.asBytes(&K_th_bit), "foo");
std.debug.warn("outside function call: {}\n", K_th_bit);
const x = printUsize(K_th_bit);
std.debug.warn("from function call: {}\n", x);
} Output:
|
here's a reduction that is useful for IR and LLVM-IR production: zig build-obj bug.zig --verbose-ir --verbose-llvm-irfn set_v0(a: *u10) void {
a.* = 1023;
}
fn set_v1(a: []u8) void {
for (a) |*p,i| {
p.* = 0xff;
}
}
export fn foo() void {
@setRuntimeSafety(false);
var a: u10 = undefined;
set_v1(@import("std").mem.asBytes(&a));
var b: usize = undefined;
b = a;
}
const builtin = @import("builtin");
pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
while (true) {}
} My initial impression is that define void @foo() #2 !dbg !35 {
Entry:
%a = alloca i10, align 2
%0 = alloca %"[]u8", align 8
%b = alloca i64, align 8
call void @llvm.dbg.declare(metadata i10* %a, metadata !39, metadata !DIExpression()), !dbg !43
%1 = call fastcc [2 x i8]* @std.mem.asBytes(i10* %a), !dbg !44
%2 = getelementptr inbounds %"[]u8", %"[]u8"* %0, i32 0, i32 0, !dbg !44
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0, !dbg !44
store i8* %3, i8** %2, !dbg !44
%4 = getelementptr inbounds %"[]u8", %"[]u8"* %0, i32 0, i32 1, !dbg !44
store i64 2, i64* %4, !dbg !44
call fastcc void @set_v1(%"[]u8"* %0), !dbg !45
call void @llvm.dbg.declare(metadata i64* %b, metadata !42, metadata !DIExpression()), !dbg !46
%5 = load i10, i10* %a, align 2, !dbg !47
%6 = zext i10 %5 to i64, !dbg !47
store i64 %6, i64* %b, align 8, !dbg !47
ret void, !dbg !48
} Here's x86_64 asm showing use of _foo:
Lfunc_begin1:
.loc 1 11 0
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $32, %rsp
leaq -2(%rbp), %rdi
Ltmp2:
.loc 1 15 38 prologue_end
callq _std.mem.asBytes
movq %rax, -24(%rbp)
movq $2, -16(%rbp)
leaq -24(%rbp), %rdi
.loc 1 15 11 is_stmt 0
callq _set_v1
.loc 1 18 9 is_stmt 1
movzwl -2(%rbp), %ecx
movl %ecx, %eax
movq %rax, -32(%rbp)
.loc 1 11 22
addq $32, %rsp
popq %rbp
retq
Ltmp3:
Lfunc_end1:
.cfi_endproc |
transcript of related IRC discussion: daurnimator: okay fun.... maybe this is an LLVM bug rather than a zig bug? I see a |
This is working as designed.
In this code I have inserted comments: var a: u10 = undefined;
// Here a is undefined
set_v1(@import("std").mem.asBytes(&a));
// Here a is undefined
var b: usize = undefined;
// Here b is undefined
b = a;
// Here b is undefined |
Looking into the CI failure of #2843 I arrived at the following reduced test case:
With this applied to make things more obvious:
Based on how I was able to reduce the test case, it appears as though the call to
.get
doesn't mask off bits as it implicitly casts fromIndex
tousize
.The text was updated successfully, but these errors were encountered: