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(es/minifier): Fix detection of direct eval #6215

Merged
merged 9 commits into from
Oct 21, 2022

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Oct 20, 2022

Description:

The existence of eval make the name mangler abort, but partial try logic causes minifier to generate an invalid code.

This is what I get if I disable eval detection.

image

With eval detection, I get

                     function r(r) {
                        var o = e[r];
                        if (void 0 !== o) return o.exports;
                        var n = e[r] = {
                            exports: {}
                        };
                        return t[r](n, n.exports, r), n.exports;
                    }

which is wrong

Related issue:


Investigation

image

Conclusion

eval breaks the name mangler.

@kdy1 kdy1 added this to the Planned milestone Oct 20, 2022
@kdy1 kdy1 self-assigned this Oct 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Fix a bug fix(es/minifier): Fix detection of eval Oct 20, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Fix detection of eval fix(es/minifier): Fix detection of direct eval Oct 20, 2022
@kdy1 kdy1 requested a review from jridgewell October 20, 2022 04:20
c();
var d = a.b;
d();
var e = eval;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an indirect eval, so it cannot use c nor b

Copy link
Member Author

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_transforms_base
  • swc_ecma_minifier

foo();
(0, foo.bar)();
(0, eval)("console.log(foo);");
Copy link
Member Author

Choose a reason for hiding this comment

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

This is also an indirect eval, so we can mangle names

@kdy1 kdy1 marked this pull request as ready for review October 20, 2022 04:26
kodiakhq[bot]
kodiakhq bot previously approved these changes Oct 20, 2022
var i = e[r] = {
exports: {}
};
return t[r](i, i.exports, o), i.exports;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the fix

@Smrtnyk
Copy link

Smrtnyk commented Oct 20, 2022

Does this also fix #5068?

@kdy1
Copy link
Member Author

kdy1 commented Oct 20, 2022

No

@kdy1
Copy link
Member Author

kdy1 commented Oct 20, 2022

Actually this is a workaround and we should fix #5068 ideally

Comment on lines +37 to +39
if let Callee::Expr(e) = c {
if let Expr::Ident(Ident {
sym: js_word!("eval"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how you represent optional chains, but eval?.() is not a direct eval either.

Copy link
Member Author

Choose a reason for hiding this comment

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

image

Nice catch, thank you!

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh it was OptCall which does not contain Callee

function f1(a, eval, c, d, e) {
return a("c") + eval;
function f1(n, eval, c, r, t) {
return n("c") + eval;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this eval can be renamed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added code to explicitly prevent it because terser does so.

@kdy1 kdy1 enabled auto-merge (squash) October 21, 2022 02:51
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 disabled auto-merge October 21, 2022 03:17
@kdy1 kdy1 merged commit 780b5de into swc-project:main Oct 21, 2022
@kdy1 kdy1 deleted the minifier-feedback branch October 21, 2022 03:18
@kdy1 kdy1 modified the milestones: Planned, v1.3.10 Oct 21, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants