authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-28 19:02:16+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-29 15:52:43+01:00
log202ed7330fdc55cce22bfa9d9b5da03776e871b4
treeb0ef3791a3e10ea9e2b553cf2457f0a0fc079a27
parent196ba706a05046b2209529744d2df47215819691
signaturelock-open Commit is signed but in an unrecognized format.

fix memory leaks


2 files changed, 42 insertions(+), 19 deletions(-)

src/link/Wasm.zig+8-13
......@@ -33,7 +33,6 @@ const Object = @import("Wasm/Object.zig");
3333const Symbol = @import("Wasm/Symbol.zig");
3434const Type = @import("../type.zig").Type;
3535const TypedValue = @import("../TypedValue.zig");
36const Value = @import("../value.zig").Value;
3736const ZigObject = @import("Wasm/ZigObject.zig");
3837
3938pub const Atom = @import("Wasm/Atom.zig");
......@@ -72,7 +71,7 @@ files: std.MultiArrayList(File.Entry) = .{},
7271/// to support existing code.
7372/// TODO: Allow setting this through a flag?
7473host_name: []const u8 = "env",
75/// List of all symbols generated by Zig code.
74/// List of symbols generated by the linker.
7675synthetic_symbols: std.ArrayListUnmanaged(Symbol) = .{},
7776/// Maps atoms to their segment index
7877atoms: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
......@@ -179,10 +178,6 @@ undefs: std.AutoArrayHashMapUnmanaged(u32, SymbolLoc) = .{},
179178/// Undefined (and synthetic) symbols do not have an Atom and therefore cannot be mapped.
180179symbol_atom: std.AutoHashMapUnmanaged(SymbolLoc, Atom.Index) = .{},
181180
182/// List of atom indexes of functions that are generated by the backend,
183/// rather than by the linker.
184synthetic_functions: std.ArrayListUnmanaged(Atom.Index) = .{},
185
186181pub const Alignment = types.Alignment;
187182
188183pub const Segment = struct {
......@@ -259,7 +254,7 @@ pub const InitFuncLoc = struct {
259254 /// our own ctors.
260255 file: File.Index,
261256 /// Symbol index within the corresponding object file.
262 index: u32,
257 index: Symbol.Index,
263258 /// The priority in which the constructor must be called.
264259 priority: u32,
265260
......@@ -270,7 +265,7 @@ pub const InitFuncLoc = struct {
270265
271266 /// Turns the given `InitFuncLoc` into a `SymbolLoc`
272267 fn getSymbolLoc(loc: InitFuncLoc) SymbolLoc {
273 return .{ .file = loc.file, .index = @enumFromInt(loc.index) };
268 return .{ .file = loc.file, .index = loc.index };
274269 }
275270
276271 /// Returns true when `lhs` has a higher priority (e.i. value closer to 0) than `rhs`.
......@@ -1411,9 +1406,9 @@ pub fn deinit(wasm: *Wasm) void {
14111406 archive.deinit(gpa);
14121407 }
14131408
1414 for (wasm.synthetic_functions.items) |atom_index| {
1415 const atom = wasm.getAtomPtr(atom_index);
1416 atom.deinit(gpa);
1409 if (wasm.findGlobalSymbol("__wasm_init_tls")) |loc| {
1410 const atom = wasm.symbol_atom.get(loc).?;
1411 wasm.getAtomPtr(atom).deinit(gpa);
14171412 }
14181413
14191414 wasm.synthetic_symbols.deinit(gpa);
......@@ -1441,7 +1436,7 @@ pub fn deinit(wasm: *Wasm) void {
14411436 wasm.exports.deinit(gpa);
14421437
14431438 wasm.string_table.deinit(gpa);
1444 wasm.synthetic_functions.deinit(gpa);
1439 wasm.files.deinit(gpa);
14451440}
14461441
14471442pub fn updateFunc(wasm: *Wasm, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void {
......@@ -1763,7 +1758,7 @@ fn setupInitFunctions(wasm: *Wasm) !void {
17631758 }
17641759 log.debug("appended init func '{s}'\n", .{object.string_table.get(symbol.name)});
17651760 wasm.init_funcs.appendAssumeCapacity(.{
1766 .index = init_func.symbol_index,
1761 .index = @enumFromInt(init_func.symbol_index),
17671762 .file = file_index,
17681763 .priority = init_func.priority,
17691764 });
src/link/Wasm/ZigObject.zig+34-6
......@@ -37,12 +37,16 @@ segment_free_list: std.ArrayListUnmanaged(u32) = .{},
3737string_table: StringTable = .{},
3838/// Map for storing anonymous declarations. Each anonymous decl maps to its Atom's index.
3939anon_decls: std.AutoArrayHashMapUnmanaged(InternPool.Index, Atom.Index) = .{},
40/// List of atom indexes of functions that are generated by the backend.
41synthetic_functions: std.ArrayListUnmanaged(Atom.Index) = .{},
4042/// Represents the symbol index of the error name table
4143/// When this is `null`, no code references an error using runtime `@errorName`.
4244/// During initializion, a symbol with corresponding atom will be created that is
4345/// used to perform relocations to the pointer of this table.
4446/// The actual table is populated during `flush`.
4547error_table_symbol: Symbol.Index = .null,
48/// Atom index of the table of symbol names. This is stored so we can clean up the atom.
49error_names_atom: Atom.Index = .null,
4650/// Amount of functions in the `import` sections.
4751imported_functions_count: u32 = 0,
4852/// Amount of globals in the `import` section.
......@@ -150,9 +154,6 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void {
150154 gpa.free(segment_info.name);
151155 }
152156
153 // For decls and anon decls we free the memory of its atoms.
154 // The memory of atoms parsed from object files is managed by
155 // the object file itself, and therefore we can skip those.
156157 {
157158 var it = zig_object.decls_map.valueIterator();
158159 while (it.next()) |decl_info| {
......@@ -175,6 +176,31 @@ pub fn deinit(zig_object: *ZigObject, wasm_file: *Wasm) void {
175176 atom.deinit(gpa);
176177 }
177178 }
179 if (zig_object.findGlobalSymbol("__zig_errors_len")) |sym_index| {
180 const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = sym_index }).?;
181 wasm_file.getAtomPtr(atom_index).deinit(gpa);
182 }
183 if (wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = zig_object.error_table_symbol })) |atom_index| {
184 const atom = wasm_file.getAtomPtr(atom_index);
185 atom.deinit(gpa);
186 }
187 for (zig_object.synthetic_functions.items) |atom_index| {
188 const atom = wasm_file.getAtomPtr(atom_index);
189 atom.deinit(gpa);
190 }
191 zig_object.synthetic_functions.deinit(gpa);
192 for (zig_object.func_types.items) |*ty| {
193 ty.deinit(gpa);
194 }
195 if (zig_object.error_names_atom != .null) {
196 const atom = wasm_file.getAtomPtr(zig_object.error_names_atom);
197 atom.deinit(gpa);
198 }
199 zig_object.global_syms.deinit(gpa);
200 zig_object.func_types.deinit(gpa);
201 zig_object.atom_types.deinit(gpa);
202 zig_object.functions.deinit(gpa);
203 zig_object.imports.deinit(gpa);
178204 zig_object.decls_map.deinit(gpa);
179205 zig_object.anon_decls.deinit(gpa);
180206 zig_object.symbols.deinit(gpa);
......@@ -602,7 +628,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
602628 const atom = wasm_file.getAtomPtr(atom_index);
603629
604630 const error_name = mod.intern_pool.stringToSlice(error_name_nts);
605 const len = @as(u32, @intCast(error_name.len + 1)); // names are 0-termianted
631 const len: u32 = @intCast(error_name.len + 1); // names are 0-terminated
606632
607633 const slice_ty = Type.slice_const_u8_sentinel_0;
608634 const offset = @as(u32, @intCast(atom.code.items.len));
......@@ -614,9 +640,9 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
614640 .index = @intFromEnum(names_atom.sym_index),
615641 .relocation_type = .R_WASM_MEMORY_ADDR_I32,
616642 .offset = offset,
617 .addend = @as(i32, @intCast(addend)),
643 .addend = @intCast(addend),
618644 });
619 atom.size += @as(u32, @intCast(slice_ty.abiSize(mod)));
645 atom.size += @intCast(slice_ty.abiSize(mod));
620646 addend += len;
621647
622648 // as we updated the error name table, we now store the actual name within the names atom
......@@ -627,6 +653,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
627653 log.debug("Populated error name: '{s}'", .{error_name});
628654 }
629655 names_atom.size = addend;
656 zig_object.error_names_atom = names_atom_index;
630657}
631658
632659/// Either creates a new import, or updates one if existing.
......@@ -1174,6 +1201,7 @@ pub fn createFunction(
11741201 atom.code = function_body.moveToUnmanaged();
11751202 atom.relocs = relocations.moveToUnmanaged();
11761203
1204 try zig_object.synthetic_functions.append(gpa, atom_index);
11771205 return sym_index;
11781206}
11791207