Fork of the Android NDK crazy linker.
[chromium-blink-merge.git] / third_party / android_crazy_linker / src / tests / test_util.h
blobe6e722c5f14bc33ba3cf26584e71d5e0445b5c16
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A set of common helper functions used by crazy_linker tests.
6 // IMPORTANT: ALL FUNCTIONS HERE ARE INLINED. This avoids adding a
7 // dependency on another source file for all tests that include this
8 // header.
10 #ifndef TEST_UTIL_H
11 #define TEST_UTIL_H
13 #include <crazy_linker.h>
15 #include <dirent.h>
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/mman.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
27 namespace {
29 // Print an error message and exit the process.
30 // Message must be terminated by a newline.
31 inline void Panic(const char* fmt, ...) {
32 va_list args;
33 fprintf(stderr, "PANIC: ");
34 va_start(args, fmt);
35 vfprintf(stderr, fmt, args);
36 va_end(args);
37 exit(1);
40 // Print an error message, the errno message, then exit the process.
41 // Message must not be terminated by a newline.
42 inline void PanicErrno(const char* fmt, ...) {
43 int old_errno = errno;
44 va_list args;
45 fprintf(stderr, "PANIC: ");
46 va_start(args, fmt);
47 vfprintf(stderr, fmt, args);
48 va_end(args);
49 fprintf(stderr, ": %s\n", strerror(old_errno));
50 exit(1);
53 // Simple string class.
54 class String {
55 public:
56 String() : str_(NULL), len_(0) {}
58 String(const String& other) { String(other.str_, other.len_); }
60 String(const char* str) { String(str, strlen(str)); }
62 String(const char* str, size_t len) : str_(NULL), len_(0) {
63 Append(str, len);
66 ~String() {
67 if (str_) {
68 free(str_);
69 str_ = NULL;
73 String& operator+=(const char* str) {
74 Append(str, strlen(str));
75 return *this;
78 String& operator+=(const String& other) {
79 Append(other.str_, other.len_);
80 return *this;
83 String& operator+=(char ch) {
84 Append(&ch, 1);
85 return *this;
88 const char* c_str() const { return len_ ? str_ : ""; }
89 char* ptr() { return str_; }
90 size_t size() const { return len_; }
92 void Append(const char* str, size_t len) {
93 size_t old_len = len_;
94 Resize(len_ + len);
95 memcpy(str_ + old_len, str, len);
98 void Resize(size_t len) {
99 str_ = reinterpret_cast<char*>(realloc(str_, len + 1));
100 if (len > len_)
101 memset(str_ + len_, '\0', len - len_);
102 str_[len] = '\0';
103 len_ = len;
106 void Format(const char* fmt, ...) {
107 va_list args;
108 va_start(args, fmt);
109 Resize(128);
110 for (;;) {
111 va_list args2;
112 va_copy(args2, args);
113 int ret = vsnprintf(str_, len_ + 1, fmt, args2);
114 va_end(args2);
115 if (ret < static_cast<int>(len_ + 1))
116 break;
118 Resize(len_ * 2);
122 private:
123 char* str_;
124 size_t len_;
127 // Helper class to create a temporary directory that gets deleted on scope exit,
128 // as well as all regular files it contains.
129 class TempDirectory {
130 public:
131 TempDirectory() {
132 snprintf(path_, sizeof path_, "/data/local/tmp/temp-XXXXXX");
133 if (!mktemp(path_))
134 Panic("Could not create temporary directory name: %s\n", strerror(errno));
135 if (mkdir(path_, 0700) < 0)
136 Panic("Could not create temporary directory %s: %s\n", strerror(errno));
139 ~TempDirectory() {
140 // Remove any file in this directory.
141 DIR* d = opendir(path_);
142 if (!d)
143 Panic("Could not open directory %s: %s\n", strerror(errno));
145 struct dirent* entry;
146 while ((entry = readdir(d)) != NULL) {
147 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
148 continue;
149 String file_path;
150 file_path.Format("%s/%s", path_, entry->d_name);
151 if (unlink(file_path.c_str()) < 0)
152 Panic("Could not remove %s: %s\n", file_path.c_str(), strerror(errno));
154 closedir(d);
156 if (rmdir(path_) < 0)
157 Panic("Could not remove dir %s: %s\n", path_, strerror(errno));
160 const char* path() const { return path_; }
162 private:
163 char path_[PATH_MAX];
166 // Scoped FILE* class. Always closed on destruction.
167 class ScopedFILE {
168 public:
169 ScopedFILE() : file_(NULL) {}
171 ~ScopedFILE() {
172 if (file_) {
173 fclose(file_);
174 file_ = NULL;
178 void Open(const char* path, const char* mode) {
179 file_ = fopen(path, mode);
180 if (!file_)
181 Panic("Could not open file for reading: %s: %s\n", path, strerror(errno));
184 FILE* file() const { return file_; }
186 private:
187 FILE* file_;
190 // Retrieve current executable path as a String.
191 inline String GetCurrentExecutable() {
192 String path;
193 path.Resize(PATH_MAX);
194 ssize_t ret =
195 TEMP_FAILURE_RETRY(readlink("/proc/self/exe", path.ptr(), path.size()));
196 if (ret < 0)
197 Panic("Could not read /proc/self/exe: %s\n", strerror(errno));
199 return path;
202 // Retrieve current executable directory as a String.
203 inline String GetCurrentExecutableDirectory() {
204 String path = GetCurrentExecutable();
205 // Find basename.
206 const char* p = reinterpret_cast<const char*>(strrchr(path.c_str(), '/'));
207 if (p == NULL)
208 Panic("Current executable does not have directory root?: %s\n",
209 path.c_str());
211 path.Resize(p - path.c_str());
212 return path;
215 // Copy a file named |src_file_name| in directory |src_file_dir| into
216 // a file named |dst_file_name| in directory |dst_file_dir|. Panics on error.
217 inline void CopyFile(const char* src_file_name,
218 const char* src_file_dir,
219 const char* dst_file_name,
220 const char* dst_file_dir) {
221 String src_path;
222 src_path.Format("%s/%s", src_file_dir, src_file_name);
224 ScopedFILE src_file;
225 src_file.Open(src_path.c_str(), "rb");
227 String dst_path;
228 dst_path.Format("%s/%s", dst_file_dir, dst_file_name);
229 ScopedFILE dst_file;
230 dst_file.Open(dst_path.c_str(), "wb");
232 char buffer[8192];
233 for (;;) {
234 size_t read = fread(buffer, 1, sizeof buffer, src_file.file());
235 if (read > 0) {
236 size_t written = fwrite(buffer, 1, read, dst_file.file());
237 if (written != read)
238 Panic("Wrote %d bytes instead of %d into %s\n",
239 written,
240 read,
241 dst_path.c_str());
243 if (read < sizeof buffer)
244 break;
248 // Send a file descriptor |fd| through |socket|.
249 // Return 0 on success, -1/errno on failure.
250 inline int SendFd(int socket, int fd) {
251 struct iovec iov;
253 char buffer[1];
254 buffer[0] = 0;
256 iov.iov_base = buffer;
257 iov.iov_len = 1;
259 struct msghdr msg;
260 struct cmsghdr* cmsg;
261 char cms[CMSG_SPACE(sizeof(int))];
263 ::memset(&msg, 0, sizeof(msg));
264 msg.msg_iov = &iov;
265 msg.msg_iovlen = 1;
266 msg.msg_control = reinterpret_cast<caddr_t>(cms);
267 msg.msg_controllen = CMSG_LEN(sizeof(int));
269 cmsg = CMSG_FIRSTHDR(&msg);
270 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
271 cmsg->cmsg_level = SOL_SOCKET;
272 cmsg->cmsg_type = SCM_RIGHTS;
273 ::memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
275 int ret = sendmsg(socket, &msg, 0);
276 if (ret < 0)
277 return -1;
279 if (ret != iov.iov_len) {
280 errno = EIO;
281 return -1;
284 return 0;
287 inline int ReceiveFd(int socket, int* fd) {
288 char buffer[1];
289 struct iovec iov;
291 iov.iov_base = buffer;
292 iov.iov_len = 1;
294 struct msghdr msg;
295 struct cmsghdr* cmsg;
296 char cms[CMSG_SPACE(sizeof(int))];
298 ::memset(&msg, 0, sizeof msg);
299 msg.msg_name = 0;
300 msg.msg_namelen = 0;
301 msg.msg_iov = &iov;
302 msg.msg_iovlen = 1;
304 msg.msg_control = reinterpret_cast<caddr_t>(cms);
305 msg.msg_controllen = sizeof(cms);
307 int ret = recvmsg(socket, &msg, 0);
308 if (ret < 0)
309 return -1;
310 if (ret == 0) {
311 errno = EIO;
312 return -1;
315 cmsg = CMSG_FIRSTHDR(&msg);
316 ::memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
317 return 0;
320 // Check that there are exactly |expected_count| memory mappings in
321 // /proc/self/maps that point to a RELRO ashmem region.
322 inline void CheckRelroMaps(int expected_count) {
323 printf("Checking for %d RELROs in /proc/self/maps\n", expected_count);
325 FILE* file = fopen("/proc/self/maps", "rb");
326 if (!file)
327 Panic("Could not open /proc/self/maps (pid %d): %s\n",
328 getpid(),
329 strerror(errno));
331 char line[512];
332 int count_relros = 0;
333 printf("proc/%d/maps:\n", getpid());
334 while (fgets(line, sizeof line, file)) {
335 if (strstr(line, "with_relro")) {
336 // The supported library names are "lib<name>_with_relro.so".
337 printf("%s", line);
338 if (strstr(line, "/dev/ashmem/RELRO:")) {
339 count_relros++;
340 // Check that they are read-only mappings.
341 if (!strstr(line, " r--"))
342 Panic("Shared RELRO mapping is not readonly!\n");
343 // Check that they can't be remapped read-write.
344 unsigned vma_start, vma_end;
345 if (sscanf(line, "%x-%x", &vma_start, &vma_end) != 2)
346 Panic("Could not parse VM address range!\n");
347 int ret = ::mprotect(
348 (void*)vma_start, vma_end - vma_start, PROT_READ | PROT_WRITE);
349 if (ret == 0)
350 Panic("Could remap shared RELRO as writable, should not happen!\n");
352 if (errno != EACCES)
353 Panic("remapping shared RELRO to writable failed with: %s\n",
354 strerror(errno));
358 fclose(file);
360 if (count_relros != expected_count)
361 Panic(
362 "Invalid shared RELRO sections in /proc/self/maps: %d"
363 " (expected %d)\n",
364 count_relros,
365 expected_count);
367 printf("RELRO count check ok!\n");
370 struct RelroInfo {
371 size_t start;
372 size_t size;
373 int fd;
376 struct RelroLibrary {
377 const char* name;
378 crazy_library_t* library;
379 RelroInfo relro;
381 void Init(const char* name, crazy_context_t* context) {
382 printf("Loading %s\n", name);
383 this->name = name;
384 if (!crazy_library_open(&this->library, name, context)) {
385 Panic("Could not open %s: %s\n", name, crazy_context_get_error(context));
389 void Close() { crazy_library_close(this->library); }
391 void CreateSharedRelro(crazy_context_t* context, size_t load_address) {
392 if (!crazy_library_create_shared_relro(this->library,
393 context,
394 load_address,
395 &this->relro.start,
396 &this->relro.size,
397 &this->relro.fd)) {
398 Panic("Could not create shared RELRO for %s: %s",
399 this->name,
400 crazy_context_get_error(context));
403 printf("Parent %s relro info relro_start=%p relro_size=%p relro_fd=%d\n",
404 this->name,
405 (void*)this->relro.start,
406 (void*)this->relro.size,
407 this->relro.fd);
410 void EnableSharedRelro(crazy_context_t* context) {
411 CreateSharedRelro(context, 0);
412 UseSharedRelro(context);
415 void SendRelroInfo(int fd) {
416 if (SendFd(fd, this->relro.fd) < 0) {
417 Panic("Could not send %s RELRO fd: %s", this->name, strerror(errno));
420 int ret =
421 TEMP_FAILURE_RETRY(::write(fd, &this->relro, sizeof(this->relro)));
422 if (ret != static_cast<int>(sizeof(this->relro))) {
423 Panic("Parent could not send %s RELRO info: %s",
424 this->name,
425 strerror(errno));
429 void ReceiveRelroInfo(int fd) {
430 // Receive relro information from parent.
431 int relro_fd = -1;
432 if (ReceiveFd(fd, &relro_fd) < 0) {
433 Panic("Could not receive %s relro descriptor from parent", this->name);
436 printf("Child received %s relro fd %d\n", this->name, relro_fd);
438 int ret = TEMP_FAILURE_RETRY(::read(fd, &this->relro, sizeof(this->relro)));
439 if (ret != static_cast<int>(sizeof(this->relro))) {
440 Panic("Could not receive %s relro information from parent", this->name);
443 this->relro.fd = relro_fd;
444 printf("Child received %s relro start=%p size=%p\n",
445 this->name,
446 (void*)this->relro.start,
447 (void*)this->relro.size);
450 void UseSharedRelro(crazy_context_t* context) {
451 if (!crazy_library_use_shared_relro(this->library,
452 context,
453 this->relro.start,
454 this->relro.size,
455 this->relro.fd)) {
456 Panic("Could not use %s shared RELRO: %s\n",
457 this->name,
458 crazy_context_get_error(context));
463 } // namespace
465 #endif // TEST_UTIL_H