Bug Summary

File:build/source/bolt/runtime/common.h
Warning:line 281, column 10
Branch condition evaluates to a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name hugify.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -target-cpu x86-64 -target-feature -sse -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins/tools/bolt/bolt_rt-bins -resource-dir /usr/lib/llvm-16/lib/clang/16 -I . -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -source-date-epoch 1670278128 -O3 -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins/tools/bolt/bolt_rt-bins -ferror-limit 19 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2022-12-06-005535-15974-1 -x c++ /build/source/bolt/runtime/hugify.cpp

/build/source/bolt/runtime/hugify.cpp

1//===- bolt/runtime/hugify.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===---------------------------------------------------------------------===//
8
9#if defined (__x86_64__1) && !defined(__APPLE__)
10
11#include "common.h"
12
13#pragma GCC visibility push(hidden)
14
15// Enables a very verbose logging to stderr useful when debugging
16// #define ENABLE_DEBUG
17
18#ifdef ENABLE_DEBUG
19#define DEBUG(X){} \
20 { X; }
21#else
22#define DEBUG(X){} \
23 {}
24#endif
25
26// Function constains trampoline to _start,
27// so we can resume regular execution of the function that we hooked.
28extern void __bolt_hugify_start_program();
29
30// The __hot_start and __hot_end symbols set by Bolt. We use them to figure
31// out the rage for marking huge pages.
32extern uint64_t __hot_start;
33extern uint64_t __hot_end;
34
35static void getKernelVersion(uint32_t *Val) {
36 // release should be in the format: %d.%d.%d
37 // major, minor, release
38 struct UtsNameTy UtsName;
39 int Ret = __uname(&UtsName);
1
Calling '__uname'
2
Returning from '__uname'
40 const char *Buf = UtsName.release;
41 const char *End = Buf + strLen(Buf);
3
Calling 'strLen'
42 const char Delims[2][2] = {".", "."};
43
44 for (int i = 0; i < 3; ++i) {
45 if (!scanUInt32(Buf, End, Val[i])) {
46 return;
47 }
48 if (i < sizeof(Delims) / sizeof(Delims[0])) {
49 const char *Ptr = Delims[i];
50 while (*Ptr != '\0') {
51 if (*Ptr != *Buf) {
52 return;
53 }
54 ++Ptr;
55 ++Buf;
56 }
57 }
58 }
59}
60
61/// Check whether the kernel supports THP via corresponding sysfs entry.
62/// thp works only starting from 5.10
63static bool hasPagecacheTHPSupport() {
64 char Buf[64];
65
66 int FD = __open("/sys/kernel/mm/transparent_hugepage/enabled",
67 0 /* O_RDONLY */, 0);
68 if (FD < 0)
69 return false;
70
71 memset(Buf, 0, sizeof(Buf));
72 const size_t Res = __read(FD, Buf, sizeof(Buf));
73 if (Res < 0)
74 return false;
75
76 if (!strStr(Buf, "[always]") && !strStr(Buf, "[madvise]"))
77 return false;
78
79 struct KernelVersionTy {
80 uint32_t major;
81 uint32_t minor;
82 uint32_t release;
83 };
84
85 KernelVersionTy KernelVersion;
86
87 getKernelVersion((uint32_t *)&KernelVersion);
88 if (KernelVersion.major >= 5 && KernelVersion.minor >= 10)
89 return true;
90
91 return false;
92}
93
94static void hugifyForOldKernel(uint8_t *From, uint8_t *To) {
95 const size_t Size = To - From;
96
97 uint8_t *Mem = reinterpret_cast<uint8_t *>(
98 __mmap(0, Size, 0x3 /* PROT_READ | PROT_WRITE */,
99 0x22 /* MAP_PRIVATE | MAP_ANONYMOUS */, -1, 0));
100
101 if (Mem == ((void *)-1) /* MAP_FAILED */) {
102 char Msg[] = "[hugify] could not allocate memory for text move\n";
103 reportError(Msg, sizeof(Msg));
104 }
105
106 DEBUG(reportNumber("[hugify] allocated temporary address: ", (uint64_t)Mem,{}
107 16);){}
108 DEBUG(reportNumber("[hugify] allocated size: ", (uint64_t)Size, 16);){}
109
110 // Copy the hot code to a temporary location.
111 memcpy(Mem, From, Size);
112
113 __prctl(41 /* PR_SET_THP_DISABLE */, 0, 0, 0, 0);
114 // Maps out the existing hot code.
115 if (__mmap(reinterpret_cast<uint64_t>(From), Size,
116 0x3 /* PROT_READ | PROT_WRITE */,
117 0x32 /* MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE */, -1,
118 0) == ((void *)-1) /*MAP_FAILED*/) {
119 char Msg[] =
120 "[hugify] failed to mmap memory for large page move terminating\n";
121 reportError(Msg, sizeof(Msg));
122 }
123
124 // Mark the hot code page to be huge page.
125 if (__madvise(From, Size, 14 /* MADV_HUGEPAGE */) == -1) {
126 char Msg[] = "[hugify] setting MADV_HUGEPAGE is failed\n";
127 reportError(Msg, sizeof(Msg));
128 }
129
130 // Copy the hot code back.
131 memcpy(From, Mem, Size);
132
133 // Change permission back to read-only, ignore failure
134 __mprotect(From, Size, 0x5 /* PROT_READ | PROT_EXEC */);
135
136 __munmap(Mem, Size);
137}
138
139extern "C" void __bolt_hugify_self_impl() {
140 uint8_t *HotStart = (uint8_t *)&__hot_start;
141 uint8_t *HotEnd = (uint8_t *)&__hot_end;
142 // Make sure the start and end are aligned with huge page address
143 const size_t HugePageBytes = 2L * 1024 * 1024;
144 uint8_t *From = HotStart - ((intptr_t)HotStart & (HugePageBytes - 1));
145 uint8_t *To = HotEnd + (HugePageBytes - 1);
146 To -= (intptr_t)To & (HugePageBytes - 1);
147
148 DEBUG(reportNumber("[hugify] hot start: ", (uint64_t)HotStart, 16);){}
149 DEBUG(reportNumber("[hugify] hot end: ", (uint64_t)HotEnd, 16);){}
150 DEBUG(reportNumber("[hugify] aligned huge page from: ", (uint64_t)From, 16);){}
151 DEBUG(reportNumber("[hugify] aligned huge page to: ", (uint64_t)To, 16);){}
152
153 if (!hasPagecacheTHPSupport()) {
154 DEBUG(report({}
155 "[hugify] workaround with memory alignment for kernel < 5.10\n");){}
156 hugifyForOldKernel(From, To);
157 return;
158 }
159
160 if (__madvise(From, (To - From), 14 /* MADV_HUGEPAGE */) == -1) {
161 char Msg[] = "[hugify] failed to allocate large page\n";
162 // TODO: allow user to control the failure behavior.
163 reportError(Msg, sizeof(Msg));
164 }
165}
166
167/// This is hooking ELF's entry, it needs to save all machine state.
168extern "C" __attribute((naked)) void __bolt_hugify_self() {
169#if defined(__x86_64__1)
170 __asm__ __volatile__(SAVE_ALL"push %%rax\n" "push %%rbx\n" "push %%rcx\n" "push %%rdx\n" "push %%rdi\n"
"push %%rsi\n" "push %%rbp\n" "push %%r8\n" "push %%r9\n" "push %%r10\n"
"push %%r11\n" "push %%r12\n" "push %%r13\n" "push %%r14\n" "push %%r15\n"
"sub $8, %%rsp\n"
"call __bolt_hugify_self_impl\n" RESTORE_ALL"add $8, %%rsp\n" "pop %%r15\n" "pop %%r14\n" "pop %%r13\n" "pop %%r12\n"
"pop %%r11\n" "pop %%r10\n" "pop %%r9\n" "pop %%r8\n" "pop %%rbp\n"
"pop %%rsi\n" "pop %%rdi\n" "pop %%rdx\n" "pop %%rcx\n" "pop %%rbx\n"
"pop %%rax\n"
171 "jmp __bolt_hugify_start_program\n" ::
172 :);
173#else
174 exit(1);
175#endif
176}
177#endif

/build/source/bolt/runtime/common.h

1//===- bolt/runtime/common.h ------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#if !defined(__APPLE__)
10
11#include <cstddef>
12#include <cstdint>
13
14#include "config.h"
15
16#ifdef HAVE_ELF_H
17#include <elf.h>
18#endif
19
20#else
21
22typedef __SIZE_TYPE__long unsigned int size_t;
23#define __SSIZE_TYPE__ \
24 __typeof__(_Generic((__SIZE_TYPE__long unsigned int)0, unsigned long long int \
25 : (long long int)0, unsigned long int \
26 : (long int)0, unsigned int \
27 : (int)0, unsigned short \
28 : (short)0, unsigned char \
29 : (signed char)0))
30typedef __SSIZE_TYPE__ ssize_t;
31
32typedef unsigned long long uint64_t;
33typedef unsigned uint32_t;
34typedef unsigned char uint8_t;
35
36typedef long long int64_t;
37typedef int int32_t;
38
39#endif
40
41// Save all registers while keeping 16B stack alignment
42#define SAVE_ALL"push %%rax\n" "push %%rbx\n" "push %%rcx\n" "push %%rdx\n" "push %%rdi\n"
"push %%rsi\n" "push %%rbp\n" "push %%r8\n" "push %%r9\n" "push %%r10\n"
"push %%r11\n" "push %%r12\n" "push %%r13\n" "push %%r14\n" "push %%r15\n"
"sub $8, %%rsp\n"
\
43 "push %%rax\n" \
44 "push %%rbx\n" \
45 "push %%rcx\n" \
46 "push %%rdx\n" \
47 "push %%rdi\n" \
48 "push %%rsi\n" \
49 "push %%rbp\n" \
50 "push %%r8\n" \
51 "push %%r9\n" \
52 "push %%r10\n" \
53 "push %%r11\n" \
54 "push %%r12\n" \
55 "push %%r13\n" \
56 "push %%r14\n" \
57 "push %%r15\n" \
58 "sub $8, %%rsp\n"
59
60// Mirrors SAVE_ALL
61#define RESTORE_ALL"add $8, %%rsp\n" "pop %%r15\n" "pop %%r14\n" "pop %%r13\n" "pop %%r12\n"
"pop %%r11\n" "pop %%r10\n" "pop %%r9\n" "pop %%r8\n" "pop %%rbp\n"
"pop %%rsi\n" "pop %%rdi\n" "pop %%rdx\n" "pop %%rcx\n" "pop %%rbx\n"
"pop %%rax\n"
\
62 "add $8, %%rsp\n" \
63 "pop %%r15\n" \
64 "pop %%r14\n" \
65 "pop %%r13\n" \
66 "pop %%r12\n" \
67 "pop %%r11\n" \
68 "pop %%r10\n" \
69 "pop %%r9\n" \
70 "pop %%r8\n" \
71 "pop %%rbp\n" \
72 "pop %%rsi\n" \
73 "pop %%rdi\n" \
74 "pop %%rdx\n" \
75 "pop %%rcx\n" \
76 "pop %%rbx\n" \
77 "pop %%rax\n"
78
79// Functions that are required by freestanding environment. Compiler may
80// generate calls to these implicitly.
81extern "C" {
82void *memcpy(void *Dest, const void *Src, size_t Len) {
83 uint8_t *d = static_cast<uint8_t *>(Dest);
84 const uint8_t *s = static_cast<const uint8_t *>(Src);
85 while (Len--)
86 *d++ = *s++;
87 return Dest;
88}
89
90void *memmove(void *Dest, const void *Src, size_t Len) {
91 uint8_t *d = static_cast<uint8_t *>(Dest);
92 const uint8_t *s = static_cast<const uint8_t *>(Src);
93 if (d < s) {
94 while (Len--)
95 *d++ = *s++;
96 } else {
97 s += Len - 1;
98 d += Len - 1;
99 while (Len--)
100 *d-- = *s--;
101 }
102
103 return Dest;
104}
105
106void *memset(void *Buf, int C, size_t Size) {
107 char *S = (char *)Buf;
108 for (size_t I = 0; I < Size; ++I)
109 *S++ = C;
110 return Buf;
111}
112
113int memcmp(const void *s1, const void *s2, size_t n) {
114 const uint8_t *c1 = static_cast<const uint8_t *>(s1);
115 const uint8_t *c2 = static_cast<const uint8_t *>(s2);
116 for (; n--; c1++, c2++) {
117 if (*c1 != *c2)
118 return *c1 < *c2 ? -1 : 1;
119 }
120 return 0;
121}
122} // extern "C"
123
124// Anonymous namespace covering everything but our library entry point
125namespace {
126
127constexpr uint32_t BufSize = 10240;
128
129#define _STRINGIFY(x)"x" #x
130#define STRINGIFY(x)"x" _STRINGIFY(x)"x"
131
132uint64_t __read(uint64_t fd, const void *buf, uint64_t count) {
133 uint64_t ret;
134#if defined(__APPLE__)
135#define READ_SYSCALL0 0x2000003
136#else
137#define READ_SYSCALL0 0
138#endif
139 __asm__ __volatile__("movq $" STRINGIFY(READ_SYSCALL)"0" ", %%rax\n"
140 "syscall\n"
141 : "=a"(ret)
142 : "D"(fd), "S"(buf), "d"(count)
143 : "cc", "rcx", "r11", "memory");
144 return ret;
145}
146
147uint64_t __write(uint64_t fd, const void *buf, uint64_t count) {
148 uint64_t ret;
149#if defined(__APPLE__)
150#define WRITE_SYSCALL1 0x2000004
151#else
152#define WRITE_SYSCALL1 1
153#endif
154 __asm__ __volatile__("movq $" STRINGIFY(WRITE_SYSCALL)"1" ", %%rax\n"
155 "syscall\n"
156 : "=a"(ret)
157 : "D"(fd), "S"(buf), "d"(count)
158 : "cc", "rcx", "r11", "memory");
159 return ret;
160}
161
162void *__mmap(uint64_t addr, uint64_t size, uint64_t prot, uint64_t flags,
163 uint64_t fd, uint64_t offset) {
164#if defined(__APPLE__)
165#define MMAP_SYSCALL9 0x20000c5
166#else
167#define MMAP_SYSCALL9 9
168#endif
169 void *ret;
170 register uint64_t r8 asm("r8") = fd;
171 register uint64_t r9 asm("r9") = offset;
172 register uint64_t r10 asm("r10") = flags;
173 __asm__ __volatile__("movq $" STRINGIFY(MMAP_SYSCALL)"9" ", %%rax\n"
174 "syscall\n"
175 : "=a"(ret)
176 : "D"(addr), "S"(size), "d"(prot), "r"(r10), "r"(r8),
177 "r"(r9)
178 : "cc", "rcx", "r11", "memory");
179 return ret;
180}
181
182uint64_t __munmap(void *addr, uint64_t size) {
183#if defined(__APPLE__)
184#define MUNMAP_SYSCALL11 0x2000049
185#else
186#define MUNMAP_SYSCALL11 11
187#endif
188 uint64_t ret;
189 __asm__ __volatile__("movq $" STRINGIFY(MUNMAP_SYSCALL)"11" ", %%rax\n"
190 "syscall\n"
191 : "=a"(ret)
192 : "D"(addr), "S"(size)
193 : "cc", "rcx", "r11", "memory");
194 return ret;
195}
196
197#define SIG_BLOCK0 0
198#define SIG_UNBLOCK1 1
199#define SIG_SETMASK2 2
200
201static const uint64_t MaskAllSignals[] = {-1ULL};
202
203uint64_t __sigprocmask(int how, const void *set, void *oldset) {
204#if defined(__APPLE__)
205#define SIGPROCMASK_SYSCALL14 0x2000030
206#else
207#define SIGPROCMASK_SYSCALL14 14
208#endif
209 uint64_t ret;
210 register long r10 asm("r10") = sizeof(uint64_t);
211 __asm__ __volatile__("movq $" STRINGIFY(SIGPROCMASK_SYSCALL)"14" ", %%rax\n"
212 "syscall\n"
213 : "=a"(ret)
214 : "D"(how), "S"(set), "d"(oldset), "r"(r10)
215 : "cc", "rcx", "r11", "memory");
216 return ret;
217}
218
219uint64_t __exit(uint64_t code) {
220#if defined(__APPLE__)
221#define EXIT_SYSCALL231 0x2000001
222#else
223#define EXIT_SYSCALL231 231
224#endif
225 uint64_t ret;
226 __asm__ __volatile__("movq $" STRINGIFY(EXIT_SYSCALL)"231" ", %%rax\n"
227 "syscall\n"
228 : "=a"(ret)
229 : "D"(code)
230 : "cc", "rcx", "r11", "memory");
231 return ret;
232}
233
234// Helper functions for writing strings to the .fdata file. We intentionally
235// avoid using libc names to make it clear it is our impl.
236
237/// Write number Num using Base to the buffer in OutBuf, returns a pointer to
238/// the end of the string.
239char *intToStr(char *OutBuf, uint64_t Num, uint32_t Base) {
240 const char *Chars = "0123456789abcdef";
241 char Buf[21];
242 char *Ptr = Buf;
243 while (Num) {
244 *Ptr++ = *(Chars + (Num % Base));
245 Num /= Base;
246 }
247 if (Ptr == Buf) {
248 *OutBuf++ = '0';
249 return OutBuf;
250 }
251 while (Ptr != Buf)
252 *OutBuf++ = *--Ptr;
253
254 return OutBuf;
255}
256
257/// Copy Str to OutBuf, returns a pointer to the end of the copied string
258char *strCopy(char *OutBuf, const char *Str, int32_t Size = BufSize) {
259 while (*Str) {
260 *OutBuf++ = *Str++;
261 if (--Size <= 0)
262 return OutBuf;
263 }
264 return OutBuf;
265}
266
267/// Compare two strings, at most Num bytes.
268int strnCmp(const char *Str1, const char *Str2, size_t Num) {
269 while (Num && *Str1 && (*Str1 == *Str2)) {
270 Num--;
271 Str1++;
272 Str2++;
273 }
274 if (Num == 0)
275 return 0;
276 return *(unsigned char *)Str1 - *(unsigned char *)Str2;
277}
278
279uint32_t strLen(const char *Str) {
280 uint32_t Size = 0;
281 while (*Str++)
4
Branch condition evaluates to a garbage value
282 ++Size;
283 return Size;
284}
285
286void *strStr(const char *const Haystack, const char *const Needle) {
287 int j = 0;
288
289 for (int i = 0; i < strLen(Haystack); i++) {
290 if (Haystack[i] == Needle[0]) {
291 for (j = 1; j < strLen(Needle); j++) {
292 if (Haystack[i + j] != Needle[j])
293 break;
294 }
295 if (j == strLen(Needle))
296 return (void *)&Haystack[i];
297 }
298 }
299 return nullptr;
300}
301
302void reportNumber(const char *Msg, uint64_t Num, uint32_t Base) {
303 char Buf[BufSize];
304 char *Ptr = Buf;
305 Ptr = strCopy(Ptr, Msg, BufSize - 23);
306 Ptr = intToStr(Ptr, Num, Base);
307 Ptr = strCopy(Ptr, "\n");
308 __write(2, Buf, Ptr - Buf);
309}
310
311void report(const char *Msg) { __write(2, Msg, strLen(Msg)); }
312
313unsigned long hexToLong(const char *Str, char Terminator = '\0') {
314 unsigned long Res = 0;
315 while (*Str != Terminator) {
316 Res <<= 4;
317 if ('0' <= *Str && *Str <= '9')
318 Res += *Str++ - '0';
319 else if ('a' <= *Str && *Str <= 'f')
320 Res += *Str++ - 'a' + 10;
321 else if ('A' <= *Str && *Str <= 'F')
322 Res += *Str++ - 'A' + 10;
323 else
324 return 0;
325 }
326 return Res;
327}
328
329/// Starting from character at \p buf, find the longest consecutive sequence
330/// of digits (0-9) and convert it to uint32_t. The converted value
331/// is put into \p ret. \p end marks the end of the buffer to avoid buffer
332/// overflow. The function \returns whether a valid uint32_t value is found.
333/// \p buf will be updated to the next character right after the digits.
334static bool scanUInt32(const char *&Buf, const char *End, uint32_t &Ret) {
335 uint64_t Result = 0;
336 const char *OldBuf = Buf;
337 while (Buf < End && ((*Buf) >= '0' && (*Buf) <= '9')) {
338 Result = Result * 10 + (*Buf) - '0';
339 ++Buf;
340 }
341 if (OldBuf != Buf && Result <= 0xFFFFFFFFu) {
342 Ret = static_cast<uint32_t>(Result);
343 return true;
344 }
345 return false;
346}
347
348#if !defined(__APPLE__)
349// We use a stack-allocated buffer for string manipulation in many pieces of
350// this code, including the code that prints each line of the fdata file. This
351// buffer needs to accomodate large function names, but shouldn't be arbitrarily
352// large (dynamically allocated) for simplicity of our memory space usage.
353
354// Declare some syscall wrappers we use throughout this code to avoid linking
355// against system libc.
356uint64_t __open(const char *pathname, uint64_t flags, uint64_t mode) {
357 uint64_t ret;
358 __asm__ __volatile__("movq $2, %%rax\n"
359 "syscall"
360 : "=a"(ret)
361 : "D"(pathname), "S"(flags), "d"(mode)
362 : "cc", "rcx", "r11", "memory");
363 return ret;
364}
365
366struct dirent {
367 unsigned long d_ino; /* Inode number */
368 unsigned long d_off; /* Offset to next linux_dirent */
369 unsigned short d_reclen; /* Length of this linux_dirent */
370 char d_name[]; /* Filename (null-terminated) */
371 /* length is actually (d_reclen - 2 -
372 offsetof(struct linux_dirent, d_name)) */
373};
374
375long __getdents(unsigned int fd, dirent *dirp, size_t count) {
376 long ret;
377 __asm__ __volatile__("movq $78, %%rax\n"
378 "syscall"
379 : "=a"(ret)
380 : "D"(fd), "S"(dirp), "d"(count)
381 : "cc", "rcx", "r11", "memory");
382 return ret;
383}
384
385uint64_t __readlink(const char *pathname, char *buf, size_t bufsize) {
386 uint64_t ret;
387 __asm__ __volatile__("movq $89, %%rax\n"
388 "syscall"
389 : "=a"(ret)
390 : "D"(pathname), "S"(buf), "d"(bufsize)
391 : "cc", "rcx", "r11", "memory");
392 return ret;
393}
394
395uint64_t __lseek(uint64_t fd, uint64_t pos, uint64_t whence) {
396 uint64_t ret;
397 __asm__ __volatile__("movq $8, %%rax\n"
398 "syscall\n"
399 : "=a"(ret)
400 : "D"(fd), "S"(pos), "d"(whence)
401 : "cc", "rcx", "r11", "memory");
402 return ret;
403}
404
405int __close(uint64_t fd) {
406 uint64_t ret;
407 __asm__ __volatile__("movq $3, %%rax\n"
408 "syscall\n"
409 : "=a"(ret)
410 : "D"(fd)
411 : "cc", "rcx", "r11", "memory");
412 return ret;
413}
414
415int __madvise(void *addr, size_t length, int advice) {
416 int ret;
417 __asm__ __volatile__("movq $28, %%rax\n"
418 "syscall\n"
419 : "=a"(ret)
420 : "D"(addr), "S"(length), "d"(advice)
421 : "cc", "rcx", "r11", "memory");
422 return ret;
423}
424
425#define _UTSNAME_LENGTH65 65
426
427struct UtsNameTy {
428 char sysname[_UTSNAME_LENGTH65]; /* Operating system name (e.g., "Linux") */
429 char nodename[_UTSNAME_LENGTH65]; /* Name within "some implementation-defined
430 network" */
431 char release[_UTSNAME_LENGTH65]; /* Operating system release (e.g., "2.6.28") */
432 char version[_UTSNAME_LENGTH65]; /* Operating system version */
433 char machine[_UTSNAME_LENGTH65]; /* Hardware identifier */
434 char domainname[_UTSNAME_LENGTH65]; /* NIS or YP domain name */
435};
436
437int __uname(struct UtsNameTy *Buf) {
438 int Ret;
439 __asm__ __volatile__("movq $63, %%rax\n"
440 "syscall\n"
441 : "=a"(Ret)
442 : "D"(Buf)
443 : "cc", "rcx", "r11", "memory");
444 return Ret;
445}
446
447struct timespec {
448 uint64_t tv_sec; /* seconds */
449 uint64_t tv_nsec; /* nanoseconds */
450};
451
452uint64_t __nanosleep(const timespec *req, timespec *rem) {
453 uint64_t ret;
454 __asm__ __volatile__("movq $35, %%rax\n"
455 "syscall\n"
456 : "=a"(ret)
457 : "D"(req), "S"(rem)
458 : "cc", "rcx", "r11", "memory");
459 return ret;
460}
461
462int64_t __fork() {
463 uint64_t ret;
464 __asm__ __volatile__("movq $57, %%rax\n"
465 "syscall\n"
466 : "=a"(ret)
467 :
468 : "cc", "rcx", "r11", "memory");
469 return ret;
470}
471
472int __mprotect(void *addr, size_t len, int prot) {
473 int ret;
474 __asm__ __volatile__("movq $10, %%rax\n"
475 "syscall\n"
476 : "=a"(ret)
477 : "D"(addr), "S"(len), "d"(prot)
478 : "cc", "rcx", "r11", "memory");
479 return ret;
480}
481
482uint64_t __getpid() {
483 uint64_t ret;
484 __asm__ __volatile__("movq $39, %%rax\n"
485 "syscall\n"
486 : "=a"(ret)
487 :
488 : "cc", "rcx", "r11", "memory");
489 return ret;
490}
491
492uint64_t __getppid() {
493 uint64_t ret;
494 __asm__ __volatile__("movq $110, %%rax\n"
495 "syscall\n"
496 : "=a"(ret)
497 :
498 : "cc", "rcx", "r11", "memory");
499 return ret;
500}
501
502int __setpgid(uint64_t pid, uint64_t pgid) {
503 int ret;
504 __asm__ __volatile__("movq $109, %%rax\n"
505 "syscall\n"
506 : "=a"(ret)
507 : "D"(pid), "S"(pgid)
508 : "cc", "rcx", "r11", "memory");
509 return ret;
510}
511
512uint64_t __getpgid(uint64_t pid) {
513 uint64_t ret;
514 __asm__ __volatile__("movq $121, %%rax\n"
515 "syscall\n"
516 : "=a"(ret)
517 : "D"(pid)
518 : "cc", "rcx", "r11", "memory");
519 return ret;
520}
521
522int __kill(uint64_t pid, int sig) {
523 int ret;
524 __asm__ __volatile__("movq $62, %%rax\n"
525 "syscall\n"
526 : "=a"(ret)
527 : "D"(pid), "S"(sig)
528 : "cc", "rcx", "r11", "memory");
529 return ret;
530}
531
532int __fsync(int fd) {
533 int ret;
534 __asm__ __volatile__("movq $74, %%rax\n"
535 "syscall\n"
536 : "=a"(ret)
537 : "D"(fd)
538 : "cc", "rcx", "r11", "memory");
539 return ret;
540}
541
542// %rdi %rsi %rdx %r10 %r8
543// sys_prctl int option unsigned unsigned unsigned unsigned
544// long arg2 long arg3 long arg4 long arg5
545int __prctl(int Option, unsigned long Arg2, unsigned long Arg3,
546 unsigned long Arg4, unsigned long Arg5) {
547 int Ret;
548 register long rdx asm("rdx") = Arg3;
549 register long r8 asm("r8") = Arg5;
550 register long r10 asm("r10") = Arg4;
551 __asm__ __volatile__("movq $157, %%rax\n"
552 "syscall\n"
553 : "=a"(Ret)
554 : "D"(Option), "S"(Arg2), "d"(rdx), "r"(r10), "r"(r8)
555 :);
556 return Ret;
557}
558
559#endif
560
561void reportError(const char *Msg, uint64_t Size) {
562 __write(2, Msg, Size);
563 __exit(1);
564}
565
566void assert(bool Assertion, const char *Msg) {
567 if (Assertion)
568 return;
569 char Buf[BufSize];
570 char *Ptr = Buf;
571 Ptr = strCopy(Ptr, "Assertion failed: ");
572 Ptr = strCopy(Ptr, Msg, BufSize - 40);
573 Ptr = strCopy(Ptr, "\n");
574 reportError(Buf, Ptr - Buf);
575}
576
577class Mutex {
578 volatile bool InUse{false};
579
580public:
581 bool acquire() { return !__atomic_test_and_set(&InUse, __ATOMIC_ACQUIRE2); }
582 void release() { __atomic_clear(&InUse, __ATOMIC_RELEASE3); }
583};
584
585/// RAII wrapper for Mutex
586class Lock {
587 Mutex &M;
588 uint64_t SignalMask[1] = {};
589
590public:
591 Lock(Mutex &M) : M(M) {
592 __sigprocmask(SIG_BLOCK0, MaskAllSignals, SignalMask);
593 while (!M.acquire()) {
594 }
595 }
596
597 ~Lock() {
598 M.release();
599 __sigprocmask(SIG_SETMASK2, SignalMask, nullptr);
600 }
601};
602
603/// RAII wrapper for Mutex
604class TryLock {
605 Mutex &M;
606 bool Locked = false;
607
608public:
609 TryLock(Mutex &M) : M(M) {
610 int Retry = 100;
611 while (--Retry && !M.acquire())
612 ;
613 if (Retry)
614 Locked = true;
615 }
616 bool isLocked() { return Locked; }
617
618 ~TryLock() {
619 if (isLocked())
620 M.release();
621 }
622};
623
624inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
625 return (Value + Align - 1) / Align * Align;
626}
627
628} // anonymous namespace