Search code examples
valgrindzig

Valgrind illegal hardware instruction with Zig


I'm trying to debug the memory using Valgrind for a simple Zig code that leaks memory.

This is the code I'm using

const std = @import("std");

const Point = struct {
    x: i32,
    y: i32,
};

pub fn main() !void {
    const allocator = std.heap.c_allocator;
    const point = try allocator.create(Point);

    // defer allocator.destroy(point);

    point.* = Point{
        .x = 1234,
        .y = 5678,
    };

    std.debug.print("point={}\n", .{point});
}

I'm compiling the code using

zig build-exe leak.zig --library c

Then I'm running valgrind using

valgrind ./leak

This is the output I got from valgrind

==21676== Memcheck, a memory error detector
==21676== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==21676== Using Valgrind-3.21.0 and LibVEX; rerun with -h for copyright info
==21676== Command: ./leak
==21676== 
point=leak.Point{ .x = vex amd64->IR: unhandled instruction bytes: 0x62 0xF2 0x7D 0x28 0x7A 0xC6 0xF 0x1F 0x44 0x0
vex amd64->IR:   REX=0 REX.W=0 REX.R=0 REX.X=0 REX.B=0
vex amd64->IR:   VEX=0 VEX.L=0 VEX.nVVVV=0x0 ESC=NONE
vex amd64->IR:   PFX.66=0 PFX.F2=0 PFX.F3=0
==21676== valgrind: Unrecognised instruction at address 0x25ca25.
==21676==    at 0x25CA25: memset (in /home/mattia/dev/test-zig/vv/leak)
==21676==    by 0x249C79: fmt.formatInt__anon_7676 (fmt.zig:1418)
==21676==    by 0x249BB3: fmt.formatIntValue__anon_7673 (fmt.zig:784)
==21676==    by 0x249B63: fmt.formatValue__anon_7672 (fmt.zig:733)
==21676==    by 0x243DBA: fmt.formatType__anon_7382 (fmt.zig:487)
==21676==    by 0x2382D7: fmt.formatType__anon_7092 (fmt.zig:596)
==21676==    by 0x22F46E: fmt.formatType__anon_6492 (fmt.zig:625)
==21676==    by 0x22F396: fmt.format__anon_6443 (fmt.zig:184)
==21676==    by 0x20C0E0: io.writer.Writer(fs.file.File,error{Unexpected,DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,DeviceBusy,InvalidArgument,AccessDenied,BrokenPipe,SystemResources,OperationAborted,NotOpenForWriting,LockViolation,WouldBlock,ConnectionResetByPeer},(function 'write')).print__anon_4362 (writer.zig:28)
==21676==    by 0x20A0BD: debug.print__anon_3012 (debug.zig:90)
==21676==    by 0x209E35: leak.main (leak.zig:19)
==21676==    by 0x20A673: callMain (start.zig:608)
==21676==    by 0x20A673: initEventLoopAndCallMain (start.zig:542)
==21676==    by 0x20A673: callMainWithArgs (start.zig:492)
==21676==    by 0x20A673: main (start.zig:507)
==21676== Your program just tried to execute an instruction that Valgrind
==21676== did not recognise.  There are two possible reasons for this.
==21676== 1. Your program has a bug and erroneously jumped to a non-code
==21676==    location.  If you are running Memcheck and you just saw a
==21676==    warning about a bad jump, it's probably your program's fault.
==21676== 2. The instruction is legitimate but Valgrind doesn't handle it,
==21676==    i.e. it's Valgrind's fault.  If you think this is the case or
==21676==    you are not sure, please let us know and we'll try to fix it.
==21676== Either way, Valgrind will now raise a SIGILL signal which will
==21676== probably kill your program.

I'm using zig version 0.11.0-dev.4006 and valgrind version valgrind-3.21.0 on Ubuntu 22.04.

Why is this happening and how can I run valgrind on zig code?


Solution

  • That’s vpbroadcastb ymm0,esi which is an avx512 instruction, unsupported by Valgrind. Try compiling without avx512.