Replace COSMO define with _COSMO_SOURCE

This change might cause ABI breakages for /opt/cosmos. It's needed to
help us better conform to header declaration practices.
This commit is contained in:
Justine Tunney 2023-08-13 20:31:27 -07:00
parent a033b65a33
commit c776a32f75
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
238 changed files with 858 additions and 1069 deletions

View file

@ -1,53 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/memtrack.internal.h"
dontasan bool AreMemoryIntervalsOk(const struct MemoryIntervals *mm) {
/* asan runtime depends on this function */
int i;
size_t wantsize;
for (i = 0; i < mm->i; ++i) {
if (mm->p[i].y < mm->p[i].x) {
STRACE("AreMemoryIntervalsOk() y should be >= x!");
return false;
}
wantsize = (size_t)(mm->p[i].y - mm->p[i].x) * FRAMESIZE;
if (!(wantsize < mm->p[i].size && mm->p[i].size <= wantsize + FRAMESIZE)) {
STRACE("AreMemoryIntervalsOk(%p) size is wrong!"
" %'zu not within %'zu .. %'zu",
(uintptr_t)mm->p[i].x << 16, mm->p[i].size, wantsize,
wantsize + FRAMESIZE);
return false;
}
if (i) {
if (mm->p[i].h != -1 || mm->p[i - 1].h != -1) {
if (mm->p[i].x <= mm->p[i - 1].y) {
return false;
}
} else {
if (!(mm->p[i - 1].y + 1 <= mm->p[i].x)) {
STRACE("AreMemoryIntervalsOk() out of order or overlap!");
return false;
}
}
}
}
return true;
}

View file

@ -197,7 +197,7 @@ static uint64_t __asan_roundup2pow(uint64_t x) {
static char *__asan_utf8cpy(char *p, unsigned c) {
uint64_t z;
z = _tpenc(c);
z = tpenc(c);
do *p++ = z;
while ((z >>= 8));
return p;
@ -403,7 +403,7 @@ static bool __asan_is_mapped(int x) {
struct MemoryIntervals *m;
__mmi_lock();
m = _weaken(_mmi);
i = FindMemoryInterval(m, x);
i = __find_memory(m, x);
res = i < m->i && x >= m->p[i].x;
__mmi_unlock();
return res;
@ -1411,10 +1411,10 @@ void __asan_map_shadow(uintptr_t p, size_t n) {
flag = MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS;
sm = _weaken(sys_mmap)(addr, size, prot, flag, -1, 0);
if (sm.addr == MAP_FAILED ||
_weaken(TrackMemoryInterval)(m, a, a + i - 1, sm.maphandle,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
false, false, 0, size) == -1) {
_weaken(__track_memory)(m, a, a + i - 1, sm.maphandle,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, false,
false, 0, size) == -1) {
kprintf("error: could not map asan shadow memory\n");
__asan_die()();
__asan_unreachable();
@ -1487,7 +1487,7 @@ void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) {
}
REQUIRE(_mmi);
REQUIRE(sys_mmap);
REQUIRE(TrackMemoryInterval);
REQUIRE(__track_memory);
if (_weaken(hook_malloc) || _weaken(hook_calloc) || _weaken(hook_realloc) ||
_weaken(hook_realloc_in_place) || _weaken(hook_free) ||
_weaken(hook_malloc_usable_size)) {

View file

@ -1,8 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_BITS_H_
#define COSMOPOLITAN_LIBC_BITS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef _COSMO_SOURCE
COSMOPOLITAN_C_START_
#ifdef COSMO
int _bitreverse8(int) pureconst;
int _bitreverse16(int) pureconst;
@ -99,7 +99,7 @@ uint64_t _bitreverse64(uint64_t) pureconst;
__p + 64 / 8; \
}))
#endif /* COSMO */
COSMOPOLITAN_C_END_
#endif /* _COSMO_SOURCE */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_H_ */

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_DLL_H_
#define COSMOPOLITAN_LIBC_INTRIN_DLL_H_
#ifdef COSMO
#ifdef _COSMO_SOURCE
#define dll_make_first __dll_make_first
#define dll_make_last __dll_make_last
#define dll_remove __dll_remove
@ -53,5 +53,5 @@ void dll_splice_after(struct Dll *, struct Dll *) paramsnonnull((1)) libcesque;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* COSMOPOLITAN_LIBC_INTRIN_DLL_H_ */

View file

@ -37,8 +37,8 @@ static void *_mapframe(void *p, int f) {
flags = f | MAP_ANONYMOUS | MAP_FIXED;
if ((dm = sys_mmap(p, G, prot, flags, -1, 0)).addr == p) {
__mmi_lock();
rc = TrackMemoryInterval(&_mmi, (uintptr_t)p >> 16, (uintptr_t)p >> 16,
dm.maphandle, prot, flags, false, false, 0, G);
rc = __track_memory(&_mmi, (uintptr_t)p >> 16, (uintptr_t)p >> 16,
dm.maphandle, prot, flags, false, false, 0, G);
__mmi_unlock();
if (!rc) {
return p;

View file

@ -19,7 +19,7 @@
#include "libc/assert.h"
#include "libc/runtime/memtrack.internal.h"
dontasan unsigned FindMemoryInterval(const struct MemoryIntervals *mm, int x) {
dontasan unsigned __find_memory(const struct MemoryIntervals *mm, int x) {
unsigned l, m, r;
l = 0;
r = mm->i;

View file

@ -1,15 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_BITS_HILBERT_H_
#define COSMOPOLITAN_LIBC_BITS_HILBERT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef _COSMO_SOURCE
COSMOPOLITAN_C_START_
#ifdef COSMO
#define hilbert __hilbert
#define unhilbert __unhilbert
long hilbert(long, long, long) pureconst;
axdx_t unhilbert(long, long) pureconst;
#endif /* COSMO */
COSMOPOLITAN_C_END_
#endif /* _COSMO_SOURCE */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_HILBERT_H_ */

View file

@ -33,7 +33,7 @@
/**
* Returns true if host platform is WSL 1.0.
*/
bool IsWsl1(void) {
bool __iswsl1(void) {
static char res;
if (res) return res & 1;
if (!IsLinux()) return res = 2, false;

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_
#define COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_
#ifdef COSMO
#ifdef _COSMO_SOURCE
#define kprintf __kprintf
#define ksnprintf __ksnprintf
@ -26,5 +26,5 @@ void _klog(const char *, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ */

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_BITS_LIKELY_H_
#define COSMOPOLITAN_LIBC_BITS_LIKELY_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#ifdef _COSMO_SOURCE
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
@ -18,6 +18,6 @@
#define VERY_UNLIKELY(x) UNLIKELY(x)
#endif
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_LIKELY_H_ */

View file

@ -24,7 +24,6 @@
#include "libc/intrin/bits.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/strace.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
@ -37,18 +36,8 @@
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/errfuns.h"
#if IsModeDbg()
#define ASSERT_MEMTRACK() \
if (!AreMemoryIntervalsOk(mm)) { \
PrintMemoryIntervals(2, mm); \
notpossible; \
}
#else
#define ASSERT_MEMTRACK()
#endif
static void *MoveMemoryIntervals(struct MemoryInterval *d,
const struct MemoryInterval *s, int n) {
static void *__shove_memory(struct MemoryInterval *d,
const struct MemoryInterval *s, int n) {
int i;
unassert(n >= 0);
if (d > s) {
@ -63,14 +52,14 @@ static void *MoveMemoryIntervals(struct MemoryInterval *d,
return d;
}
static void RemoveMemoryIntervals(struct MemoryIntervals *mm, int i, int n) {
static void __remove_memory(struct MemoryIntervals *mm, int i, int n) {
unassert(i >= 0);
unassert(i + n <= mm->i);
MoveMemoryIntervals(mm->p + i, mm->p + i + n, mm->i - (i + n));
__shove_memory(mm->p + i, mm->p + i + n, mm->i - (i + n));
mm->i -= n;
}
static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) {
static bool __extend_memory(struct MemoryIntervals *mm) {
int prot, flags;
char *base, *shad;
size_t gran, size;
@ -89,7 +78,7 @@ static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) {
}
dm = sys_mmap(base, gran, prot, flags, -1, 0);
if (!dm.addr) return false;
MoveMemoryIntervals(dm.addr, mm->p, mm->i);
__shove_memory(dm.addr, mm->p, mm->i);
mm->p = dm.addr;
mm->n = gran / sizeof(*mm->p);
} else {
@ -104,21 +93,20 @@ static bool ExtendMemoryIntervals(struct MemoryIntervals *mm) {
if (!dm.addr) return false;
mm->n = (size + gran) / sizeof(*mm->p);
}
ASSERT_MEMTRACK();
return true;
}
int CreateMemoryInterval(struct MemoryIntervals *mm, int i) {
static int __mint_memory(struct MemoryIntervals *mm, int i) {
unassert(i >= 0);
unassert(i <= mm->i);
unassert(mm->n >= 0);
if (UNLIKELY(mm->i == mm->n) && !ExtendMemoryIntervals(mm)) return enomem();
MoveMemoryIntervals(mm->p + i + 1, mm->p + i, mm->i++ - i);
if (mm->i == mm->n && !__extend_memory(mm)) return enomem();
__shove_memory(mm->p + i + 1, mm->p + i, mm->i++ - i);
return 0;
}
static int PunchHole(struct MemoryIntervals *mm, int x, int y, int i) {
if (CreateMemoryInterval(mm, i) == -1) return -1;
static int __punch_memory(struct MemoryIntervals *mm, int x, int y, int i) {
if (__mint_memory(mm, i) == -1) return -1;
mm->p[i + 0].size -= (size_t)(mm->p[i + 0].y - (x - 1)) * FRAMESIZE;
mm->p[i + 0].y = x - 1;
mm->p[i + 1].size -= (size_t)((y + 1) - mm->p[i + 1].x) * FRAMESIZE;
@ -126,19 +114,18 @@ static int PunchHole(struct MemoryIntervals *mm, int x, int y, int i) {
return 0;
}
int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y,
void wf(struct MemoryIntervals *, int, int)) {
int __untrack_memory(struct MemoryIntervals *mm, int x, int y,
void wf(struct MemoryIntervals *, int, int)) {
unsigned l, r;
ASSERT_MEMTRACK();
unassert(y >= x);
if (!mm->i) return 0;
// binary search for the lefthand side
l = FindMemoryInterval(mm, x);
l = __find_memory(mm, x);
if (l == mm->i) return 0;
if (y < mm->p[l].x) return 0;
// binary search for the righthand side
r = FindMemoryInterval(mm, y);
r = __find_memory(mm, y);
if (r == mm->i || (r > l && y < mm->p[r].x)) --r;
unassert(r >= l);
unassert(x <= mm->p[r].y);
@ -152,7 +139,7 @@ int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y,
// this isn't possible on windows because we track each
// 64kb segment on that platform using a separate entry
if (l == r && x > mm->p[l].x && y < mm->p[l].y) {
return PunchHole(mm, x, y, l);
return __punch_memory(mm, x, y, l);
}
// trim the right side of the lefthand map
@ -189,18 +176,17 @@ int ReleaseMemoryIntervals(struct MemoryIntervals *mm, int x, int y,
if (IsWindows() && wf) {
wf(mm, l, r);
}
RemoveMemoryIntervals(mm, l, r - l + 1);
__remove_memory(mm, l, r - l + 1);
}
return 0;
}
int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h,
int prot, int flags, bool readonlyfile, bool iscow,
long offset, long size) {
int __track_memory(struct MemoryIntervals *mm, int x, int y, long h, int prot,
int flags, bool readonlyfile, bool iscow, long offset,
long size) {
unsigned i;
ASSERT_MEMTRACK();
unassert(y >= x);
i = FindMemoryInterval(mm, x);
i = __find_memory(mm, x);
// try to extend the righthand side of the lefthand entry
// we can't do that if we're tracking independent handles
@ -216,7 +202,7 @@ int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h,
prot == mm->p[i].prot && flags == mm->p[i].flags) {
mm->p[i - 1].y = mm->p[i].y;
mm->p[i - 1].size += mm->p[i].size;
RemoveMemoryIntervals(mm, i, 1);
__remove_memory(mm, i, 1);
}
}
@ -231,7 +217,7 @@ int TrackMemoryInterval(struct MemoryIntervals *mm, int x, int y, long h,
// otherwise, create a new entry and memmove the items
else {
if (CreateMemoryInterval(mm, i) == -1) return -1;
if (__mint_memory(mm, i) == -1) return -1;
mm->p[i].x = x;
mm->p[i].y = y;
mm->p[i].h = h;

View file

@ -1,8 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_
#define COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_
#ifdef _COSMO_SOURCE
#include "libc/macros.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \
!defined(__MNO_RED_ZONE__)
@ -54,6 +54,6 @@
})
#endif
#endif /* COSMO */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* _COSMO_SOURCE */
#endif /* COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ */

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_
#define COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#ifdef _COSMO_SOURCE
forceinline void repmovsb(void **dest, const void **src, size_t cx) {
char *di = (char *)*dest;
@ -23,6 +23,6 @@ forceinline void repmovsb(void **dest, const void **src, size_t cx) {
})
#endif
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ */

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_
#define COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#ifdef _COSMO_SOURCE
forceinline void *repstosb(void *dest, unsigned char al, size_t cx) {
unsigned char *di = (unsigned char *)dest;
@ -22,6 +22,6 @@ forceinline void *repstosb(void *dest, unsigned char al, size_t cx) {
})
#endif
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ */

View file

@ -2,7 +2,7 @@
#define COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#ifdef COSMO
#ifdef _COSMO_SOURCE
/**
* Reads scalar from memory, offset by segment.
@ -20,7 +20,7 @@
Pk; \
})
#endif /* COSMO */
#endif /* _COSMO_SOURCE */
#endif /* __GNUC__ && !__STRICT_ANSI__ */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ */

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bsr.h"
#include "libc/str/str.h"
static const uint16_t kTpEnc[32 - 7] = {
1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 2 | 0340 << 8,
@ -29,7 +30,7 @@ static const uint16_t kTpEnc[32 - 7] = {
/**
* Encodes Thompson-Pike variable-length integer.
*/
uint64_t _tpenc(uint32_t c) {
uint64_t tpenc(uint32_t c) {
int e, n;
uint64_t w;
if (0 <= c && c <= 127) return c;