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 #463 ; Debug build mode fixing in Windows #506

Merged

Conversation

usagi
Copy link
Contributor

@usagi usagi commented Mar 26, 2020

Related: #463 (comment)

@@ -22,6 +22,11 @@ cfg_if! {
println!("cargo:rustc-link-search=native={}", &node_lib_path.display());
println!("cargo:rustc-link-lib={}", &node_lib_file_path.file_stem().unwrap().to_str().unwrap());

if debug
{
println!("cargo:rustc-link-lib=msvcrtd");
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't appear to be impacting other compilers (tests are passing), but, just to be safe, can you add a cfg to target just msvc?

Copy link
Contributor Author

@usagi usagi Mar 26, 2020

Choose a reason for hiding this comment

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

👍 Yes I agree to your suggestion. I'll try to the fixup. Thank you.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What is msvcrtd? Is that the debug mode of Microsoft's C runtime library?

@kjvalencik
Copy link
Member

Were you able to reproduce the integer overflow from the linked issue without this fix?

@usagi
Copy link
Contributor Author

usagi commented Mar 26, 2020

@kjvalencik Ah... unfortunately(or maybe a good news), I could not reproduce the Error: internal error in Neon module: attempt to subtract with overflow or any happenings. And I could not contact the original error reporter then I could not know the repro codes that happened the overflow error.

But, the fix works fine with these examples. And it's debuggable with msvc-debugger and lldb. Lines, variables, dynamic links, registers and step execution in a rust codes are looks good to me.

I think maybe, that overflow error occered from the implementation difference of a memory allocation between msvcrt.lib and msvcrtd.lib .


I'll happy if the original reporter @anurbol would be share the repro code.

@anurbol
Copy link

anurbol commented Mar 26, 2020

Hi @usagi, you've done great work, wow. I am sorry, I can neither share the code, because I forgot everything about this problem, and where the code is actually, nor I think it could be useful because the code was pretty huge and I never knew which exact part caused the problem.

@d4hines
Copy link

d4hines commented May 5, 2020

Any more thoughts on this? We're eagerly awaiting debug builds on Windows!

@dherman
Copy link
Collaborator

dherman commented May 5, 2020

Thank you for this submission @usagi! I think we should merge it, even if we aren't 100% sure all the issues are gone since this is only for debug builds. If those issues start to surface again we can just try to fix them, but they shouldn't break production systems.

@dherman
Copy link
Collaborator

dherman commented May 5, 2020

@usagi After we merge this PR and publish a new Neon version, do people still need to use any of the manual steps in #463 (comment) or can they just do neon build --debug?

@usagi
Copy link
Contributor Author

usagi commented May 7, 2020

@dherman Just do neon build --debug ( = neon build).

Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

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

This looks like pure win to me! The worst that can happen is there might still be bugs in the Windows debug builds, but we can deal with those as we come across them, and there seems to be no risk for production builds.

Thank you so much for the contribution, @usagi! ❤️

@dherman dherman merged commit 326c0cb into neon-bindings:master May 8, 2020
@dherman
Copy link
Collaborator

dherman commented May 8, 2020

@usagi In retrospect, it seems worthwhile to add a comment to the line you added that links msvcrtd, since it's otherwise fairly mysterious. Would you be up for a quick followup PR to add the comment?

@usagi
Copy link
Contributor Author

usagi commented May 10, 2020

@dherman I will PR in near future about what is the trick if I could find the time.

@retep998
Copy link
Contributor

This seems incorrect. Rust always uses the release CRT, so linking the debug CRT would conflict with that. Why was this solution preferred over simply building the C code in release on Windows as I did in my PR from a year ago that was ignored?

@dherman
Copy link
Collaborator

dherman commented May 14, 2020

Oh wow, thank you @retep998, and my apologies for overlooking your contribution. That never feels good. We do our best to keep on top of things but we don't always live up to our intentions.

Maybe what we could do is, I can write a backout PR for this and then that should fix the merge conflicts for #400. That way we don't lose the history or attribution.

@retep998 Just to make sure I understand: how would the conflicts manifest -- would it result in link-time or runtime errors? Any idea why @usagi's tests didn't run into any errors? Also, do you happen to have any links to info where I can learn about how and why Rust uses the release CRT in Windows?

As always, I really appreciate you sharing your expertise in Windows and Rust!

@retep998
Copy link
Contributor

retep998 commented May 14, 2020

libc as depended on by std will always link to the release CRT: https://github.com/rust-lang/libc/blob/b61d8c617a782688f81ccb8fe25919c20dcd374e/src/windows/mod.rs#L238-L242

Depending on the order of inputs to the linker, this may result in all linked rust code using only the debug CRT, only the release CRT, or a mixture of both which could cause link errors or runtime UB. If the user depends on any other crates that also link to C code, then that C code may end up linking against the debug CRT despite having been built against the release CRT which could cause link errors or runtime UB. I know even if it doesn't cause a linker error, the linker will usually spew warnings, however Rust currently does not print linker output unless linking fails so there is no way to see those warnings.

In the future Rust may gain the option to link to the debug CRT: rust-lang/rust#39016

@dherman
Copy link
Collaborator

dherman commented May 15, 2020

Thanks @retep998! You saved us from some likely extremely hard to diagnose bugs down the road. I’ll submit a backout PR and then see if your PR can be merged.

dherman added a commit that referenced this pull request May 15, 2020
@dherman
Copy link
Collaborator

dherman commented May 15, 2020

Submitted #515 to back this one out. @usagi Thank you for contributing this PR—even though this particular solution didn't end up being the one we'll use, it still spurred the right conversation and will get us to a fix for the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants