Implement more security stuff

- Support deterministic stacks on OpenBSD
- Support OpenBSD system call origin verification
- Fix overrun by one in chibicc string token allocator
- Get all chibicc tests passing under Address Sanitizer
This commit is contained in:
Justine Tunney
2021-02-02 20:21:06 -08:00
parent cbfd4ccd1e
commit c843243322
56 changed files with 376 additions and 245 deletions

View File

@ -18,7 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/str/str.h"
static noasan uint64_t UncheckedAlignedRead64(unsigned char *p) {
static inline noasan uint64_t UncheckedAlignedRead64(unsigned char *p) {
return (uint64_t)p[7] << 070 | (uint64_t)p[6] << 060 | (uint64_t)p[5] << 050 |
(uint64_t)p[4] << 040 | (uint64_t)p[3] << 030 | (uint64_t)p[2] << 020 |
(uint64_t)p[1] << 010 | (uint64_t)p[0] << 000;

View File

@ -19,6 +19,8 @@
#include "libc/str/oldutf16.internal.h"
#include "libc/str/str.h"
/* TODO(jart): DELETE */
/**
* Encodes character to string as UTF-16.
*

View File

@ -19,7 +19,7 @@
#include "libc/assert.h"
#include "libc/str/str.h"
static noasan uint64_t UncheckedAlignedRead64(unsigned char *p) {
static inline noasan uint64_t UncheckedAlignedRead64(unsigned char *p) {
return (uint64_t)p[7] << 070 | (uint64_t)p[6] << 060 | (uint64_t)p[5] << 050 |
(uint64_t)p[4] << 040 | (uint64_t)p[3] << 030 | (uint64_t)p[2] << 020 |
(uint64_t)p[1] << 010 | (uint64_t)p[0] << 000;

View File

@ -187,7 +187,6 @@ char16_t *tinystrstr16(const char16_t *, const char16_t *) strlenesque;
void *tinymemmem(const void *, size_t, const void *, size_t) strlenesque;
void *tinymemccpy(void *, const void *, int, size_t) memcpyesque;
void *memtolower(void *, size_t);
char *strntolower(char *, size_t);
char *strtolower(char *) paramsnonnull();
char *strntoupper(char *, size_t);

View File

@ -18,16 +18,18 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/str/str.h"
/**
* Mutates string to ASCII uppercase w/ limit.
*
* @praam s is string
* @praam n is max bytes to consider
* @return string
*/
char *strntoupper(char *s, size_t n) {
unsigned char *p = (unsigned char *)s;
for (;;) {
if (n-- && *p) {
if ('a' <= *p && *p <= 'z') {
*p -= 'a' - 'A';
}
++p;
} else {
break;
size_t i;
for (i = 0; s[i] && i < n; ++i) {
if ('a' <= s[i] && s[i] <= 'z') {
s[i] -= 'a' - 'A';
}
}
return s;

View File

@ -16,9 +16,20 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/math.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/str/str.h"
bool ctz(double x, double y) {
return __builtin_islessgreater(x, y);
/**
* Mutates string to ASCII lowercase.
*
* @praam s is string
* @return string
*/
char *strtolower(char *s) {
size_t i;
for (i = 0; s[i]; ++i) {
if ('A' <= s[i] && s[i] <= 'Z') {
s[i] += 'a' - 'A';
}
}
return s;
}

35
libc/str/strtoupper.c Normal file
View File

@ -0,0 +1,35 @@
/*-*- 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/str/str.h"
/**
* Mutates string to ASCII uppercase.
*
* @praam s is string
* @return string
*/
char *strtoupper(char *s) {
size_t i;
for (i = 0; s[i]; ++i) {
if ('a' <= s[i] && s[i] <= 'z') {
s[i] -= 'a' - 'A';
}
}
return s;
}

View File

@ -17,15 +17,21 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/str/str.h"
#include "libc/str/tinymemmem.internal.h"
/**
* Naïve substring search implementation.
* @see libc/alg/memmem.c
*/
void *tinymemmem(const void *haystk, size_t haystksize, const void *needle,
void *tinymemmem(const void *haystack, size_t haystacksize, const void *needle,
size_t needlesize) {
return (/*unconst*/ void *)tinymemmemi(
(const unsigned char *)haystk, haystksize, (const unsigned char *)needle,
needlesize);
size_t i;
const char *p, *pe;
for (p = haystack, pe = p + haystacksize; p < pe;) {
for (++p, i = 0;;) {
if (++i > needlesize) return p - 1;
if (p == pe) break;
if (((const char *)needle)[i - 1] != (p - 1)[i - 1]) break;
}
}
return !haystacksize && !needlesize ? haystack : NULL;
}

View File

@ -1,26 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_STR_TINYSTRSTR_H_
#define COSMOPOLITAN_LIBC_STR_TINYSTRSTR_H_
#ifndef __STRICT_ANSI__
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline void *tinymemmemi(const void *haystk, size_t haystksize,
const void *needle, size_t needlesize) {
const char *p = (const char *)haystk;
const char *pe = (const char *)haystk + haystksize;
while (p < pe) {
size_t i = 0;
++p;
for (;;) {
++i;
if (i > needlesize) return (/*unconst*/ char *)(p - 1);
if (p == pe) break;
if (((const char *)needle)[i - 1] != (p - 1)[i - 1]) break;
}
}
return (/*unconst*/ char *)(!haystksize && !needlesize ? haystk : NULL);
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_STR_TINYMEMMEM_H_ */

View File

@ -17,13 +17,22 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/str/internal.h"
#include "libc/str/tinystrstr.internal.h"
/**
* Naïve substring search implementation.
* @see libc/str/strstr.c
* @asyncsignalsafe
*/
char *(tinystrstr)(const char *haystack, const char *needle) {
return (/*unconst*/ char *)tinystrstr(haystack, needle);
char *tinystrstr(const char *haystack, const char *needle) {
size_t i;
for (;;) {
for (i = 0;;) {
if (!needle[i]) return (/*unconst*/ char *)haystack;
if (!haystack[i]) break;
if (needle[i] != haystack[i]) break;
++i;
}
if (!*haystack++) break;
}
return NULL;
}

View File

@ -1,26 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_STR_TINYSTRSTR_H_
#define COSMOPOLITAN_LIBC_STR_TINYSTRSTR_H_
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define tinystrstr(HAYSTACK, NEEDLE) \
({ \
autotype(HAYSTACK) Haystack = (HAYSTACK); \
typeof(Haystack) Needle = (NEEDLE); \
for (;;) { \
size_t i = 0; \
for (;;) { \
if (!Needle[i]) goto Found; \
if (!Haystack[i]) break; \
if (Needle[i] != Haystack[i]) break; \
++i; \
} \
if (!*Haystack++) break; \
} \
Haystack = NULL; \
Found: \
Haystack; \
})
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_STR_TINYSTRSTR_H_ */

View File

@ -17,7 +17,6 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/str/internal.h"
#include "libc/str/tinystrstr.internal.h"
/**
* Naïve substring search implementation.
@ -25,5 +24,15 @@
* @asyncsignalsafe
*/
char16_t *tinystrstr16(const char16_t *haystack, const char16_t *needle) {
return (/*unconst*/ char16_t *)tinystrstr(haystack, needle);
size_t i;
for (;;) {
for (i = 0;;) {
if (!needle[i]) return (/*unconst*/ char16_t *)haystack;
if (!haystack[i]) break;
if (needle[i] != haystack[i]) break;
++i;
}
if (!*haystack++) break;
}
return NULL;
}

View File

@ -21,6 +21,8 @@
#include "libc/str/tpdecode.internal.h"
#include "libc/str/tpdecodecb.internal.h"
/* TODO(jart): DELETE */
forceinline int getbyte(void *arg, uint32_t i) {
return ((const unsigned char *)arg)[i];
}

View File

@ -6,19 +6,14 @@ COSMOPOLITAN_C_START_
uint64_t tpenc(int32_t) pureconst;
#ifndef __STRICT_ANSI__
#define tpenc(CODE) \
({ \
long Buf; \
int32_t Code = (CODE); \
if (0 <= Code && Code <= 127) { \
Buf = Code; \
} else { \
asm("call\ttpenc" \
: "=a"(Buf), "+D"(Code) \
: /* inputs */ \
: "rcx", "rdx", "cc"); \
} \
Buf; \
#define tpenc(CODE) \
({ \
long Edi, Buf; \
asm("call\ttpenc" \
: "=a"(Buf), "=D"(Edi) \
: "1"(CODE) \
: "rcx", "rdx", "cc"); \
Buf; \
})
#endif