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 TableKey / ObjKey conversion on Android x86 #2477

Merged
merged 16 commits into from
Jun 30, 2021

Conversation

DominicFrei
Copy link
Contributor

@DominicFrei DominicFrei commented Jun 28, 2021

Description

When retrieving TableKey and ObjKey from Core we interpret the returned values of type UInt32 and Int64 as TableKey and ObjKey without any conversion.

On Android x86 this leads to a misalignment in memory though since TableKey and ObjKey are structs which leads to a crash as described in #2456.

This PR reads the return values correctly and converts them in their corresponding Handle into the structs used on the .NET side.

N.B.: No further tests added. The problem that this was not caught by our tests is not the test suite itself but the fact that we do not test on Android x86 at the moment. As soon as such a device is added and run the current tests will implicitly apply.

In addition to that the hash code of objects is now calculated using table key in addition to just the object key (see #2473).

Fixes #2456
Fixes #2473

TODO

  • Changelog entry
  • Tests (if applicable)

@DominicFrei DominicFrei force-pushed the df/fix-tablekey-conversion-crash branch from 32273cf to 0856397 Compare June 29, 2021 11:35
@DominicFrei DominicFrei requested a review from nirinchev June 29, 2021 11:42
Copy link
Member

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

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

Final nits, otherwise it looks good.

// ObjKey is incompatible with C, so we return just the value.
return object.obj().get_key().value;
auto table_key_value = object.obj().get_table()->get_key().value;
auto table_key_hash = std::hash<int>{}(table_key_value);
Copy link
Member

Choose a reason for hiding this comment

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

Do we need these std::hash-es here? Can we use table_key_value directly - maybe with a cast?

Copy link
Contributor Author

@DominicFrei DominicFrei Jun 29, 2021

Choose a reason for hiding this comment

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

I guess the normal way would be to only use them since they are already provided by the standard library but the other way round should be fine as well. Removed them.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm not familiar with how C++ does things, so happy to use the idiomatic way there - it's just coming from C#, you would rarely use GetHashCode on a primitive type when you can just use the value itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did some more research now that you mention it. Good point about the primitives:
https://stackoverflow.com/questions/38304877/why-stdhashint-seems-to-be-identity-function
It makes sense but means that I can just remove it since std::hash<int>(i) == i anyway.

I'll just keep it our way then. There are certainly many more ways but if the current implementation is fine I guess we can just keep it.
I thought about just concatenating table key and object key since that would always lead to a unique 'hash':

auto table_key_value = object.obj().get_table()->get_key().value;
auto object_key_value = object.obj().get_key().value;

std::string concatenation = "" + table_key_value + object_key_value;
int hashCode = std::stoi(concatenation);

return hashCode;

@DominicFrei
Copy link
Contributor Author

I had to add c249f25 since there was a valid warning on UWP:

'return': conversion from '__int64' to 'int32_t', possible loss of data

@nirinchev fyi + above comment about hashes.

It's just a tiny change, but I don't like merging unseen commits.

@DominicFrei DominicFrei requested a review from nirinchev June 29, 2021 16:01
auto table_key_value = object.obj().get_table()->get_key().value;
auto object_key_value = object.obj().get_key().value;

int32_t hashCode = -9999999769;
Copy link
Member

Choose a reason for hiding this comment

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

That warning is correct - -9999999769 is beyond what int32 can hold. This is what VS generated for a simple struct with table key and object key if you want to use it:

            public override int GetHashCode()
            {
                var hashCode = -986587137;
                hashCode = hashCode * -1521134295 + ObjKey.GetHashCode();
                hashCode = hashCode * -1521134295 + TableKey.GetHashCode();
                return hashCode;
            }

@nirinchev nirinchev force-pushed the df/fix-tablekey-conversion-crash branch from 85f88c5 to 270ab19 Compare June 30, 2021 02:34
@nirinchev nirinchev merged commit a6c5571 into master Jun 30, 2021
@nirinchev nirinchev deleted the df/fix-tablekey-conversion-crash branch June 30, 2021 03:50
nirinchev added a commit that referenced this pull request Jul 6, 2021
* master:
  Update the AppConfiguration.Logger obsolete message (#2495)
  Add more serializer types to the preserved ones (#2489)
  Update changelog from Core (#2488)
  Build an xcframework for iOS (#2475)
  Prepare for vNext (#2487)
  Prepare for 10.2.1 (#2484)
  Fix TableKey / ObjKey conversion on Android x86 (#2477)
  Update the build manager locations (#2474)
  Clean up native resources on Unity application exit (#2467)
  Fix some native warnings (#2483)
  Don't try to get the app from a removed user (#2480)
  Use delete_files from core when deleting a Realm. (#2401)
  Fixes for the sync datatype tests (#2461)
  Clean up the package.json and add some docs (#2452)
  Run iOS tests on CI (#2405)
  Verify that objects do not belong to different realm when added to collection (#2465)
  Updated README instructions (#2459)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants