c-std-porting.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: python-cffi@googlegroups.com
Cc: c-std-porting@lists.linux.dev
Subject: cffi.FFI.cdef and implicit function declarations
Date: Wed, 08 Feb 2023 11:10:37 +0100	[thread overview]
Message-ID: <87a61oqxgi.fsf@oldenburg.str.redhat.com> (raw)

We are trying to build Fedora with a compiler that does not support
implicit function declarations:

  <https://fedoraproject.org/wiki/Changes/PortingToModernC>
  <https://fedoraproject.org/wiki/Toolchain/PortingToModernC>

Gentoo is doing something simlar:

  <https://wiki.gentoo.org/wiki/Modern_C_porting

While investigating a false positive from our tester triggered by the
python-cffi testsuite, I noticed a curiousity with cdef: it does not
inject the specified prototype into the generated C stub.  This means
that unless Python.h happens to include a header with a function
declaration, an implicit declaration is the result.

For example, this snippet:

“
import cffi

ffi = cffi.FFI()
ffi.cdef("int puts(const char *);")
ffi.cdef("const char *getenv(const char *);")
ffi.cdef("const char *gnu_get_libc_release(void);")
lib = ffi.verify()
lib.puts(lib.getenv(b"TERM"))
lib.puts(lib.gnu_get_libc_release())
”

prints this on x86 32-bit:

“
vt100
stable
”

But the call to gnu_get_libc_release() crashes on x86-64 64-bit because
the implicit declaration has return type int, and the pointer is clipped
to 32 bits.

This not an issue if the function returns int, though.  And to support
implicit declarations, many POSIX interfaces return int anyway, so it
just works, although and ugly warning is printed on first use.

This will stop working once the compiler no longer accepts such implicit
function declarations.  How common is such use of cdef?  Do we need to
worry about it?  Is there something we need to do as part of the new
compiler rollout?

Thanks,
Florian


             reply	other threads:[~2023-02-08 10:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-08 10:10 Florian Weimer [this message]
2023-02-08 12:12 ` [python-cffi] cffi.FFI.cdef and implicit function declarations Armin Rigo
2023-02-08 12:18   ` Armin Rigo
2023-02-08 13:57     ` Florian Weimer

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=87a61oqxgi.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=c-std-porting@lists.linux.dev \
    --cc=python-cffi@googlegroups.com \
    /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 NNTP newsgroup(s).