mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
6b7ce19161
These instances were detected by searching for files that include stdlib.h, but don't match the regex: \\b(_abort|abort|abs|aligned_alloc|arc4random|arc4random_buf|arc4random_ uniform|atexit|atof|atoi|atol|atoll|bsearch|calloc|clearenv|div|div_t|ex it|_Exit|EXIT_FAILURE|EXIT_SUCCESS|free|getenv|getprogname|grantpt|labs| ldiv|ldiv_t|llabs|lldiv|lldiv_t|malloc|malloc_good_size|malloc_size|mble n|mbstowcs|mbtowc|mkdtemp|mkstemp|mkstemps|mktemp|posix_memalign|posix_o penpt|ptsname|ptsname_r|putenv|qsort|qsort_r|rand|RAND_MAX|random|reallo c|realpath|secure_getenv|serenity_dump_malloc_stats|serenity_setenv|sete nv|setprogname|srand|srandom|strtod|strtof|strtol|strtold|strtoll|strtou l|strtoull|system|unlockpt|unsetenv|wcstombs|wctomb)\\b (Without the linebreaks.) This regex is pessimistic, so there might be more files that don't actually use anything from the stdlib. In theory, one might use LibCPP to detect things like this automatically, but let's do this one step after another.
108 lines
3.1 KiB
C++
108 lines
3.1 KiB
C++
/*
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Types.h>
|
|
#include <LibTest/TestCase.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
static u8 read_buffer[0x100000];
|
|
|
|
static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length)
|
|
{
|
|
u64 mmoffset = base % sysconf(_SC_PAGESIZE);
|
|
void* mmp = mmap(NULL, mmoffset + length, PROT_READ, MAP_SHARED, fd, base - mmoffset);
|
|
if (mmp == MAP_FAILED)
|
|
return false;
|
|
if (munmap(mmp, mmoffset + length) < 0)
|
|
perror("munmap");
|
|
return true;
|
|
}
|
|
|
|
enum class ReadResult {
|
|
SeekFailure,
|
|
ReadFailure,
|
|
ReadSuccess,
|
|
};
|
|
|
|
static ALWAYS_INLINE ReadResult read_chunk(int fd, u64 base, u64 length)
|
|
{
|
|
VERIFY(length <= sizeof(read_buffer));
|
|
auto rs = lseek(fd, base, SEEK_SET);
|
|
if (rs < 0) {
|
|
fprintf(stderr, "Couldn't seek to offset %" PRIi64 " while verifying: %s\n", base, strerror(errno));
|
|
return ReadResult::SeekFailure;
|
|
}
|
|
if (read(fd, read_buffer, length) < 0)
|
|
return ReadResult::ReadFailure;
|
|
return ReadResult::ReadSuccess;
|
|
}
|
|
|
|
TEST_CASE(test_memory_access_device_read)
|
|
{
|
|
int rc = geteuid();
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
int fd = open("/dev/mem", O_RDONLY);
|
|
EXPECT(fd >= 0);
|
|
|
|
// FIXME: This is expected to work on QEMU machines (both 440FX and Q35),
|
|
// however, it will be much nicer to have some sort of a node in the ProcFS
|
|
// to expose physical memory ranges (e820 memory map).
|
|
|
|
auto read_result = read_chunk(fd, 0x0, 0x100000);
|
|
EXPECT_EQ(read_result, ReadResult::ReadFailure);
|
|
|
|
read_result = read_chunk(fd, 0xe0000, 0x100000 - 0xe0000);
|
|
EXPECT_EQ(read_result, ReadResult::ReadSuccess);
|
|
|
|
read_result = read_chunk(fd, 0x100000, 0x200000 - 0x100000);
|
|
EXPECT_EQ(read_result, ReadResult::ReadFailure);
|
|
|
|
read_result = read_chunk(fd, 0xf0000, 70000);
|
|
EXPECT_EQ(read_result, ReadResult::ReadFailure);
|
|
|
|
read_result = read_chunk(fd, 0xfffc0000, 16384);
|
|
EXPECT_EQ(read_result, ReadResult::ReadSuccess);
|
|
|
|
read_result = read_chunk(fd, 0xfffc0000, 0x100000);
|
|
EXPECT_EQ(read_result, ReadResult::ReadFailure);
|
|
}
|
|
|
|
TEST_CASE(test_memory_access_device_mmap)
|
|
{
|
|
int rc = geteuid();
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
int fd = open("/dev/mem", O_RDONLY);
|
|
EXPECT(fd >= 0);
|
|
|
|
// FIXME: This is expected to work on QEMU machines (both 440FX and Q35),
|
|
// however, it will be much nicer to have some sort of a node in the ProcFS
|
|
// to expose physical memory ranges (e820 memory map).
|
|
|
|
auto mmap_result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000);
|
|
EXPECT_EQ(mmap_result, true);
|
|
|
|
mmap_result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000);
|
|
EXPECT_EQ(mmap_result, false);
|
|
|
|
mmap_result = mem_chunk(fd, 0xf0000, 70000);
|
|
EXPECT_EQ(mmap_result, false);
|
|
|
|
mmap_result = mem_chunk(fd, 0xfffc0000, 16384);
|
|
EXPECT_EQ(mmap_result, true);
|
|
|
|
mmap_result = mem_chunk(fd, 0xfffc0000, 0x100000);
|
|
EXPECT_EQ(mmap_result, false);
|
|
}
|