You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The notion of simply blitting the lowest bits of a bool instance is undefined. It also makes this usage troublesome if a user defines SkipLocalsInitAttribute, which with the current implementation leave the upper bits of Value in an undefined state thus making a .NET false possibly be non-zero.
Wow, that's a good caveat to know. Is that just with the default value, or even if you then assign false?
With the current implementation, even if a .NET false value was set, it would only set the least significant byte in the Value field. This means that if the higher 3 bytes are non-zero due to not being explicitly initialized to zero then only the lowest byte would be set to the assumed value of 0.
Does that make this implicit operator incorrect as well?
Lachlan Ennis (@elachlan) Good lord, I didn't even pay attention to that. Yes, that is very suspect. The Windows BOOL is typically defined to be 0 is false and true is non-zero. There are some Win32 APIs that do questionable things with checking flags - blech.
But then, when comparing a native bool value. It is supposed to equal? I think this allows the byte value to be passed to a native bool and then get the value back again.
Instead of lossy implicit conversions to bool, what if you instead have an explicit conversion to bool but also declare operator true and operator false?
That's a very interesting idea. I didn't know about operator true. Thanks for sharing.
I haven't deeply studied the problem yet, but in passing it seems like the problem is a risk that we read more bytes than are guaranteed to be initialized. Isn't that solvable without changing the fundamental idea of preserving the underlying value (the part of it that's guaranteed to be initialized)?
Yes. Are you calling out that SkipLocalsInit can be applied assembly-wide, thus the generated code can misbehave?
Yes. I can accept that data coming from an unmanaged context may not want to be normalized but going in the other direction the code should really just be this.value = v ? 1 : 0;
What value is there in not being lossy in one conversion but being lossy in the other? Since C# makes extracting the non-binary value from a boolvery difficult, it seems to me the value in being non-lossy is primarily in being able to jump across BOOL -> bool -> BOOL and having the original value.
BOOL -> bool -> BOOL and having the original value.
I don't see how that would work in practice. It would mean the bool, which is always passed by value, maintains the interop binary value which isn't guarantee nor a good idea in practice. It is basically assuming the runtime will define its bool ABI to be what the interop layer decided is appropriate.
Stephen Toub (@stephentoub), sorry to snipe you like this. Could you please chime in on how we should be handling interop BOOL/BOOLEAN to bool conversion or blitting? Should bool ever have an underlying value other than true or false?
It is basically assuming the runtime will define its bool ABI to be what the interop layer decided is appropriate.
The runtime cannot change the memory layout of bool at this point, can it? We expect it to be 1 byte long, and we may copy 1 byte into that memory. C# emits IL for bool conditions that treat any non-zero value as truthy, just as C does. I'm struggling to see how it's problematic then to copy the value from BOOL or BOOLEAN into bool in an implicit conversion so that another implicit conversion can change it back. And this isn't me just trying to be clever... I had to work this out to resolve another issue (linked above).
Andrew Arnott (@AArnott) this checked conversion is a hard blocker for WinForms. We're getting back BOOL values from Windows APIs that overflow. I've tried providing true and false operators, but they don't take precedence unfortunately. :(
It's way too risky for us to have checked operations with BOOL. We're probably going to have to shelve working on our porting exercise until we can get this resolved.
Activity
[-]`BOOL` template is incorrect[/-][+]`BOOL`/`BOOLEAN` templates are incorrect[/+]JeremyKuhne commentedon Jul 27, 2022
Wow, that's a good caveat to know. Is that just with the default value, or even if you then assign
false?jnm2 commentedon Jul 27, 2022
Also, comparisons in different contexts don't operate the same on
boolvalues that aren't identical totrueorfalse: dotnet/roslyn#24865 (comment)AaronRobinsonMSFT commentedon Jul 27, 2022
With the current implementation, even if a .NET
falsevalue was set, it would only set the least significant byte in theValuefield. This means that if the higher 3 bytes are non-zero due to not being explicitly initialized to zero then only the lowest byte would be set to the assumed value of0.Fixes microsoft#624
elachlan commentedon Jul 27, 2022
This documentation comment is in the winforms code base.
https://github.com/dotnet/winforms/blob/d46ad2e8dc76248739d9ae22b28b399a6a6b299e/src/System.Windows.Forms.Primitives/src/Interop/Interop.BOOL.cs#L9-L17
Does that make this implicit operator incorrect as well?
CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOL.cs
Lines 4 to 8 in ab63c9a
AaronRobinsonMSFT commentedon Jul 28, 2022
Lachlan Ennis (@elachlan) Good lord, I didn't even pay attention to that. Yes, that is very suspect. The Windows
BOOLis typically defined to be0isfalseandtrueis non-zero. There are some Win32 APIs that do questionable things with checking flags - blech.elachlan commentedon Jul 28, 2022
Aaron R Robinson (@AaronRobinsonMSFT) , do we just go with this?
AaronRobinsonMSFT commentedon Jul 28, 2022
Sure or, even simpler -
return this.value != 0;changes from discussions in microsoft#624
elachlan commentedon Jul 28, 2022
Aaron R Robinson (@AaronRobinsonMSFT) the
BOOLEANclass has a byte value backing field.CsWin32/src/Microsoft.Windows.CsWin32/templates/BOOLEAN.cs
Lines 3 to 8 in ab63c9a
The tests seem strange:
https://github.com/microsoft/CsWin32/blob/ab63c9ad139a77a43ad331f7334d38799d6125e2/test/GenerationSandbox.Tests/BooleanTests.cs
It looks like
BOOLEANis supposed to not equal when different values are supplied:CsWin32/test/GenerationSandbox.Tests/BooleanTests.cs
Lines 41 to 48 in ab63c9a
But then, when comparing a native
boolvalue. It is supposed to equal? I think this allows the byte value to be passed to a native bool and then get the value back again.CsWin32/test/GenerationSandbox.Tests/BooleanTests.cs
Lines 19 to 39 in ab63c9a
So the changes to
BOOLEANshould not be made according to the tests.elachlan commentedon Jul 28, 2022
I think the same tests exist for
BOOLas well.elachlan commentedon Jul 28, 2022
The documentation on the datatypes doesn't say anything about preserving the underlying values.
https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types
Andrew Arnott (@AArnott) have you got any guidance?
elachlan commentedon Jul 28, 2022
CC: Igor Velikorossov (@RussKie)
6 remaining items
jnm2 commentedon Jul 28, 2022
Instead of lossy implicit conversions to
bool, what if you instead have an explicit conversion toboolbut also declareoperator trueandoperator false?AaronRobinsonMSFT commentedon Jul 28, 2022
Aren't these types injected into the user's project as source?
AArnott commentedon Jul 28, 2022
Yes. Are you calling out that
SkipLocalsInitcan be applied assembly-wide, thus the generated code can misbehave?That's a very interesting idea. I didn't know about
operator true. Thanks for sharing.I haven't deeply studied the problem yet, but in passing it seems like the problem is a risk that we read more bytes than are guaranteed to be initialized. Isn't that solvable without changing the fundamental idea of preserving the underlying value (the part of it that's guaranteed to be initialized)?
AaronRobinsonMSFT commentedon Jul 28, 2022
Yes. I can accept that data coming from an unmanaged context may not want to be normalized but going in the other direction the code should really just be
this.value = v ? 1 : 0;AArnott commentedon Jul 28, 2022
What value is there in not being lossy in one conversion but being lossy in the other? Since C# makes extracting the non-binary value from a
boolvery difficult, it seems to me the value in being non-lossy is primarily in being able to jump across BOOL -> bool -> BOOL and having the original value.AaronRobinsonMSFT commentedon Jul 28, 2022
I don't see how that would work in practice. It would mean the
bool, which is always passed by value, maintains the interop binary value which isn't guarantee nor a good idea in practice. It is basically assuming the runtime will define itsboolABI to be what the interop layer decided is appropriate.elachlan commentedon Jul 28, 2022
Stephen Toub (@stephentoub), sorry to snipe you like this. Could you please chime in on how we should be handling interop
BOOL/BOOLEANtoboolconversion or blitting? Shouldboolever have an underlying value other thantrueorfalse?AArnott commentedon Jul 30, 2022
The runtime cannot change the memory layout of
boolat this point, can it? We expect it to be 1 byte long, and we may copy 1 byte into that memory. C# emits IL for bool conditions that treat any non-zero value as truthy, just as C does. I'm struggling to see how it's problematic then to copy the value from BOOL or BOOLEAN intoboolin an implicit conversion so that another implicit conversion can change it back. And this isn't me just trying to be clever... I had to work this out to resolve another issue (linked above).elachlan commentedon Aug 2, 2022
Do we just initialize the bytes ourselves when doing the conversion?
JeremyKuhne commentedon Aug 9, 2022
Andrew Arnott (@AArnott) this checked conversion is a hard blocker for WinForms. We're getting back
BOOLvalues from Windows APIs that overflow. I've tried providingtrueandfalseoperators, but they don't take precedence unfortunately. :(It's way too risky for us to have checked operations with
BOOL. We're probably going to have to shelve working on our porting exercise until we can get this resolved.Avoid overflow exceptions when converting `BOOL` to `bool`
Avoid overflow exceptions when converting `BOOL` to `bool`
Avoid overflow exceptions when converting `BOOL` to `bool`