Valgrind 使用 Zig 进行非法硬件指令

问题描述 投票:0回答:0

我正在尝试使用 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)
==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.

我在 Ubuntu 22.04 上使用 zig 版本 0.11.0-dev.4006 和 valgrind 版本 valgrind-3.21.0。

valgrind zig
© www.soinside.com 2019 - 2024. All rights reserved.