Blaine Garst | 86d0ba4 | 2010-08-04 23:34:21 +0000 | [diff] [blame] | 1 | // |
| 2 | // The LLVM Compiler Infrastructure |
| 3 | // |
| 4 | // This file is distributed under the University of Illinois Open Source |
| 5 | // License. See LICENSE.TXT for details. |
| 6 | |
| 7 | /* |
| 8 | * fail.c |
| 9 | * testObjects |
| 10 | * |
| 11 | * Created by Blaine Garst on 9/16/08. |
| 12 | * |
| 13 | */ |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdbool.h> |
| 20 | |
| 21 | |
| 22 | bool readfile(char *buffer, const char *from) { |
| 23 | int fd = open(from, 0); |
| 24 | if (fd < 0) return false; |
| 25 | int count = read(fd, buffer, 512); |
| 26 | if (count < 0) return false; |
| 27 | buffer[count] = 0; // zap newline |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | // basic idea, take compiler args, run compiler, and verify that expected failure matches any existing one |
| 32 | |
| 33 | int main(int argc, char *argv[]) { |
| 34 | if (argc == 1) return 0; |
| 35 | char *copy[argc+1]; // make a copy |
| 36 | // find and strip off -e "errorfile" |
| 37 | char *errorfile = NULL; |
| 38 | int counter = 0, i = 0; |
| 39 | for (i = 1; i < argc; ++i) { // skip 0 arg which is "fail" |
| 40 | if (!strncmp(argv[i], "-e", 2)) { |
| 41 | errorfile = argv[++i]; |
| 42 | } |
| 43 | else { |
| 44 | copy[counter++] = argv[i]; |
| 45 | } |
| 46 | } |
| 47 | copy[counter] = NULL; |
| 48 | pid_t child = fork(); |
| 49 | char buffer[512]; |
| 50 | if (child == 0) { |
| 51 | // in child |
| 52 | sprintf(buffer, "/tmp/errorfile_%d", getpid()); |
| 53 | close(1); |
| 54 | int fd = creat(buffer, 0777); |
| 55 | if (fd != 1) { |
| 56 | fprintf(stderr, "didn't open custom error file %s as 1, got %d\n", buffer, fd); |
| 57 | exit(1); |
| 58 | } |
| 59 | close(2); |
| 60 | dup(1); |
| 61 | int result = execv(copy[0], copy); |
| 62 | exit(10); |
| 63 | } |
| 64 | if (child < 0) { |
| 65 | printf("fork failed\n"); |
| 66 | exit(1); |
| 67 | } |
| 68 | int status = 0; |
| 69 | pid_t deadchild = wait(&status); |
| 70 | if (deadchild != child) { |
| 71 | printf("wait got %d instead of %d\n", deadchild, child); |
| 72 | exit(1); |
| 73 | } |
| 74 | if (WEXITSTATUS(status) == 0) { |
| 75 | printf("compiler exited normally, not good under these circumstances\n"); |
| 76 | exit(1); |
| 77 | } |
| 78 | //printf("exit status of child %d was %d\n", child, WEXITSTATUS(status)); |
| 79 | sprintf(buffer, "/tmp/errorfile_%d", child); |
| 80 | if (errorfile) { |
| 81 | //printf("ignoring error file: %s\n", errorfile); |
| 82 | char desired[512]; |
| 83 | char got[512]; |
| 84 | bool gotErrorFile = readfile(desired, errorfile); |
| 85 | bool gotOutput = readfile(got, buffer); |
| 86 | if (!gotErrorFile && gotOutput) { |
| 87 | printf("didn't read errorfile %s, it should have something from\n*****\n%s\n*****\nin it.\n", |
| 88 | errorfile, got); |
| 89 | exit(1); |
| 90 | } |
| 91 | else if (gotErrorFile && gotOutput) { |
| 92 | char *where = strstr(got, desired); |
| 93 | if (!where) { |
| 94 | printf("didn't find contents of %s in %s\n", errorfile, buffer); |
| 95 | exit(1); |
| 96 | } |
| 97 | } |
| 98 | else { |
| 99 | printf("errorfile %s and output %s inconsistent\n", errorfile, buffer); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | unlink(buffer); |
| 104 | printf("success\n"); |
| 105 | exit(0); |
| 106 | } |
| 107 | |