Skip to content

Commit

Permalink
[nfc] Add an inspector test case which expresses a null pointer deref…
Browse files Browse the repository at this point in the history
…erence

This is an attempt to reproduce #2564.

The test doesn't fail as I expect, but it's still useful, so if it passes on all CI, I'm going to leave it uncommented.
  • Loading branch information
harrishancock committed Aug 20, 2024
1 parent 249464f commit dffb76e
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/workerd/server/tests/inspector/driver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import assert from "node:assert";
import CDP from "chrome-remote-interface";
import { WorkerdServerHarness } from "@workerd/test/server-harness.mjs";

// Globals that are reset for each test.
// Global that is reset for each test.
let workerd;
let inspectorClient;

assert(env.WORKERD_BINARY !== undefined, "You must set the WORKERD_BINARY environment variable.");
assert(env.WORKERD_CONFIG !== undefined, "You must set the WORKERD_CONFIG environment variable.");

// Start workerd and connect to its inspector port with our CDP library.
// Start workerd.
beforeEach(async () => {
workerd = new WorkerdServerHarness({
workerdBinary: env.WORKERD_BINARY,
Expand All @@ -22,9 +21,18 @@ beforeEach(async () => {
});

await workerd.start();
});

inspectorClient = await CDP({
port: await workerd.getListenInspectorPort(),
// Stop workerd.
afterEach(async () => {
const [code, signal] = await workerd.stop();
assert(code === 0 || signal === "SIGTERM");
workerd = null;
});

async function connectInspector(port) {
return await CDP({
port,

// Hard-coded to match a service name expected in the `workerdConfig` file.
target: "/main",
Expand All @@ -33,19 +41,27 @@ beforeEach(async () => {
// implement the inspector protocol message in question.
local: true,
});
});
}

// Stop both our CDP client and workerd.
afterEach(async () => {
await inspectorClient.close();
inspectorClient = null;
// TODO(soon): This test reproduces a null pointer dereference in workerd (possibly the same issue
// as https://github.com/cloudflare/workerd/issues/2564), but the test doesn't fail. :(
test("Can repeatedly connect and disconnect to the inspector port", async () => {
for (let i = 0; i < 5; ++i) {
let inspectorClient = await connectInspector(await workerd.getListenInspectorPort());

const [code, signal] = await workerd.stop();
assert(code === 0 || signal === "SIGTERM");
workerd = null;
// Drive the worker with a test request.
let httpPort = await workerd.getListenPort("http");
const response = await fetch(`http://localhost:${httpPort}`);
let body = await response.arrayBuffer();
console.log(body);

await inspectorClient.close();
}
});

test("Profiler mostly sees deriveBits() frames", async () => {
let inspectorClient = await connectInspector(await workerd.getListenInspectorPort());

// Enable and start profiling.
await inspectorClient.Profiler.enable();
await inspectorClient.Profiler.start();
Expand Down Expand Up @@ -86,4 +102,6 @@ test("Profiler mostly sees deriveBits() frames", async () => {
// the most frequently sampled function.
assert.equal(max.name, "deriveBits");
assert.notEqual(max.count, 0);

await inspectorClient.close();
});

0 comments on commit dffb76e

Please sign in to comment.