Get --ftrace working on aarch64

This change implements a new approach to function call logging, that's
based on the GCC flag: -fpatchable-function-entry. Read the commentary
in build/config.mk to learn how it works.
This commit is contained in:
Justine Tunney 2023-06-05 23:35:31 -07:00
parent 5b908bc756
commit eb40cb371d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
934 changed files with 2259 additions and 1268 deletions

View file

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_AARCH64_ASMDEFS_H_
#define COSMOPOLITAN_LIBC_INTRIN_AARCH64_ASMDEFS_H_
#include "libc/macros.internal.h"
#ifdef __ASSEMBLER__
// clang-format off
@ -44,7 +45,9 @@ GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI|FEATURE_1_PAC)
.global name; \
.type name,%function; \
.align alignment; \
.ftrace1; \
name: \
.ftrace2; \
.cfi_startproc; \
BTI_C;

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __memchr_aarch64 memchr

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __memcmp_aarch64 memcmp

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __memcpy_aarch64_simd memcpy
#define __memmove_aarch64_simd memmove

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __memrchr_aarch64 memrchr

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __memset_aarch64 memset

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __stpcpy_aarch64 stpcpy

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strchr_aarch64 strchr

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strchrnul_aarch64 strchrnul

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strcmp_aarch64 strcmp

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strcpy_aarch64 strcpy

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strlen_aarch64 strlen

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strncmp_aarch64 strncmp

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strnlen_aarch64 strnlen

View file

@ -25,7 +25,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/intrin/aarch64/asmdefs.h"
#include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strrchr_aarch64 strrchr

View file

@ -26,6 +26,7 @@
#include "libc/intrin/weaken.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/thread/thread.h"
@ -38,7 +39,7 @@ relegated void __assert_fail(const char *expr, const char *file, int line) {
strace_enabled(-1);
ftrace_enabled(-1);
owner = 0;
me = __tls_enabled ? __get_tls()->tib_tid : sys_gettid();
me = __tls_enabled ? __get_tls()->tib_tid : __pid;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
kprintf("%s:%d: assert(%s) failed (tid %d) %m\n", file, line, expr, me);
if (__vforked ||

View file

@ -33,9 +33,10 @@
// @param rsi:rdi is 128-bit unsigned 𝑥 value
// @return eax number in range [0,128) or undef if 𝑥 is 0
// @see also treasure trove of nearly identical functions
.ftrace1
_bsr128:
.ftrace2
.leafprologue
.profilable
bsr %rsi,%rax
jnz 2f
bsr %rdi,%rax

View file

@ -26,7 +26,7 @@
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
typedef long long xmm_a __attribute__((__vector_size__(16), __aligned__(16)));
static void bzero_sse(char *p, size_t n) {
static void bzero128(char *p, size_t n) {
xmm_t v = {0};
if (IsAsan()) __asan_verify(p, n);
if (n <= 32) {
@ -162,6 +162,6 @@ void bzero(void *p, size_t n) {
bzero_avx(b, n);
#endif
} else {
bzero_sse(b, n);
bzero128(b, n);
}
}

View file

@ -16,6 +16,7 @@ LIBC_INTRIN_A_CHECKS = $(LIBC_INTRIN_A).pkg
ifeq ($(ARCH), aarch64)
LIBC_INTRIN_A_SRCS_S += $(wildcard libc/intrin/aarch64/*.S)
LIBC_INTRIN_A_HDRS += libc/intrin/aarch64/asmdefs.internal.h
endif
LIBC_INTRIN_A_OBJS = \
@ -49,39 +50,39 @@ $(LIBC_INTRIN_A).pkg: \
# we can't use asan because:
# __strace_init() calls this before asan is initialized
o/$(MODE)/libc/intrin/strace_enabled.o: private \
OVERRIDE_COPTS += \
COPTS += \
-fno-sanitize=address
# we can't use asan because:
# asan guard pages haven't been allocated yet
o/$(MODE)/libc/intrin/directmap.o \
o/$(MODE)/libc/intrin/directmap-nt.o: private \
OVERRIDE_COPTS += \
COPTS += \
-ffreestanding \
-fno-sanitize=address
# we want small code size because:
# to keep .text.head under 4096 bytes
o/$(MODE)/libc/intrin/mman.greg.o: private \
OVERRIDE_COPTS += \
COPTS += \
-Os
# we can't use asan and ubsan because:
# this is asan and ubsan
o/$(MODE)/libc/intrin/asan.o \
o/$(MODE)/libc/intrin/ubsan.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fno-sanitize=all \
-fno-stack-protector
o/$(MODE)/libc/intrin/asan.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-O2 \
-finline \
-finline-functions
o/$(MODE)/libc/intrin/asanthunk.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-x-no-pg \
$(MNO_FENTRY) \
-ffreestanding \
@ -95,7 +96,7 @@ o/$(MODE)/libc/intrin/strerrno.greg.o \
o/$(MODE)/libc/intrin/strerrdoc.greg.o \
o/$(MODE)/libc/intrin/strerror_wr.greg.o \
o/$(MODE)/libc/intrin/kprintf.greg.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fpie \
-fwrapv \
-x-no-pg \
@ -109,10 +110,9 @@ o/$(MODE)/libc/intrin/kprintf.greg.o: private \
o/$(MODE)/libc/intrin/futex_wait.o \
o/$(MODE)/libc/intrin/futex_wake.o \
o/$(MODE)/libc/intrin/gettid.greg.o \
o/$(MODE)/libc/intrin/sys_gettid.greg.o \
o/$(MODE)/libc/intrin/_trylock_debug_4.o \
o/$(MODE)/libc/intrin/_spinlock_debug_4.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fwrapv \
-x-no-pg \
$(MNO_FENTRY) \
@ -125,13 +125,13 @@ o/$(MODE)/libc/intrin/_spinlock_debug_4.o: private \
# global gone could be raised
o/$(MODE)/libc/intrin/exit.o \
o/$(MODE)/libc/intrin/restorewintty.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fno-sanitize=all
# we can't use -ftrapv because:
# this file implements it
o/$(MODE)/libc/intrin/ftrapv.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-ffunction-sections \
-ffreestanding \
-fwrapv
@ -142,7 +142,7 @@ o/$(MODE)/libc/intrin/describeflags.o \
o/$(MODE)/libc/intrin/describeframe.o \
o/$(MODE)/libc/intrin/describemapflags.o \
o/$(MODE)/libc/intrin/describeprotflags.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fno-sanitize=address
o/$(MODE)/libc/intrin/exit1.greg.o \
@ -180,7 +180,7 @@ o/$(MODE)/libc/intrin/createfilemappingnuma.o \
o/$(MODE)/libc/intrin/waitformultipleobjects.o \
o/$(MODE)/libc/intrin/generateconsolectrlevent.o \
o/$(MODE)/libc/intrin/wsawaitformultipleevents.o: private\
OVERRIDE_CFLAGS += \
CFLAGS += \
-Os \
-fwrapv \
-ffreestanding \
@ -188,20 +188,20 @@ o/$(MODE)/libc/intrin/wsawaitformultipleevents.o: private\
-fno-sanitize=all
o//libc/intrin/memmove.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fno-toplevel-reorder
o//libc/intrin/bzero.o \
o//libc/intrin/memcmp.o \
o//libc/intrin/memset.o \
o//libc/intrin/memmove.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-O2 -finline
o/$(MODE)/libc/intrin/bzero.o \
o/$(MODE)/libc/intrin/memcmp.o \
o/$(MODE)/libc/intrin/memmove.o: private \
OVERRIDE_CFLAGS += \
CFLAGS += \
-fpie
# these assembly files are safe to build on aarch64

View file

@ -382,7 +382,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
if (tib) {
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
} else {
x = sys_gettid();
x = __pid;
}
if (!__nocolor && p + 7 <= e) {
*p++ = '\e';

View file

@ -19,13 +19,14 @@
#include "libc/dce.h"
#include "libc/sysv/consts/nr.h"
#include "libc/macros.internal.h"
.privileged
// Relinquishes scheduled quantum.
//
// @return 0 on success, or -1 w/ errno
// @norestart
.ftrace1
sched_yield:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp

View file

@ -19,6 +19,7 @@
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/str/str.h"
#ifndef __aarch64__
/**
* Compares NUL-terminated strings.
@ -28,7 +29,7 @@
* @return is <0, 0, or >0 based on uint8_t comparison
* @asyncsignalsafe
*/
noasan int strcmp(const char *a, const char *b) {
int strcmp(const char *a, const char *b) {
int c;
size_t i = 0;
uint64_t v, w, d;
@ -58,3 +59,5 @@ noasan int strcmp(const char *a, const char *b) {
}
return (a[i] & 255) - (b[i] & 255);
}
#endif /* __aarch64__ */

View file

@ -24,7 +24,7 @@
__msabi extern typeof(GetCurrentThreadId) *const __imp_GetCurrentThreadId;
privileged int sys_gettid(void) {
int sys_gettid(void) {
#ifdef __x86_64__
int tid;
int64_t wut;

View file

@ -23,8 +23,9 @@
// @param edi is int to encode
// @return rax is word-encoded byte buffer
// @note invented on a napkin in a new jersey diner
_tpenc: .leafprologue
.profilable
.ftrace1
_tpenc: .ftrace2
.leafprologue
mov %edi,%edi
xor %eax,%eax
cmp $127,%edi
@ -47,20 +48,20 @@ _tpenc: .leafprologue
.rodata
.balign 4
.underrun
kTpenc: .rept 4 # MSB10 (0x7FF)
.byte 1,0b11000000 # len,mark
kTpenc: .rept 4 // MSB10 (0x7FF)
.byte 1,0b11000000 // len,mark
.endr
.rept 5 # MSB15 (0xFFFF)
.byte 2,0b11100000 # len,mark
.rept 5 // MSB15 (0xFFFF)
.byte 2,0b11100000 // len,mark
.endr
.rept 5 # MSB20 (0x1FFFFF)
.byte 3,0b11110000 # len,mark
.rept 5 // MSB20 (0x1FFFFF)
.byte 3,0b11110000 // len,mark
.endr
.rept 5 # MSB25 (0x3FFFFFF)
.byte 4,0b11111000 # len,mark
.rept 5 // MSB25 (0x3FFFFFF)
.byte 4,0b11111000 // len,mark
.endr
.rept 6 # MSB31 (0xffffffff)
.byte 5,0b11111100 # len,mark
.rept 6 // MSB31 (0xffffffff)
.byte 5,0b11111100 // len,mark
.endr
.zero 2
.endobj kTpenc