This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Use intptr_t instead of ptrdiff_t for messages_base::catalog
ClosedPublic

Authored by ldionne on Sep 22 2022, 1:58 AM.

Details

Summary

On GLibc, FreeBSD and macOS systems nl_catd is a pointer type, and
round-tripping this in a variable of ptrdiff_t is not portable.
In fact such a round-trip yields a non-dereferenceable pointer on
CHERI-enabled architectures such as Arm Morello. There pointers (and
therefore intptr_t) are twice the size of ptrdiff_t, which means casting
to ptrdiff_t strips the high (metadata) bits (as well as a hidden pointer
validity bit).

Since catalog is now guaranteed to be the same size or larger than nl_catd,
we can store all return values safely and the shifting workaround from
commit 0c68ed006d4f38c3cdcab6a565aa3e208015895f should not be needed
anymore (this is also not portable to CHERI systems on since shifting a
valid pointer right will create a massively out-of-bounds pointer that
may not be representable).

This can be fixed by using intptr_t which should be the same type as
ptrdiff_t on all currently supported architectures.

See also: https://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2028

Diff Detail

Event Timeline

arichardson created this revision.Sep 22 2022, 1:58 AM
arichardson requested review of this revision.Sep 22 2022, 1:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 22 2022, 1:58 AM
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald Transcript
Mordante added inline comments.
libcxx/include/locale
3464

Did you check these types are the same on all supported platforms.
I noticed intptr_t is not guaranteed to be available on all platforms. Is that an issue?

3539

Is this code used in C++03? static_assert is a C++11 feature.

philnik added inline comments.
libcxx/include/locale
3539

We have static_assert in C++03. We #define static_assert(...) _Static_assert(__VA_ARGS__) in __config.

arichardson added inline comments.Sep 26 2022, 4:39 AM
libcxx/include/locale
3464

I've looked at clang and for almost all targetinfo classes PtrDiffType uses the same type as IntPtrType. The only exception I can see is MIPS O32 ABI which uses Int for ptrdiff_t but long for intptr_t (those are the same size just different integer types). In case this is a problem, I'm not sure if MIPS O32 ABI is actually supported - if 32-bit is supported I would assume it's only N32 ABI.

FreeBSD stddef.h checks SIZEOF_PTRDIFF_T and SIZEOF_POINTER, so if those match inside clang they will use the same types (which is true for all FreeBSD architectures other than CHERI-extended ones, which are not supported upstream)

I think in theory they could be different with other OS/compiler-provided stddef.h, but I'd be surprised if they were since we don't yet support any architectures that don't have a flat address space (which should imply size_t==intptr_t).

arichardson added inline comments.Sep 26 2022, 4:45 AM
libcxx/include/locale
3464

I guess this could change mangled names for 32-bit MIPS, but https://libcxx.llvm.org/#platform-and-compiler-support does not list MIPS anywhere, so maybe this is okay? Otherwise I could add ifdefs (but I'd prefer not to).

rebase for CI

Mordante accepted this revision as: Mordante.Sep 29 2022, 9:12 AM
Mordante added a subscriber: ldionne.

LGTM, but I think it would be good for @ldionne to have a look too.

libcxx/include/locale
3464

Thanks for the info, then it seems it's not an issue to make this change.

3539

Thanks.

@ldionne do you think this is acceptable, or should I add additional ifdefs to avoid potential name mangling changes on 32-bit MIPS? To be honest I'm not sure if this type ever leaks into mangled names or if it's only ever used in inlined functions.

ping @ldionne . Would be nice to land this so that I can successfully run the libc++ testsuite on Morello.

ping @ldionne. I am pretty certain this is ABI compatible with all supported architectures, but I can add a release note if you like?

Is there any chance someone else who has the ability to approve this for #libc could do so since it appears @ldionne is not interested in this change. I get that this currently does not break any production targets, but IMO it does clean up the code for glibc and macOS as well.

Is there any chance someone else who has the ability to approve this for #libc could do so since it appears @ldionne is not interested in this change. I get that this currently does not break any production targets, but IMO it does clean up the code for glibc and macOS as well.

I'm not disinterested, I just didn't see it amongst the ton of changes that I get pinged on. Let me take a look now.

ldionne accepted this revision.Jul 10 2023, 9:38 AM

This seems like a good change to me. I had ABI impact concerns but I read and re-read your commit + the discussion in the thread, and I am fine with this.

LGTM but we need to add a few tests, rebase this and get a green CI run.

libcxx/include/locale
3456

Can we add a libc++ specific test that asserts is_same<messages_base::catalog, intptr_t>?

Also a non-libc++-specific test to check for the implementation of LWG2028? It could assert that is_signed<messages_base::catalog>.

This revision is now accepted and ready to land.Jul 10 2023, 9:38 AM

Oh and thanks for the cleanup BTW, this is a lot better than what we had before.

Oh and thanks for the cleanup BTW, this is a lot better than what we had before.

Thanks for the review, will try to address your comments ASAP

ldionne commandeered this revision.Sep 7 2023, 8:30 AM
ldionne edited reviewers, added: arichardson; removed: ldionne.

[Github PR transition cleanup]

Commandeering to finish.

ldionne updated this revision to Diff 556161.Sep 7 2023, 8:39 AM
ldionne marked 5 inline comments as done.

Address comments and rebase.

arichardson accepted this revision.Sep 7 2023, 6:54 PM

Thanks for updating this change and apologies for not getting to it myself!

ldionne accepted this revision.Sep 8 2023, 6:15 AM

No worries!

Shipping now, the CI failure is something else.

This revision was landed with ongoing or failed builds.Sep 8 2023, 6:16 AM
This revision was automatically updated to reflect the committed changes.