mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Add more tests for strlcpy()
I asked ChatGPT o1 for an optimized version of strlcpy() but it couldn't produce an implementation that didn't crash. Eventually it just gave up.
This commit is contained in:
parent
e142124730
commit
1260f9d0ed
2 changed files with 105 additions and 32 deletions
|
@ -16,12 +16,71 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/safemacros.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/sysconf.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/testlib/benchmark.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
size_t todd(char *dst, const char *src, size_t dsize) {
|
||||
const char *osrc = src;
|
||||
size_t nleft = dsize;
|
||||
if (nleft != 0)
|
||||
while (--nleft != 0)
|
||||
if ((*dst++ = *src++) == '\0')
|
||||
break;
|
||||
if (nleft == 0) {
|
||||
if (dsize != 0)
|
||||
*dst = '\0';
|
||||
while (*src++)
|
||||
;
|
||||
}
|
||||
return src - osrc - 1;
|
||||
}
|
||||
|
||||
TEST(strlcpy, fuzz) {
|
||||
int pagesz = sysconf(_SC_PAGESIZE);
|
||||
char *map1 = (char *)mmap(0, pagesz * 2, PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
npassert(map1 != MAP_FAILED);
|
||||
npassert(!mprotect(map1 + pagesz, pagesz, PROT_NONE));
|
||||
char *map2 = (char *)mmap(0, pagesz * 2, PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
npassert(map2 != MAP_FAILED);
|
||||
npassert(!mprotect(map2 + pagesz, pagesz, PROT_NONE));
|
||||
char *map3 = (char *)mmap(0, pagesz * 2, PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
npassert(map3 != MAP_FAILED);
|
||||
npassert(!mprotect(map3 + pagesz, pagesz, PROT_NONE));
|
||||
for (int dsize = 1; dsize < 128; ++dsize) {
|
||||
char *volatile dst1 = map1 + pagesz - dsize;
|
||||
char *volatile dst2 = map1 + pagesz - dsize;
|
||||
for (int i = 0; i < dsize; ++i)
|
||||
dst1[i] = dst2[i] = max(rand() & 255, 1);
|
||||
for (int ssize = 1; ssize < dsize * 2; ++ssize) {
|
||||
char *volatile src = map3 + pagesz - (ssize + 1);
|
||||
for (int i = 0; i < ssize; ++i)
|
||||
src[i] = max(rand() & 255, 1);
|
||||
src[ssize] = 0;
|
||||
ASSERT_EQ(todd(dst1, src, dsize), strlcpy(dst2, src, dsize));
|
||||
ASSERT_EQ(0, memcmp(dst1, dst2, dsize));
|
||||
}
|
||||
}
|
||||
npassert(!munmap(map3, pagesz * 2));
|
||||
npassert(!munmap(map2, pagesz * 2));
|
||||
npassert(!munmap(map1, pagesz * 2));
|
||||
}
|
||||
|
||||
TEST(strlcpy, testEmptyBuffer_doesNothing) {
|
||||
EXPECT_EQ(5, strlcpy(NULL, "hello", 0));
|
||||
}
|
||||
|
@ -38,12 +97,25 @@ TEST(strlcpy, testShortBuffer_copies) {
|
|||
EXPECT_STREQ("h", buf);
|
||||
}
|
||||
|
||||
#define N 4096
|
||||
|
||||
BENCH(strlcpy, bench) {
|
||||
char buf[256];
|
||||
EZBENCH2(
|
||||
"strlcpy", donothing,
|
||||
__expropriate(strlcpy(__veil("r", buf), "hello there", sizeof(buf))));
|
||||
EZBENCH2(
|
||||
"strncpy", donothing,
|
||||
__expropriate(strncpy(__veil("r", buf), "hello there", sizeof(buf))));
|
||||
char dst[N];
|
||||
char src[N + 1];
|
||||
|
||||
printf("\n");
|
||||
for (int n = 1; n <= N; n *= 2) {
|
||||
for (int i = 0; i < n; ++i)
|
||||
src[i] = max(rand() & 255, 1);
|
||||
src[n] = 0;
|
||||
BENCHMARK(100, n, X(strlcpy(dst, src, V(N))));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
for (int n = 1; n <= N; n *= 2) {
|
||||
for (int i = 0; i < n; ++i)
|
||||
src[i] = max(rand() & 255, 1);
|
||||
src[n] = 0;
|
||||
BENCHMARK(100, n, X(todd(dst, src, V(N))));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue