this repo has no description
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */
2#pragma once
3
4#include <cstdio>
5
6#include "globals.h"
7
8namespace py {
9
10typedef void (*SignalHandler)(int);
11typedef void* (*ThreadFunction)(void*);
12
13class OS {
14 public:
15 enum { kPageSize = 4 * kKiB };
16
17 enum Protection { kNoAccess, kReadWrite, kReadExecute, kReadWriteExecute };
18
19 static const word kNumSignals;
20 static bool volatile pending_signals_[];
21
22 static const int kRtldGlobal;
23 static const int kRtldLocal;
24 static const int kRtldNow;
25
26 struct Signal {
27 const char* name;
28 int signum;
29 };
30 static const Signal kStandardSignals[];
31 static const Signal kPlatformSignals[];
32
33 // Allocate a page-sized chunk of memory initialized to zero. If
34 // allocated_size is not nullptr, the rounded-up size will be written to it.
35 static byte* allocateMemory(word size, word* allocated_size);
36
37 // Returns whether the user has access to the specified path with the given
38 // mode (which represents a bit mask of flags for the file existing, being
39 // readable, writable, or executable).
40 static bool access(const char* path, int mode);
41
42 // Starts a new thread, calling the given function with the given argument.
43 static void createThread(ThreadFunction func, void* arg);
44
45 // Returns an absolute path to the current executable. The path may contain
46 // unresolved symlinks.
47 static char* executablePath();
48
49 static bool freeMemory(byte* ptr, word size);
50
51 // Returns the system page size
52 static int pageSize();
53
54 static bool protectMemory(byte* address, word size, Protection);
55
56 static bool secureRandom(byte* ptr, word size);
57
58 static SignalHandler setSignalHandler(int signum, SignalHandler handler);
59 static SignalHandler signalHandler(int signum);
60
61 static byte* readFile(FILE* fp, word* len_out);
62
63 static const char* name();
64
65 static bool dirExists(const char* dir);
66
67 static bool fileExists(const char* file);
68
69 // Read value of symbolic link and return a null-terminated string. Returns
70 // nullptr if path is not a link or cannot be read. Caller is responsible for
71 // freeing the return value with free().
72 static char* readLink(const char* path);
73
74 static double currentTime();
75
76 static void* openSharedObject(const char* filename, int mode,
77 const char** error_msg);
78
79 static void* sharedObjectSymbolAddress(void* handle, const char* symbol,
80 const char** error_msg);
81
82 static word sharedObjectSymbolName(void* addr, char* buf, word size);
83
84 private:
85 DISALLOW_IMPLICIT_CONSTRUCTORS(OS);
86};
87
88} // namespace py