151 lines
6.1 KiB
Python
151 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise the real constructor snapshot with a recycled native pipe address."""
|
|
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
FIXTURE = r'''
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "nohelper/pipe_admission_snapshot.h"
|
|
|
|
#define OWNER_GLOBAL ((uintptr_t)0x145e7a098ULL)
|
|
#define CONNECTION_TABLE ((uint64_t)0x1450396b0ULL)
|
|
#define PIPE_TABLE ((uint64_t)0x144e59fe8ULL)
|
|
|
|
static unsigned char owner[0x230], connection[0x30], pipe_object[24];
|
|
static uint64_t queue_owner, holder_pipe, listener[2];
|
|
static uint64_t callbacks[64][2], retired[64], original_table[10];
|
|
static uint64_t connection_methods[2] = {0x1434db6b0ULL, 0x1434db7a0ULL};
|
|
static uint64_t owner_vtable = 0x1111222233334444ULL;
|
|
static unsigned checks;
|
|
|
|
static void put64(void *at, uint64_t value) { memcpy(at, &value, 8); }
|
|
static void put32(void *at, uint32_t value) { memcpy(at, &value, 4); }
|
|
static int read_fixture(void *context, uintptr_t address, void *destination,
|
|
size_t bytes) {
|
|
(void)context;
|
|
if (address == OWNER_GLOBAL) {
|
|
if (bytes != 8) return 0;
|
|
put64(destination, (uintptr_t)owner);
|
|
} else if (address == CONNECTION_TABLE + 17 * 8) {
|
|
if (bytes != sizeof(connection_methods)) return 0;
|
|
memcpy(destination, connection_methods, bytes);
|
|
} else if (address == PIPE_TABLE - 8) {
|
|
if (bytes != sizeof(original_table)) return 0;
|
|
memcpy(destination, original_table, bytes);
|
|
} else {
|
|
memcpy(destination, (const void *)address, bytes);
|
|
}
|
|
return 1;
|
|
}
|
|
static int executable(void *context, uintptr_t address) {
|
|
(void)context;
|
|
return address == 0x1100 || address == 0x1200;
|
|
}
|
|
static int writable(void *context, uintptr_t address, size_t bytes) {
|
|
(void)context;
|
|
return address == (uintptr_t)pipe_object && bytes == 8;
|
|
}
|
|
#define CHECK(x) do { ++checks; if (!(x)) { \
|
|
fprintf(stderr, "failed at line %d: %s\n", __LINE__, #x); return 1; \
|
|
} } while (0)
|
|
|
|
int main(void) {
|
|
struct gw_pipe_control control = {.abi = 1,
|
|
.size = sizeof(struct gw_pipe_control)};
|
|
struct gw_private_pipe_tables tables = {.abi = 1,
|
|
.size = sizeof(tables), .count = 64};
|
|
struct gw_pipe_admission_output out;
|
|
struct gw_pipe_admission_input in = {
|
|
.read = read_fixture, .executable = executable, .writable = writable,
|
|
.expected_owner = (uintptr_t)owner,
|
|
.expected_bound_owner_vtable = 0x1111222233334444ULL,
|
|
.current_thread = 42, .primary_thread = 42,
|
|
.auth_state = 2, .server_proof_verified = 1,
|
|
.transport_prepared = 1, .credentials_generation = 2,
|
|
.pipe_generation = 2,
|
|
.callback_module_begin = 0x1000, .callback_module_end = 0x2000,
|
|
.current_control = &control, .current_tables = &tables,
|
|
.epoch_callbacks = callbacks, .epoch_callbacks_abi = 1,
|
|
.epoch_callbacks_size = 1040, .epoch_callbacks_count = 64,
|
|
.retired_receivers = retired, .retired_receivers_abi = 1,
|
|
.retired_receivers_size = 528, .retired_receivers_count = 64,
|
|
};
|
|
static const uint64_t methods[9] = {
|
|
0x141e4ca60ULL, 0x141e4d120ULL, 0x1401d9200ULL,
|
|
0x141e4e8e0ULL, 0x14091e6e0ULL, 0x1409e95f0ULL,
|
|
0x141e4c8f0ULL, 0x141e4c9e0ULL, 0x141e4ca20ULL,
|
|
};
|
|
memcpy(original_table + 1, methods, sizeof(methods));
|
|
callbacks[0][0] = 0x1100; callbacks[0][1] = 0x1200;
|
|
callbacks[1][0] = 0x1100; callbacks[1][1] = 0x1200;
|
|
retired[0] = (uintptr_t)pipe_object; /* Original allocator reused it. */
|
|
put64(owner, owner_vtable);
|
|
put32(owner + 0x98, 4);
|
|
put64(owner + 0xa0, (uintptr_t)&queue_owner);
|
|
put64(owner + 0x1a0, (uintptr_t)connection);
|
|
put64(owner + 0x220, (uintptr_t)listener);
|
|
put64(owner + 0x228, (uintptr_t)&holder_pipe);
|
|
queue_owner = (uintptr_t)owner;
|
|
put64(connection, CONNECTION_TABLE);
|
|
put64(connection + 0x28, (uintptr_t)listener);
|
|
listener[1] = (uintptr_t)owner;
|
|
holder_pipe = (uintptr_t)pipe_object;
|
|
put64(pipe_object, PIPE_TABLE);
|
|
put64(pipe_object + 8, (uintptr_t)owner);
|
|
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == 0);
|
|
CHECK(out.control_template.pipe == (uintptr_t)pipe_object);
|
|
CHECK(out.prevalidated.generation == 2);
|
|
CHECK(out.prevalidated.receiver_fresh);
|
|
|
|
retired[1] = (uintptr_t)pipe_object;
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == -12);
|
|
retired[1] = 0;
|
|
put32(owner + 0x98, 3);
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == -7);
|
|
put32(owner + 0x98, 4);
|
|
put64(pipe_object + 8, 0);
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == -10);
|
|
put64(pipe_object + 8, (uintptr_t)owner);
|
|
callbacks[1][0] = 0x3000;
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == -5);
|
|
callbacks[1][0] = 0x1100;
|
|
CHECK(gw_pipe_admission_snapshot(&in, &out) == 0);
|
|
printf("PASS recycled pipe address with current-epoch and live-owner guards (%u checks)\n", checks);
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
class ReusedReceiverTests(unittest.TestCase):
|
|
def test_actual_snapshot_allows_allocator_reuse(self):
|
|
compiler = shutil.which("cc") or shutil.which("gcc")
|
|
self.assertIsNotNone(compiler, "C compiler required for source fixture")
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
source = Path(directory) / "reused_pipe.c"
|
|
binary = Path(directory) / "reused_pipe.exe"
|
|
source.write_text(FIXTURE)
|
|
result = subprocess.run(
|
|
[compiler, "-std=c11", "-Wall", "-Wextra", "-Werror",
|
|
"-I" + str(ROOT / "native"), str(source),
|
|
str(ROOT / "native/nohelper/pipe_admission_snapshot.c"),
|
|
"-o", str(binary)], capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
result = subprocess.run([str(binary)], capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("PASS recycled pipe address", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|