Skip to content

Commit

Permalink
fuzzy_cvtres: Fix incomplete avoidance of /FOLDDUPS miscompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Nov 25, 2024
1 parent abe2021 commit 06a086b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 12 additions & 5 deletions test/fuzzy_cvtres.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ test "cvtres fuzz" {
const common_data = data_buffer.items;

const num_resources = rand.intRangeAtMost(usize, 1, 16);
for (0..num_resources) |resource_i| {
var have_written_a_non_zero_data_resource = false;
for (0..num_resources) |_| {
var options: utils.RandomResourceOptions = switch (rand.uintAtMost(u8, 4)) {
0 => .{ .set_name = common_name },
1 => .{ .set_type = common_type },
Expand All @@ -55,10 +56,11 @@ test "cvtres fuzz" {
// 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) {
if (have_written_a_non_zero_data_resource and rand.float(f32) < 0.20) {
options.set_data = common_data;
}
try utils.writeRandomValidResource(allocator, rand, res_buffer.writer(), options);
const written_data_size = try utils.writeRandomValidResource(allocator, rand, res_buffer.writer(), options);
if (written_data_size != 0) have_written_a_non_zero_data_resource = true;
}

// also write it to the top-level tmp dir for debugging
Expand All @@ -83,13 +85,18 @@ test "cvtres fuzz" {
else => unreachable,
};

try utils.expectSameCvtResOutput(allocator, res_buffer.items, .{
const options: utils.GetCvtResResultOptions = .{
.cwd = tmp.dir,
.cwd_path = tmp_path,
.target = random_target,
.read_only = rand.boolean(),
.define_external_symbol = random_symbol_define,
.fold_duplicate_data = rand.boolean(),
});
};

utils.expectSameCvtResOutput(allocator, res_buffer.items, options) catch |err| {
std.debug.print("options: {}\n", .{options});
return err;
};
}
}
5 changes: 4 additions & 1 deletion test/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ pub const RandomResourceOptions = struct {
set_data: ?[]const u8 = null,
};

pub fn writeRandomValidResource(allocator: Allocator, rand: std.Random, writer: anytype, options: RandomResourceOptions) !void {
/// Returns the data size of the resource that was written
pub fn writeRandomValidResource(allocator: Allocator, rand: std.Random, writer: anytype, options: RandomResourceOptions) !u32 {
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);
Expand Down Expand Up @@ -881,6 +882,8 @@ pub fn writeRandomValidResource(allocator: Allocator, rand: std.Random, writer:
}
const num_padding_bytes = resinator.compile.Compiler.numPaddingBytesNeeded(data_size);
try writer.writeByteNTimes(0, num_padding_bytes);

return data_size;
}

pub fn getRandomNameOrOrdinal(allocator: Allocator, rand: std.Random, max_name_len: usize) !resinator.res.NameOrOrdinal {
Expand Down

0 comments on commit 06a086b

Please sign in to comment.