2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/bits/bits.h"
|
|
|
|
#include "libc/bits/xchg.h"
|
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/fmt/fmt.h"
|
2020-09-28 08:13:56 +00:00
|
|
|
#include "libc/log/log.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/gc.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/runtime/memtrack.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/msync.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
#include "libc/x/x.h"
|
|
|
|
|
|
|
|
TEST(mmap, testMapFile) {
|
|
|
|
int fd;
|
|
|
|
char *p;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
|
|
|
|
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
|
|
|
|
EXPECT_EQ(5, write(fd, "hello", 5));
|
|
|
|
EXPECT_NE(-1, fdatasync(fd));
|
|
|
|
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 0)));
|
|
|
|
EXPECT_STREQ("hello", p);
|
|
|
|
EXPECT_NE(-1, munmap(p, 5));
|
|
|
|
EXPECT_NE(-1, close(fd));
|
|
|
|
EXPECT_NE(-1, unlink(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(mmap, testMapFile_fdGetsClosed_makesNoDifference) {
|
|
|
|
int fd;
|
|
|
|
char *p, buf[16], path[PATH_MAX];
|
|
|
|
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
|
|
|
|
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
|
|
|
|
EXPECT_EQ(5, write(fd, "hello", 5));
|
|
|
|
EXPECT_NE(-1, fdatasync(fd));
|
|
|
|
EXPECT_NE(MAP_FAILED,
|
|
|
|
(p = mmap(NULL, 5, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)));
|
|
|
|
EXPECT_NE(-1, close(fd));
|
|
|
|
EXPECT_STREQ("hello", p);
|
|
|
|
p[1] = 'a';
|
|
|
|
EXPECT_NE(-1, msync(p, PAGESIZE, MS_SYNC));
|
|
|
|
ASSERT_NE(-1, (fd = open(path, O_RDONLY)));
|
|
|
|
EXPECT_EQ(5, read(fd, buf, 5));
|
|
|
|
EXPECT_STREQN("hallo", buf, 5);
|
|
|
|
EXPECT_NE(-1, close(fd));
|
|
|
|
EXPECT_NE(-1, munmap(p, 5));
|
|
|
|
EXPECT_NE(-1, unlink(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(mmap, testMapFixed_destroysEverythingInItsPath) {
|
2020-08-25 11:23:25 +00:00
|
|
|
unsigned m1 = _mmi.i;
|
2020-09-03 12:44:37 +00:00
|
|
|
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedmapStart + FRAMESIZE * 0),
|
2020-06-15 14:18:57 +00:00
|
|
|
FRAMESIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
2020-09-03 12:44:37 +00:00
|
|
|
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedmapStart + FRAMESIZE * 1),
|
2020-06-15 14:18:57 +00:00
|
|
|
FRAMESIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
2020-09-03 12:44:37 +00:00
|
|
|
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedmapStart + FRAMESIZE * 2),
|
2020-06-15 14:18:57 +00:00
|
|
|
FRAMESIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
2020-09-03 12:44:37 +00:00
|
|
|
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedmapStart + FRAMESIZE * 0),
|
2020-06-15 14:18:57 +00:00
|
|
|
FRAMESIZE * 3, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
2020-08-25 11:23:25 +00:00
|
|
|
ASSERT_GT(_mmi.i, m1);
|
2020-09-03 12:44:37 +00:00
|
|
|
EXPECT_NE(-1, munmap((void *)kFixedmapStart, FRAMESIZE * 3));
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(isheap, nullPtr) {
|
|
|
|
ASSERT_FALSE(isheap(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(isheap, malloc) {
|
|
|
|
ASSERT_TRUE(isheap(gc(malloc(1))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(isheap, emptyMalloc) {
|
|
|
|
ASSERT_TRUE(isheap(gc(malloc(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(isheap, mallocOffset) {
|
2020-09-28 08:13:56 +00:00
|
|
|
char *p = gc(malloc(131072));
|
|
|
|
ASSERT_TRUE(isheap(p + 100000));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|