aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/storage_analysis_test.js
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 07:28:39 +0000
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 07:28:39 +0000
commit7451eb090d890fb5ea5e4d40627a1b4df15a49de (patch)
tree357bdf4b528e860bffa688865306f656b788d2c2 /tests/drivers/storage_analysis_test.js
parentanalyzer: close the ownership gaps that stopped the compiler compiling itself (diff)
build: embed images with #embed, keep everything generated in build/
the cores take their genome in with c23 #embed through src/core/genome.h, so images are plain little-endian word files (genome.bin) instead of generated c headers. zc0 writes that format, mkgenome checks an image against its map and shows a compiler's diagnostics when it gets them, and the javascript vm and the editor's vm host read the same files. src/tools/findcc.sh picks a compiler with #embed (clang 19 or gcc 15). zc gets an embed declaration, embed "file" as [type] name, for globals and locals: a byte string by default, char*, numbers read little-endian, and arrays of numbers. the compiler and language server carry their library and source texts through it, so embed.sh and the text it spliced into compiler.zc are gone. make writes executables to build/bin, the default map to build/core/map, the web page to build/web and the extension staging to build/vsix, and the genome now rebuilds when its sources change. the test suite lays out its own map and bootstrap and reads nothing from build/. also: rt.put_s of a byte string in zc0 images, retained function sources no longer count as live objects, a missing include no longer cascades into undefined-name errors, consecutive comment lines join into one hover paragraph, and stale test fixtures follow the include format.
Diffstat (limited to 'tests/drivers/storage_analysis_test.js')
-rw-r--r--tests/drivers/storage_analysis_test.js48
1 files changed, 26 insertions, 22 deletions
diff --git a/tests/drivers/storage_analysis_test.js b/tests/drivers/storage_analysis_test.js
index 774fa89..704a844 100644
--- a/tests/drivers/storage_analysis_test.js
+++ b/tests/drivers/storage_analysis_test.js
@@ -1,8 +1,12 @@
// definite storage findings use the compiler's parser and the editor protocol.
+// interpreted hosts (no jit) are far slower; ZC_TEST_TIMEOUT_SCALE stretches every deadline
+const TIMEOUT_SCALE = Number(process.env.ZC_TEST_TIMEOUT_SCALE) || 1;
const assert = require('assert');
const cp = require('child_process');
const server = process.argv[2];
function check(text, needle) {
+ // the runtime is an explicit import; it shares the first line so line numbers stay as written
+ text = 'include rt; ' + text;
const uri = 'file:///storage-analysis.zc';
const messages = [{ jsonrpc: '2.0', id: 1, method: 'initialize', params: {} },
{ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, version: 1, text } } }];
@@ -13,7 +17,7 @@ function check(text, needle) {
}
messages.push({ jsonrpc: '2.0', method: 'exit' });
const input = Buffer.concat(messages.map(m => { const b = Buffer.from(JSON.stringify(m)); return Buffer.concat([Buffer.from('Content-Length: ' + b.length + '\r\n\r\n'), b]); }));
- const r = cp.spawnSync(server, process.argv.slice(3), { input, timeout: 20000, maxBuffer: 8 << 20 });
+ const r = cp.spawnSync(server, process.argv.slice(3), { input, timeout: 20000 * TIMEOUT_SCALE, maxBuffer: 8 << 20 });
assert.strictEqual(r.status, 0, String(r.stderr));
const replies = []; let data = r.stdout;
while (data.length) {
@@ -27,50 +31,50 @@ function check(text, needle) {
}
const prefix = 'unchecked fn main() {\n';
for (const [type, length] of [['int', 3], ['u8', 3], ['fixed int[?]', 3], ['u8[?]', 3]]) {
- const r = check(prefix + ` let p = new(${type}, ${length});\n let alias = p;\n return alias[${length}];\n}\n`, 'alias[');
+ const r = check(prefix + ` let p = rt.new(${type}, ${length});\n let alias = p;\n return alias[${length}];\n}\n`, 'alias[');
assert.strictEqual(r.warnings.length, 1, JSON.stringify(r.diagnostics));
assert.match(r.warnings[0].message, /out-of-bounds.*index 3.*length 3/);
assert.match(r.hover, /size: 3 elements.*lifetime: heap storage; live here/);
assert.strictEqual(r.warnings[0].range.start.line, 3);
}
-const negative = check(prefix + ' let p = new(3); return p[-1];\n}\n');
+const negative = check(prefix + ' let p = rt.new(3); return p[-1];\n}\n');
assert.match(negative.warnings[0]?.message || '', /index -1.*length 3/);
-const freed = check(prefix + ' let p = new(int, 3);\n let q = p;\n free(p);\n return q[0];\n}\n', 'q[');
+const freed = check(prefix + ' let p = rt.new(int, 3);\n let q = p;\n rt.free(p);\n return q[0];\n}\n', 'q[');
assert.strictEqual(freed.warnings.length, 1, JSON.stringify(freed.diagnostics));
assert.match(freed.warnings[0].message, /use after free.*line 4/);
assert.match(freed.hover, /size: 3 elements.*lifetime: ended on line 4/);
-const dereference = check(prefix + ' let p = new(int); free(p); return *p;\n}\n');
+const dereference = check(prefix + ' let p = rt.new(int); rt.free(p); return *p;\n}\n');
assert.match(dereference.warnings[0]?.message || '', /use after free/);
-const field = check('struct item { int value; };\n' + prefix + ' let p = new(item); free(p); return p.value;\n}\n');
+const field = check('struct item { int value; };\n' + prefix + ' let p = rt.new(item); rt.free(p); return p.value;\n}\n');
assert.match(field.warnings[0]?.message || '', /use after free/);
const scope = check(prefix + ' int* p;\n { let a[3]; p = a; }\n return p[0];\n}\n', 'p[');
assert.match(scope.warnings[0]?.message || '', /use after free.*line 3/);
assert.match(scope.hover, /lifetime: ended on line 3/);
const safe = [
- prefix + ' let p = new(int, 3); let q = p as u8*; return q[23]; }',
- prefix + ' let p = new(int, 3); let q = (u8*)p; return q[23]; }',
+ prefix + ' let p = rt.new(int, 3); let q = p as u8*; return q[23]; }',
+ prefix + ' let p = rt.new(int, 3); let q = (u8*)p; return q[23]; }',
prefix + ' int n = 2; let p = &n; let q = n; return q[2]; }',
- prefix + ' let p = new(int, 3); let ignored = 0 && free(p); return p[0]; }',
- prefix + ' let p = new(int, 3); let ignored = 1 || free(p); return p[0]; }',
- prefix + ' let p = new(int, 50); return p[(u8)300]; }',
- prefix + ' let p = new(int, 50); return p[300 as u8]; }',
- prefix + ' let p = new(int, 3); let q = p; q[2] = 7; free(p); return 0; }',
- prefix + ' let p = new(3); free(p); p = new(3); return p[0]; }',
- 'unchecked fn unknown(int* p) {}\n' + prefix + ' let p = new(int, 3); unknown(p); return p[9]; }',
- 'fn maybe(int n) { return n; }\n' + prefix + ' let p = new(int, 3); return p[maybe(9)]; }',
- prefix + ' let p = new(int, 3); if (1) free(p); return p[0]; }',
- prefix + ' let p = new(int, 3); while (0) free(p); return p[0]; }',
- prefix + ' let p = new(int, 3); let f = fn() { free(p); return 0; }; return p[0]; }',
- prefix + ' let p = new(int[?], 0); push(p, 9); return p[0]; }',
+ prefix + ' let p = rt.new(int, 3); let ignored = 0 && rt.free(p); return p[0]; }',
+ prefix + ' let p = rt.new(int, 3); let ignored = 1 || rt.free(p); return p[0]; }',
+ prefix + ' let p = rt.new(int, 50); return p[(u8)300]; }',
+ prefix + ' let p = rt.new(int, 50); return p[300 as u8]; }',
+ prefix + ' let p = rt.new(int, 3); let q = p; q[2] = 7; rt.free(p); return 0; }',
+ prefix + ' let p = rt.new(3); rt.free(p); p = rt.new(3); return p[0]; }',
+ 'unchecked fn unknown(int* p) {}\n' + prefix + ' let p = rt.new(int, 3); unknown(p); return p[9]; }',
+ 'fn maybe(int n) { return n; }\n' + prefix + ' let p = rt.new(int, 3); return p[maybe(9)]; }',
+ prefix + ' let p = rt.new(int, 3); if (1) rt.free(p); return p[0]; }',
+ prefix + ' let p = rt.new(int, 3); while (0) rt.free(p); return p[0]; }',
+ prefix + ' let p = rt.new(int, 3); let f = fn() { rt.free(p); return 0; }; return p[0]; }',
+ prefix + ' let p = rt.new(int[?], 0); push(p, 9); return p[0]; }',
];
for (const source of safe) assert.deepStrictEqual(check(source).warnings, [], source);
-const preserved = check('int read(int* p) { return p[0]; }\n' + prefix + ' let p = new(int, 3); read(p); return p[9]; }');
+const preserved = check('int read(int* p) { return p[0]; }\n' + prefix + ' let p = rt.new(int, 3); read(p); return p[9]; }');
assert.match(preserved.warnings[0]?.message || '', /index 9.*length 3/);
const unknown = check('int read(int* p) { return p[0]; }\nfn main() { int n = 1; return read(&n); }\n', 'p[');
assert.match(unknown.hover, /size: unknown.*lifetime: unknown/);
const stack = check('fn main() { let a[3]; return a[2]; }\n', 'a[');
assert.match(stack.hover, /size: 3 elements.*lifetime: stack storage/);
-const replacement = check('fn main() {\n let owner = new(1), replacement = new(2);\n free(owner); owner = replacement;\n return owner[0];\n}\n', 'replacement;');
+const replacement = check('fn main() {\n let owner = rt.new(1), replacement = rt.new(2);\n rt.free(owner); owner = replacement;\n return owner[0];\n}\n', 'replacement;');
assert.ok(!replacement.diagnostics.some(d => d.severity === 1), JSON.stringify(replacement.diagnostics));
assert.match(replacement.hover, /moved: replaces a released owner in `owner` on line 3/);
console.log('ok definite bounds, free and scope lifetimes, alias hovers, and conservative unknown cases');