Skip to content

Commit

Permalink
[libc][NFC] Move sys/mman entrypoints to the default build configs.
Browse files Browse the repository at this point in the history
Specifically, mmap and munmap have been moved to the default build list
of entrypoints. To support this, certain deps and includes have been
adjusted. The use of errno in some cases has been updated.
  • Loading branch information
Siva Chandra Reddy committed Jan 11, 2022
1 parent d345ce6 commit 134e9d1
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 34 deletions.
6 changes: 1 addition & 5 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,11 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.trunc
libc.src.math.truncf
libc.src.math.truncl
)

if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# sys/mman.h entrypoints
libc.src.sys.mman.mmap
libc.src.sys.mman.munmap
)
endif()
)

set(TARGET_LLVMLIBC_ENTRYPOINTS
${TARGET_LIBC_ENTRYPOINTS}
Expand Down
8 changes: 4 additions & 4 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.strtoll
libc.src.stdlib.strtoul
libc.src.stdlib.strtoull

# sys/mman.h entrypoints
libc.src.sys.mman.mmap
libc.src.sys.mman.munmap
)

set(TARGET_LIBM_ENTRYPOINTS
Expand Down Expand Up @@ -203,10 +207,6 @@ if(LLVM_LIBC_FULL_BUILD)
# libc.src.signal.sigfillset
# libc.src.signal.signal

# sys/mman.h entrypoints
libc.src.sys.mman.mmap
libc.src.sys.mman.munmap

# threads.h entrypoints
libc.src.threads.call_once
libc.src.threads.cnd_broadcast
Expand Down
1 change: 0 additions & 1 deletion libc/loader/linux/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ add_loader_object(
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.string.memcpy
libc.src.sys.mman.mmap
COMPILE_OPTIONS
-fno-omit-frame-pointer
-ffreestanding # To avoid compiler warnings about calling the main function.
Expand Down
1 change: 0 additions & 1 deletion libc/loader/linux/x86_64/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "include/sys/syscall.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/string/memcpy.h"
#include "src/sys/mman/mmap.h"

#include <asm/prctl.h>
#include <linux/auxvec.h>
Expand Down
6 changes: 4 additions & 2 deletions libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ add_subdirectory(math)
add_subdirectory(string)
add_subdirectory(stdlib)

if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(sys)
endif()

if(NOT LLVM_LIBC_FULL_BUILD)
return()
endif()
Expand All @@ -17,8 +21,6 @@ endif()
# add_subdirectory(assert)
# add_subdirectory(signal)
add_subdirectory(stdio)
# TODO: Add this target conditional to the target OS.
add_subdirectory(sys)
add_subdirectory(threads)
add_subdirectory(time)
add_subdirectory(unistd)
6 changes: 3 additions & 3 deletions libc/src/sys/mman/linux/mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#include "src/sys/mman/mmap.h"

#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"

#include <errno.h>
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <sys/syscall.h> // For syscall numbers.

namespace __llvm_libc {

Expand Down Expand Up @@ -53,7 +53,7 @@ LLVM_LIBC_FUNCTION(void *, mmap,
// return value corresponding to a location in the last page is an error
// value.
if (ret_val < 0 && ret_val > -EXEC_PAGESIZE) {
llvmlibc_errno = -ret_val;
errno = -ret_val;
return MAP_FAILED;
}

Expand Down
7 changes: 4 additions & 3 deletions libc/src/sys/mman/linux/munmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#include "src/sys/mman/munmap.h"

#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"

#include <errno.h>
#include <sys/syscall.h> // For syscall numbers.

namespace __llvm_libc {

Expand All @@ -24,7 +25,7 @@ LLVM_LIBC_FUNCTION(int, munmap, (void *addr, size_t size)) {
// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret_val < 0) {
llvmlibc_errno = -ret_val;
errno = -ret_val;
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/mman/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_SYS_MMAN_MMAP_H
#define LLVM_LIBC_SRC_SYS_MMAN_MMAP_H

#include "include/sys/mman.h" // For size_t and off_t
#include <sys/mman.h> // For size_t and off_t

namespace __llvm_libc {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/mman/munmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_SYS_MMAN_MUNMAP_H
#define LLVM_LIBC_SRC_SYS_MMAN_MUNMAP_H

#include "include/sys/mman.h" // For size_t.
#include <sys/mman.h> // For size_t.

namespace __llvm_libc {

Expand Down
5 changes: 3 additions & 2 deletions libc/src/unistd/linux/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"

#include <errno.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(ssize_t, write, (int fd, const void *buf, size_t count)) {
long ret = __llvm_libc::syscall(SYS_write, fd, buf, count);
if (ret < 0) {
llvmlibc_errno = -ret;
errno = -ret;
return -1;
}
return ret;
Expand Down
7 changes: 4 additions & 3 deletions libc/test/ErrnoSetterMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#ifndef LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H
#define LLVM_LIBC_TEST_ERRNOSETTERMATCHER_H

#include "src/errno/llvmlibc_errno.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>

namespace __llvm_libc {
namespace testing {

Expand Down Expand Up @@ -42,8 +43,8 @@ template <typename T> class ErrnoSetterMatcher : public Matcher<T> {

bool match(T Got) {
ActualReturn = Got;
ActualErrno = llvmlibc_errno;
llvmlibc_errno = 0;
ActualErrno = errno;
errno = 0;
return Got == ExpectedReturn && ActualErrno == ExpectedErrno;
}
};
Expand Down
5 changes: 4 additions & 1 deletion libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ add_subdirectory(math)
add_subdirectory(string)
add_subdirectory(stdlib)

if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(sys)
endif()

if(NOT LLVM_LIBC_FULL_BUILD)
return()
endif()
Expand All @@ -43,7 +47,6 @@ endif()
# add_subdirectory(assert)
# add_subdirectory(signal)
add_subdirectory(stdio)
add_subdirectory(sys)
add_subdirectory(threads)
add_subdirectory(time)
add_subdirectory(unistd)
Expand Down
12 changes: 6 additions & 6 deletions libc/test/src/sys/mman/linux/mmap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
//
//===----------------------------------------------------------------------===//

#include "include/errno.h"
#include "include/sys/mman.h"
#include "src/errno/llvmlibc_errno.h"
#include "src/sys/mman/mmap.h"
#include "src/sys/mman/munmap.h"
#include "test/ErrnoSetterMatcher.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <sys/mman.h>

using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;

TEST(LlvmLibcMMapTest, NoError) {
size_t alloc_size = 128;
llvmlibc_errno = 0;
errno = 0;
void *addr = __llvm_libc::mmap(nullptr, alloc_size, PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
EXPECT_EQ(0, llvmlibc_errno);
EXPECT_EQ(0, errno);
EXPECT_NE(addr, MAP_FAILED);

int *array = reinterpret_cast<int *>(addr);
Expand All @@ -34,7 +34,7 @@ TEST(LlvmLibcMMapTest, NoError) {
}

TEST(LlvmLibcMMapTest, Error_InvalidSize) {
llvmlibc_errno = 0;
errno = 0;
void *addr = __llvm_libc::mmap(nullptr, 0, PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
EXPECT_THAT(addr, Fails(EINVAL, MAP_FAILED));
Expand Down
3 changes: 2 additions & 1 deletion libc/test/src/unistd/write_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include "include/errno.h"
#include "src/unistd/write.h"
#include "test/ErrnoSetterMatcher.h"
#include "utils/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>

TEST(LlvmLibcUniStd, WriteBasic) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *HELLO = "hello";
Expand Down

0 comments on commit 134e9d1

Please sign in to comment.