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