windows: use array of tmp bufs as backing store for input memory to ntdll

This commit is contained in:
Jakub Konka 2022-12-02 10:20:05 +01:00 committed by Andrew Kelley
parent 16dc86a49e
commit 5eaacf1ce9

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const assert = std.debug.assert;
const mem = std.mem; const mem = std.mem;
const Target = std.Target; const Target = std.Target;
@ -89,6 +90,8 @@ fn getCpuInfoFromRegistry(core: usize, args: anytype) !void {
.DefaultLength = 0, .DefaultLength = 0,
}; };
var tmp_bufs: [fields_info.len][max_value_len]u8 align(@alignOf(std.os.windows.UNICODE_STRING)) = undefined;
inline for (fields_info) |field, i| { inline for (fields_info) |field, i| {
const ctx: *anyopaque = blk: { const ctx: *anyopaque = blk: {
switch (@field(args, field.name).value_type) { switch (@field(args, field.name).value_type) {
@ -96,26 +99,20 @@ fn getCpuInfoFromRegistry(core: usize, args: anytype) !void {
REG.EXPAND_SZ, REG.EXPAND_SZ,
REG.MULTI_SZ, REG.MULTI_SZ,
=> { => {
var buf: [max_value_len / 2]u16 = undefined; comptime assert(@sizeOf(std.os.windows.UNICODE_STRING) % 2 == 0);
var unicode = std.os.windows.UNICODE_STRING{ const unicode = @ptrCast(*std.os.windows.UNICODE_STRING, &tmp_bufs[i]);
unicode.* = .{
.Length = 0, .Length = 0,
.MaximumLength = max_value_len, .MaximumLength = max_value_len - @sizeOf(std.os.windows.UNICODE_STRING),
.Buffer = &buf, .Buffer = @ptrCast([*]u16, tmp_bufs[i][@sizeOf(std.os.windows.UNICODE_STRING)..]),
}; };
break :blk &unicode; break :blk unicode;
}, },
REG.DWORD, REG.DWORD,
REG.DWORD_BIG_ENDIAN, REG.DWORD_BIG_ENDIAN,
=> { REG.QWORD,
var buf: [4]u8 = undefined; => break :blk &tmp_bufs[i],
break :blk &buf;
},
REG.QWORD => {
var buf: [8]u8 = undefined;
break :blk &buf;
},
else => unreachable, else => unreachable,
} }