mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Make a few fixups
This commit is contained in:
parent
a5849f8549
commit
6070a53e89
8 changed files with 176 additions and 88 deletions
|
@ -390,10 +390,14 @@ void __asan_unpoison(void *p, long n) {
|
|||
static bool __asan_is_mapped(int x) {
|
||||
// xxx: we can't lock because no reentrant locks yet
|
||||
int i;
|
||||
bool res;
|
||||
struct MemoryIntervals *m;
|
||||
__mmi_lock();
|
||||
m = weaken(_mmi);
|
||||
i = FindMemoryInterval(m, x);
|
||||
return i < m->i && x >= m->p[i].x;
|
||||
res = i < m->i && x >= m->p[i].x;
|
||||
__mmi_unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool __asan_is_image(const unsigned char *p) {
|
||||
|
@ -812,7 +816,7 @@ dontdiscard static __asan_die_f *__asan_report(const void *addr, int size,
|
|||
p = __asan_format_section(p, _base, _etext, ".text", addr);
|
||||
p = __asan_format_section(p, _etext, _edata, ".data", addr);
|
||||
p = __asan_format_section(p, _end, _edata, ".bss", addr);
|
||||
// xxx: we can't lock because no reentrant locks yet
|
||||
__mmi_lock();
|
||||
for (m = weaken(_mmi), i = 0; i < m->i; ++i) {
|
||||
x = m->p[i].x;
|
||||
y = m->p[i].y;
|
||||
|
@ -823,6 +827,7 @@ dontdiscard static __asan_die_f *__asan_report(const void *addr, int size,
|
|||
if (x <= z && z <= y) p = __stpcpy(p, " ←shadow");
|
||||
*p++ = '\n';
|
||||
}
|
||||
__mmi_unlock();
|
||||
*p = 0;
|
||||
kprintf("%s", __fatalbuf);
|
||||
__asan_report_memory_origin(addr, size, kind);
|
||||
|
|
|
@ -1,62 +0,0 @@
|
|||
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
||||
│vi: set et ft=asm ts=8 tw=8 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/nexgen32e/x86feature.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
||||
// Searches for last instance of byte in memory region.
|
||||
//
|
||||
// @param rdi points to data to search
|
||||
// @param esi is treated as unsigned char
|
||||
// @param rdx is byte length of rdi
|
||||
// @return rax is address of last %sil in %rdi, or NULL
|
||||
// @note AVX2 requires Haswell (2014+) or Excavator (2015+)
|
||||
// @asyncsignalsafe
|
||||
memrchr:
|
||||
.leafprologue
|
||||
.profilable
|
||||
#if !IsTiny()
|
||||
cmp $32,%rdx
|
||||
jb 5f
|
||||
testb X86_HAVE(AVX2)+kCpuids(%rip)
|
||||
jz 5f
|
||||
vmovd %esi,%xmm0
|
||||
vpbroadcastb %xmm0,%ymm0
|
||||
3: vmovdqu -32(%rdi,%rdx),%ymm1
|
||||
vpcmpeqb %ymm1,%ymm0,%ymm1
|
||||
vpmovmskb %ymm1,%eax
|
||||
lzcnt %eax,%eax
|
||||
mov %eax,%ecx
|
||||
sub %rcx,%rdx
|
||||
cmp $32,%eax
|
||||
jne 5f
|
||||
cmp $31,%rdx
|
||||
ja 3b
|
||||
vzeroupper
|
||||
#endif
|
||||
5: xor %eax,%eax
|
||||
mov %rdx,%rcx
|
||||
6: sub $1,%rcx
|
||||
jb 9f
|
||||
cmp %sil,-1(%rdi,%rdx)
|
||||
mov %rcx,%rdx
|
||||
jne 6b
|
||||
lea (%rdi,%rcx),%rax
|
||||
9: .leafepilogue
|
||||
.endfn memrchr,globl
|
79
libc/str/memrchr.c
Normal file
79
libc/str/memrchr.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*-*- 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 2021 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/dce.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
|
||||
|
||||
static inline const unsigned char *memrchr_pure(const unsigned char *s,
|
||||
unsigned char c, size_t n) {
|
||||
size_t i;
|
||||
for (i = n; i--;) {
|
||||
if (s[i] == c) {
|
||||
return s + i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
noasan static inline const unsigned char *memrchr_sse(const unsigned char *s,
|
||||
unsigned char c,
|
||||
size_t n) {
|
||||
size_t i;
|
||||
unsigned k, m;
|
||||
xmm_t v, t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
|
||||
for (i = n; i >= 16;) {
|
||||
v = *(const xmm_t *)(s + (i -= 16));
|
||||
m = __builtin_ia32_pmovmskb128(v == t);
|
||||
if (m) {
|
||||
m = __builtin_clzl(m) ^ (sizeof(long) * CHAR_BIT - 1);
|
||||
return s + i + m;
|
||||
}
|
||||
}
|
||||
while (i--) {
|
||||
if (s[i] == c) {
|
||||
return s + i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns pointer to first instance of character.
|
||||
*
|
||||
* @param s is memory to search
|
||||
* @param c is search byte which is masked with 255
|
||||
* @param n is byte length of p
|
||||
* @return is pointer to first instance of c or NULL if not found
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
void *memrchr(const void *s, int c, size_t n) {
|
||||
const void *r;
|
||||
if (!IsTiny() && X86_HAVE(SSE)) {
|
||||
if (IsAsan()) {
|
||||
__asan_verify(s, n);
|
||||
}
|
||||
r = memrchr_sse(s, c, n);
|
||||
} else {
|
||||
r = memrchr_pure(s, c, n);
|
||||
}
|
||||
return (void *)r;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue