Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 6 Sep 2022 10:36:52 -0600
From:      Alan Somers <asomers@freebsd.org>
To:        Warner Losh <imp@bsdimp.com>
Cc:        Konstantin Belousov <kostikbel@gmail.com>, FreeBSD CURRENT <freebsd-current@freebsd.org>
Subject:   Re: Header symbols that shouldn't be visible to ports?
Message-ID:  <CAOtMX2jWrcmGueEVDmkkCxceYtx4MXXxfWwGDMuqF6dYi4aPSA@mail.gmail.com>
In-Reply-To: <CANCZdfq%2BTYTgbU_DidjuAjy5CjQyJoug2qv1srKj96EK0C_HAQ@mail.gmail.com>
References:  <CAOtMX2h_=6AXYDSZNF77qQH9fF1gsJKuDP%2BM3dD%2Bq6Xw97bHmg@mail.gmail.com> <YxQzIf/xmwqz1Sn1@kib.kiev.ua> <CAOtMX2g0TkQvM6N0yf_fr667XpAFupyY6auf8_8H6VJt7MqEkA@mail.gmail.com> <YxdMXrzZHNlrgPXg@kib.kiev.ua> <CANCZdfq%2BTYTgbU_DidjuAjy5CjQyJoug2qv1srKj96EK0C_HAQ@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Sep 6, 2022 at 9:07 AM Warner Losh <imp@bsdimp.com> wrote:
>
>
>
> On Tue, Sep 6, 2022 at 7:34 AM Konstantin Belousov <kostikbel@gmail.com> wrote:
>>
>> On Mon, Sep 05, 2022 at 08:41:58AM -0600, Alan Somers wrote:
>> > On Sat, Sep 3, 2022 at 11:10 PM Konstantin Belousov <kostikbel@gmail.com> wrote:
>> > >
>> > > On Sat, Sep 03, 2022 at 10:19:12AM -0600, Alan Somers wrote:
>> > > > Our /usr/include headers define a lot of symbols that are used by
>> > > > critical utilities in the base system like ps and ifconfig, but aren't
>> > > > stable across major releases.  Since they aren't stable, utilities
>> > > > built for older releases won't run correctly on newer ones.  Would it
>> > > > make sense to guard these symbols so they can't be used by programs in
>> > > > the ports tree?  There is some precedent for that, for example
>> > > > _WANT_SOCKET and _WANT_MNTOPTNAMES.
>> > > _WANT_SOCKET is clearly about exposing parts of the kernel definitions
>> > > for userspace code that wants to dig into kernel structures.  Similarly
>> > > for _WANT_MNTOPTNAMES, but in fact this thing is quite stable.  The
>> > > definitions are guarded by additional defines not due to their instability,
>> > > but because using them in userspace requires (much) more preparation from
>> > > userspace environment, which is either not trivial (_WANT_SOCKET) or
>> > > contradicts to standartized use of the header (_WANT_MNTOPTNAMES +
>> > > sys/mount.h).
>> > >
>> > > >
>> > > > I'm particular, I'm thinking about symbols like the following:
>> > > > MINCORE_SUPER
>> > > Why this symbol should be hidden?  It is implementation-defined and
>> > > intended to be exposed to userspace.  All MINCORE_* not only MINCORE_SUPER
>> > > are under BSD_VISIBLE braces, because POSIX does not define the symbols.
>> >
>> > Because it isn't stable.  It changed for example in rev 847ab36bf22
>> > for 13.0.  Programs using the older value (including virtually every
>> > Rust program) won't work on 13.0 and later.
>> As Mark replied, older values still mostly work.  It was considered to
>> not make unacceptable ABI change.
>>
>> >
>> > >
>> > > > TDF_*
>> > > These symbols coming from non-standard header sys/proc.h.  If userspace
>> > > includes the header, it is already outside any formal standard, and I
>> > > do not see a reason to make the implementation more convoluted there.
>> > >
>> > > > PRI_MAX*
>> > > > PRI_MIN*
>> > > > PI_*, PRIBIO, PVFS, etc
>> > > > IFCAP_*
>> > > These are all implementation-specific and come from non-standard headers,
>> > > unless I am mistaken, then please correct me.
>> > >
>> > > > RLIM_NLIMITS
>> > > > IFF_*
>> > > Same.
>> > >
>> > > > *_MAXID
>> > > This is too broad.
>> >
>> > I'm talking about symbols like IPV6CTL_MAXID, which record the size of
>> > sysctl lists.  Obviously, these symbols can't be stable, and probably
>> > aren't useful outside of the base system.
>> The programs are not forced to use the symbols.  FFI bindings should not
>> provide them, why do we need to specifically hide such defines?

Because if anybody ever adds it to the libc crate, then it's basically
stuck there forever.  There's precedent for hiding defines like this:
https://reviews.freebsd.org/D25816

>>
>> >
>> > >
>> > > >
>> > > > Clearly delineating private symbols like this would ease the
>> > > > maintenance burden on languages that rely on FFI, like Ruby and Rust.
>> > > > FFI basically assumes that symbols once defined will never change.
>> > >
>> > > Why e.g. sys/proc.h is ever consumed by FFI wrappers?
>> >
>> > I should add a little detail.  Rust uses FFI to access C functions,
>> > and #define'd constants are redefined in the Rust bindings.  For most
>> > Rust programs, the build process doesn't check the contents of
>> > /usr/include in any way.  Instead, all of that stuff is hard-coded in
>> > the Rust bindings.  That makes cross-compiling a breeze!
>> Well, at the cost of the maintaining Rust libc crate.
>> [Sorry, cannot refrain https://kib.kiev.ua/kib/rust_c_ffi.png ]
>>
>> > But it does
>> > cause problems when the C library changes.  Adding a new symbol, like
>> > copy_file_range, isn't so bad.  If your Rust program doesn't use it,
>> > then the Rust binding will become an unused symbol and get eliminated
>> > by the linker.  If your Rust program does use it OTOH, then it will be
>> > resolved by the dynamic linker at runtime - if you're running on
>> > FreeBSD 13 or newer.  Otherwise, your program will fail to run.
>> The program would either fail at start if it does not reference the
>> symbol version in some other way (due to other symbol), or at runtime
>> when trying to do dynamic binding to that symbol otherwise.
>>
>> > A
>> > bigger problem is with symbols that change.  For example, the 64-bit
>> > inode stuff.  Rust programs still use a FreeBSD 11 ABI (we're working
>> > on that).
>> We did not changed symbols for ino64.  Old symbols were retained, the new
>> symbols were added under the new version.

Yes, I spoke imprecisely.  I should've said "defines", not "symbols"
as in "ELF symbols".  The changes to defines like "struct stat" are
the reason that the libc crate is stuck with the FreeBSD 11 ABI.

>>
>> > But other symbols change more frequently.  Things like
>> > PRI_MAX_REALTIME can change between any two releases.  That creates a
>> > big maintenance burden to keep track of them in the FFI bindings.  And
>> > they also aren't very useful in cross-compiled programs targeting a
>> > FreeBSD 11 ABI.  Instead, they really need to have bindings
>> > automatically generated at build time.  That's possible, but it's not
>> > the default.
>> >
>> > So what the Rust community really needs is a way to know which symbols
>> > will be stable across releases, and which might vary.
>> Symbols, as something exported from libc/libthr/libm, are stable.
>> We promise this and follow this promise strictly from FreeBSD 6.x.
>>
>> Some defines from headers are not stable, but they do not form the exported
>> system ABI anyway.  You need to know what you are doing when changing libc.
>> Similarly, when you update Rust libc crate, you have to know what you are
>> providing, it cannot be done automatically.
>
>
> FreeBSD developers get this wrong from time to time. We have to carefully curate
> new symbols to the libraires, and deal with #defines that are part of the core ABI that
> we've kept stable. We don't have any formal tests here to ensure things work, apart
> from people trying things and having them break due to some oversight.
>
> For example, recently, the CAM ioctls changed so that old passthrough CCB ioctls
> stopped working. It took months for people to notice. It was broken in a release. The
> fix was a trivial one-liner once someone noticed and made the effort. Since there was
> no automation to test it, it went unnoticed.
>
>>
>> Expecting that we (FreeBSD developers) would mark up each definition in
>> the headers files is unreasonable.  Even if this enormous work would be
>> done once, it rot immediately.  The outcome of the work is not used by
>> anything in either the base system, or in 99.999% of the ports.  As result,
>> anybody doing any work on the base libraries, make mistakes.
>>
>> > Are you
>> > suggesting that anything from a non-POSIX header file should be
>> > considered variable?
>> No, I suggest that anything not in POSIX namespace should be scrutinized
>> for ABI stability, instead of stating that 'it is available, so lets make
>> bindings for it'.

Well, that would be nice.  But it's too late :( .  People add defines
all the time, and then we're pretty much stuck with them, owing to
libc's very conservative backwards-compatibility policy.  For example
https://github.com/rust-lang/libc/pull/2572 , which added a bunch of
MNTK_* defines.

>>
>> I have the sympathy for Rust decision to provide isolated libc crate.
>> It certainly makes sense for the Rust ecosystem.
>>
>> But then having this crate to depend on autogeneration from /usr/include
>> negates the intent of isolation.  I think, if you want to have
>> automatic binding generation used, you must provide the white list of
>> symbols and definitions that go into the crate.

Actually, that's technically how it works.  There is no autogeneration
for libc.  But there _is_ autogeneration during its CI tests.  So
effectively all of the defined constants _are_ a whitelist.  The
problem is that people add to the whitelist too eagerly.

>
>
> I agree with kib: I don't see how asking developers to know, a priori,
> which of our unstable interfaces will change and which won't.  If you import
> things that aren't in the POSIX namespace, you need to work with those
> namespace providers to import them properly. Whitelisting is the only way
> that I see it working.
>
> At most, we have resources to allow you to maintain this list, and allow you
> to make changes on a best effort basis that history has suggested will be
> about 90% successful. Absent some automation that enforces it, it will break.
> And even with automation, you'll need someone to review breakage and/or
> additions to ensure the right things get updated. Developers might be able
> to lend a hand, but that can't be relied upon without oversight to work.
>
> Warner

So you both seem to be saying that there's no rule we can use to
exclude some defines from libc, and that everything will have to be
evaluated on a case-by-case basis, right?  Ok, I can do that.  It'll
just be more effort than I'd hoped for.
-Alan



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAOtMX2jWrcmGueEVDmkkCxceYtx4MXXxfWwGDMuqF6dYi4aPSA>