mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 00:02:21 +00:00
fix(performance): validate receivers and support worker buffer state
Route Performance methods and attributes through native WebIDL brands so invalid receivers fail before author code, with errors from the callee realm. Remove the duplicate callback-local slot checks. Share native resource-buffer configuration with worker contexts instead of requiring a page host. Preserve owner isolation and context-owned cleanup. Cover Window/iframe and Dedicated Worker receiver behavior, borrowed timing operations, registry isolation, and worker teardown. Validated with fmt, workspace Clippy, 20,250 Nextest tests, CLI probes, and 20 upstream WPT cases.
This commit is contained in:
@@ -689,10 +689,6 @@ fn performance_attribute_getter_callback<'s>(
|
||||
rv.set_undefined();
|
||||
return;
|
||||
};
|
||||
if performance_slot_number(scope, args.this(), PERFORMANCE_TIME_ORIGIN_SLOT).is_none() {
|
||||
throw_type_error(scope, "Illegal invocation");
|
||||
return;
|
||||
}
|
||||
if let Some(subobject) = super::lazy_subobjects::PerformanceSubobject::from_slot(slot) {
|
||||
match super::lazy_subobjects::ensure_performance_subobject(scope, args.this(), subobject) {
|
||||
Ok(value) => rv.set(value),
|
||||
|
||||
@@ -2429,6 +2429,7 @@ mod observer_documents;
|
||||
mod observer_receivers;
|
||||
mod offline_audio_context_interfaces;
|
||||
mod offline_audio_float;
|
||||
mod performance_receivers;
|
||||
mod periodic_wave_interfaces;
|
||||
mod popup_root_window;
|
||||
mod post_parse;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn performance_bindings_validate_receivers_before_author_code_in_windows_and_workers() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://performance-receivers.test/");
|
||||
let window = vm
|
||||
.eval(include_str!(
|
||||
"../../../tests/fixtures/performance-receivers.js"
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(window, r#"{"total":436,"failures":[]}"#);
|
||||
|
||||
vm.eval("globalThis.__performanceReceiverWorkerResult = null")
|
||||
.unwrap();
|
||||
let worker = include_str!("../../../tests/fixtures/performance-receivers-worker.js");
|
||||
vm.eval(&format!(
|
||||
"({}).then(result => __performanceReceiverWorkerResult = result, error => __performanceReceiverWorkerResult = String(error));",
|
||||
worker.trim().trim_end_matches(';')
|
||||
))
|
||||
.unwrap();
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(__performanceReceiverWorkerResult !== null)",
|
||||
"true",
|
||||
"Performance worker receiver checks",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("__performanceReceiverWorkerResult").unwrap(),
|
||||
r#"{"total":155,"failures":[]}"#
|
||||
);
|
||||
}
|
||||
@@ -80,6 +80,9 @@ async fn worker_teardown_releases_context_owned_v8_finalizers_before_isolate_dro
|
||||
new Blob([`payload-${index}`], { type: "text/plain" })
|
||||
);
|
||||
}
|
||||
performance.setResourceTimingBufferSize(0);
|
||||
performance.setResourceTimingBufferSize(2);
|
||||
performance.clearResourceTimings();
|
||||
postMessage(globalThis.__finalizerBlobs.length);
|
||||
"#
|
||||
.into(),
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
(async () => {
|
||||
const source = `postMessage((${__runPerformanceReceiverChecks.toString()})());`;
|
||||
const url = URL.createObjectURL(new Blob([source], {type: 'text/javascript'}));
|
||||
const worker = new Worker(url);
|
||||
try {
|
||||
return await new Promise((resolve, reject) => {
|
||||
worker.onmessage = event => resolve(event.data);
|
||||
worker.onerror = event => reject(new Error(event.message));
|
||||
});
|
||||
} finally {
|
||||
worker.terminate();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,121 @@
|
||||
globalThis.__runPerformanceReceiverChecks = () => {
|
||||
const rows = [];
|
||||
const assert = (condition, message) => { if (!condition) throw new Error(message); };
|
||||
const check = (name, test) => {
|
||||
try { test(); rows.push({name, passed: true}); }
|
||||
catch (error) { rows.push({name, passed: false, error: String(error)}); }
|
||||
};
|
||||
const frame = typeof document === 'undefined' ? null :
|
||||
document.body.appendChild(document.createElement('iframe'));
|
||||
const realms = [['main', globalThis]];
|
||||
if (frame) realms.push(['child', frame.contentWindow]);
|
||||
let reads = 0;
|
||||
const poison = () => { reads++; throw new Error('author code ran'); };
|
||||
const text = {toString: poison};
|
||||
const number = {valueOf: poison};
|
||||
const methods = [
|
||||
['now', [], []], ['toJSON', [], []],
|
||||
['mark', [text, {get startTime() { return poison(); }}], ['receiver-mark', {startTime: 2}]],
|
||||
['clearMarks', [text], ['receiver-mark']],
|
||||
['measure', [text, {get start() { return poison(); }}], ['receiver-measure', {start: 1, end: 3}]],
|
||||
['clearMeasures', [text], ['receiver-measure']],
|
||||
['getEntries', [], []], ['getEntriesByType', [text], ['mark']],
|
||||
['getEntriesByName', [text, text], ['receiver-mark', 'mark']],
|
||||
['clearResourceTimings', [], []], ['setResourceTimingBufferSize', [number], [250]],
|
||||
];
|
||||
const attributes = ['timeOrigin', 'onresourcetimingbufferfull'];
|
||||
if (frame) attributes.push('timing', 'navigation', 'eventCounts', 'memory');
|
||||
const invalidReceivers = realm => {
|
||||
const real = realm.performance, prototype = realm.Performance.prototype;
|
||||
const revoked = Proxy.revocable(real, {}); revoked.revoke();
|
||||
const getters = Object.fromEntries(['timeOrigin', 'timing', 'navigation'].map(name =>
|
||||
[name, {get: poison, configurable: true}]));
|
||||
return [
|
||||
['null', null], ['undefined', undefined], ['plain', {}], ['prototype', prototype],
|
||||
['inherited', Object.create(real)],
|
||||
['copied', Object.create(prototype, Object.getOwnPropertyDescriptors(real))],
|
||||
['author Proxy', new Proxy(real, {get: poison, getPrototypeOf: poison, has: poison})],
|
||||
['revoked Proxy', revoked.proxy], ['other interface', new realm.EventTarget()],
|
||||
['forged getters', Object.create(prototype, getters)],
|
||||
];
|
||||
};
|
||||
const rejects = (realm, call) => {
|
||||
reads = 0;
|
||||
let error;
|
||||
try { call(); } catch (caught) { error = caught; }
|
||||
assert(error instanceof realm.TypeError, 'must throw the binding realm TypeError');
|
||||
assert(reads === 0, 'must validate before author getters, conversion, or Proxy traps');
|
||||
};
|
||||
try {
|
||||
for (const [calleeName, realm] of realms) {
|
||||
const prototype = realm.Performance.prototype;
|
||||
const invalid = invalidReceivers(realm);
|
||||
for (const [name, poisoned, valid] of methods) {
|
||||
const fn = prototype[name];
|
||||
for (const [kind, receiver] of invalid) {
|
||||
check(`${calleeName} ${name} rejects ${kind}`, () => rejects(realm, () => fn.apply(receiver, poisoned)));
|
||||
}
|
||||
for (const [owner, window] of realms) {
|
||||
check(`${calleeName} ${name} accepts ${owner} Performance`, () => {
|
||||
const result = fn.apply(window.performance, valid);
|
||||
if (name === 'now') assert(Number.isFinite(result) && result >= 0, 'finite relative time');
|
||||
else if (name === 'toJSON') assert(result.timeOrigin === window.performance.timeOrigin, 'receiver time origin');
|
||||
else if (name.startsWith('getEntries')) assert(Array.isArray(result), 'sequence result is an Array');
|
||||
else if (name === 'mark' || name === 'measure') assert(result.name === valid[0], 'created entry name');
|
||||
else assert(result === undefined, 'void result');
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const name of attributes) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
||||
for (const [kind, receiver] of invalid) {
|
||||
check(`${calleeName} ${name} getter rejects ${kind}`, () => rejects(realm, () => descriptor.get.call(receiver)));
|
||||
if (descriptor.set) check(`${calleeName} ${name} setter rejects ${kind}`, () =>
|
||||
rejects(realm, () => descriptor.set.call(receiver, poison)));
|
||||
}
|
||||
for (const [owner, window] of realms) {
|
||||
check(`${calleeName} ${name} getter accepts ${owner} Performance`, () => {
|
||||
const result = descriptor.get.call(window.performance);
|
||||
if (name === 'memory') assert(typeof result.usedJSHeapSize === 'number', 'memory snapshot');
|
||||
else assert(result === window.performance[name], 'receiver attribute identity');
|
||||
});
|
||||
if (descriptor.set) check(`${calleeName} ${name} setter updates ${owner} Performance`, () => {
|
||||
const receiver = window.performance, saved = receiver[name];
|
||||
const handler = () => {};
|
||||
try {
|
||||
descriptor.set.call(receiver, handler);
|
||||
assert(receiver[name] === handler, 'receiver handler identity');
|
||||
} finally { descriptor.set.call(receiver, saved); }
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const [owner, window] of realms) {
|
||||
check(`${calleeName} borrowed timing operations keep ${owner} entries isolated`, () => {
|
||||
const receiver = window.performance;
|
||||
const name = `receiver-isolation-${calleeName}-${owner}`;
|
||||
try {
|
||||
const mark = prototype.mark.call(receiver, name, {startTime: 10});
|
||||
const measure = prototype.measure.call(receiver, name, {start: name, end: 20});
|
||||
const entries = prototype.getEntriesByName.call(receiver, name);
|
||||
assert(entries.length === 2 && entries[0] === mark && entries[1] === measure, 'receiver owns entries');
|
||||
assert(measure.startTime === 10 && measure.duration === 10, 'measure resolves receiver marks');
|
||||
for (const [, other] of realms) if (other !== window)
|
||||
assert(other.performance.getEntriesByName(name).length === 0, 'other timeline remains separate');
|
||||
prototype.clearMarks.call(receiver, name);
|
||||
assert(receiver.getEntriesByName(name).length === 1, 'clearMarks affects receiver');
|
||||
prototype.clearMeasures.call(receiver, name);
|
||||
assert(receiver.getEntriesByName(name).length === 0, 'clearMeasures affects receiver');
|
||||
} finally { receiver.clearMarks(name); receiver.clearMeasures(name); }
|
||||
});
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
for (const [, realm] of realms) {
|
||||
realm.performance.clearMarks('receiver-mark');
|
||||
realm.performance.clearMeasures('receiver-measure');
|
||||
}
|
||||
if (frame) frame.remove();
|
||||
}
|
||||
return JSON.stringify({total: rows.length, failures: rows.filter(row => !row.passed)});
|
||||
};
|
||||
__runPerformanceReceiverChecks();
|
||||
Reference in New Issue
Block a user