Skip to content

Commit 0bf44c3

Browse files
jacobly0andrewrk
authored andcommitted
x86_64: fix @errorName data
The final offset was clobbering the first error name, which is revealed by an out of bounds when the global error set is empty. Closes #22362
1 parent 137787e commit 0bf44c3

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/codegen.zig

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,23 @@ pub fn generateLazySymbol(
148148
if (lazy_sym.ty == .anyerror_type) {
149149
alignment.* = .@"4";
150150
const err_names = ip.global_error_set.getNamesFromMainThread();
151-
mem.writeInt(u32, try code.addManyAsArray(4), @intCast(err_names.len), endian);
152-
var offset = code.items.len;
153-
try code.resize((err_names.len + 1) * 4);
151+
var offset_index: u32 = @intCast(code.items.len);
152+
var string_index: u32 = @intCast(4 * (1 + err_names.len + @intFromBool(err_names.len > 0)));
153+
try code.resize(offset_index + string_index);
154+
mem.writeInt(u32, code.items[offset_index..][0..4], @intCast(err_names.len), endian);
155+
if (err_names.len == 0) return .ok;
156+
offset_index += 4;
154157
for (err_names) |err_name_nts| {
155158
const err_name = err_name_nts.toSlice(ip);
156-
mem.writeInt(u32, code.items[offset..][0..4], @intCast(code.items.len), endian);
157-
offset += 4;
159+
mem.writeInt(u32, code.items[offset_index..][0..4], string_index, endian);
160+
offset_index += 4;
158161
try code.ensureUnusedCapacity(err_name.len + 1);
159162
code.appendSliceAssumeCapacity(err_name);
160163
code.appendAssumeCapacity(0);
164+
string_index += @intCast(err_name.len + 1);
161165
}
162-
mem.writeInt(u32, code.items[offset..][0..4], @intCast(code.items.len), endian);
163-
return Result.ok;
166+
mem.writeInt(u32, code.items[offset_index..][0..4], string_index, endian);
167+
return .ok;
164168
} else if (Type.fromInterned(lazy_sym.ty).zigTypeTag(pt.zcu) == .@"enum") {
165169
alignment.* = .@"1";
166170
const enum_ty = Type.fromInterned(lazy_sym.ty);
@@ -171,8 +175,8 @@ pub fn generateLazySymbol(
171175
code.appendSliceAssumeCapacity(tag_name);
172176
code.appendAssumeCapacity(0);
173177
}
174-
return Result.ok;
175-
} else return .{ .fail = try ErrorMsg.create(
178+
return .ok;
179+
} else return .{ .fail = try .create(
176180
gpa,
177181
src_loc,
178182
"TODO implement generateLazySymbol for {s} {}",

test/standalone/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@
180180
.run_output_caching = .{
181181
.path = "run_output_caching",
182182
},
183+
.empty_global_error_set = .{
184+
.path = "empty_global_error_set",
185+
},
183186
},
184187
.paths = .{
185188
"build.zig",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
4+
pub fn build(b: *std.Build) void {
5+
const test_step = b.step("test", "Test it");
6+
b.default_step = test_step;
7+
8+
for ([_]bool{ false, true }) |use_llvm| {
9+
const main = b.addObject(.{
10+
.name = "main",
11+
.root_module = b.createModule(.{
12+
.root_source_file = b.path("main.zig"),
13+
.target = b.resolveTargetQuery(.{
14+
.cpu_arch = .x86_64,
15+
.os_tag = .linux,
16+
}),
17+
}),
18+
.use_llvm = use_llvm,
19+
.use_lld = use_llvm,
20+
});
21+
_ = main.getEmittedBin();
22+
test_step.dependOn(&main.step);
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn errorName(err: anyerror) [:0]const u8 {
2+
return @errorName(err);
3+
}
4+
export const error_name: *const anyopaque = &errorName;

0 commit comments

Comments
 (0)