Skip to content

Commit e40c66e

Browse files
own: <any>undefined / <any>null in an object literal's field is moved in, not released twice (#978)
own_facts::isConstantData took null or undefined seen through its casts for data nothing owns - a box into `any` included - so checkValueMoves returned before checking the store of `{ t: <any>undefined }`'s box into the object as a move: the store took the box and the temporary's release gave it back too, a double free that wasm's checked heap and Linux caught (Windows did not). A cast into `any` or `unknown` makes a block of its own. Test 00any_undefined_field.ts (compile, JIT, rc, own); TSLANG 153. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2eaeab7 commit e40c66e

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎TSLANG_ISSUES.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,12 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT,
21862186
declares them optional (`return?`, `throw?`: a generator declared otherwise has neither) once a compiler with
21872187
them is released. Under -mm=own a call through an optional one is read as moving the generator, so there a
21882188
program declaring them required calls them.
2189+
- -mm=own: an object literal's field of `<any>undefined` or `<any>null` (`{ t: <any>undefined, n: 1 }`) was
2190+
released twice: own_facts::isConstantData took null or undefined seen through its casts for data nothing owns,
2191+
a box into `any` included - so the store of the box into the object was not checked as a move, and the
2192+
temporary's release gave the box back as well as the object's. wasm's heap caught it ("free of ...: not a block
2193+
the heap gave out"), and Linux under -mm=own; Windows did not. A cast into `any` or `unknown` is a block of its
2194+
own. (The generator object's `.thrown` holds `<any>0`, from before this was found.) Test `00any_undefined_field.ts`.
21892195
- Found on the way, generators and `try` (all memory models):
21902196
- A throw or a call after a resumed yield inside a `try` went past it - its catch did not catch, its finally did
21912197
not run (`try { yield 1; throw "x"; } catch (e) {...}`: "uncaught exception"). A yield splits the body at its
@@ -2202,11 +2208,6 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT,
22022208
(it crashed in LLVM's instruction selection; before the fixes above the catch did not catch).
22032209
22042210
Still open, looked at:
2205-
- -mm=own: an object literal's field of `<any>undefined` or `<any>null` (`{ t: <any>undefined, n: 1 }`) is
2206-
released twice: OwnershipInferencePass does not count the store of its box into the object as a move (a box of
2207-
`<any>5` it does), so the release at the end of the function stays and the object's release frees the box again.
2208-
wasm's heap caught it ("free of ...: not a block the heap gave out"), and Linux under -mm=own; Windows did not.
2209-
The generator object's `.thrown` holds `<any>0` for that reason (its first version was `<any>undefined`).
22102211
- A yield in a finally block is skipped (`try { yield 1; } finally { yield 2; }` gives 1 and not 2), as before.
22112212
- -mm=own: next(value) of a generator sending it an object (`Generator<T, void, Box>`) is refused ("'this value'
22122213
is a parameter, kept here, but -mm=own cannot make the callers of this function expression give it up"): next()

‎tslang/lib/TypeScript/OwnershipFacts.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,10 +1934,18 @@ inline bool isConstantData(mlir::Value value)
19341934
}
19351935

19361936
// null or undefined seen as what may hold a block (`on() ? mk(3) : null` as a `Node | null`):
1937-
// it holds none
1937+
// it holds none - but not one boxed into `any` (or `unknown`) on the way, a block of its own which
1938+
// its release frees: taken for constant data, `{ t: <any>undefined }` was not checked for a move,
1939+
// so the store into the object took the box and the temporary's release gave it back as well (a
1940+
// double free, which wasm's checked heap and Linux caught; TSLANG 153)
19381941
auto root = value;
19391942
while (auto castOp = root.getDefiningOp<mlir_ts::CastOp>())
19401943
{
1944+
if (mlir::isa<mlir_ts::AnyType, mlir_ts::UnknownType>(castOp.getType()))
1945+
{
1946+
return false;
1947+
}
1948+
19411949
root = castOp.getIn();
19421950
}
19431951

‎tslang/test/tester/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ tslang_add_test(NAME test-compile-00-generator-typed-yield COMMAND test-runner "
471471
tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts")
472472
tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
473473
tslang_add_test(NAME test-compile-00-generator-return-throw COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
474+
tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
474475
tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
475476
tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts")
476477
tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts")
@@ -1312,6 +1313,9 @@ tslang_add_test(NAME test-jit-own-00-generator-next-value COMMAND test-runner -j
13121313
tslang_add_test(NAME test-jit-00-generator-return-throw COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
13131314
tslang_add_test(NAME test-jit-rc-00-generator-return-throw COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
13141315
tslang_add_test(NAME test-jit-own-00-generator-return-throw COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
1316+
tslang_add_test(NAME test-jit-00-any-undefined-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
1317+
tslang_add_test(NAME test-jit-rc-00-any-undefined-field COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
1318+
tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
13151319
tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
13161320
tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
13171321
tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// An object literal's field of `<any>undefined` or `<any>null`: the box is a block of its own, which the
2+
// store into the object moves in. Under -mm=own it was taken for constant data (null or undefined seen
3+
// through its casts), so the move was not checked and the temporary's release gave the box back as well
4+
// as the object's: a double free, which wasm's checked heap and Linux caught (TSLANG 153).
5+
function mk() {
6+
return { t: <any>undefined, n: 1 };
7+
}
8+
9+
function mkNull() {
10+
return { t: <any>null, n: 2 };
11+
}
12+
13+
function main() {
14+
let total = 0;
15+
for (let i = 0; i < 1000; i++) {
16+
const o = mk();
17+
const p = mkNull();
18+
total += o.n + p.n;
19+
if (o.t !== undefined || p.t !== null) total = -1000000;
20+
}
21+
22+
assert(total == 3000, "fields");
23+
print("done.");
24+
}

0 commit comments

Comments
 (0)