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 as.exe crash on Windows #20

Merged
merged 11 commits into from
Mar 26, 2024
Merged

Conversation

grendello
Copy link
Contributor

Code previously used to convert command-line arguments from wide encoding to UTF-8
we used when as.exe was built with MinGW, is no longer necessary. It was causing
the app to crash on Windows.

@grendello grendello requested a review from jonpryor March 19, 2024 16:42
@grendello grendello marked this pull request as draft March 19, 2024 16:45
@grendello
Copy link
Contributor Author

Doesn't crash, but no longer finds the wide-char filenames... search continues.

@grendello grendello force-pushed the dev/grendel/fix-as-windows-crash branch from 221c870 to df9ddb1 Compare March 19, 2024 18:17
Better option than porting getopt to Windows
Windows doesn't build yet, Unix side works.
wide string conversion is... broken. Wide characters and wide strings
are evil
...at least if we don't touch wide strings.  The code added in previous
commits to cope with narrow vs wide strings is left in place, "just in
case".  Both Unix and Windows builds effectively use narrow strings,
though.
@grendello grendello marked this pull request as ready for review March 26, 2024 16:07
// and the warning generated by -Wdangling-gsl was effectively non-actionable:
// warning: object backing the pointer will be destroyed at the end of the full-expression
//
const char *const epath = executable_path.string ().c_str ();
Copy link
Member

Choose a reason for hiding this comment

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

This actually worries me; the "full expression" is this entire line, so once executable_path.string().c_str() has finished evaluation, it is potentially invalidated, meaning that epath may be garbage.

I think it would be safer -- and would also fix the -Wdangling-gsl warning -- if you instead did auto path = executable_path.string(), and then everywhere you had epath you instead used path.c_str(). Then we know the value returned from .c_str() won't be destructed until path is destructed, which is at end of scope, vs "end of the full expression".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That means code duplication and doesn't really change anything though... It's a cosmetic change with no real effect - the temporary is still removed and we still duplicate the C string we get from the temporary

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but that's later. My recollection is that this full expression is basically:

auto __tmp = executable_path.string();
const char *const epath = __tmp.c_str();

which looks fine, except for the all important question: when is __tmp destructed? At various points in time this was up to the compiler, and some compilers would destruct __tmp at end of enclosing scope (i.e. "you're fine") and some would destruct __tmp at end of expression, i.e.:

const char *const epath;
{ auto __tmp = executable_path.string();
  epath = __tmp.c_str();
  /* __tmp destructed at end of full expression */ }
/* `epath` now pointing to garbage */

Perhaps the semantics have since been standardized, but at least in C++98 this was not well specified, and different compilers would do different things. The warning is trying to warn you about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The local object is strictly destroyed when it leaves its scope, in this case the function.

return s;
#else
std::string ret (s.length (), 0);
std::transform (
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused wrt how this actually transforms to UTF-8 on Windows, when platform::string is std::wstring. This looks like it'll just truncate wchar_t to char.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

platform::string is now uniformly std::string, I stopped using wstring but kept the abstraction "just in case"

@grendello grendello merged commit fbd41d6 into main Mar 26, 2024
7 checks passed
@grendello grendello deleted the dev/grendel/fix-as-windows-crash branch March 26, 2024 20:14
grendello added a commit to dotnet/android that referenced this pull request Apr 2, 2024
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.1
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.2
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.2.1
Changes: dotnet/android-native-tools@L_17.0.6-7.1.0...L_17.0.6-7.1.1
Changes: dotnet/android-native-tools@L_17.0.6-7.1.1...L_17.0.6-7.1.2
Changes: dotnet/android-native-tools@L_17.0.6-7.1.2...L_17.0.6-7.2.0
Changes: dotnet/android-native-tools@L_17.0.6-7.2.0...L_17.0.6-7.2.1

This release makes changes to how we build and run the `as.exe` wrapper, so that it can be
verified by API Scan without any issues.  Otherwise it is identical to the previous release, using
the same version of LLVM.

* [ci] Build and sign in a DevDiv pipeline by @pjcollins in dotnet/android-native-tools#7
* [ci] Migrate to the 1ES template by @pjcollins in dotnet/android-native-tools#8
* [ci] Improve binskim scan performance by @pjcollins in dotnet/android-native-tools#11
* [ci] Improve triggers and support test signing by @pjcollins in dotnet/android-native-tools#12
* [ci] Fix unsigned artifact uploading by @pjcollins in dotnet/android-native-tools#13
* [ci] Disable automatic GitHub action trigger by @pjcollins in dotnet/android-native-tools#16
* [ci] Build on performance build pools by @pjcollins in dotnet/android-native-tools#15
* Build `as.exe` on windows by @grendello in dotnet/android-native-tools#10
* [ci] Add API Scan job by @pjcollins in dotnet/android-native-tools#9
*  Fix `as.exe` crash on Windows by @grendello in dotnet/android-native-tools#20
* [ci] Use managed identity for API Scan by @pjcollins in dotnet/android-native-tools#21
* Back to wide strings on Windows + magic encantations by @grendello in dotnet/android-native-tools#22
grendello added a commit that referenced this pull request Apr 2, 2024
* main:
  Back to wide strings on Windows + magic encantations (#22)
  [ci] Use managed identity for API Scan (#21)
  Fix `as.exe` crash on Windows (#20)
  Partially revert 9d342d5 (#19)
grendello added a commit to dotnet/android that referenced this pull request Apr 11, 2024
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.1
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.2
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.2.1
Changes: dotnet/android-native-tools@L_17.0.6-7.1.0...L_17.0.6-7.1.1
Changes: dotnet/android-native-tools@L_17.0.6-7.1.1...L_17.0.6-7.1.2
Changes: dotnet/android-native-tools@L_17.0.6-7.1.2...L_17.0.6-7.2.0
Changes: dotnet/android-native-tools@L_17.0.6-7.2.0...L_17.0.6-7.2.1

This release makes changes to how we build and run the `as.exe` wrapper, so that it can be
verified by API Scan without any issues.  Otherwise it is identical to the previous release, using
the same version of LLVM.

* [ci] Build and sign in a DevDiv pipeline by @pjcollins in dotnet/android-native-tools#7
* [ci] Migrate to the 1ES template by @pjcollins in dotnet/android-native-tools#8
* [ci] Improve binskim scan performance by @pjcollins in dotnet/android-native-tools#11
* [ci] Improve triggers and support test signing by @pjcollins in dotnet/android-native-tools#12
* [ci] Fix unsigned artifact uploading by @pjcollins in dotnet/android-native-tools#13
* [ci] Disable automatic GitHub action trigger by @pjcollins in dotnet/android-native-tools#16
* [ci] Build on performance build pools by @pjcollins in dotnet/android-native-tools#15
* Build `as.exe` on windows by @grendello in dotnet/android-native-tools#10
* [ci] Add API Scan job by @pjcollins in dotnet/android-native-tools#9
*  Fix `as.exe` crash on Windows by @grendello in dotnet/android-native-tools#20
* [ci] Use managed identity for API Scan by @pjcollins in dotnet/android-native-tools#21
* Back to wide strings on Windows + magic encantations by @grendello in dotnet/android-native-tools#22
jonathanpeppers pushed a commit to dotnet/android that referenced this pull request May 8, 2024
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.1
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.1.2
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.2.1
Context: https://github.com/xamarin/xamarin-android-binutils/releases/tag/L_17.0.6-7.2.2
Changes: dotnet/android-native-tools@L_17.0.6-7.1.0...L_17.0.6-7.1.1
Changes: dotnet/android-native-tools@L_17.0.6-7.1.1...L_17.0.6-7.1.2
Changes: dotnet/android-native-tools@L_17.0.6-7.1.2...L_17.0.6-7.2.0
Changes: dotnet/android-native-tools@L_17.0.6-7.2.0...L_17.0.6-7.2.1
Changes: dotnet/android-native-tools@L_17.0.6-7.2.1...L_17.0.6-7.2.2

This release makes changes to how we build and run the `as.exe` wrapper, so that it can be verified by API Scan without any issues.  Otherwise it is identical to the previous release, using the same version of LLVM.

* [ci] Build and sign in a DevDiv pipeline by @pjcollins in dotnet/android-native-tools#7
* [ci] Migrate to the 1ES template by @pjcollins in dotnet/android-native-tools#8
* [ci] Improve binskim scan performance by @pjcollins in dotnet/android-native-tools#11
* [ci] Improve triggers and support test signing by @pjcollins in dotnet/android-native-tools#12
* [ci] Fix unsigned artifact uploading by @pjcollins in dotnet/android-native-tools#13
* [ci] Disable automatic GitHub action trigger by @pjcollins in dotnet/android-native-tools#16
* [ci] Build on performance build pools by @pjcollins in dotnet/android-native-tools#15
* Build `as.exe` on windows by @grendello in dotnet/android-native-tools#10
* [ci] Add API Scan job by @pjcollins in dotnet/android-native-tools#9
*  Fix `as.exe` crash on Windows by @grendello in dotnet/android-native-tools#20
* [ci] Use managed identity for API Scan by @pjcollins in dotnet/android-native-tools#21
* Back to wide strings on Windows + magic encantations by @grendello in dotnet/android-native-tools#22
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.

2 participants