authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-18 15:10:50-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
log85b53730fe248cf02468a213903ddac4bffe8234
tree22f04c8edeed5562a6dce6383fdf55c846f69317
parent2d899e9a9f0864385939955902e0ba5f322176fc

add safety for calling functions that get virtual addrs


3 files changed, 20 insertions(+), 8 deletions(-)

src/arch/wasm/Emit.zig+6-5
......@@ -108,7 +108,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
108108 inst += 1;
109109 continue :loop tags[inst];
110110 } else {
111 const addr = try wasm.errorNameTableAddr();
111 const addr: u32 = wasm.errorNameTableAddr();
112112 leb.writeIleb128(code.fixedWriter(), addr) catch unreachable;
113113
114114 inst += 1;
......@@ -931,7 +931,7 @@ fn uavRefOffExe(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.UavRef
931931 try code.ensureUnusedCapacity(gpa, 11);
932932 code.appendAssumeCapacity(@intFromEnum(opcode));
933933
934 const addr = try wasm.uavAddr(data.uav_exe);
934 const addr = wasm.uavAddr(data.uav_exe);
935935 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + data.offset))) catch unreachable;
936936}
937937
......@@ -957,8 +957,9 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff
957957 });
958958 code.appendNTimesAssumeCapacity(0, 5);
959959 } else {
960 const addr = try wasm.navAddr(data.nav_index);
961 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + data.offset))) catch unreachable;
960 const function_imports_len: u32 = @intCast(wasm.function_imports.entries.len);
961 const func_index = Wasm.FunctionIndex.fromIpNav(wasm, data.nav_index).?;
962 leb.writeUleb128(code.fixedWriter(), function_imports_len + @intFromEnum(func_index)) catch unreachable;
962963 }
963964 } else {
964965 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
......@@ -972,7 +973,7 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff
972973 });
973974 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
974975 } else {
975 const addr = try wasm.navAddr(data.nav_index);
976 const addr = wasm.navAddr(data.nav_index);
976977 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + data.offset))) catch unreachable;
977978 }
978979 }
src/link/Wasm.zig+9-3
......@@ -3331,21 +3331,27 @@ pub fn refUavExe(wasm: *Wasm, pt: Zcu.PerThread, ip_index: InternPool.Index) !Ua
33313331 return uav_index;
33323332}
33333333
3334pub fn uavAddr(wasm: *Wasm, uav_index: UavsExeIndex) Allocator.Error!u32 {
3334/// Asserts it is called after `Wasm.data_segments` is fully populated and sorted.
3335pub fn uavAddr(wasm: *Wasm, uav_index: UavsExeIndex) u32 {
3336 assert(wasm.flush_buffer.memory_layout_finished);
33353337 const comp = wasm.base.comp;
33363338 assert(comp.config.output_mode != .Obj);
33373339 const ds_id: DataSegment.Id = .pack(wasm, .{ .uav_exe = uav_index });
33383340 return wasm.data_segments.get(ds_id).?;
33393341}
33403342
3341pub fn navAddr(wasm: *Wasm, nav_index: InternPool.Nav.Index) Allocator.Error!u32 {
3343/// Asserts it is called after `Wasm.data_segments` is fully populated and sorted.
3344pub fn navAddr(wasm: *Wasm, nav_index: InternPool.Nav.Index) u32 {
3345 assert(wasm.flush_buffer.memory_layout_finished);
33423346 const comp = wasm.base.comp;
33433347 assert(comp.config.output_mode != .Obj);
33443348 const ds_id: DataSegment.Id = .pack(wasm, .{ .nav_exe = @enumFromInt(wasm.navs_exe.getIndex(nav_index).?) });
33453349 return wasm.data_segments.get(ds_id).?;
33463350}
33473351
3348pub fn errorNameTableAddr(wasm: *Wasm) Allocator.Error!u32 {
3352/// Asserts it is called after `Wasm.data_segments` is fully populated and sorted.
3353pub fn errorNameTableAddr(wasm: *Wasm) u32 {
3354 assert(wasm.flush_buffer.memory_layout_finished);
33493355 const comp = wasm.base.comp;
33503356 assert(comp.config.output_mode != .Obj);
33513357 return wasm.data_segments.get(.__zig_error_name_table).?;
src/link/Wasm/Flush.zig+5
......@@ -28,10 +28,14 @@ missing_exports: std.AutoArrayHashMapUnmanaged(String, void) = .empty,
2828
2929indirect_function_table: std.AutoArrayHashMapUnmanaged(Wasm.OutputFunctionIndex, u32) = .empty,
3030
31/// For debug purposes only.
32memory_layout_finished: bool = false,
33
3134pub fn clear(f: *Flush) void {
3235 f.binary_bytes.clearRetainingCapacity();
3336 f.data_segment_groups.clearRetainingCapacity();
3437 f.indirect_function_table.clearRetainingCapacity();
38 f.memory_layout_finished = false;
3539}
3640
3741pub fn deinit(f: *Flush, gpa: Allocator) void {
......@@ -348,6 +352,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
348352 if (shared_memory) wasm.memories.limits.flags.is_shared = true;
349353 log.debug("maximum memory pages: {?d}", .{wasm.memories.limits.max});
350354 }
355 f.memory_layout_finished = true;
351356
352357 var section_index: u32 = 0;
353358 // Index of the code section. Used to tell relocation table where the section lives.