lang/gprolog improvements: sparc64, PIE, system malloc(3)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lang/gprolog improvements: sparc64, PIE, system malloc(3)

Jeremie Courreges-Anglas-2

As pointed out by Daniel, gprolog can now work on sparc64 if we help it
a little.  Detection of sigaction/siginfo_t fails for a dubious reason
(the system returns the address of the page of the faulty access,
instead of the faulty address).  The diff below attempts to fix it in
a portable way.

Testing on sparc64 proved successful even though '-fno-pie -nopie'
wasn't used in CFLAGS.  Using those seems needed only on i386, and maybe
powerpc (can't test those archs).  We want to use PIE when possible, so
the diff below restricts the use of '-fno-pie -nopie' to i386 and
powerpc.

Finally, this switches from Doug Lea malloc shipped with the source to
system malloc(3).  Doug Lea malloc is only used on *BSD systems, no
rationale is given.  I think we also want to use our malloc(3) when
possible.

Make test passes on amd64 and sparc64.  Tests and feedback welcome.


Index: Makefile
===================================================================
RCS file: /cvs/ports/lang/gprolog/Makefile,v
retrieving revision 1.22
diff -u -p -r1.22 Makefile
--- Makefile 16 Nov 2018 22:19:45 -0000 1.22
+++ Makefile 17 Nov 2018 11:34:18 -0000
@@ -1,10 +1,12 @@
 # $OpenBSD: Makefile,v 1.22 2018/11/16 22:19:45 jca Exp $
 
-ONLY_FOR_ARCHS= amd64 i386 powerpc
+ONLY_FOR_ARCHS= amd64 i386 powerpc sparc64
 
 COMMENT= Prolog compiler
 
 DISTNAME= gprolog-1.4.5
+REVISION= 0
+
 CATEGORIES= lang
 
 HOMEPAGE= http://www.gprolog.org/
Index: patches/patch-EnginePl_dl_malloc_c
===================================================================
RCS file: patches/patch-EnginePl_dl_malloc_c
diff -N patches/patch-EnginePl_dl_malloc_c
--- patches/patch-EnginePl_dl_malloc_c 12 Nov 2018 19:15:40 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,20 +0,0 @@
-$OpenBSD: patch-EnginePl_dl_malloc_c,v 1.1 2018/11/12 19:15:40 daniel Exp $
-
-Index: EnginePl/dl_malloc.c
---- EnginePl/dl_malloc.c.orig
-+++ EnginePl/dl_malloc.c
-@@ -572,6 +572,14 @@ MAX_RELEASE_CHECK_RATE   default: 4095 unless not HAVE
- #endif  /* HAVE_MORECORE */
- #endif  /* DARWIN */
-
-+#if defined(__OpenBSD__)
-+/* Avoid sbrk on OpenBSD; use mmap instead */
-+#ifndef HAVE_MORECORE
-+#define HAVE_MORECORE 0
-+#define HAVE_MMAP 1
-+#endif
-+#endif
-+
- #ifndef LACKS_SYS_TYPES_H
- #include <sys/types.h>  /* For size_t */
- #endif  /* LACKS_SYS_TYPES_H */
Index: patches/patch-EnginePl_machine_h
===================================================================
RCS file: /cvs/ports/lang/gprolog/patches/patch-EnginePl_machine_h,v
retrieving revision 1.2
diff -u -p -r1.2 patch-EnginePl_machine_h
--- patches/patch-EnginePl_machine_h 8 Jun 2017 15:25:57 -0000 1.2
+++ patches/patch-EnginePl_machine_h 17 Nov 2018 11:34:18 -0000
@@ -1,5 +1,6 @@
 $OpenBSD: patch-EnginePl_machine_h,v 1.2 2017/06/08 15:25:57 espie Exp $
 
+Hunk 1:
 XXX not quite sure why this is needed as it's built without PIE anyway, but
 this fixes a segfault on i386 during build following the switch to PIE by
 default.
@@ -13,6 +14,9 @@ compilation failed
 *** Error 1 in /tmp_ports/gprolog-1.3.1/gprolog-1.3.1/src (Makefile:47 'all')
 ===
 
+Hunk 2:
+use system malloc(3), not Doug Lea malloc backed by sbrk(2)
+
 Index: EnginePl/machine.h
 --- EnginePl/machine.h.orig
 +++ EnginePl/machine.h
@@ -25,3 +29,12 @@ Index: EnginePl/machine.h
 
  #ifdef NO_USE_EBP
  #    define M_USED_REGS            {"ebx", 0}
+@@ -196,7 +196,7 @@ void M_Check_Magic_Words(void); /* not compiled if not
+  *---------------------------------*/
+
+ #if defined(__OpenBSD__) || defined(M_bsd)
+-#define USE_DL_MALLOC
++//#define USE_DL_MALLOC
+ #endif
+
+
Index: patches/patch-EnginePl_try_sigaction_c
===================================================================
RCS file: patches/patch-EnginePl_try_sigaction_c
diff -N patches/patch-EnginePl_try_sigaction_c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ patches/patch-EnginePl_try_sigaction_c 17 Nov 2018 11:34:18 -0000
@@ -0,0 +1,49 @@
+$OpenBSD$
+
+Relax siginfo_t si_addr check, sparc64 only provides page granularity.
+
+Index: EnginePl/try_sigaction.c
+--- EnginePl/try_sigaction.c.orig
++++ EnginePl/try_sigaction.c
+@@ -51,6 +51,7 @@
+  * si_addr is wrong for SIGBUS :-(
+  */
+ #define BAD_ADDR  ((int *) 0x2EA4F0)
++int *bad_addr;
+
+ void
+ SIGSEGV_Handler(int sig, siginfo_t * sip)
+@@ -59,7 +60,7 @@ SIGSEGV_Handler(int sig, siginfo_t * sip)
+ #if 0
+   printf("bad addr: %p\n", addr);
+ #endif
+-  _exit(addr != BAD_ADDR);
++  _exit(addr != bad_addr);
+ }
+
+
+@@ -68,6 +69,7 @@ int
+ main(int argc, char *argv[])
+ {
+   struct sigaction act;
++  long page_size;
+
+   act.sa_handler = NULL;
+   act.sa_sigaction = (void (*)()) SIGSEGV_Handler;
+@@ -79,7 +81,15 @@ main(int argc, char *argv[])
+   sigaction(SIGBUS, &act, NULL);
+ #endif
+
+-  *BAD_ADDR = 128;
++  bad_addr = BAD_ADDR;
++  /*
++   * some architectures can't provide more then page-level
++   * granularity.
++   */
++  if ((page_size = sysconf(_SC_PAGESIZE)) != -1L)
++    bad_addr = (int *)((long)BAD_ADDR & ~(page_size - 1));
++
++  *bad_addr = 128;
+
+   return 1;
+ }
Index: patches/patch-configure_in
===================================================================
RCS file: /cvs/ports/lang/gprolog/patches/patch-configure_in,v
retrieving revision 1.5
diff -u -p -r1.5 patch-configure_in
--- patches/patch-configure_in 12 Nov 2018 19:15:40 -0000 1.5
+++ patches/patch-configure_in 17 Nov 2018 11:39:15 -0000
@@ -3,11 +3,12 @@ $OpenBSD: patch-configure_in,v 1.5 2018/
 Index: configure.in
 --- configure.in.orig
 +++ configure.in
-@@ -483,6 +483,7 @@ dnl -fomit-frame-pointer does not work on MinGW (teste
+@@ -483,6 +483,8 @@ dnl -fomit-frame-pointer does not work on MinGW (teste
          powerpc*darwin*)   CFLAGS_MACHINE='-mpowerpc -no-cpp-precomp';;
          x86_64*solaris*)   CFLAGS_MACHINE='-m64';;
          x86_64*darwin*)    CFLAGS_MACHINE='-march=x86-64 -m64';;
-+ *openbsd*)   CFLAGS_MACHINE='-fno-pie -nopie';;
++        i386*openbsd*)     CFLAGS_MACHINE='-fno-pie -nopie';;
++        powerpc*openbsd*)  CFLAGS_MACHINE='-fno-pie -nopie';;
      esac
 
      case "$host" in

--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply | Threaded
Open this post in threaded view
|

Re: lang/gprolog improvements: sparc64, PIE, system malloc(3)

Christian Weisgerber
On 2018-11-17, Jeremie Courreges-Anglas <[hidden email]> wrote:

> Testing on sparc64 proved successful even though '-fno-pie -nopie'
> wasn't used in CFLAGS.  Using those seems needed only on i386, and maybe
> powerpc (can't test those archs).  We want to use PIE when possible, so
> the diff below restricts the use of '-fno-pie -nopie' to i386 and
> powerpc.

Note that "-fno-pie" will generate text relocations on i386 and
thus break with lld.  If it is actually needed, "-Wl,-znotext" will
have to be added to the linker flags.

(Testing this is blocked until we get a new gcc 4.9 i386 adastrap
that works with lld.)

--
Christian "naddy" Weisgerber                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: lang/gprolog improvements: sparc64, PIE, system malloc(3)

Christian Weisgerber
In reply to this post by Jeremie Courreges-Anglas-2
Jeremie Courreges-Anglas:

> Testing on sparc64 proved successful even though '-fno-pie -nopie'
> wasn't used in CFLAGS.  Using those seems needed only on i386, and maybe
> powerpc (can't test those archs).  We want to use PIE when possible, so
> the diff below restricts the use of '-fno-pie -nopie' to i386 and
> powerpc.

i386 clearly does not require '-fno-pie -nopie' because we already
build the port without it.  Here's the relevant part of the configure
script:

  CFLAGS_MACHINE=
  case "$host" in
  ...
      i*86*)             CFLAGS_MACHINE='-march=i486 -m32';;
  ...
      *openbsd*)         CFLAGS_MACHINE='-fno-pie -nopie';;
  esac

The i*86* pattern matches "i386-unknown-openbsd6.4", so the *openbsd*
case is skipped.

  ...
  GNU Prolog version: 1.4.5 (Nov 18 2018)
  Operating system  : openbsd6.4
  Processor         : i386
  Size of a WAM word: 32 bits
  C compiler        : cc
  C flags           : -O2 -pipe  
  C flags machine   : -march=i486 -m32 -fno-strict-aliasing
  ...

(Unfortunately, it still errors out with text relocations when built
on i386 with lld, but that's a separate issue.)

--
Christian "naddy" Weisgerber                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: lang/gprolog improvements: sparc64, PIE, system malloc(3)

Christian Weisgerber
On 2018-11-20, Christian Weisgerber <[hidden email]> wrote:

> (Unfortunately, it still errors out with text relocations when built
> on i386 with lld, but that's a separate issue.)

There are text relocations deep in the runtime environment, somewhere
in a maze of Prolog, WAM, C and inline assembly.

That said, simply passing LDFLAGS=-Wl,-znotext to configure propagates
all the way through to the right places.  Can be done from the port
Makefile, or, if you still patch the configure script, added there.

--
Christian "naddy" Weisgerber                          [hidden email]