Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Issue 23682 - dip1000 problem with return by ref #14869

Merged
merged 1 commit into from
Feb 12, 2023

Conversation

WalterBright
Copy link
Member

No description provided.

@WalterBright WalterBright added the dip1000 memory safety with scope, ref, return label Feb 10, 2023
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Auto-close Bugzilla Severity Description
23682 normal dip1000 problem with return by ref

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#14869"

@WalterBright
Copy link
Member Author

I'm not quite sure this fix is right, so I'll look into it further.

@WalterBright WalterBright added the WIP Work In Progress - not ready for review or pulling label Feb 10, 2023
@WalterBright
Copy link
Member Author

@RazvanN7 it's failing compiling druntime because that gets compiled before the tests/compilable test suite is run. So now I'm doing another reduction job through a thicket of templates. Sigh.

@WalterBright
Copy link
Member Author

@RazvanN7 I fixed the problem compiling druntime. But the very next thing it does is try to compile Phobos. And then it fails, and I'm borked again by the order in which the test suite is run.

Every single check attempts to compile Phobos before running any tests.

@WalterBright
Copy link
Member Author

This is failing in std.uni, probably the most template heavy piece of code. I can't make much sense of it. It uses auto so much it's really hard to figure out what types are being used. Anyway, being the pragmatic sort, I'm going to put this fix behind a -preview switch. It's likely to break user code, too, that relied on the old behavior. Hopefully this will get things through the test suite.

@WalterBright
Copy link
Member Author

oops pushed the wrong button

@ibuclaw
Copy link
Member

ibuclaw commented Feb 11, 2023

Every single check attempts to compile Phobos before running any tests.

You can't run any runnable tests without a runtime library to link. You can't run any compilable LINK tests without a runtime library either (though they if any should be standalone programs).

@WalterBright
Copy link
Member Author

It should link with the previous version.

Copy link
Contributor

@dkorpel dkorpel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong fix.

Fixing a rejects-valid bug shouldn't break druntime and Phobos.

The function returning by ref doesn't mean return scope parameters return a reference to the input variable, that would always be a dangling stack pointer.

The real issue is that assigning a ref to a non-ref implicitly dereferences a pointer, but this only manifests in the glue layer, so it's invisible to escape.d.

Without redesigning dmd so the dereferencing of ref values appears in the front-end AST, here's a possible fix:
#14871

compiler/test/fail_compilation/retscope6.d Outdated Show resolved Hide resolved
druntime/src/rt/monitor_.d Outdated Show resolved Hide resolved
@WalterBright
Copy link
Member Author

****** FAIL release64 std.random
core.exception.AssertError@std/random.d(3957): Assertion failure

A random failure in std.random. I wish somehow we could create repeatable tests.

@WalterBright
Copy link
Member Author

Now this new failure on Mac OS:

core.exception.AssertError@hospital.d(35): Assertion failure

what's going on here?

@WalterBright
Copy link
Member Author

And our old friend, the socket failure, reappears:

core.exception.AssertError@std/socket.d(805): unittest failure

@WalterBright
Copy link
Member Author

dlang/phobos#8681

@WalterBright
Copy link
Member Author

trying again to see if we avoid heisenbugs

@WalterBright
Copy link
Member Author

And the heisenbugs go away! Ready to merge.

@WalterBright
Copy link
Member Author

@dkorpel ping

Copy link
Contributor

@dkorpel dkorpel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, this fix is overfitted to the single example in the one bugzilla issue, but I can fix this in my PR to solve the issue proper.


// https://issues.dlang.org/show_bug.cgi?id=23682

alias VErr = char*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is pointless

@@ -1744,7 +1744,27 @@ void escapeByValue(Expression e, EscapeByResults* er, bool live = false, bool re
Parameter p = tf.parameterList[i - j];
const stc = tf.parameterStorageClass(null, p);
ScopeRef psr = buildScopeRef(stc);
if (psr == ScopeRef.ReturnScope || psr == ScopeRef.Ref_ReturnScope)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref_ReturnScope should not be treated different to ReturnScope

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are different because a ReturnScope can be returned as a Ref

@WalterBright WalterBright merged commit 67b6ee0 into dlang:master Feb 12, 2023
@WalterBright
Copy link
Member Author

The trouble is a ref return from a struct constructor is different from a regular ref return. It's an inconsistency in the language, but I don't think there's much we can do about it.

@WalterBright WalterBright deleted the fix23682 branch February 12, 2023 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Fix dip1000 memory safety with scope, ref, return
Projects
None yet
4 participants