Skip to content

Fix support for NetBSD and enable native code on NetBSD/aarch64#11097

Merged
Octachron merged 3 commits intoocaml:trunkfrom
kit-ty-kate:netbsd-arm64
Aug 29, 2022
Merged

Fix support for NetBSD and enable native code on NetBSD/aarch64#11097
Octachron merged 3 commits intoocaml:trunkfrom
kit-ty-kate:netbsd-arm64

Conversation

@kit-ty-kate
Copy link
Copy Markdown
Member

Same as #11092 and #11096 but with some fixes to make it actually build.

Was failing with:

gcc -c -O2 -fno-strict-aliasing -fwrapv -pthread -Wall -Werror -fno-common -fexcess-precision=standard -ffunction-sections -g -fPIC  -D_FILE_OFFSET_BITS=64  -DCAMLDLLIMPORT=  -o unix.bpic.o unix.c
unix.c: In function 'caml_thread_setname':
unix.c:452:3: error: too few arguments to function 'pthread_setname_np'
  452 |   pthread_setname_np(self, name);
      |   ^~~~~~~~~~~~~~~~~~
In file included from unix.c:52:
/usr/include/pthread.h:160:5: note: declared here
  160 | int pthread_setname_np(pthread_t, const char *, void *);
      |     ^~~~~~~~~~~~~~~~~~
unix.c: In function 'caml_thread_setname':
unix.c:452:3: error: too few arguments to function 'pthread_setname_np'
  452 |   pthread_setname_np(self, name);
      |   ^~~~~~~~~~~~~~~~~~
In file included from unix.c:52:
/usr/include/pthread.h:160:5: note: declared here
  160 | int pthread_setname_np(pthread_t, const char *, void *);
      |     ^~~~~~~~~~~~~~~~~~
unix.c: In function 'caml_thread_setname':
unix.c: In function 'caml_thread_setname':
unix.c:452:3: error: too few arguments to function 'pthread_setname_np'
  452 |   pthread_setname_np(self, name);
      |   ^~~~~~~~~~~~~~~~~~
In file included from unix.c:52:
/usr/include/pthread.h:160:5: note: declared here
  160 | int pthread_setname_np(pthread_t, const char *, void *);
      |     ^~~~~~~~~~~~~~~~~~
unix.c:452:3: error: too few arguments to function 'pthread_setname_np'
  452 |   pthread_setname_np(self, name);
      |   ^~~~~~~~~~~~~~~~~~
In file included from unix.c:52:
/usr/include/pthread.h:160:5: note: declared here
  160 | int pthread_setname_np(pthread_t, const char *, void *);
      |     ^~~~~~~~~~~~~~~~~~
gmake[2]: *** [Makefile:423: unix.b.o] Error 1

and

gcc -c -O2 -fno-strict-aliasing -fwrapv -pthread -Wall -Werror -fno-common -fexcess-precision=standard -ffunction-sections -g  -D_FILE_OFFSET_BITS=64  -DCAMLDLLIMPORT= -DNATIVE_CODE -DTARGET_arm64 -DMODEL_default -DSYS_netbsd  -o sak.o sak.c
In file included from /usr/include/ctype.h:100,
                 from sak.c:29:
sak.c: In function 'add_stdlib_prefix':
sak.c:126:26: error: array subscript has type 'char' [-Werror=char-subscripts]
  126 |       *name = toupper_os(*name);
      |                          ^
sak.c:126:15: note: in expansion of macro 'toupper_os'
  126 |       *name = toupper_os(*name);
      |               ^~~~~~~~~~
cc1: all warnings being treated as errors
gmake[2]: *** [Makefile:313: sak.o] Error 1

All the manual pages necessary to understand those fixes have been added as comments in the code where they are needed.

runtime/sak.c Outdated
#define strncmp_os strncmp
#ifdef __NetBSD__
/* NOTE: See CAVEATS section in https://man.netbsd.org/ctype.3 */
#define toupper_os(x) toupper((unsigned int)x)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this from that manpage:

Some implementations of libc, such as glibc as of 2018, attempt to avoid
     the worst of the undefined behavior by defining the functions to work for
     all integer inputs representable by either unsigned char or char, and
     suppress the warning.  However, this is not an excuse for avoiding con-
     version to unsigned char: if EOF coincides with any such value, as it
     does when it is -1 on platforms with signed char, programs that pass char
     will still necessarily confuse the classification and mapping of EOF with
     the classification and mapping of some non-EOF inputs.

Should we just unconditionally cast to (unsigned int) on all platforms, including glibc?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooops, I meant to use unsigned char not unsigned int. Fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; looking at the glibc man page for toupper, it looks appropriate to always cast toupper_os()'s argument to unsigned char:

The standards require that the argument c for these functions is
       either EOF or a value that is representable in the type unsigned
       char.  If the argument c is of type char, it must be cast to
       unsigned char, as in the following example:

           char c;
           ...
           res = toupper((unsigned char) c);

       This is necessary because char may be the equivalent signed char,
       in which case a byte where the top bit is set would be sign
       extended when converting to int, yielding a value that is outside
       the range of unsigned char.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faire enough. Done

Copy link
Copy Markdown
Member

@avsm avsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NetBSD fixes look good to me. If I could get another approval on the toupper cast before merging, that'd be nice...

@dra27 dra27 added this to the 5.0.0 milestone Jun 6, 2022
@dra27 dra27 self-requested a review June 6, 2022 08:44
#define toupper_os toupper
/* NOTE: See CAVEATS section in https://man.netbsd.org/ctype.3 */
/* and NOTE section in https://man7.org/linux/man-pages/man3/toupper.3.html */
#define toupper_os(x) toupper((unsigned char)x)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I reading correctly that this change is mainly to avoid a compiler warning on NetBSD?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is triggered by -Werror=char-subscripts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the toupper_os change seems fine to me (and this is the only change that will survive rebasing).

@Octachron
Copy link
Copy Markdown
Member

This looks fine to me, I will approve and merge once rebased.

@Octachron
Copy link
Copy Markdown
Member

You may want to edit the commit message of the last commit.

@Octachron Octachron merged commit ef07bd8 into ocaml:trunk Aug 29, 2022
@kit-ty-kate kit-ty-kate deleted the netbsd-arm64 branch August 29, 2022 11:35
@dra27
Copy link
Copy Markdown
Member

dra27 commented Oct 18, 2022

I was just cherry-picking #11639 to 5.0 and it unexpectedly conflicted because this PR isn't there.

I think this PR is intended for 5.0 - is it OK for me to cherry-pick it with #11639?

@Octachron
Copy link
Copy Markdown
Member

The PR should have been cherry-picked to 5.0 indeed. So it is fine to cherry-pick it along with #11639 .

dra27 pushed a commit that referenced this pull request Oct 18, 2022
Fix support for NetBSD and enable native code on NetBSD/aarch64

(cherry picked from commit ef07bd8)
@dra27
Copy link
Copy Markdown
Member

dra27 commented Oct 18, 2022

Done: f35d5d6

kit-ty-kate added a commit to kit-ty-kate/ocaml that referenced this pull request Jul 17, 2023
xavierleroy pushed a commit that referenced this pull request Aug 31, 2023
dra27 pushed a commit to dra27/ocaml that referenced this pull request Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants