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

ENHANCE: Avoid UBSAN errors #1289

Closed
wants to merge 16 commits into from
Closed

ENHANCE: Avoid UBSAN errors #1289

wants to merge 16 commits into from

Conversation

mgondan
Copy link
Contributor

@mgondan mgondan commented Jun 17, 2024

See here: https://www.stats.ox.ac.uk/pub/bdr/memtests/clang-UBSAN/rswipl/rswipl.log

The individual errors are mentioned in the individual commits. Four error messages are still left, these require inspection, I'll open an issue.

mgondan added 10 commits June 17, 2024 22:13
index -1 out of bounds for type 'definition_ref[7]'
index -1 out of bounds for type 'definition_ref[7]'
addition of unsigned offset to 0x7f6d6eb33000 overflowed to 0x7f6d6eb32fff
applying non-zero offset 64 to null pointer
applying non-zero offset 8864 to null pointer (from incrementing NULL with ++)
index -1 out of bounds for type 'definition_ref[7]'
member access within null pointer of type 'struct clause'
applying non-zero offset 64 to null pointer
member access within null pointer of type 'var_table'
member access within null pointer of type 'struct bit_vector'
@mgondan
Copy link
Contributor Author

mgondan commented Jun 17, 2024

The above mentioned issue is here, #1290

@mgondan mgondan marked this pull request as ready for review June 17, 2024 22:34
mgondan added 2 commits June 23, 2024 16:09
If stack->top is NULL, switch to else branch
If stack->top is NULL, switch to ":" branch
@mgondan
Copy link
Contributor Author

mgondan commented Jun 23, 2024

I think the remaining problems have been solved with the last two commits.

@JanWielemaker
Copy link
Member

Hmm. Except for pl-unzip.c, most of this makes the code uglier without giving a lot of benefits. We could probably avoid some/all of the offset warnings by using the standard offsetof(), which was not standard when most of this was implemented. I'd welcome that. Do we really want to avoid the refs->preallocated - 1 index-out-of-bound? Yes, it is out of bound, but that is still the case after decrementing it and it is the whole point of these thread-safe dynamically scaling arrays.

@mgondan
Copy link
Contributor Author

mgondan commented Jul 8, 2024

Do we really want to avoid the
refs->preallocated - 1;
index-out-of-bound?

I agree that some of the workarounds are not pretty, but having code that passes UB checks is a good thing in itself.

And I mean,

p = refs->preallocated; p--;

is not that ugly, or is it?

@mgondan
Copy link
Contributor Author

mgondan commented Jul 8, 2024

Updated the comment after having pressed "submit" too quickly.

@JanWielemaker
Copy link
Member

And I mean,

p = refs->preallocated; p--;

is not that ugly, or is it?

Well, it is hard to explain and I'd probably change the code back when scanning the code later ... More seriously, it is just as "buggy", but just hides it a level deeper such that the current tools do not spot it. The code uses pointers outside an array and the code ensures these pointers used with an offset that gets/sets valid data. That is just a step too complicated for these tools.

The question to me is what are we trying to improve and/or who are we trying to satisfy? If that is sufficiently motivated I vote for some macro that captures this assignment which we can document. That avoids people asking themselves what this code is doing.

@mgondan
Copy link
Contributor Author

mgondan commented Jul 8, 2024

More seriously, it is just as "buggy", but just hides it a level deeper such that the current tools do not spot it.

Good point indeed.

The question to me is what are we trying to improve and/or who are we trying to satisfy?

The usual motivation, the R interface ("rswipl"/"rolog"). They remove the package from CRAN if it's not fixed.

@JanWielemaker
Copy link
Member

They remove the package from CRAN if it's not fixed.

That is a good reason 😢 Do you think you can fix the offset issues using standard offsetof()? I'm happy to have a look whether I can find something more elegant to this.

How do I reproduce this? I thought the code was ubsan-safe after the previous series of changes?

@mgondan
Copy link
Contributor Author

mgondan commented Jul 8, 2024

Reproduce with: CC="clang -fsanitize=undefined" cmake -DSWIPL_PACKAGES_X=OFF -DINSTALL_DOCUMENTATION=OFF ..

Unsure if clang is needed, may also work with gcc

I thought the code was ubsan-safe after the previous series of changes?

IIRC the previous fixes referred to rswipl's own routines. Now it's swipl's own build process.

@mgondan
Copy link
Contributor Author

mgondan commented Jul 8, 2024

Do you think you can fix the offset issues using standard offsetof()?

I can have a look at it next week

@mgondan
Copy link
Contributor Author

mgondan commented Jul 9, 2024

Do you think you can fix the offset issues using standard offsetof()?

Done. It's a bit less ugly now.

@JanWielemaker
Copy link
Member

All the work in this PR is handled. clang-18 seems to add some more ubsan issues, among which checking the types for calls over function pointers. This raised a couple of possible portability issues. I fixed some of them, but not yet all. I now have to work on other projects ....

Thanks for the work.

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