This is an archive of the discontinued LLVM Phabricator instance.

[libunwind] Use __builtin_alloca to avoid missing include
ClosedPublic

Authored by ldionne on May 2 2023, 1:14 AM.

Details

Summary

This is required for picolibc. picolibc (possibly erroneously) only includes alloca.h from stdlib.h if STRICT_ANSI is not defined - see https://github.com/picolibc/picolibc/blob/fe20d30008df0f3bb334a202bea344b8a08d32e7/newlib/libc/include/stdlib.h#L54 . This patch includes alloca.h by default.

Diff Detail

Event Timeline

xbjfk created this revision.May 2 2023, 1:14 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 2 2023, 1:14 AM
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald Transcript
xbjfk requested review of this revision.May 2 2023, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 1:14 AM

picolibc (possibly erroneously) only includes alloca.h from stdlib.h if STRICT_ANSI is not defined

The header is non-standard. It is correct not to include it when using -std=c* (as opposed to -std=gnu*).
If libunwind relies on it in public header files, it should be rectified.

picolibc (possibly erroneously) only includes alloca.h from stdlib.h if STRICT_ANSI is not defined

The header is non-standard. It is correct not to include it when using -std=c* (as opposed to -std=gnu*).
If libunwind relies on it in public header files, it should be rectified.

Indeed. In particular, mingw toolchains don't have an alloca.h header - this should show up as failure from those jobs in CI.

danielkiss added inline comments.
libunwind/src/config.h
130

alloca.h is needed for this so probably the simplest way is just to build with _LIBUNWIND_REMEMBER_HEAP_ALLOC if that causes trouble for you.

xbjfk added a comment.May 2 2023, 3:28 AM

Indeed. In particular, mingw toolchains don't have an alloca.h header - this should show up as failure from those jobs in CI.

I see, maybe in this case alloca.h should only be included in the #ifndef _LIBUNWIND_REMEMBER_HEAP_ALLOC and under __has_include? not sure whether this is acceptable.
or is this problem something for picolibc to solve? or the user by specifying -include alloca.h? how should this be fixed instead?

Indeed. In particular, mingw toolchains don't have an alloca.h header - this should show up as failure from those jobs in CI.

I see, maybe in this case alloca.h should only be included in the #ifndef _LIBUNWIND_REMEMBER_HEAP_ALLOC and under __has_include? not sure whether this is acceptable.
or is this problem something for picolibc to solve? or the user by specifying -include alloca.h? how should this be fixed instead?

Well I guess technically libunwind is wrong here; if we do use alloca, we should include a corresponding header. The tricky thing is just that it's rather platform specific how to do that; see e.g. https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0/openmp/runtime/src/kmp_wrapper_malloc.h#L93-L104 for one case of such an ifdef jungle.

compnerd added inline comments.
libunwind/src/config.h
16

Is this available on MSVC?

mstorsjo added inline comments.May 5 2023, 2:15 PM
libunwind/src/config.h
16

It's not, as already commented above - it's not available in either MSVC or mingw toolchains.

compnerd added inline comments.May 6 2023, 9:45 AM
libunwind/src/config.h
16

Should this be something that we put into a configuration then? Or provide shims for?

compnerd added inline comments.May 8 2023, 11:37 AM
libunwind/src/config.h
130

Sure, that makes sense, but the inclusion of the header should be guarded by _LIBUNWIND_REMEMBER_HEAP_ALLOC then right?

arichardson added inline comments.
libunwind/src/config.h
16

Could we avoid the header and just use __builtin_alloca? Or is that code path used with compilers that don't support this built-in?

ldionne added inline comments.
libunwind/src/config.h
16

+1, let's use __builtin_alloca if we can and drop the non-portable include.

barannikov88 added inline comments.May 10 2023, 10:16 AM
libunwind/src/config.h
16

+1, also thought about it. But is it available on MSVC? Or libunwind is not supposed to be compiled by MSVC?

ldionne commandeered this revision.Sep 11 2023, 2:38 PM
ldionne added a reviewer: xbjfk.

[Github PR transition cleanup]

Commandeering to finish.

ldionne updated this revision to Diff 556486.Sep 11 2023, 2:38 PM
ldionne retitled this revision from [libunwind] include alloca.h in config.h to [libunwind] Use __builtin_alloca to avoid missing include.

Use __builtin_alloca

ldionne updated this revision to Diff 556568.Sep 12 2023, 5:55 AM

Rebase and poke CI.

danielkiss added inline comments.Sep 12 2023, 7:22 AM
libunwind/src/config.h
129

maybe we could add an extra guard just to be sure

ldionne added inline comments.Sep 12 2023, 10:59 AM
libunwind/src/config.h
129

Honestly, I'd rather see it fail explicitly in that case than silently fall back to using malloc. If someone uses a compiler we don't support here, the correct path IMO would be for it to fail fast and loud (at compile-time).

ldionne accepted this revision.Sep 12 2023, 10:59 AM

Shipping since the CI issue is a fluke.

This revision is now accepted and ready to land.Sep 12 2023, 10:59 AM
This revision was landed with ongoing or failed builds.Sep 12 2023, 11:00 AM
This revision was automatically updated to reflect the committed changes.