Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,48 @@ class OwnedReturnConsumptionPass
}
}

auto mayAssignAnyGlobal = [&](mlir::Operation *op) {
auto callee = calleeOf(op);
if (callee.empty() || assignsAny.contains(callee))
{
return true;
}

auto it = assigns.find(callee);
return it != assigns.end() && !it->getValue().empty();
};

// Is this a read out of a field or an element of an object a parameter names (`p.n`, `p.a[0].v`)? The
// parameter may be a global's object (`f(HG)`), so a call that gives a global's field a new value may
// free what was read.
auto readsParameterPlace = [](mlir::Value value) {
auto inPlace = false;
for (auto depth = 0; depth < 16 && value; depth++)
{
while (auto castOp = value.getDefiningOp<mlir_ts::CastOp>())
{
value = castOp.getIn();
}

auto loadOp = value.getDefiningOp<mlir_ts::LoadOp>();
auto *place = loadOp ? loadOp.getReference().getDefiningOp() : nullptr;
if (mlir::isa_and_nonnull<mlir_ts::ParamOp>(place))
{
return inPlace;
}

if (!mlir::isa_and_nonnull<mlir_ts::PropertyRefOp, mlir_ts::ElementRefOp>(place))
{
return false;
}

inPlace = true;
value = place->getOperand(0);
}

return false;
};

auto mayAssign = [&](mlir::Operation *op, mlir::StringRef global) {
auto callee = calleeOf(op);
if (callee.empty() || assignsAny.contains(callee))
Expand Down Expand Up @@ -646,6 +688,15 @@ class OwnedReturnConsumptionPass
continue;
}

// Out of a field of an object a parameter names, to a call that may give a global's field a new
// value: the object may be that global's (`f(HG)` then `g(p.n)`, `g` setting `HG.n`), and the
// store freed the block the argument read. Under rc the caller holds it (fuzz seed 71210).
if (compileOptions.isRefCounted() && readsParameterPlace(argument) && mayAssignAnyGlobal(op))
{
held.push_back({op, argument});
continue;
}

auto global = globalOf(argument, false);
if (global.empty())
{
Expand Down
47 changes: 47 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferencePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ namespace own_inference
recordStoreUses.clear();
}

// A store of a value that owns a block into a parameter's own slot: the function's argument, which no local
// that owns what it holds took over (MLIRGen's mlirGenFunctionOwnAssignedParams makes one only of a string or a
// Shared<T> parameter). Another parameter's value may go there: the caller holds both for the whole call.
static bool assignsParameter(mlir_ts::StoreOp storeOp)
{
auto isParameterSlot = [](mlir_ts::VariableOp slot) {
auto argument = slot ? mlir::dyn_cast_or_null<mlir::BlockArgument>(slot.getInitializer()) : nullptr;
return slot && !isOwningVariable(slot) && argument && argument.getOwner()->isEntryBlock() &&
mlir::isa<mlir_ts::FuncOp>(argument.getOwner()->getParentOp());
};

if (!isParameterSlot(storeOp.getReference().getDefiningOp<mlir_ts::VariableOp>()) || !ownsHeap(storeOp.getValue()))
{
return false;
}

auto value = storeOp.getValue();
while (auto castOp = value.getDefiningOp<mlir_ts::CastOp>())
{
value = castOp.getIn();
}

auto loadOp = value.getDefiningOp<mlir_ts::LoadOp>();
return !loadOp || !isParameterSlot(loadOp.getReference().getDefiningOp<mlir_ts::VariableOp>());
}

void OwnershipInferencePass::collectOwnershipOps(FunctionOps &ops, llvm::SetVector<mlir::Operation *> &toErase)
{
auto f = getFunction();
Expand Down Expand Up @@ -207,6 +233,27 @@ namespace own_inference
signalPassFailure();
}
}
else if (auto storeOp = mlir::dyn_cast<mlir_ts::StoreOp>(op); storeOp && assignsParameter(storeOp))
{
// A parameter's slot given another value: the slot owns nothing, so the value is neither
// taken from its owner nor retained, and the owner frees it - `p = n; n = mk()` left `p`
// reading freed memory. Reported where a retain for it would have been, but there is none.
if (reporting())
{
// a parameter's name is known only from its debug location (--di)
auto name = varName(storeOp.getReference().getDefiningOp<mlir_ts::VariableOp>());
if (name == "this value")
{
op->emitError("a parameter cannot be assigned under -mm=own yet");
}
else
{
op->emitError("'") << name << "' is a parameter and cannot be assigned under -mm=own yet";
}

signalPassFailure();
}
}
else if (mlir::isa<mlir_ts::DeleteOp>(op))
{
if (reporting())
Expand Down
15 changes: 12 additions & 3 deletions tslang/lib/TypeScript/OwnershipSignatureKeepers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ namespace own_signature
return value;
}

// Does a global's initializer region give it a new array - `[]`, an array literal - and nothing
// else?
// Does a global's initializer region give it a new array - `[]`, an array literal of constants - and
// nothing else? Only what is pushed onto a keeper is made immortal (decideKeptForGood): an element its
// literal is made with - `[mk(5)]` - is not, so a read of it is no keeper's read, and a local given
// one and released frees it from the array.
static bool regionYieldsNewArray(mlir_ts::GlobalOp globalOp)
{
if (!globalOp.getInitializerRegion().hasOneBlock())
Expand All @@ -38,7 +40,14 @@ namespace own_signature
}

auto *def = castRoot(resultOp->getOperand(0)).getDefiningOp();
return mlir::isa_and_nonnull<mlir_ts::ConstantOp, mlir_ts::CreateArrayOp, mlir_ts::NewArrayOp>(def);
if (auto createArrayOp = mlir::dyn_cast_or_null<mlir_ts::CreateArrayOp>(def))
{
return llvm::all_of(createArrayOp->getOperands(), [](mlir::Value element) {
return !!castRoot(element).getDefiningOp<mlir_ts::ConstantOp>();
});
}

return mlir::isa_and_nonnull<mlir_ts::ConstantOp, mlir_ts::NewArrayOp>(def);
}

void OwnershipSignaturePass::findKeepers(mlir::ModuleOp module)
Expand Down
6 changes: 5 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ tslang_add_test(NAME test-compile-00-class-accessor-implements-field COMMAND tes
tslang_add_test(NAME test-compile-00-object-literal-contextual-lambda COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts")
tslang_add_test(NAME test-compile-own-00-object-literal-contextual-lambda COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts")
tslang_add_test(NAME test-compile-00-interface-field-getter-owned COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts")
tslang_add_test(NAME test-compile-00-call-arg-param-field-global COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00call_arg_param_field_global.ts")
tslang_add_test(NAME test-compile-00-class-accessor-untyped-setter COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts")
tslang_add_test(NAME test-compile-00-class-super COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts")
tslang_add_test(NAME test-compile-00-class-implicit-constructor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts")
Expand Down Expand Up @@ -1253,6 +1254,7 @@ tslang_add_test(NAME test-jit-00-class-accessor-implements-field COMMAND test-ru
tslang_add_test(NAME test-jit-00-object-literal-contextual-lambda COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts")
tslang_add_test(NAME test-jit-own-00-object-literal-contextual-lambda COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts")
tslang_add_test(NAME test-jit-00-interface-field-getter-owned COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts")
tslang_add_test(NAME test-jit-00-call-arg-param-field-global COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00call_arg_param_field_global.ts")
tslang_add_test(NAME test-jit-00-class-accessor-untyped-setter COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts")
tslang_add_test(NAME test-jit-00-class-super COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts")
tslang_add_test(NAME test-jit-00-class-implicit-constructor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts")
Expand Down Expand Up @@ -2267,6 +2269,7 @@ set(TSLANG_CORPUS
00class_accessor_implements_field.ts
00object_literal_contextual_lambda.ts
00interface_field_getter_owned.ts
00call_arg_param_field_global.ts
00class_accessor_untyped_setter.ts
00class_accessor.ts
00class_accessor2.ts
Expand Down Expand Up @@ -4076,7 +4079,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o
own_borrowing_record own_borrowing_record_arg
own_reduce own_fresh_call_result own_ternary_outer_loop own_destructure_elements
own_tuple_record_values own_value_record own_value_loop own_value_temporaries
own_call_field_drops own_immortal_keepers
own_call_field_drops own_immortal_keepers own_keeper_literal_elements
own_generator_method_this own_string_copy_call own_param_kept_some_paths own_method_callbacks
own_loop_read_again own_length_drops_elements own_tuple_param_kept
own_for_of_borrowed_iterator own_generator_keeps own_generator_keeps_bounded
Expand Down Expand Up @@ -4670,6 +4673,7 @@ set(own_error_cases
"own_err_global_read_callee_assigns|borrows 'G' but is used here after 'G' is released or overwritten"
"own_err_global_read_callee_assigns_transitive|borrows 'G' but is used here after 'G' is released or overwritten"
"own_err_argument_assigned|is lent to 'g', but is given a new value before the call takes it"
"own_err_param_assigned_local|:15:5: error: a parameter cannot be assigned under -mm=own yet"
"own_err_argument_assigned_conditional|is lent to 'g', but is given a new value before the call takes it"
"own_err_argument_assigned_closure|is lent to 'g', but is given a new value before the call takes it"
"own_err_global_method_transitive|:19:11: error: a value read out of the global 'G' is lent to a call that may release or overwrite it"
Expand Down
22 changes: 22 additions & 0 deletions tslang/test/tester/own/own_err_param_assigned_local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -mm=own rejects: `p = n` puts `n`'s object in a parameter's slot, which owns nothing, so the
// object is neither taken from `n` nor counted again - and `n = mk(9)` frees it while `p` still
// reads it. Own accepted this (it reported an assigned parameter only where a retain stood for the
// store, and a store into a parameter has none), and `p.x` read freed memory (mm_fuzz seed 70377).
class N {
constructor(public x: number) {}
}

function mk(i: number): N {
return new N(i);
}

function f(p: N): number {
let n: N = mk(2);
p = n;
n = mk(9);
return p.x;
}

function main() {
print(f(mk(5)));
}
51 changes: 51 additions & 0 deletions tslang/test/tester/own/own_keeper_literal_elements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// -mm=own: a keeper - an append-only array global - makes what is pushed onto it immortal, so a read
// of its elements owns nothing and any release skips it. The elements its own literal is made with
// were not made immortal: `const GA: N[] = [mk(5)]` was taken for a keeper all the same, and a local
// given `GA[0]` freed it when the local's block ended (mm_fuzz seeds 70040-70386: 9 of 10 own
// failures of one round). An array literal of anything but constants makes no keeper now, and its
// elements are borrowed like any array's: storing one in an array or a global is an error under own.
// Each value is read after churn() has reused anything freed too early.
class N {
items: number[] = [5];
constructor(public x: number) {}
}

function mk(i: number): N {
return new N(i);
}

function churn() {
const hold: N[] = [];
for (let i = 0; i < 64; i++) {
hold.push(new N(777));
}
}

interface IX {
x: number;
}

const GA: N[] = [mk(5)];

function main() {
for (let i = 0; i < 3; i++) {
let n = GA[0];
}

churn();
assert(GA[0].x == 5 && GA[0].items[0] == 5, "a local in a loop leaves the element");

{
let n = GA[0];
let ix: IX = GA[0];
}

churn();
assert(GA[0].x == 5, "a local and an interface in a block leave the element");

GA.push(mk(6));
churn();
assert(GA[1].x == 6, "a pushed element");

print("done.");
}
95 changes: 95 additions & 0 deletions tslang/test/tester/tests/00call_arg_param_field_global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// An argument read out of a field of an object a parameter names - `g(p.n)` - is borrowed by the
// callee, and the caller took no count for it. When the parameter is a global's object (`f(HG)`) and
// the callee gives that global's field a new value (`HG.n = mk(5)`), the store freed the block the
// argument read, under rc: `g` read freed memory, and a result that was the argument was freed too
// (mm_fuzz seed 71210). An argument read out of a global itself was already held across such a call
// (#647); now one read through a parameter's object is held as well, when the callee may give a
// global a new value. Each value is read after churn() has reused anything freed too early.
class V {
constructor(public x: number) {}
}

class N {
items: number[] = [1];
constructor(public v: V) {}
}

class H {
n: N = new N(new V(3));
}

function mk(i: number): N {
return new N(new V(i));
}

function churn() {
const hold: N[] = [];
for (let i = 0; i < 64; i++) {
hold.push(new N(new V(777)));
}
}

const HG = new H();

// the callee reads its parameter after giving the global's field a new value
function readsAfter(a: N): number {
HG.n = mk(5);
churn();
return a.v.x;
}

function callReadsAfter(p: H): number {
return readsAfter(p.n);
}

// the callee gives back the argument it was given
function givesBack(a: N): N {
HG.n = mk(6);
return a;
}

function callGivesBack(p: H): number {
const r = givesBack(p.n);
churn();
return r.v.x;
}

// ...into the very field it was read from
function storesBack(p: H): number {
p.n = givesBack(p.n);
churn();
return p.n.v.x;
}

// through an element
class A {
ns: N[] = [mk(8)];
}

const AG = new A();

function setsElement(a: N): number {
AG.ns[0] = mk(9);
churn();
return a.v.x;
}

function callSetsElement(p: A): number {
return setsElement(p.ns[0]);
}

function main() {
assert(callReadsAfter(HG) == 3, "the callee reads the argument after the store");
assert(HG.n.v.x == 5, "the store took");

HG.n = mk(4);
assert(callGivesBack(HG) == 4, "the callee gives the argument back");

HG.n = mk(7);
assert(storesBack(HG) == 7, "the argument stored back into its field");

assert(callSetsElement(AG) == 8, "an element read through a parameter");
assert(AG.ns[0].v.x == 9, "the element store took");

print("done.");
}
Loading