Skip to content

Commit

Permalink
fuzzy_cvtres: Prepare for /FOLDDUPS testing in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Nov 5, 2024
1 parent 6d1ea48 commit 125b2cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 19 additions & 2 deletions test/fuzzy_cvtres.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ test "cvtres fuzz" {
const tmp_path = try tmp.dir.realpathAlloc(allocator, ".");
defer allocator.free(tmp_path);

var data_buffer = std.ArrayList(u8).init(allocator);
defer data_buffer.deinit();

var res_buffer = std.ArrayList(u8).init(allocator);
defer res_buffer.deinit();

Expand All @@ -31,16 +34,30 @@ test "cvtres fuzz" {
defer common_type.deinit(allocator);
const common_lang: resinator.res.Language = @bitCast(rand.int(u16));

data_buffer.clearRetainingCapacity();
const data_size = rand.uintAtMostBiased(u32, 150);
try data_buffer.resize(data_size);
rand.bytes(data_buffer.items);
const common_data = data_buffer.items;

const num_resources = rand.intRangeAtMost(usize, 1, 16);
for (0..num_resources) |_| {
const options: utils.RandomResourceOptions = switch (rand.uintAtMost(u8, 4)) {
for (0..num_resources) |resource_i| {
var options: utils.RandomResourceOptions = switch (rand.uintAtMost(u8, 4)) {
0 => .{ .set_name = common_name },
1 => .{ .set_type = common_type },
2 => .{ .set_language = common_lang },
3 => .{ .set_name = common_name, .set_type = common_type },
4 => .{},
else => unreachable,
};
// Randomly set data to be a duplicate, but don't make the first resource have
// the duplicate data to avoid what seems like a miscompilation when /FOLDDUPS is set.
// If the first resource has the same data as another resource, the first
// data is written but is unreferenced, and then the same data is written again
// and that second data location is actually used for all duplicates of that data.
if (resource_i > 0 and rand.float(f32) < 0.20) {
options.set_data = common_data;
}
try utils.writeRandomValidResource(allocator, rand, res_buffer.writer(), options);
}

Expand Down
16 changes: 11 additions & 5 deletions test/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ pub fn getResinatorCvtResResult(allocator: Allocator, res_source: []const u8, op
resinator.cvtres.writeCoff(allocator, buf.writer(), resources, .{
.target = options.target,
}) catch |err| {
buf.deinit();
return .{
.coff_err = err,
};
Expand Down Expand Up @@ -816,10 +817,11 @@ pub const RandomResourceOptions = struct {
set_name: ?resinator.res.NameOrOrdinal = null,
set_type: ?resinator.res.NameOrOrdinal = null,
set_language: ?resinator.res.Language = null,
set_data: ?[]const u8 = null,
};

pub fn writeRandomValidResource(allocator: Allocator, rand: std.Random, writer: anytype, options: RandomResourceOptions) !void {
const data_size = rand.uintAtMostBiased(u32, 150);
const data_size: u32 = if (options.set_data) |data| @intCast(data.len) else rand.uintAtMostBiased(u32, 150);
const name_value = options.set_name orelse try getRandomNameOrOrdinal(allocator, rand, 32);
defer if (options.set_name == null) name_value.deinit(allocator);
const type_value = options.set_type orelse try getRandomNameOrOrdinal(allocator, rand, 32);
Expand All @@ -839,11 +841,15 @@ pub fn writeRandomValidResource(allocator: Allocator, rand: std.Random, writer:
const size_info = header.calcSize() catch unreachable;
try header.writeSizeInfo(writer, size_info);

const data = try allocator.alloc(u8, data_size);
defer allocator.free(data);
if (options.set_data) |data| {
try writer.writeAll(data);
} else {
const data = try allocator.alloc(u8, data_size);
defer allocator.free(data);

rand.bytes(data);
try writer.writeAll(data);
rand.bytes(data);
try writer.writeAll(data);
}
const num_padding_bytes = resinator.compile.Compiler.numPaddingBytesNeeded(data_size);
try writer.writeByteNTimes(0, num_padding_bytes);
}
Expand Down

0 comments on commit 125b2cb

Please sign in to comment.