Skip to content

Lightpanda: the headless browser designed for AI and automation

crash: fix double free in indexeddb

Karl Seguin482ea042921f
diff --git a/src/browser/tests/indexeddb.html b/src/browser/tests/indexeddb.html
index b95640f9f..105597832 100644
--- a/src/browser/tests/indexeddb.html
+++ b/src/browser/tests/indexeddb.html
@@ -1520,4 +1520,26 @@
});
}
</script>
+
+<!-- A popup that closes itself from inside an IndexedDB success handler. The
+ close detaches its context from the engine's connection gate while the
+ delete task is still on the stack; receiving the message proves neither
+ the popup nor its in-flight task was freed twice. -->
+<script id=popup_self_close type=module>
+{
+ const state = await testing.async();
+ window.addEventListener('message', (e) => {
+ if (e.data && e.data.from === 'popup_idb_self_close') {
+ state.resolve(e.data);
+ }
+ }, { once: true });
+
+ const w = window.open(testing.BASE_URL + 'window/support/popup_idb_self_close.html');
+ testing.expectTrue(w != null);
+
+ await state.done((data) => {
+ testing.expectEqual('popup_idb_self_close', data.from);
+ });
+}
+</script>
</body>
diff --git a/src/browser/tests/window/support/popup_idb_self_close.html b/src/browser/tests/window/support/popup_idb_self_close.html
new file mode 100644
index 000000000..e53b279a5
--- /dev/null
+++ b/src/browser/tests/window/support/popup_idb_self_close.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<script>
+ // deleteDatabase's success handler runs on the delete task's stack, and
+ // close() detaches this frame's context from the IndexedDB connection gate.
+ // Regression: the detach freed the in-flight task, which then freed itself
+ // again on its way out of the gate.
+ const req = indexedDB.deleteDatabase('popup-self-close-db');
+ req.onsuccess = () => {
+ window.opener.postMessage({ from: 'popup_idb_self_close' }, '*');
+ window.close();
+ };
+</script>
diff --git a/src/browser/webapi/storage/idb/IDBFactory.zig b/src/browser/webapi/storage/idb/IDBFactory.zig
index 8f96362e3..0c49df116 100644
--- a/src/browser/webapi/storage/idb/IDBFactory.zig
+++ b/src/browser/webapi/storage/idb/IDBFactory.zig
@@ -62,6 +62,12 @@ pub fn open(_: *IDBFactory, name: []const u8, version: ?u64, exec: *Execution) !
return request;
}
+const State = enum {
+ scheduled, // The scheduler's finalizer
+ parked, // cancelParked
+ running, // on the stack, will finalize when done
+};
+
const OpenContext = struct {
request: *IDBRequest,
name: []const u8,
@@ -69,9 +75,7 @@ const OpenContext = struct {
exec: *Execution,
// Our node in the engine's connection gate wait-list. See Engine.acquireGate.
_gate_waiter: Engine.GateWaiter,
- // Whether a scheduler task currently points at us; its finalizer owns our
- // destruction then. When parked on the gate instead, cancelParked owns it.
- _scheduled: bool = true,
+ _state: State = .scheduled,
// If an callback queued more requests, we need to process those requests
// on the next tick, and thus need to hold onto the transaction (which pins
@@ -90,9 +94,8 @@ const OpenContext = struct {
self.exec._factory.destroy(self);
}
- // Engine.detach cancel: our context is going away while we sit on the
- // gate. When parked there's no scheduler task, so we own our destruction;
- // in the wake->run window the task finalizer does.
+ // Engine.detach cancel: our context is going away. Only a parked context
+ // owns its destruction here; otherwise a task finalizer or `run` does.
fn cancelParked(waiter: *Engine.GateWaiter) void {
const self: *OpenContext = @fieldParentPtr("_gate_waiter", waiter);
if (self._upgrade) |txn| {
@@ -103,14 +106,14 @@ const OpenContext = struct {
txn._settled = true;
return;
}
- if (!self._scheduled) {
+ if (self._state == .parked) {
self.exec._factory.destroy(self);
}
}
fn run(ctx: *anyopaque) !?u32 {
const self: *OpenContext = @ptrCast(@alignCast(ctx));
- self._scheduled = false;
+ self._state = .running;
if (self._upgrade != null) {
return self.drainUpgrade();
@@ -127,7 +130,8 @@ const OpenContext = struct {
// connection, so it must serialize with other transactions/opens. Park
// on the gate if it's held; wakeUp re-runs us when it's handed over.
if (!engine.acquireGate(&self._gate_waiter)) {
- return null; // parked; not destroyed
+ self._state = .parked;
+ return null; // not destroyed
}
const upgrading = self.runOpen(engine) catch |err| blk: {
@@ -137,7 +141,7 @@ const OpenContext = struct {
break :blk false;
};
if (upgrading) {
- self._scheduled = true;
+ self._state = .scheduled;
return 1; // the versionchange drain continues next turn; keep the gate
}
@@ -155,7 +159,7 @@ const OpenContext = struct {
if (txn.settleStep(self.exec) or txn.abortDeliveryPending()) {
// either settle succeeded, and we have more batches to deliver, or
// abortDeliveryPending succeeded and we have an abort event.
- self._scheduled = true;
+ self._state = .scheduled;
return 1;
}
@@ -191,6 +195,7 @@ const OpenContext = struct {
// Scheduler wake-up: the connection gate was handed to us, so re-run.
fn wakeUp(waiter: *Engine.GateWaiter) void {
const self: *OpenContext = @fieldParentPtr("_gate_waiter", waiter);
+ self._state = .scheduled;
self.exec.js.scheduler.add(self, run, 0, .{
.name = "IDBFactory.open",
.finalizer = cancelled,
@@ -201,7 +206,6 @@ const OpenContext = struct {
if (self.resolveEngine()) |engine| _ = engine.releaseGate(&self._gate_waiter) else |_| {}
self.exec._factory.destroy(self);
};
- self._scheduled = true;
}
fn resolveEngine(self: *OpenContext) !*Engine {
@@ -319,8 +323,8 @@ const DeleteContext = struct {
name: []const u8,
exec: *Execution,
_gate_waiter: Engine.GateWaiter,
- // See OpenContext._scheduled.
- _scheduled: bool = true,
+ // See OpenContext._state.
+ _state: State = .scheduled,
fn cancelled(ctx: *anyopaque) void {
// What if we're gated? Well, A scheduled task is only canceled on
@@ -332,14 +336,14 @@ const DeleteContext = struct {
// See OpenContext.cancelParked.
fn cancelParked(waiter: *Engine.GateWaiter) void {
const self: *DeleteContext = @fieldParentPtr("_gate_waiter", waiter);
- if (!self._scheduled) {
+ if (self._state == .parked) {
self.exec._factory.destroy(self);
}
}
fn run(ctx: *anyopaque) !?u32 {
const self: *DeleteContext = @ptrCast(@alignCast(ctx));
- self._scheduled = false;
+ self._state = .running;
const engine = self.resolveEngine() catch |err| {
self.exec._factory.destroy(self);
@@ -349,7 +353,8 @@ const DeleteContext = struct {
};
if (!engine.acquireGate(&self._gate_waiter)) {
- return null; // parked; not destroyed
+ self._state = .parked;
+ return null; // not destroyed
}
defer self.exec._factory.destroy(self);
defer _ = engine.releaseGate(&self._gate_waiter);
@@ -365,6 +370,7 @@ const DeleteContext = struct {
// Scheduler wake-up: the connection gate was handed to us, so re-run.
fn wakeUp(waiter: *Engine.GateWaiter) void {
const self: *DeleteContext = @fieldParentPtr("_gate_waiter", waiter);
+ self._state = .scheduled;
self.exec.js.scheduler.add(self, run, 0, .{
.name = "IDBFactory.deleteDatabase",
.finalizer = cancelled,
@@ -375,7 +381,6 @@ const DeleteContext = struct {
if (self.resolveEngine()) |engine| _ = engine.releaseGate(&self._gate_waiter) else |_| {}
self.exec._factory.destroy(self);
};
- self._scheduled = true;
}
fn resolveEngine(self: *DeleteContext) !*Engine {