Commit graph

88 commits

Author SHA1 Message Date
Kyle Evans 9bfd3b4076 Add a build knob for _FORTIFY_SOURCE
In the future, we will Default to _FORTIFY_SOURCE=2 if SSP is enabled,
otherwise default to _FORTIFY_SOURCE=0.  For now we default it to 0
unconditionally to ease bisect across older versions without the new
symbols, and we'll put out a call for testing.

include/*.h include their ssp/*.h equivalents as needed based on the
knob. Programs and users are allowed to override FORTIFY_SOURCE in their
Makefiles or src.conf/make.conf to force it off.

Reviewed by:	des, markj
Relnotes:	yes
Sponsored by:	Stormshield
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D32308
2024-05-13 00:23:50 -05:00
Konstantin Belousov f8bbbce458 libthr: remove explicit sys/cdefs.h includes
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2024-03-06 09:25:13 +02:00
Brooks Davis eb90239d08 lib{c,thr}: add DT_RUNPATH for gcc -m32
To allow gcc -m32 to work, link libc and libthr with --rpath-/usr/lib32.
When called with -m32, gcc is currently unable to communicate to
the bfd linker that it should look in /usr/lib32 to resolve needed (as
opposed to explicitly linked) libraries so we need to provide a hint.

See also: https://sourceware.org/bugzilla/show_bug.cgi?id=31395

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D43910
2024-02-19 22:44:08 +00:00
Brooks Davis 99ea675731 lib{c,sys}: move auxargs more firmly into libsys
Continue to filter the public interface (elf_aux_info()), but entierly
relocate the private interfaces (_elf_aux_info(),
__init_elf_aux_vector(), and __elf_aux_vector) to libsys.

This ensures that rtld updates the correct (only) copy of
__elf_aux_vector.  After 968a18975a
updates were confused and __getosreldate was failing, causing
the system to fall back to compat compat12 syscalls in some cases.

Return to explicitly linking libc to libsys and link libthr with libc
and libsys (in that order).

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D43910
2024-02-19 22:44:08 +00:00
Brooks Davis 49d684369c libthr: filter rather than link with libsys
The allows gcc + GNU ld to link programs with -m32 -pthread without
erroring out due to _umtx_op_err being undefined (unless -lsys is added
to the link command.

We now always link _umtx_op_err into libthr (not just when it's static)
and filter it with libsys so we call that implementation.  The dynamic
implementations (at least the assembly ones) should likely become stubs
as a further refinement.

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D43783
2024-02-07 19:50:47 +00:00
Brooks Davis ef9871c620 libthr: move _umtx_op_err() to libsys
Declare in sys/umtx.h and implement in libsys.  Explicitly link libthr
with libsys.

When building libthr static include _umtx_op_err so we don't break static
linkage with -lpthread.

Reviewed by:	kib, emaste, imp
Pull Request:	https://github.com/freebsd/freebsd-src/pull/908
2024-02-05 20:34:56 +00:00
Warner Losh d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Greg Becker 642cd51102 libthr: Add src.conf variable WITHOUT_PTHREADS_ASSERTIONS
This patch fixes a bug which prevents building libthr without
_PTHREADS_INVARIANTS defined. The default remains to build libthr
with -D_PTHREADS_INVARIANTS. However, with this patch, if one builds
libthr with WITHOUT_PTHREADS_ASSERTIONS=true then the latency to
acquire+release a default pthread mutex is reduced by roughly 5%, and a
robust mutex by roughly 18% (as measured by a simple synthetic test on a
Xeon E5-2697a based machine).

Reviewed by:	jhb, kib, mjg
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D40900
2023-07-07 04:45:09 +03:00
John Baldwin bbf4df1722 libthr: Disable stack unwinding on ARM.
When a thread exits, _Unwind_ForcedUnwind() is used to walk up stack
frames executing pending cleanups pushed by pthread_cleanup_push().
The cleanups are popped by thread_unwind_stop() which is passed as a
callback function to _Unwind_ForcedUnwind().

LLVM's libunwind uses a different function type for the callback on
32-bit ARM relative to all other platforms.  The previous unwind.h
header (as well as the unwind.h from libcxxrt) use the non-ARM type on
all platforms, so this has likely been broken on 32-bit arm since it
switched to using LLVM's libunwind.

For now, just disable stack unwinding on 32-bit arm to unbreak the
build until a proper fix is tested.
2022-02-10 12:47:08 -08:00
John Baldwin c00d345665 Install unwind.h into /usr/include
Install headers from LLVM's libunwind in place of the headers from
libcxxrt and allow C applications to use the library.

As part of this, remove include/unwind.h and switch libthr over to
using the installed unwind.h.

Reviewed by:	dim, emaste
MFC after:	10 days
Differential Revision: https://reviews.freebsd.org/D34065
2022-02-10 19:00:32 +01:00
Alex Richardson 9efbe526e0 libthr: work around an ASAN false-positive
I got the following error with an ASAN-instrument libthr:

==803==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffffffcdb0 at pc 0x000801863396 bp 0x7ff8
READ of size 4 at 0x7fffffffcdb0 thread T0
    #0 0x801863395 in handle_signal /local/scratch/alr48/cheri/freebsd/lib/libthr/thread/thr_sig.c:262:2
    #1 0x801860da2 in thr_sighandler /local/scratch/alr48/cheri/freebsd/lib/libthr/thread/thr_sig.c:246:2

Address 0x7fffffffcdb0 is located in stack of thread T0 at offset 208 in frame
    #0 0x80186080f in thr_sighandler /local/scratch/alr48/cheri/freebsd/lib/libthr/thread/thr_sig.c:213

  This frame has 1 object(s):
    [32, 64) 'act' (line 216) <== Memory access at offset 208 overflows this variable
HINT: this may be a false positive if your program uses some custom stack

This seems like a false-positive since the line in question is
`SIGSETOR(actp->sa_mask, ucp->uc_sigmask);` and it complains about a read
operation (from the ucontext_t argument) so this indicates to me that ASAN
does not understand that thr_sighandler() is a signal handler.

Differential Revision: https://reviews.freebsd.org/D31074
2021-08-02 14:33:24 +01:00
Alex Richardson c8c62548bf Don't add -Winline for WARNS=6
This warning is very rarely useful (inline is a hint and not mandatory).
This flag results in many warnings being printed when compiling C++
code that uses the standard library with GCC.

This flag was originally added in back in r94332 but the flag is a no-op
in Clang ("This diagnostic flag exists for GCC compatibility, and has no
effect in Clang"). Removing it should make the GCC build output slightly
more readable.

Reviewed By:	jrtc27, imp
Differential Revision: https://reviews.freebsd.org/D29235
2021-03-22 11:55:45 +00:00
Eric van Gyzen fac6dee9eb Remove tests for obsolete compilers in the build system
Assume gcc is at least 6.4, the oldest xtoolchain in the ports tree.
Assume clang is at least 6, which was in 11.2-RELEASE.  Drop conditions
for older compilers.

Reviewed by:	imp (earlier version), emaste, jhb
MFC after:	2 weeks
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D24802
2020-05-12 15:22:40 +00:00
Konstantin Belousov 5d00c5a657 Fix initial exec TLS mode for dynamically loaded shared objects.
If dso uses initial exec TLS mode, rtld tries to allocate TLS in
static space. If there is no space left, the dlopen(3) fails. If space
if allocated, initial content from PT_TLS segment is distributed to
all threads' pcbs, which was missed and caused un-initialized TLS
segment for such dso after dlopen(3).

The mode is auto-detected either due to the relocation used, or if the
DF_STATIC_TLS dynamic flag is set.  In the later case, the TLS segment
is tried to allocate earlier, which increases chance of the dlopen(3)
to succeed.  LLD was recently fixed to properly emit the flag, ld.bdf
did it always.

Initial test by:	dumbbell
Tested by:	emaste (amd64), ian (arm)
Tested by:	Gerald Aryeetey <aryeeteygerald_rogers.com> (arm64)
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D19072
2019-03-29 17:52:57 +00:00
Ed Maste 09b47fc1c2 revert r341429 "disable BIND_NOW in libc, libthr, and rtld"
r345620 by kib@ fixed the rtld issue that caused a crash at startup
during resolution of libc's ifuncs with BIND_NOW.

PR:		233333
Sponsored by:	The FreeBSD Foundation
2019-03-28 02:12:32 +00:00
Konstantin Belousov d49ca25de6 Rename rtld-elf/malloc.c to rtld-elf/rtld_malloc.c.
Then malloc.c file name is too generic to use it for libthr.a.

Sponsored by:	The FreeBSD Foundation
MFC after:	13 days
2019-01-30 16:28:27 +00:00
Konstantin Belousov 381c2d2e9a Untangle jemalloc and mutexes initialization.
The need to use libc malloc(3) from some places in libthr always
caused issues.  For instance, per-thread key allocation was switched to
use plain mmap(2) to get storage, because some third party mallocs
used keys for implementation of calloc(3).

Even more important, libthr calls calloc(3) during initialization of
pthread mutexes, and jemalloc uses pthread mutexes.  Jemalloc provides
some way to both postpone the initialization, and to make
initialization to use specialized allocator, but this is very fragile
and often breaks.  See the referenced PR for another example.

Add the small malloc implementation used by rtld, to libthr. Use it in
thr_spec.c and for mutexes initialization. This avoids the issues with
mutual dependencies between malloc and libthr in principle.  The
drawback is that some more allocations are not interceptable for
alternate malloc implementations.  There should be not too much memory
use from this allocator, and the alternative, direct use of mmap(2) is
obviously worse.

PR:	235211
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D18988
2019-01-29 22:46:44 +00:00
Ed Maste 0e450664ad disable BIND_NOW in libc, libthr, and rtld
An issue remains with BIND_NOW and processes using threads.  For now,
restore libc's BIND_NOW disable, and also disable BIND_NOW in rtld and
libthr.

A patch is in review (D18400) that likely fixes this issue, but just
disable BIND_NOW pending further testing after it is committed.

PR:		233333
Sponsored by:	The FreeBSD Foundation
2018-12-03 15:59:46 +00:00
John Baldwin 9c4a7c50ae Remove references to the LIBC_SCCS and SYSLIBC_SCCS C macros.
This language dates back to when libthr was libc_r that included its own
syscalls and replaced libc entirely.  It hasn't been relevant for a long
time.
2018-05-23 17:05:12 +00:00
Enji Cooper d511b20a69 Add HAS_TESTS to all Makefiles that are currently using the
`SUBDIR.${MK_TESTS}+= tests` idiom.

This is a follow up to r321912.
2017-08-02 08:50:42 +00:00
Enji Cooper 4b330699f8 Convert traditional ${MK_TESTS} conditional idiom for including test
directories to SUBDIR.${MK_TESTS} idiom

This is being done to pave the way for future work (and homogenity) in
^/projects/make-check-sandbox .

No functional change intended.

MFC after:	1 weeks
2017-08-02 08:35:51 +00:00
Eric van Gyzen afba14e2b8 libthr: increase WARNS to the default (6)
...and silence cast-align warnings from gcc.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10935
2017-05-26 15:57:54 +00:00
Eric van Gyzen cfc1010f6d Revert r318583 (libthr: use default WARNS level of 6)
Revert this while I fix RISC-V, SPARC, and probably all architectures
that use GCC.

Reported by:	kib
Pointy hat to:	vangyzen
Sponsored by:	Dell EMC
2017-05-22 13:21:28 +00:00
Eric van Gyzen 0b2f3f2058 libthr: Use CLI flags instead of pragmas to disable warnings
People tweaking the build system or compilers tend to look into
the Makefile and not into the source.  Having some warning controls
in the Makefile and some in the source code is surprising.

Pragmas have the advantage that they leave the warnings enabled
for more code, but that advantage isn't very relevant in these cases.

Requested by:	kib
Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:33:47 +00:00
Eric van Gyzen d71b289766 libthr: use default WARNS level of 6
Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:32:30 +00:00
Eric van Gyzen 6eccf6e38f libthr: disable thread-safety warnings
These warnings don't make sense for code that implements
the locking primitives.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:29:36 +00:00
Enji Cooper 58368ba984 Use SRCTOP-relative paths to other directories instead of .CURDIR-relative ones
This implifies pathing in make/displayed output

MFC after:    3 weeks
Sponsored by: Dell EMC Isilon
2017-01-20 04:32:19 +00:00
Glen Barber d60840138f MFH
Sponsored by:	The FreeBSD Foundation
2016-04-04 23:55:32 +00:00
Konstantin Belousov e13c604596 Fix typo.
MFC after:	3 days
2016-03-21 05:59:05 +00:00
Glen Barber c389411c8a Remove libc, librtld_db, libthr packages, and further increase
the constraints on what needs to be installed in a specific to
maintain consistency during upgrades.

Create a new clibs package containing libraries that are needed
as a bare minimum for consistency.

With much help and input from:	kib
Sponsored by:	The FreeBSD Foundation
2016-02-05 21:01:08 +00:00
Glen Barber a70cba9582 First pass through library packaging.
Sponsored by:	The FreeBSD Foundation
2016-02-04 21:16:35 +00:00
Baptiste Daroussin 18b2ee82db Revert r284417 it is not necessary anymore 2015-06-15 19:28:07 +00:00
Baptiste Daroussin 4232f82668 Enforce overwritting SHLIBDIR
Since METAMODE has been added, sys.mk loads bsd.mkopt.mk which ends load loading
bsd.own.mk which then defines SHLIBDIR before all the Makefile.inc everywhere.

This makes /lib being populated again.

Reported by:	many
2015-06-15 15:34:20 +00:00
Enji Cooper 1119ece4d3 Build/install libc, librt, libthr, and msun NetBSD test suites on all
architectures

MFC after: 1 week
2015-04-27 06:49:27 +00:00
Konstantin Belousov 0538aafc41 The lseek(2), mmap(2), truncate(2), ftruncate(2), pread(2), and
pwrite(2) syscalls are wrapped to provide compatibility with pre-7.x
kernels which required padding before the off_t parameter.  The
fcntl(2) contains compatibility code to handle kernels before the
struct flock was changed during the 8.x CURRENT development.  The
shims were reasonable to allow easier revert to the older kernel at
that time.

Now, two or three major releases later, shims do not serve any
purpose.  Such old kernels cannot handle current libc, so revert the
compatibility code.

Make padded syscalls support conditional under the COMPAT6 config
option.  For COMPAT32, the syscalls were under COMPAT6 already.

Remove WITHOUT_SYSCALL_COMPAT build option, which only purpose was to
(partially) disable the removed shims.

Reviewed by:	jhb, imp (previous versions)
Discussed with:	peter
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2015-04-18 21:50:13 +00:00
Andrew Turner 20fe2c9465 Merge all the copies of _tcb_ctor and _tcb_dtor.
The amd64, i386, and sparc64 versions were identical, with the one
difference where the former two used inline asm instead of _tcb_get. I
have compared the function before and after replacing the asm with _tcb_get
and found the object files to be identical.

The arm, mips, and powerpc versions were almost identical. The only
difference was the powerpc version used an alignment of 1 where arm and
mips used 16. As this is an increase in alignment is will be safe.

Along with this arm, mips, and powerpc all passed, when initial was true,
the value returned from _tcb_get as the first argument to
_rtld_allocate_tls. This would then return this pointer back to the caller.
We can remove these extra calls by checking if initial is set and setting
the thread control block directly. As this is what the sparc64 code does
we can use it directly.

As after these observations all the architectures can now have identical
code we can merge them into a common file.

Differential Revision:	https://reviews.freebsd.org/D1556
Reviewed by:	kib
Sponsored by:	The FreeBSD Foundation
2015-01-21 16:41:05 +00:00
Konstantin Belousov 8495e8b1e9 Fix known issues which blow up the process after dlopen("libthr.so")
(or loading a dso linked to libthr.so into process which was not
linked against threading library).

- Remove libthr interposers of the libc functions, including
  __error(). Instead, functions calls are indirected through the
  interposing table, similar to how pthread stubs in libc are already
  done.  Libc by default points either to syscall trampolines or to
  existing libc implementations.  On libthr load, libthr rewrites the
  pointers to the cancellable implementations already in libthr.  The
  interposition table is separate from pthreads stubs indirection
  table to not pull pthreads stubs into static binaries.

- Postpone the malloc(3) internal mutexes initialization until libthr
  is loaded.  This avoids recursion between calloc(3) and static
  pthread_mutex_t initialization.

- Reinstall signal handlers with wrapper on libthr load.  The
  _rtld_is_dlopened(3) is used to avoid useless calls to sigaction(2)
  when libthr is statically referenced from the main binary.

In the process, fix openat(2), swapcontext(2) and setcontext(2)
interposing.  The libc symbols were exported at different versions
than libthr interposers.  Export both libc and libthr versions from
libc now, with default set to the higher version from libthr.

Remove unused and disconnected swapcontext(3) userspace implementation
from libc/gen.

No objections from:	deischen
Tested by:	pho, antoine (exp-run) (previous versions)
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2015-01-03 18:38:46 +00:00
Enji Cooper 3eee258dfb Add reachover Makefiles for contrib/netbsd-tests/lib/libpthread as
lib/libthr/tests

A variant of this code has been tested on amd64/i386 for some time by
EMC/Isilon on 10-STABLE/11-CURRENT. It builds on other architectures, but the
code will remain off until it's proven it works on virtual hardware or real
hardware on other architectures

Original work by: pho
Sponsored by: EMC / Isilon Storage Division
2014-11-16 06:35:20 +00:00
Warner Losh c6063d0da8 Use src.opts.mk in preference to bsd.own.mk except where we need stuff
from the latter.
2014-05-06 04:22:01 +00:00
Warner Losh a5fc5b6223 Convert from WITHOUT_SYSCALL_COMPAT to MK_SYSCALL_COMPAT. 2014-04-05 17:54:43 +00:00
David Xu 0935fc89af Oops, don't remove -fexceptions flag. 2010-10-08 01:53:33 +00:00
David Xu 0c3c9625a0 unwind.h was imported, gcc directory is no longer needed. 2010-10-08 01:47:14 +00:00
David Xu f4213b9006 To support stack unwinding for cancellation points, add -fexceptions flag
for them, two functions _pthread_cancel_enter and _pthread_cancel_leave
are added to let thread enter and leave a cancellation point, it also
makes it possible that other functions can be cancellation points in
libraries without having to be rewritten in libthr.
2010-09-25 01:57:47 +00:00
David Xu 3832fd24f1 add code to support stack unwinding when thread exits. note that only
defer-mode cancellation works, asynchrnous mode does not work because
it lacks of libuwind's support. stack unwinding is not enabled unless
LIBTHR_UNWIND_STACK is defined in Makefile.
2010-09-15 02:56:32 +00:00
Warner Losh 10f3ae5899 Merge from tbemd, with a small amount of rework:
For all libthr contexts, use ${MACHINE_CPUARCH}
for all libc contexts, use ${MACHINE_ARCH} if it exists, otherwise use
${MACHINE_CPUARCH}
Move some common code up a layer (the .PATH statement was the same in
all the arch submakefiles).

# Hope she hasn't busted powerpc64 with this...
2010-09-13 01:43:10 +00:00
Nathan Whitehorn f4fbf9c859 Unify 32-bit and 64-bit PowerPC libthr support. This reduces code
duplication, and simplifies the TBEMD import.

Requested by:	imp
2010-08-24 20:50:08 +00:00
Konstantin Belousov 6d772c3b9e Revert r199830 for now. Too many ports dlopen() libraries linked with
libthr, but forgot to link main binary with it.
2009-11-28 14:34:28 +00:00
Konstantin Belousov 86665365a8 Libthr cannot be dynamically loaded into the running process.
Mark it with -z nodlopen for now.

Discussed with:	jhb, kan
MFC after:	3 weeks
2009-11-26 14:01:14 +00:00
David Xu 35c608253d Turn on nodelete linker flag because libthr can not be unloaded safely,
it does hook on to libc.
2009-03-31 02:50:18 +00:00
David Xu c30c187d60 Don't reference non-existent __fcntl_compat if WITHOUT_SYSCALL_COMPAT is defined.
Submitted by:	Pawel Worach "pawel dot worach at gmail dot com"
2009-03-09 02:34:02 +00:00