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

proposal: cmd/vet: warn for passing invalid arguments to runtime.SetFinalizer #48019

Closed
KimMachineGun opened this issue Aug 27, 2021 · 9 comments

Comments

@KimMachineGun
Copy link
Contributor

KimMachineGun commented Aug 27, 2021

Proposal

Now, the vet tool don't warn for calling runtime.SetFinalizer with arguments that do not meet its specification, even if it makes unrecoverable error.

The argument obj must be a pointer to an object allocated by calling new, by taking the address of a composite literal, or by taking the address of a local variable. The argument finalizer must be a function that takes a single argument to which obj's type can be assigned, and can have arbitrary ignored return values. If either of these is not true, SetFinalizer may abort the program.

So, I suggest that make vet warn for passing invalid arguments to runtime.SetFinalizer.

golang/tools#335

@timothy-king
Copy link
Contributor

Can you clarify the check that the proposed Analyzer will enforce in the description? The documentation is a bit ambiguous on the syntactic requirements on obj. I think being clearer about what the analyzer will do will help an expert on runtime.SetFinalizer evaluate whether the check makes sense.

@timothy-king
Copy link
Contributor

runtime.SetFinalizer usage is somewhat uncommon so this may fail the Frequency requirement for cmd/vet:

Vet is run every day by many programmers, often as part of every compilation or
submission. The cost in execution time is considerable, especially in aggregate,
so checks must be likely enough to find real problems that they are worth the
overhead of the added check. A new check that finds only a handful of problems
across all existing programs, even if the problem is significant, is not worth
adding to the suite everyone runs daily.

(Not an obvious no but something to consider.)

@bcmills
Copy link
Contributor

bcmills commented Aug 27, 2021

What is the failure mode of runtime.SetFinalizer if you pass invalid arguments? If it panics or throws immediately, then I'm not sure a vet check is worth its implementation cost: even trivial test coverage would turn up the error, and finalizers are already subtle enough that no one should be so brazen as to use them without really outstanding test coverage.

@robpike
Copy link
Contributor

robpike commented Aug 29, 2021

How are you supposed to statically determine how a variable was allocated? I don't think this is possible to do nearly precisely enough, even if it were a valuable test to add.

@KimMachineGun
Copy link
Contributor Author

Firstly, thank you all for your nice feedbacks!


@timothy-king Can you clarify the check that the proposed Analyzer will enforce in the description? The documentation is a bit ambiguous on the syntactic requirements on obj. I think being clearer about what the analyzer will do will help an expert on runtime.SetFinalizer evaluate whether the check makes sense.



Ok, I’ll leave a comment about the details soon.

(Basically, I copied the arguments validation logics of runtime.SetFinalizer and transformed them into using AST.)

@timothy-king runtime.SetFinalizer usage is somewhat uncommon so this may fail the Frequency requirement for cmd/vet:


Of course, this check may not meet the frequency requirements as much as unmarshal or structtag analyzers, but given precedents like asmdecl and framepointer, I think it doesn't seem impossible.

@robpike How are you supposed to statically determine how a variable was allocated? I don't think this is possible to do nearly precisely enough, even if it were a valuable test to add.

That’s true. I also think it’s impossible to catch a case where needs runtime information. But, we can still prevent many errors through this analyzer, and since it is not different from what we did in the existing analyzers such as unmarshal and sortslice, I think it’s worth the cost of implementation.

@smasher164
Copy link
Member

@bcmills

What is the failure mode of runtime.SetFinalizer if you pass invalid arguments? If it panics or throws immediately, then I'm not sure a vet check is worth its implementation cost: even trivial test coverage would turn up the error, and finalizers are already subtle enough that no one should be so brazen as to use them without really outstanding test coverage.

runtime.SetFinalizer throws immediately after checking that obj is not a non-nil pointer, and that finalizer is a function with the correct signature.

@KimMachineGun

But, we can still prevent many errors through this analyzer

Is there an estimate of how many uses of runtime.SetFinalizer in the wild can be statically analyzed by this check?

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Sep 6, 2021
@ianlancetaylor
Copy link
Contributor

I agree with @bcmills's implication that this check is not worth adding to vet. Everything that vet can check will be checked at runtime by SetFinalizer. The only way that a vet check could help a real program is if the tests never execute the call to SetFinalizer. That doesn't seem like a significant enough case to worry about.

@KimMachineGun
Copy link
Contributor Author

Sorry for not leaving a comment sooner. I've been very busy these days. 😅

After thinking about this proposal for a few days, I decided to withdraw my proposal.
As many of you've said, this proposal may not seem to be as helpful as I thought before.

Thanks again for your feedbacks.

@rsc
Copy link
Contributor

rsc commented Sep 15, 2021

This proposal has been declined as retracted.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals (old) Sep 15, 2021
@rsc rsc moved this from Active to Declined in Proposals (old) Sep 22, 2021
@golang golang locked and limited conversation to collaborators Sep 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

8 participants