Skip to content

Commit 4930094

Browse files
jedisct1andrewrk
authored andcommitted
valgrind.memcheck: fix makeMem*()
The `makeMem*()` functions crashed under valgrind in Debug and ReleaseSafe modes. The reason being that `doMemCheckClientRequestExpr()` returns `0` when not running under Valgrind, and `maxInt(usize)` when running under Valgrind. Thus, `@as(i1, @intcast(maxInt(usize)))` always fails and these functions crashed before returning. That being said, what these functions used to return was quite unexpected: `0` on error and `-1` on success (=running under valgrind). That doesn't match any Zig nor C conventions. But that return value doesn't seem to be very useful. Either we are running under Valgrind or we are not. There's no point in checking this for every single call. Applications are likely to always discard it. So, just return a `void` instead. Also avoid function comments that start with `Similarly, ...` because that doesn't refer to anything in the context of autodoc or in IDEs.
1 parent 0bdbd3e commit 4930094

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

lib/std/valgrind/memcheck.zig

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,29 @@ fn doMemCheckClientRequestStmt(request: MemCheckClientRequest, a1: usize, a2: us
2929
}
3030

3131
/// Mark memory at qzz.ptr as unaddressable for qzz.len bytes.
32-
/// This returns -1 when run on Valgrind and 0 otherwise.
33-
pub fn makeMemNoAccess(qzz: []u8) i1 {
34-
return @as(i1, @intCast(doMemCheckClientRequestExpr(0, // default return
35-
.MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0)));
32+
pub fn makeMemNoAccess(qzz: []u8) void {
33+
_ = doMemCheckClientRequestExpr(0, // default return
34+
.MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
3635
}
3736

38-
/// Similarly, mark memory at qzz.ptr as addressable but undefined
39-
/// for qzz.len bytes.
40-
/// This returns -1 when run on Valgrind and 0 otherwise.
41-
pub fn makeMemUndefined(qzz: []u8) i1 {
42-
return @as(i1, @intCast(doMemCheckClientRequestExpr(0, // default return
43-
.MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0)));
37+
/// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes.
38+
pub fn makeMemUndefined(qzz: []u8) void {
39+
_ = doMemCheckClientRequestExpr(0, // default return
40+
.MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
4441
}
4542

46-
/// Similarly, mark memory at qzz.ptr as addressable and defined
47-
/// for qzz.len bytes.
48-
pub fn makeMemDefined(qzz: []u8) i1 {
49-
// This returns -1 when run on Valgrind and 0 otherwise.
50-
return @as(i1, @intCast(doMemCheckClientRequestExpr(0, // default return
51-
.MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0)));
43+
/// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes.
44+
pub fn makeMemDefined(qzz: []u8) void {
45+
_ = doMemCheckClientRequestExpr(0, // default return
46+
.MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
5247
}
5348

5449
/// Similar to makeMemDefined except that addressability is
5550
/// not altered: bytes which are addressable are marked as defined,
5651
/// but those which are not addressable are left unchanged.
57-
/// This returns -1 when run on Valgrind and 0 otherwise.
58-
pub fn makeMemDefinedIfAddressable(qzz: []u8) i1 {
59-
return @as(i1, @intCast(doMemCheckClientRequestExpr(0, // default return
60-
.MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0)));
52+
pub fn makeMemDefinedIfAddressable(qzz: []u8) void {
53+
_ = doMemCheckClientRequestExpr(0, // default return
54+
.MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
6155
}
6256

6357
/// Create a block-description handle. The description is an ascii

0 commit comments

Comments
 (0)