public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Aliaksey Kandratsenka <alkondratenko@gmail.com>,
	libc-help@sourceware.org
Subject: Re: Seeking advise on most portable way to detect 64-bit off_t
Date: Mon, 4 Mar 2024 09:04:53 -0300	[thread overview]
Message-ID: <b4b68b4b-5b6e-436e-ab6a-7fc6048522ce@linaro.org> (raw)
In-Reply-To: <CADpJO7xVotURFVnwhLqwT3EQNzb0Qrc7_CQyLVsubprpNTN4Mw@mail.gmail.com>



On 01/03/24 18:06, Aliaksey Kandratsenka via Libc-help wrote:
> Hi.
> 
> In gperftools we have the feature to hook mmap calls which works by
> interposing over glibc's mmap. So it has to deal with off_t complexities.
> We don't use that offset argument, other than just passing it to real mmap
> implementation. I am also trying to support different Linux libc-s just for
> completeness. And musl being new enough and wise enough has always had
> 64-bit off_t (bionic hasn't and there is really no excuse...)
> 
> So with that I need some means to detect 32-bit off_t that works across
> multiple libcs and has the highest hope of working in the future.
> 
> By looking around, I choose to detect 32-bit-ness of off_t by looking at
> semi-obscure define _POSIX_V7_ILP32_OFF32:
> https://github.com/gperftools/gperftools/blob/37b59a206e36b405dcb2ac09d502875dd629a80b/src/mmap_hook.cc#L275
> 
> It does seem to do the right thing across implementations I checked. Well
> mostly.
> 
> These days, Debian folk are doing a massive 64-bit time_t transition (which
> also includes mass-enabling 64-bit off_t, at last) and they're rebuilding
> everything with _FILE_OFFSET_BITS=64. And my "approach" fails.
> https://buildd.debian.org/status/fetch.php?pkg=google-perftools&arch=armel&ver=2.15-1.1&stamp=1709166723&file=log
> 
> Glibc doesn't adjust _POSIX_V7_ILP32_{BIGOFF,OFF32} defines based on
> _FILE_OFFSET_BITS. Perhaps rightly so. But with that situation I need some
> other, and hopefully more robust means to detect off_t width.
> 
> I am thinking of 3 options:
> 
> * Keep _POSIX_V7 thingy but also add a check for _FILE_OFFSET_BITS == 64.
> And then behave as if off_t is 32-bit.
> * #undef _FILE_OFFSET_BITS at the first line in mmap_hooks.cc. It will
> cause glibc on those legacy 32-bit off_t systems to define 32-bit off_t and
> give me right defines and my code will define mmap symbol with 32-bit
> offset arg and mmap64 symbol with 64-bit offset. And thus matching glibc.
> * just manually hardcode knowledge that glibc +

You need to undefine _FILE_OFFSET_BITS to get the expected types for 
off_t and off64_t on the mmap_hook.cc TU, since the whole idea is to
override the symbol.  You can also try to poke on glibc internals and
use __off_t and __off64_t, which would not be redefined depending the
the preprocessor (but tying to internal implementation is always a bad
idea).

So I would recommend this option, which seems to what you did and it is
what sanitizers do also [1].

> {i386,arm{el,hf},mips32,ppc32} has 32-bit off_t. But that leaves the
> question of how to deal with e.g. bionic (well, I could in principle say,
> bionic already being broken enough is only supported for 64-bit systems,
> which is where android systems are moving anyways)
> 
> I am looking for any comments or suggestions. Particularly I'd like my fix
> to be robust w.r.t. any further glibc evolution.

[1] https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.cpp#L15

  reply	other threads:[~2024-03-04 12:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-01 21:06 Aliaksey Kandratsenka
2024-03-04 12:04 ` Adhemerval Zanella Netto [this message]
2024-03-07 18:31   ` Aliaksey Kandratsenka
2024-03-12 12:06     ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b4b68b4b-5b6e-436e-ab6a-7fc6048522ce@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=alkondratenko@gmail.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).