Skip to content

BOOL/BOOLEAN templates are incorrect #624

Closed

Description

The BOOL and BOOLEAN template constructors taking a C# bool are invalid.

internal unsafe BOOL(bool value) => this.Value = *(sbyte*)&value;

internal unsafe BOOLEAN(bool value) => this.Value = *(byte*)&value;

The BOOL template should be, the BOOLEAN is similar.

internal unsafe BOOL(bool value) => this.Value = value ? 1 : 0;

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.

Activity

  1. changed the title [-]`BOOL` template is incorrect[/-] [+]`BOOL`/`BOOLEAN` templates are incorrect[/+] on Jul 27, 2022
  2. JeremyKuhne commented on Jul 27, 2022

    @JeremyKuhne
    Member

    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?

  3. jnm2 commented on Jul 27, 2022

    @jnm2
    Contributor

    Also, comparisons in different contexts don't operate the same on bool values that aren't identical to true or false: dotnet/roslyn#24865 (comment)

  4. AaronRobinsonMSFT commented on Jul 27, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    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.

  5. added a commit that references this issue on Jul 27, 2022
    c3eedf8
  6. elachlan commented on Jul 27, 2022

    @elachlan
    Contributor

    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?

    public static unsafe implicit operator bool(BOOL value)
    {
    sbyte v = checked((sbyte)value.Value);
    return *(bool*)&v;
    }

  7. AaronRobinsonMSFT commented on Jul 28, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    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.

  8. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor

    Aaron R Robinson (@AaronRobinsonMSFT) , do we just go with this?

    public static unsafe implicit operator bool(BOOL value)
    {
    return this.value != 0 ? true : false;
    }
  9. AaronRobinsonMSFT commented on Jul 28, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    Aaron R Robinson (@AaronRobinsonMSFT) , do we just go with this?

    Sure or, even simpler - return this.value != 0;

  10. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor

    Aaron R Robinson (@AaronRobinsonMSFT) the BOOLEAN class has a byte value backing field.

    internal unsafe BOOLEAN(bool value) => this.Value = *(byte*)&value;
    public static unsafe implicit operator bool(BOOLEAN value)
    {
    byte v = checked((byte)value.Value);
    return *(bool*)&v;
    }

    The tests seem strange:
    https://github.com/microsoft/CsWin32/blob/ab63c9ad139a77a43ad331f7334d38799d6125e2/test/GenerationSandbox.Tests/BooleanTests.cs

    It looks like BOOLEAN is supposed to not equal when different values are supplied:

    [Fact]
    public void BOOLEANEqualsComparesExactValue()
    {
    BOOLEAN b1 = new BOOLEAN(1);
    BOOLEAN b2 = new BOOLEAN(2);
    Assert.Equal(b1, b1);
    Assert.NotEqual(b1, b2);
    }

    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.

    [Theory]
    [InlineData(3)]
    [InlineData(0xff)]
    public void NotLossyConversionBetweenBoolAndBOOLEAN(byte ordinal)
    {
    BOOLEAN nativeBool = new BOOLEAN(ordinal);
    bool managedBool = nativeBool;
    BOOLEAN roundtrippedNativeBool = managedBool;
    Assert.Equal(nativeBool, roundtrippedNativeBool);
    }
    [Theory]
    [InlineData(3)]
    [InlineData(0xff)]
    public void NotLossyConversionBetweenBoolAndBOOLEAN_Ctors(byte ordinal)
    {
    BOOLEAN nativeBool = new BOOLEAN(ordinal);
    bool managedBool = nativeBool;
    BOOLEAN roundtrippedNativeBool = new BOOLEAN(managedBool);
    Assert.Equal(nativeBool, roundtrippedNativeBool);
    }

    So the changes to BOOLEAN should not be made according to the tests.

  11. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor

    I think the same tests exist for BOOL as well.

  12. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor

    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?

  13. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor
  14. 6 remaining items

  15. jnm2 commented on Jul 28, 2022

    @jnm2
    Contributor

    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?

  16. AaronRobinsonMSFT commented on Jul 28, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    It sounds like we don't have a bug at this point because we in fact don't yet set SkipLocalsInit

    Aren't these types injected into the user's project as source?

  17. AArnott commented on Jul 28, 2022

    @AArnott
    Member

    Aren't these types injected into the user's project as source?

    Yes. Are you calling out that SkipLocalsInit can be applied assembly-wide, thus the generated code can misbehave?

    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)?

  18. AaronRobinsonMSFT commented on Jul 28, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    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;

  19. AArnott commented on Jul 28, 2022

    @AArnott
    Member

    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 bool very 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.

  20. AaronRobinsonMSFT commented on Jul 28, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    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.

  21. elachlan commented on Jul 28, 2022

    @elachlan
    Contributor

    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?

  22. AArnott commented on Jul 30, 2022

    @AArnott
    Member

    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).

  23. elachlan commented on Aug 2, 2022

    @elachlan
    Contributor

    Do we just initialize the bytes ourselves when doing the conversion?

  24. JeremyKuhne commented on Aug 9, 2022

    @JeremyKuhne
    Member

    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.

  25. added 3 commits that reference this issue on Aug 9, 2022
    a5db865
    a0bc5e5
    55f4867
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugpartner

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions