Bug 13711

Summary: Android API 21+ missing definitions for wait3 and issetugid
Product: Busybox Reporter: Chris Renshaw <osm0sis>
Component: OtherAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Host: Target:
Build:
Attachments: platform.h patch (1) for the reported issue
platform.h patch (2) for the reported issue

Description Chris Renshaw 2021-04-01 00:15:52 UTC
Created attachment 8871 [details]
platform.h patch (1) for the reported issue

Linker error only occurs on arm64, x86_64 + mips64 with Android NDK building with API 21+

miscutils/lib.a(time.o): In function `.L13':
time.c:(.text+0xe4): undefined reference to `wait3'
miscutils/lib.a(time.o): In function `.L19':
time.c:(.text+0x104): undefined reference to `wait3'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:719: busybox_unstripped] Error 1

I've got a patch for this to wrap wait3 to wait4, and add the stub definition for issetugid on Android API 21+ which helps with adding glob for hush support.

Then supporting both the unified and deprecated Android NDK headers needs a further tweak to avoid this error:

  CC      libbb/platform.o
libbb/platform.c:218:5: error: conflicting types for 'issetugid'
 int issetugid(void)
     ^
In file included from include/libbb.h:13:0,
                 from libbb/platform.c:9:
include/platform.h:624:12: note: previous declaration of 'issetugid' was here
 extern int issetugid(void) FAST_FUNC;
            ^
make[1]: *** [scripts/Makefile.build:198: libbb/platform.o] Error 1

John Wu, aka. topjohnwu, the Magisk/Android root dev submitted another patch to resolve this, so I've included it as well so hopefully both can be included to retain his authorship.
Comment 1 Chris Renshaw 2021-04-01 00:16:40 UTC
Created attachment 8876 [details]
platform.h patch (2) for the reported issue
Comment 2 Chris Renshaw 2021-04-01 00:19:27 UTC
I forgot to mention the CC platform.c error topjohnwu's patch fixes only occurs on Android x86 compiles.
Comment 3 Denys Vlasenko 2021-04-14 16:49:21 UTC
+int issetugid(void)
+{
+       /* for Bionic, this is sufficient */
+       return 0;
+}

What is issetuid? busybox does not use issetuid(), it should not be needed.

 # if __ANDROID_API__ < 21
 #  undef HAVE_TTYNAME_R
 #  undef HAVE_GETLINE
 #  undef HAVE_STPCPY
+# else
+#  undef HAVE_WAIT3
 # endif

what does this say? "ANDROID_API before version 21 had wait3(), bit starting from 21 it does not"? That's... surprising.

-# if __GNUC_PREREQ(3,0) && defined(i386)
+# if !defined(__ANDROID__) && __GNUC_PREREQ(3,0) && defined(i386)
 /* stdcall makes callee to pop arguments from stack, not caller */
 #  define FAST_FUNC __attribute__((regparm(3),stdcall))
 /* #elif ... - add your favorite arch today! */

What does __attribute__((regparm(3),stdcall)) break?
Comment 4 Chris Renshaw 2021-04-14 19:36:06 UTC
I agree issetugid is a bit extra for this issue. For Android, busybox hush fails to build due to missing glob. In order to add glob (a much more involved patch) a dummy definition for issetugid also needed to be added.

Yeah:
/* Wrap wait3() to wait4() for libc implementations without (e.g. Bionic on ANDROID >= 21) */

Crazy, huh?

The __attribute__((regparm(3),stdcall)) is that CC platform.c error topjohnwu's patch fixes, which only occurs on Android x86 compiles, admittedly only after issetugid is added, so may be beyond what you'd want to add.

My intent here was just to make the glob patch easier since busybox already would have the prerequisites missing Android calls. Anyway, end of the day, wait3 to wait4 is still important, and I guess you can ignore the rest of the patches if you like.
Comment 5 Denys Vlasenko 2021-04-14 20:16:57 UTC
I added the fix for wait3()
Comment 6 Chris Renshaw 2021-04-14 22:22:08 UTC
Thanks! I realize you had to take just the wait3() stuff out of what I gave you here, but shouldn't I still get mention/credit for the fix?
Comment 7 Chris Renshaw 2021-04-15 01:25:28 UTC
Or at least arguably this guy where my fix was adapted from:

wait3() wrapper from 'sys-wait.h.patch' by Fredrik Fornwall:
https://github.com/termux/termux-packages/tree/master/ndk_patches/