public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Correct way to provide a C callback function nside C++
@ 2023-01-27 13:23 Pepe
  2023-01-27 13:48 ` Tom Kacvinsky
  2023-01-27 14:07 ` Jonathan Wakely
  0 siblings, 2 replies; 7+ messages in thread
From: Pepe @ 2023-01-27 13:23 UTC (permalink / raw)
  To: gcc-help

Hi there,

I’m in an ongoing discussion about whether or not one should use extern “C” when defining a function that will be used as a callback in a statically linked C library. For example:

c_func.h:
// …
void reg_callback(void (*fn)());
// …

cpp_impl.cpp:
// …
extern “C” {
#include “c_func.h”
}

// my callback function with internal linkage
namespace {

extern “C” {
static void my_callback_A() {
	// …
}
} // extern “C”

void my_callback_B() {
	// …
}

} // namespace

void do_something() {
	reg_callback(my_callback_A);
	reg_callback(my_callback_B);
}

Both callbacks have internal linkage. Both work fine, and something like my_callback_B is found in lots of code bases.

In my opinion, using callback B is implementation defined behaviour, because it is not guaranteed that C and C++ use the same calling conventions. Therefore a function must adhere to the C calling conventions to be used as a callback in a C library, which would be callback A.

I’ve been trying to find something definitive for days now, but to no avail. Now I’m not sure what’s true or not. The counter argument is the following: The compiler should know reg_callback is a C function and make sure that a given argument would either be valid or cause a compiler error. That sounds reasonable, so I would love to know how to do it properly for future reference. Given we use gcc I was hoping to get a definitive answer in this mailing list. Thanks a lot!

Pepe

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 13:23 Correct way to provide a C callback function nside C++ Pepe
@ 2023-01-27 13:48 ` Tom Kacvinsky
  2023-01-27 13:51   ` Jonathan Wakely
  2023-01-27 14:07 ` Jonathan Wakely
  1 sibling, 1 reply; 7+ messages in thread
From: Tom Kacvinsky @ 2023-01-27 13:48 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1867 bytes --]

On Fri, Jan 27, 2023 at 8:24 AM Pepe via Gcc-help <gcc-help@gcc.gnu.org>
wrote:

> Hi there,
>
> I’m in an ongoing discussion about whether or not one should use extern
> “C” when defining a function that will be used as a callback in a
> statically linked C library. For example:
>
> c_func.h:
> // …
> void reg_callback(void (*fn)());
> // …
>
> cpp_impl.cpp:
> // …
> extern “C” {
> #include “c_func.h”
> }
>
> // my callback function with internal linkage
> namespace {
>
> extern “C” {
> static void my_callback_A() {
>         // …
> }
> } // extern “C”
>
> void my_callback_B() {
>         // …
> }
>
> } // namespace
>
> void do_something() {
>         reg_callback(my_callback_A);
>         reg_callback(my_callback_B);
> }
>
> Both callbacks have internal linkage. Both work fine, and something like
> my_callback_B is found in lots of code bases.
>
> In my opinion, using callback B is implementation defined behaviour,
> because it is not guaranteed that C and C++ use the same calling
> conventions. Therefore a function must adhere to the C calling conventions
> to be used as a callback in a C library, which would be callback A.
>

You can do a callback (using function pointers) in C++ code.

https://en.cppreference.com/w/cpp/language/pointer


> I’ve been trying to find something definitive for days now, but to no
> avail. Now I’m not sure what’s true or not. The counter argument is the
> following: The compiler should know reg_callback is a C function and make
> sure that a given argument would either be valid or cause a compiler error.
> That sounds reasonable, so I would love to know how to do it properly for
> future reference. Given we use gcc I was hoping to get a definitive answer
> in this mailing list. Thanks a lot!
>
> Pepe
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 13:48 ` Tom Kacvinsky
@ 2023-01-27 13:51   ` Jonathan Wakely
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2023-01-27 13:51 UTC (permalink / raw)
  To: Tom Kacvinsky; +Cc: gcc-help

On Fri, 27 Jan 2023 at 13:49, Tom Kacvinsky via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
> You can do a callback (using function pointers) in C++ code.
>
> https://en.cppreference.com/w/cpp/language/pointer

That's not what the OP is asking about though.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 13:23 Correct way to provide a C callback function nside C++ Pepe
  2023-01-27 13:48 ` Tom Kacvinsky
@ 2023-01-27 14:07 ` Jonathan Wakely
  2023-01-27 14:29   ` Michael Pape
  2023-01-27 14:30   ` Xi Ruoyao
  1 sibling, 2 replies; 7+ messages in thread
From: Jonathan Wakely @ 2023-01-27 14:07 UTC (permalink / raw)
  To: Pepe; +Cc: gcc-help

On Fri, 27 Jan 2023 at 13:24, Pepe via Gcc-help <gcc-help@gcc.gnu.org> wrote:
>
> Hi there,
>
> I’m in an ongoing discussion about whether or not one should use extern “C” when defining a function that will be used as a callback in a statically linked C library. For example:
>
> c_func.h:
> // …
> void reg_callback(void (*fn)());
> // …
>
> cpp_impl.cpp:
> // …
> extern “C” {
> #include “c_func.h”
> }
>
> // my callback function with internal linkage
> namespace {
>
> extern “C” {
> static void my_callback_A() {
>         // …
> }
> } // extern “C”
>
> void my_callback_B() {
>         // …
> }
>
> } // namespace
>
> void do_something() {
>         reg_callback(my_callback_A);
>         reg_callback(my_callback_B);
> }
>
> Both callbacks have internal linkage. Both work fine, and something like my_callback_B is found in lots of code bases.
>
> In my opinion, using callback B is implementation defined behaviour, because it is not guaranteed that C and C++ use the same calling conventions. Therefore a function must adhere to the C calling conventions to be used as a callback in a C library, which would be callback A.
>
> I’ve been trying to find something definitive for days now, but to no avail. Now I’m not sure what’s true or not. The counter argument is the following: The compiler should know reg_callback is a C function and make sure that a given argument would either be valid or cause a compiler error. That sounds reasonable, so I would love to know how to do it properly for future reference. Given we use gcc I was hoping to get a definitive answer in this mailing list. Thanks a lot!

You are (mostly) correct. The C++ standard says that extern "C"
functions and extern "C++" functions have different types, and so this
should not even compile:

extern "C" {
using callback = void(*)();
void f(callback);
}

void g() { };
void h() { f(g); }

There should be a compilation error when trying to pass g (which is an
extern "C++" function) to f (which accepts a pointer to an extern "C"
function).

GCC (and most other compilers) do not actually conform to that
requirement in the standard, and the types are identical. Which means
there is no compilation error, and the code works fine.

I think it's safe to assume that *either* the code compiles and works
as expected, or fails to compile. And in practice it compiles and
works with all widely used compilers. You will not find a C++
implementation where the types are not compatible, but the code
compiles anyway and then misbehaves at runtime.

The relevant GCC bug about this nonconformance is
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316 (and will probably
never be fixed, because it would break far too much code).

The relevant quote from the C++ standard is in [dcl.link]:
"The default language linkage of all function types, functions, and
variables is C ++ language linkage. Two function types with different
language linkages are distinct types even if they are otherwise
identical."

Being distinct types means that there should be not implicit
conversion from &g in the example above to the type callback. An
explicit conversion (e.g. using reinterpret_cast) would be allowed,
but then it would be undefined behaviour to actually call the function
g() through a pointer to a different function type. In practice, that
isn't a problem because they're not distinct types with GCC, so the
code works.






>
> Pepe

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 14:07 ` Jonathan Wakely
@ 2023-01-27 14:29   ` Michael Pape
  2023-01-27 14:30   ` Xi Ruoyao
  1 sibling, 0 replies; 7+ messages in thread
From: Michael Pape @ 2023-01-27 14:29 UTC (permalink / raw)
  To: gcc-help



On 27 Jan 2023, at 15:07, Jonathan Wakely wrote:

> On Fri, 27 Jan 2023 at 13:24, Pepe via Gcc-help <gcc-help@gcc.gnu.org> wrote:
>>
>> Hi there,
>>
>> I’m in an ongoing discussion about whether or not one should use extern “C” when defining a function that will be used as a callback in a statically linked C library. For example:
>>
>> c_func.h:
>> // …
>> void reg_callback(void (*fn)());
>> // …
>>
>> cpp_impl.cpp:
>> // …
>> extern “C” {
>> #include “c_func.h”
>> }
>>
>> // my callback function with internal linkage
>> namespace {
>>
>> extern “C” {
>> static void my_callback_A() {
>>         // …
>> }
>> } // extern “C”
>>
>> void my_callback_B() {
>>         // …
>> }
>>
>> } // namespace
>>
>> void do_something() {
>>         reg_callback(my_callback_A);
>>         reg_callback(my_callback_B);
>> }
>>
>> Both callbacks have internal linkage. Both work fine, and something like my_callback_B is found in lots of code bases.
>>
>> In my opinion, using callback B is implementation defined behaviour, because it is not guaranteed that C and C++ use the same calling conventions. Therefore a function must adhere to the C calling conventions to be used as a callback in a C library, which would be callback A.
>>
>> I’ve been trying to find something definitive for days now, but to no avail. Now I’m not sure what’s true or not. The counter argument is the following: The compiler should know reg_callback is a C function and make sure that a given argument would either be valid or cause a compiler error. That sounds reasonable, so I would love to know how to do it properly for future reference. Given we use gcc I was hoping to get a definitive answer in this mailing list. Thanks a lot!
>
> You are (mostly) correct. The C++ standard says that extern "C"
> functions and extern "C++" functions have different types, and so this
> should not even compile:
>
> extern "C" {
> using callback = void(*)();
> void f(callback);
> }
>
> void g() { };
> void h() { f(g); }
>
> There should be a compilation error when trying to pass g (which is an
> extern "C++" function) to f (which accepts a pointer to an extern "C"
> function).
>
> GCC (and most other compilers) do not actually conform to that
> requirement in the standard, and the types are identical. Which means
> there is no compilation error, and the code works fine.
>
> I think it's safe to assume that *either* the code compiles and works
> as expected, or fails to compile. And in practice it compiles and
> works with all widely used compilers. You will not find a C++
> implementation where the types are not compatible, but the code
> compiles anyway and then misbehaves at runtime.
>
> The relevant GCC bug about this nonconformance is
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316 (and will probably
> never be fixed, because it would break far too much code).
>
> The relevant quote from the C++ standard is in [dcl.link]:
> "The default language linkage of all function types, functions, and
> variables is C ++ language linkage. Two function types with different
> language linkages are distinct types even if they are otherwise
> identical."
>
> Being distinct types means that there should be not implicit
> conversion from &g in the example above to the type callback. An
> explicit conversion (e.g. using reinterpret_cast) would be allowed,
> but then it would be undefined behaviour to actually call the function
> g() through a pointer to a different function type. In practice, that
> isn't a problem because they're not distinct types with GCC, so the
> code works.
>
>
>
>
>
>

Thank you very much, that perfectly answered my question :)

>>
>> Pepe

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 14:07 ` Jonathan Wakely
  2023-01-27 14:29   ` Michael Pape
@ 2023-01-27 14:30   ` Xi Ruoyao
  2023-01-27 15:32     ` Jonathan Wakely
  1 sibling, 1 reply; 7+ messages in thread
From: Xi Ruoyao @ 2023-01-27 14:30 UTC (permalink / raw)
  To: Jonathan Wakely, Pepe; +Cc: gcc-help

On Fri, 2023-01-27 at 14:07 +0000, Jonathan Wakely via Gcc-help wrote:
> I think it's safe to assume that *either* the code compiles and works
> as expected, or fails to compile. And in practice it compiles and
> works with all widely used compilers. You will not find a C++
> implementation where the types are not compatible, but the code
> compiles anyway and then misbehaves at runtime.

FWIW if we do "some strange thing" we may end up shooting our own foot.
Like:

a.cc:

#include <cstdio>

extern "C"
{
	struct A { struct {} x; int a; };
	void callback(void (*fn)(struct A *));
}

void f(struct A *p)
{
	std::printf("%d\n", p->a);
}

int main()
{
	callback(f);
}

b.c:

#include <stdio.h>

struct A { struct {} x; int a; };
void callback(void (*fn)(struct A *))
{
	struct A foo = { .a = 42 };
	fn(&foo);
}

This thing won't work with GCC because struct A will have different
layouts in GNU C and C++.  Note that the C standard does not allow an
empty struct at all (a pedantic C compiler should reject b.c as an empty
struct violates the syntax rule of C).  But GNU C supports it as an
extension.

> The relevant GCC bug about this nonconformance is
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316 (and will probably
> never be fixed, because it would break far too much code).

Perhaps we should document it as an extension as well and add a warning
option...  But this will be the lowest priority even if we'd spend our
time for the job.
-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Correct way to provide a C callback function nside C++
  2023-01-27 14:30   ` Xi Ruoyao
@ 2023-01-27 15:32     ` Jonathan Wakely
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Wakely @ 2023-01-27 15:32 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: Pepe, gcc-help

On Fri, 27 Jan 2023 at 14:30, Xi Ruoyao wrote:
>
> On Fri, 2023-01-27 at 14:07 +0000, Jonathan Wakely via Gcc-help wrote:
> > I think it's safe to assume that *either* the code compiles and works
> > as expected, or fails to compile. And in practice it compiles and
> > works with all widely used compilers. You will not find a C++
> > implementation where the types are not compatible, but the code
> > compiles anyway and then misbehaves at runtime.
>
> FWIW if we do "some strange thing" we may end up shooting our own foot.
> Like:
>
> a.cc:
>
> #include <cstdio>
>
> extern "C"
> {
>         struct A { struct {} x; int a; };
>         void callback(void (*fn)(struct A *));
> }
>
> void f(struct A *p)
> {
>         std::printf("%d\n", p->a);
> }
>
> int main()
> {
>         callback(f);
> }
>
> b.c:
>
> #include <stdio.h>
>
> struct A { struct {} x; int a; };
> void callback(void (*fn)(struct A *))
> {
>         struct A foo = { .a = 42 };
>         fn(&foo);
> }
>
> This thing won't work with GCC because struct A will have different
> layouts in GNU C and C++.  Note that the C standard does not allow an
> empty struct at all (a pedantic C compiler should reject b.c as an empty
> struct violates the syntax rule of C).  But GNU C supports it as an
> extension.

Strictly speaking, a pedantic C compiler should issue a diagnostic. It
doesn't have to fail to compile.

$ gcc -pedantic b.c
b.c:3:12: warning: struct has no members [-Wpedantic]
   3 | struct A { struct {} x; int a; };
     |            ^~~~~~

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-01-27 15:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 13:23 Correct way to provide a C callback function nside C++ Pepe
2023-01-27 13:48 ` Tom Kacvinsky
2023-01-27 13:51   ` Jonathan Wakely
2023-01-27 14:07 ` Jonathan Wakely
2023-01-27 14:29   ` Michael Pape
2023-01-27 14:30   ` Xi Ruoyao
2023-01-27 15:32     ` Jonathan Wakely

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).