File size: 4,044 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node

import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import assert from "node:assert/strict";
import os from "node:os";
import { spawn, spawnSync } from "node:child_process";

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

const DEBUG = process.env.DEBUG || false;
// Maximum number of gdb processes to spawn in parallel
const MAX_PARALLEL_PROCS = +process.env.MAX_PARALLEL_PROCS || 32;
// Default to true for now. It's slower, but async execution occasionally gets stuck
const SYNC_GDB_EXECUTION = process.env.SYNC_GDB_EXECUTION || true;

// Usage: console.log(CYAN_FMT, "This shows up in cyan!")
const CYAN_FMT = "\x1b[36m%s\x1b[0m";
const YELLOW_FMT = "\x1b[33m%s\x1b[0m";

const TEST_DIR = __dirname + "/";
const BUILD_DIR = path.join(TEST_DIR, "build");

const GDB_DEFAULT_ARGS = [
    "-batch",
    "--eval-command=set disable-randomization off", // allow execution on docker
    `--command=${TEST_DIR}gdb-extract-def`,
    // Set a breakpoint "in the future", which all the test binaries can then share
    "--eval-command=set breakpoint pending on",
    "--eval-command=break loop",
    "--eval-command=catch signal SIGFPE",
    "--eval-command=catch signal SIGILL",
    "--eval-command=catch signal SIGSEGV",
    "--eval-command=catch signal SIGBUS",
];

/* Split up an array into semi-evenly sized chunks */
function chunk(source, num_chunks)
{
    const arr = source.slice();
    const ret = [];

    let rem_chunks = num_chunks;
    while(rem_chunks > 0)
    {
        // We guarantee that the entire array is processed because when rem_chunk=1 -> len/1 = len
        ret.push(arr.splice(0, Math.floor(arr.length / rem_chunks)));
        rem_chunks--;
    }
    return ret;
}
assert(
    JSON.stringify(chunk("0 0 1 1 2 2 2 3 3 3".split(" "), 4)) ===
        JSON.stringify([["0", "0"],
                        ["1", "1"],
                        ["2", "2", "2"],
                        ["3", "3", "3"]]),
    "Chunk"
);

const dir_files = fs.readdirSync(BUILD_DIR);
const test_files = dir_files.filter(name => {
    return name.endsWith(".img");
}).map(name => {
    return name.slice(0, -4);
}).filter(name => {
    const bin_file = path.join(BUILD_DIR, `${name}.img`);
    const fixture_file = path.join(BUILD_DIR, `${name}.fixture`);
    if(!fs.existsSync(fixture_file))
    {
        return true;
    }
    return fs.statSync(bin_file).mtime > fs.statSync(fixture_file).mtime;
});

const nr_of_cpus = Math.min(
    os.cpus().length || 1,
    test_files.length,
    MAX_PARALLEL_PROCS
);

if(SYNC_GDB_EXECUTION)
{
    console.log("[+] Generating %d fixtures", test_files.length);
}
else
{
    console.log("[+] Using %d cpus to generate %d fixtures", nr_of_cpus, test_files.length);
}

const workloads = chunk(test_files, nr_of_cpus);

function test_arg_formatter(workload)
{
    return workload.map(test => {
        const test_path = path.join(BUILD_DIR, test);
        return `--eval-command=extract-state ${test_path}.img ${test_path}.fixture`;
    });
}

function set_proc_handlers(proc, n)
{
    proc.on("close", (code) => on_proc_close(code, n));

    if(DEBUG)
    {
        proc.stdout.on("data", (data) => {
            console.log(CYAN_FMT, "stdout", `${n}: ${data}`);
        });

        proc.stderr.on("data", (data) => {
            console.log(YELLOW_FMT, "stderr", `${n}: ${data}`);
        });
    }
}

function on_proc_close(code, n)
{
    console.log(`[+] child process ${n} exited with code ${code}`);
    if(code !== 0)
    {
        process.exit(code);
    }
}

for(let i = 0; i < nr_of_cpus; i++)
{
    const gdb_args = GDB_DEFAULT_ARGS.concat(test_arg_formatter(workloads[i]));

    if(DEBUG)
    {
        console.log(CYAN_FMT, "[DEBUG]", "gdb", gdb_args.join(" "));
    }

    if(SYNC_GDB_EXECUTION || nr_of_cpus === 1)
    {
        const { status: code } = spawnSync("gdb", gdb_args);
        on_proc_close(code, i);
    }
    else
    {
        const gdb = spawn("gdb", gdb_args);
        set_proc_handlers(gdb, i);
    }
}