diff options
author | vvvv <vvvv@ydb.tech> | 2024-03-28 09:20:41 +0300 |
---|---|---|
committer | vvvv <vvvv@ydb.tech> | 2024-03-28 09:30:09 +0300 |
commit | 8deb352c61c1e941cac7c21b303a5d26f63c03af (patch) | |
tree | f4f88f8f40f279f8f1aeb7a772d29c7b39584db6 /contrib | |
parent | f828a2a3fec6993c8ab442ede0fd5f4a617a78f2 (diff) | |
download | ydb-8deb352c61c1e941cac7c21b303a5d26f63c03af.tar.gz |
export contrib/libs/libc_compat/ubuntu_14
0ec9dab7e194664a7c2b8df657f03408fe3378bc
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/libs/cxxsupp/libcxx/include/uchar.h | 56 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/aligned_alloc.c | 6 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/c16rtomb.c | 35 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/c32rtomb.c | 7 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/features.h | 5 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/getauxval.cpp | 10 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/glibc.cpp | 111 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/glibc.h | 20 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/mbrtoc16.c | 30 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/mbrtoc32.c | 13 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/secure_getenv.cpp | 12 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/timespec_get.c | 10 | ||||
-rw-r--r-- | contrib/libs/libc_compat/ubuntu_14/ya.make | 36 |
13 files changed, 351 insertions, 0 deletions
diff --git a/contrib/libs/cxxsupp/libcxx/include/uchar.h b/contrib/libs/cxxsupp/libcxx/include/uchar.h new file mode 100644 index 0000000000..546113f7ea --- /dev/null +++ b/contrib/libs/cxxsupp/libcxx/include/uchar.h @@ -0,0 +1,56 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_UCHAR_H +#define _LIBCPP_UCHAR_H + +/* + uchar.h synopsis // since C++11 + +Macros: + + __STDC_UTF_16__ + __STDC_UTF_32__ + +Types: + + mbstate_t + size_t + +size_t mbrtoc8(char8_t* pc8, const char* s, size_t n, mbstate_t* ps); // since C++20 +size_t c8rtomb(char* s, char8_t c8, mbstate_t* ps); // since C++20 +size_t mbrtoc16(char16_t* pc16, const char* s, size_t n, mbstate_t* ps); +size_t c16rtomb(char* s, char16_t c16, mbstate_t* ps); +size_t mbrtoc32(char32_t* pc32, const char* s, size_t n, mbstate_t* ps); +size_t c32rtomb(char* s, char32_t c32, mbstate_t* ps); + +*/ + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if !defined(_LIBCPP_CXX03_LANG) + +// Some platforms don't implement <uchar.h> and we don't want to give a hard +// error on those platforms. When the platform doesn't provide <uchar.h>, at +// least include <stddef.h> so we get the declaration for size_t, and try to +// get the declaration of mbstate_t too. +#if __has_include_next(<uchar.h>) +# include_next <uchar.h> +#else +# include <__mbstate_t.h> +# include <stddef.h> +#endif + +#endif // _LIBCPP_CXX03_LANG + +#endif // _LIBCPP_UCHAR_H diff --git a/contrib/libs/libc_compat/ubuntu_14/aligned_alloc.c b/contrib/libs/libc_compat/ubuntu_14/aligned_alloc.c new file mode 100644 index 0000000000..c4a1378624 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/aligned_alloc.c @@ -0,0 +1,6 @@ +#include <malloc.h> +#include <stdlib.h> + +__attribute__((weak)) void* aligned_alloc(size_t alignment, size_t size) { + return memalign(alignment, size); +} diff --git a/contrib/libs/libc_compat/ubuntu_14/c16rtomb.c b/contrib/libs/libc_compat/ubuntu_14/c16rtomb.c new file mode 100644 index 0000000000..39ca3758fa --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/c16rtomb.c @@ -0,0 +1,35 @@ +#include <uchar.h> +#include <errno.h> +#include <wchar.h> + +size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps) +{ + static unsigned internal_state; + if (!ps) ps = (void *)&internal_state; + unsigned *x = (unsigned *)ps; + wchar_t wc; + + if (!s) { + if (*x) goto ilseq; + return 1; + } + + if (!*x && c16 - 0xd800u < 0x400) { + *x = c16 - 0xd7c0 << 10; + return 0; + } + + if (*x) { + if (c16 - 0xdc00u >= 0x400) goto ilseq; + else wc = *x + c16 - 0xdc00; + *x = 0; + } else { + wc = c16; + } + return wcrtomb(s, wc, 0); + +ilseq: + *x = 0; + errno = EILSEQ; + return -1; +} diff --git a/contrib/libs/libc_compat/ubuntu_14/c32rtomb.c b/contrib/libs/libc_compat/ubuntu_14/c32rtomb.c new file mode 100644 index 0000000000..67851328e8 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/c32rtomb.c @@ -0,0 +1,7 @@ +#include <uchar.h> +#include <wchar.h> + +size_t c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps) +{ + return wcrtomb(s, c32, ps); +} diff --git a/contrib/libs/libc_compat/ubuntu_14/features.h b/contrib/libs/libc_compat/ubuntu_14/features.h new file mode 100644 index 0000000000..9fbab45ab5 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/features.h @@ -0,0 +1,5 @@ +#pragma once + +#define weak __attribute__((__weak__)) +#define weak_alias(old, new) \ + extern __typeof(old) new __attribute__((__weak__, __alias__(#old))) diff --git a/contrib/libs/libc_compat/ubuntu_14/getauxval.cpp b/contrib/libs/libc_compat/ubuntu_14/getauxval.cpp new file mode 100644 index 0000000000..9f20dd0195 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/getauxval.cpp @@ -0,0 +1,10 @@ +#include <sys/auxv.h> + +#include "glibc.h" +#include "features.h" + +extern "C" { + unsigned long getauxval(unsigned long item) noexcept { + return NUbuntuCompat::GetGlibc().GetAuxVal(item); + } +} diff --git a/contrib/libs/libc_compat/ubuntu_14/glibc.cpp b/contrib/libs/libc_compat/ubuntu_14/glibc.cpp new file mode 100644 index 0000000000..1cc444bce1 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/glibc.cpp @@ -0,0 +1,111 @@ +#include <elf.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "glibc.h" +#include "features.h" + +namespace { + void ReadAuxVector(Elf64_auxv_t** begin, Elf64_auxv_t** end) noexcept { + int fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC); + if (fd == -1) { + return; + } + + constexpr size_t item_size = sizeof(Elf64_auxv_t); + constexpr size_t block_size = item_size * 32; + + size_t bytes_read = 0; + size_t size = 0; + + struct TBuffer { + ~TBuffer() { + free(Pointer); + } + char* Pointer = nullptr; + } buffer; + + while (true) { + size_t bytes_left = size - bytes_read; + + if (!bytes_left) { + size += block_size; + char* new_buffer = (char*)realloc(buffer.Pointer, size); + if (!new_buffer) { + return; + } + buffer.Pointer = new_buffer; + continue; + } + + ssize_t r = read(fd, buffer.Pointer + bytes_read, bytes_left); + if (!r) { + break; + } else if (r < 0) { + if (errno == EINTR) { + continue; + } else { + return; + } + } + + bytes_read += r; + } + + size_t item_count = bytes_read / item_size; + *begin = (Elf64_auxv_t*)buffer.Pointer; + *end = (Elf64_auxv_t*)(buffer.Pointer + item_count * item_size); + buffer.Pointer = nullptr; + } +} + +extern "C" { + weak unsigned long __getauxval(unsigned long item); +} + +namespace NUbuntuCompat { + + TGlibc::TGlibc() noexcept + : AuxVectorBegin(nullptr) + , AuxVectorEnd(nullptr) + { + if (!__getauxval) { + ReadAuxVector((Elf64_auxv_t**)&AuxVectorBegin, (Elf64_auxv_t**)&AuxVectorEnd); + } + + Secure = (bool)GetAuxVal(AT_SECURE); + } + + TGlibc::~TGlibc() noexcept { + free(AuxVectorBegin); + } + + unsigned long TGlibc::GetAuxVal(unsigned long item) noexcept { + if (__getauxval) { + return __getauxval(item); + } + + for (Elf64_auxv_t* p = (Elf64_auxv_t*)AuxVectorBegin; p < (Elf64_auxv_t*)AuxVectorEnd; ++p) { + if (p->a_type == item) { + return p->a_un.a_val; + } + } + + errno = ENOENT; + return 0; + } + + bool TGlibc::IsSecure() noexcept { + return Secure; + } + + static TGlibc __attribute__((__init_priority__(101))) GlibcInstance; + + TGlibc& GetGlibc() noexcept { + return GlibcInstance; + } +} diff --git a/contrib/libs/libc_compat/ubuntu_14/glibc.h b/contrib/libs/libc_compat/ubuntu_14/glibc.h new file mode 100644 index 0000000000..fdabcb0158 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/glibc.h @@ -0,0 +1,20 @@ +#pragma once + +typedef unsigned long (*TGetAuxVal)(unsigned long); + +namespace NUbuntuCompat { + class TGlibc { + public: + TGlibc() noexcept; + ~TGlibc() noexcept; + unsigned long GetAuxVal(unsigned long item) noexcept; + bool IsSecure() noexcept; + + private: + void* AuxVectorBegin; + void* AuxVectorEnd; + bool Secure; + }; + + TGlibc& GetGlibc() noexcept; +} diff --git a/contrib/libs/libc_compat/ubuntu_14/mbrtoc16.c b/contrib/libs/libc_compat/ubuntu_14/mbrtoc16.c new file mode 100644 index 0000000000..765ff9037c --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/mbrtoc16.c @@ -0,0 +1,30 @@ +#include <uchar.h> +#include <wchar.h> + +size_t mbrtoc16(char16_t *restrict pc16, const char *restrict s, size_t n, mbstate_t *restrict ps) +{ + static unsigned internal_state; + if (!ps) ps = (void *)&internal_state; + unsigned *pending = (unsigned *)ps; + + if (!s) return mbrtoc16(0, "", 1, ps); + + /* mbrtowc states for partial UTF-8 characters have the high bit set; + * we use nonzero states without high bit for pending surrogates. */ + if ((int)*pending > 0) { + if (pc16) *pc16 = *pending; + *pending = 0; + return -3; + } + + wchar_t wc; + size_t ret = mbrtowc(&wc, s, n, ps); + if (ret <= 4) { + if (wc >= 0x10000) { + *pending = (wc & 0x3ff) + 0xdc00; + wc = 0xd7c0 + (wc >> 10); + } + if (pc16) *pc16 = wc; + } + return ret; +} diff --git a/contrib/libs/libc_compat/ubuntu_14/mbrtoc32.c b/contrib/libs/libc_compat/ubuntu_14/mbrtoc32.c new file mode 100644 index 0000000000..9b6b236739 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/mbrtoc32.c @@ -0,0 +1,13 @@ +#include <uchar.h> +#include <wchar.h> + +size_t mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n, mbstate_t *restrict ps) +{ + static unsigned internal_state; + if (!ps) ps = (void *)&internal_state; + if (!s) return mbrtoc32(0, "", 1, ps); + wchar_t wc; + size_t ret = mbrtowc(&wc, s, n, ps); + if (ret <= 4 && pc32) *pc32 = wc; + return ret; +} diff --git a/contrib/libs/libc_compat/ubuntu_14/secure_getenv.cpp b/contrib/libs/libc_compat/ubuntu_14/secure_getenv.cpp new file mode 100644 index 0000000000..14e3e90906 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/secure_getenv.cpp @@ -0,0 +1,12 @@ +#include <stdlib.h> + +#include "glibc.h" + +extern "C" { + char *secure_getenv(const char *name) noexcept { + if (NUbuntuCompat::GetGlibc().IsSecure()) { + return nullptr; + } + return getenv(name); + } +} diff --git a/contrib/libs/libc_compat/ubuntu_14/timespec_get.c b/contrib/libs/libc_compat/ubuntu_14/timespec_get.c new file mode 100644 index 0000000000..742b62ec84 --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/timespec_get.c @@ -0,0 +1,10 @@ +#include <time.h> + +/* There is no other implemented value than TIME_UTC; all other values + * are considered erroneous. */ +int timespec_get(struct timespec * ts, int base) +{ + if (base != TIME_UTC) return 0; + int ret = clock_gettime(CLOCK_REALTIME, ts); + return ret < 0 ? 0 : base; +} diff --git a/contrib/libs/libc_compat/ubuntu_14/ya.make b/contrib/libs/libc_compat/ubuntu_14/ya.make new file mode 100644 index 0000000000..3690c8613a --- /dev/null +++ b/contrib/libs/libc_compat/ubuntu_14/ya.make @@ -0,0 +1,36 @@ +LIBRARY() + +WITHOUT_LICENSE_TEXTS() + +LICENSE(BSD-3-Clause) + +NO_PLATFORM() + +NO_RUNTIME() + +NO_UTIL() + +DISABLE(NEED_PLATFORM_PEERDIRS) +DISABLE(OPENSOURCE_EXPORT) + +IF (OS_SDK == "ubuntu-14") + PEERDIR( + build/platform/linux_sdk + ) + SRCS( + aligned_alloc.c + c16rtomb.c + c32rtomb.c + getauxval.cpp + mbrtoc16.c + mbrtoc32.c + secure_getenv.cpp + timespec_get.c + ) + SRC_C_PIC( + glibc.cpp + -fno-lto + ) +ENDIF() + +END() |