Valgrind与Zig一起出现非法硬件指令

huangapple go评论68阅读模式
英文:

Valgrind illegal hardware instruction with Zig

问题

这是我使用Valgrind调试一个简单的Zig代码泄漏内存时的代码:

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});
}

我使用以下命令编译代码:

zig build-exe leak.zig --library c

然后我使用以下命令运行Valgrind:

valgrind ./leak

这是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)
...

我使用的Zig版本是0.11.0-dev.4006,Valgrind版本是valgrind-3.21.0,运行在Ubuntu 22.04上。

发生这种情况的原因是Valgrind无法处理Zig编译生成的某些指令,这导致Valgrind报告了未识别的指令并终止了程序的执行。要解决这个问题,你可以考虑以下几种方法:

  1. 更新Zig和Valgrind版本: 确保你正在使用最新版本的Zig和Valgrind,因为新版本可能会修复一些问题。

  2. 尝试其他工具: 考虑使用其他内存分析工具,例如AddressSanitizer(ASan)或MemorySanitizer(MSan),它们可能与Zig更兼容。

  3. 禁用Valgrind的某些功能: 你可以尝试通过Valgrind的选项来禁用某些功能,以防止Valgrind报告错误。例如,你可以尝试使用--smc-check=all选项来禁用Self-Modifying Code(SMC)检查,但这可能会降低Valgrind的检测能力。

请注意,Valgrind在处理某些特定的编程语言或编译器生成的代码时可能会遇到问题,因此有时需要尝试不同的工具或方法来调试内存泄漏问题。

英文:

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?

答案1

得分: 1

这是 vpbroadcastb ymm0,esi,这是一个AVX512指令,在Valgrind中不支持。尝试在不使用AVX512的情况下编译。

英文:

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

huangapple
  • 本文由 发表于 2023年8月4日 02:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定