| 1 | const std = @import("../std.zig"); |
| 2 | const testing = std.testing; |
| 3 | const valgrind = std.valgrind; |
| 4 | |
| 5 | pub const ClientRequest = enum(usize) { |
| 6 | MakeMemNoAccess = valgrind.ToolBase("MC".*), |
| 7 | MakeMemUndefined, |
| 8 | MakeMemDefined, |
| 9 | Discard, |
| 10 | CheckMemIsAddressable, |
| 11 | CheckMemIsDefined, |
| 12 | DoLeakCheck, |
| 13 | CountLeaks, |
| 14 | GetVbits, |
| 15 | SetVbits, |
| 16 | CreateBlock, |
| 17 | MakeMemDefinedIfAddressable, |
| 18 | CountLeakBlocks, |
| 19 | EnableAddrErrorReportingInRange, |
| 20 | DisableAddrErrorReportingInRange, |
| 21 | }; |
| 22 | |
| 23 | fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize { |
| 24 | return valgrind.doClientRequest(default, @as(usize, @intCast(@backingInt(request))), a1, a2, a3, a4, a5); |
| 25 | } |
| 26 | |
| 27 | fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void { |
| 28 | _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5); |
| 29 | } |
| 30 | |
| 31 | /// Mark memory at qzz.ptr as unaddressable for qzz.len bytes. |
| 32 | pub fn makeMemNoAccess(qzz: []const u8) void { |
| 33 | _ = doClientRequestExpr(0, // default return |
| 34 | .MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 35 | } |
| 36 | |
| 37 | /// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes. |
| 38 | pub fn makeMemUndefined(qzz: []const u8) void { |
| 39 | _ = doClientRequestExpr(0, // default return |
| 40 | .MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 41 | } |
| 42 | |
| 43 | /// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes. |
| 44 | pub fn makeMemDefined(qzz: []const u8) void { |
| 45 | _ = doClientRequestExpr(0, // default return |
| 46 | .MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 47 | } |
| 48 | |
| 49 | /// Similar to makeMemDefined except that addressability is |
| 50 | /// not altered: bytes which are addressable are marked as defined, |
| 51 | /// but those which are not addressable are left unchanged. |
| 52 | pub fn makeMemDefinedIfAddressable(qzz: []const u8) void { |
| 53 | _ = doClientRequestExpr(0, // default return |
| 54 | .MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 55 | } |
| 56 | |
| 57 | /// Create a block-description handle. The description is an ascii |
| 58 | /// string which is included in any messages pertaining to addresses |
| 59 | /// within the specified memory range. Has no other effect on the |
| 60 | /// properties of the memory range. |
| 61 | pub fn createBlock(qzz: []const u8, desc: [*:0]const u8) usize { |
| 62 | return doClientRequestExpr(0, // default return |
| 63 | .CreateBlock, @intFromPtr(qzz.ptr), qzz.len, @intFromPtr(desc), 0, 0); |
| 64 | } |
| 65 | |
| 66 | /// Discard a block-description-handle. Returns 1 for an |
| 67 | /// invalid handle, 0 for a valid handle. |
| 68 | pub fn discard(blkindex: usize) bool { |
| 69 | return doClientRequestExpr(0, // default return |
| 70 | .Discard, 0, blkindex, 0, 0, 0) != 0; |
| 71 | } |
| 72 | |
| 73 | /// Check that memory at qzz.ptr is addressable for qzz.len bytes. |
| 74 | /// If suitable addressability is not established, Valgrind prints an |
| 75 | /// error message and returns the address of the first offending byte. |
| 76 | /// Otherwise it returns zero. |
| 77 | pub fn checkMemIsAddressable(qzz: []const u8) usize { |
| 78 | return doClientRequestExpr(0, .CheckMemIsAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 79 | } |
| 80 | |
| 81 | /// Check that memory at qzz.ptr is addressable and defined for |
| 82 | /// qzz.len bytes. If suitable addressability and definedness are not |
| 83 | /// established, Valgrind prints an error message and returns the |
| 84 | /// address of the first offending byte. Otherwise it returns zero. |
| 85 | pub fn checkMemIsDefined(qzz: []const u8) usize { |
| 86 | return doClientRequestExpr(0, .CheckMemIsDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 87 | } |
| 88 | |
| 89 | /// Do a full memory leak check (like --leak-check=full) mid-execution. |
| 90 | pub fn doLeakCheck() void { |
| 91 | doClientRequestStmt(.DoLeakCheck, 0, 0, 0, 0, 0); |
| 92 | } |
| 93 | |
| 94 | /// Same as doLeakCheck() but only showing the entries for |
| 95 | /// which there was an increase in leaked bytes or leaked nr of blocks |
| 96 | /// since the previous leak search. |
| 97 | pub fn doAddedLeakCheck() void { |
| 98 | doClientRequestStmt(.DoLeakCheck, 0, 1, 0, 0, 0); |
| 99 | } |
| 100 | |
| 101 | /// Same as doAddedLeakCheck() but showing entries with |
| 102 | /// increased or decreased leaked bytes/blocks since previous leak |
| 103 | /// search. |
| 104 | pub fn doChangedLeakCheck() void { |
| 105 | doClientRequestStmt(.DoLeakCheck, 0, 2, 0, 0, 0); |
| 106 | } |
| 107 | |
| 108 | /// Do a summary memory leak check (like --leak-check=summary) mid-execution. |
| 109 | pub fn doQuickLeakCheck() void { |
| 110 | doClientRequestStmt(.DoLeakCheck, 1, 0, 0, 0, 0); |
| 111 | } |
| 112 | |
| 113 | /// Return number of leaked, dubious, reachable and suppressed bytes found by |
| 114 | /// all previous leak checks. |
| 115 | const CountResult = struct { |
| 116 | leaked: usize, |
| 117 | dubious: usize, |
| 118 | reachable: usize, |
| 119 | suppressed: usize, |
| 120 | }; |
| 121 | |
| 122 | pub fn countLeaks() CountResult { |
| 123 | var res: CountResult = .{ |
| 124 | .leaked = 0, |
| 125 | .dubious = 0, |
| 126 | .reachable = 0, |
| 127 | .suppressed = 0, |
| 128 | }; |
| 129 | doClientRequestStmt( |
| 130 | .CountLeaks, |
| 131 | @intFromPtr(&res.leaked), |
| 132 | @intFromPtr(&res.dubious), |
| 133 | @intFromPtr(&res.reachable), |
| 134 | @intFromPtr(&res.suppressed), |
| 135 | 0, |
| 136 | ); |
| 137 | return res; |
| 138 | } |
| 139 | |
| 140 | test countLeaks { |
| 141 | try testing.expectEqual( |
| 142 | @as(CountResult, .{ |
| 143 | .leaked = 0, |
| 144 | .dubious = 0, |
| 145 | .reachable = 0, |
| 146 | .suppressed = 0, |
| 147 | }), |
| 148 | countLeaks(), |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | pub fn countLeakBlocks() CountResult { |
| 153 | var res: CountResult = .{ |
| 154 | .leaked = 0, |
| 155 | .dubious = 0, |
| 156 | .reachable = 0, |
| 157 | .suppressed = 0, |
| 158 | }; |
| 159 | doClientRequestStmt( |
| 160 | .CountLeakBlocks, |
| 161 | @intFromPtr(&res.leaked), |
| 162 | @intFromPtr(&res.dubious), |
| 163 | @intFromPtr(&res.reachable), |
| 164 | @intFromPtr(&res.suppressed), |
| 165 | 0, |
| 166 | ); |
| 167 | return res; |
| 168 | } |
| 169 | |
| 170 | test countLeakBlocks { |
| 171 | try testing.expectEqual( |
| 172 | @as(CountResult, .{ |
| 173 | .leaked = 0, |
| 174 | .dubious = 0, |
| 175 | .reachable = 0, |
| 176 | .suppressed = 0, |
| 177 | }), |
| 178 | countLeakBlocks(), |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /// Get the validity data for addresses zza and copy it |
| 183 | /// into the provided zzvbits array. Return values: |
| 184 | /// 0 if not running on valgrind |
| 185 | /// 1 success |
| 186 | /// 2 [previously indicated unaligned arrays; these are now allowed] |
| 187 | /// 3 if any parts of zzsrc/zzvbits are not addressable. |
| 188 | /// The metadata is not copied in cases 0, 2 or 3 so it should be |
| 189 | /// impossible to segfault your system by using this call. |
| 190 | pub fn getVbits(zza: []u8, zzvbits: []u8) u2 { |
| 191 | std.debug.assert(zzvbits.len >= zza.len / 8); |
| 192 | return @as(u2, @intCast(doClientRequestExpr(0, .GetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits.ptr), zza.len, 0, 0))); |
| 193 | } |
| 194 | |
| 195 | /// Set the validity data for addresses zza, copying it |
| 196 | /// from the provided zzvbits array. Return values: |
| 197 | /// 0 if not running on valgrind |
| 198 | /// 1 success |
| 199 | /// 2 [previously indicated unaligned arrays; these are now allowed] |
| 200 | /// 3 if any parts of zza/zzvbits are not addressable. |
| 201 | /// The metadata is not copied in cases 0, 2 or 3 so it should be |
| 202 | /// impossible to segfault your system by using this call. |
| 203 | pub fn setVbits(zzvbits: []u8, zza: []u8) u2 { |
| 204 | std.debug.assert(zzvbits.len >= zza.len / 8); |
| 205 | return @as(u2, @intCast(doClientRequestExpr(0, .SetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits.ptr), zza.len, 0, 0))); |
| 206 | } |
| 207 | |
| 208 | /// Disable and re-enable reporting of addressing errors in the |
| 209 | /// specified address range. |
| 210 | pub fn disableAddrErrorReportingInRange(qzz: []u8) usize { |
| 211 | return doClientRequestExpr(0, // default return |
| 212 | .DisableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 213 | } |
| 214 | |
| 215 | pub fn enableAddrErrorReportingInRange(qzz: []u8) usize { |
| 216 | return doClientRequestExpr(0, // default return |
| 217 | .EnableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0); |
| 218 | } |