Imported Upstream version 3.2.6
[platform/upstream/ccache.git] / hashutil.c
1 /*
2  * Copyright (C) 2009-2015 Joel Rosdahl
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 3 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "ccache.h"
20 #include "hashutil.h"
21 #include "macroskip.h"
22 #include "murmurhashneutral2.h"
23
24 unsigned
25 hash_from_string(void *str)
26 {
27         return murmurhashneutral2(str, strlen((const char *)str), 0);
28 }
29
30 unsigned
31 hash_from_int(int i)
32 {
33         return murmurhashneutral2(&i, sizeof(int), 0);
34 }
35
36 int
37 strings_equal(void *str1, void *str2)
38 {
39         return str_eq((const char *)str1, (const char *)str2);
40 }
41
42 int
43 file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2)
44 {
45         return memcmp(fh1->hash, fh2->hash, 16) == 0
46                && fh1->size == fh2->size;
47 }
48
49 /*
50  * Search for the strings "__DATE__" and "__TIME__" in str.
51  *
52  * Returns a bitmask with HASH_SOURCE_CODE_FOUND_DATE and
53  * HASH_SOURCE_CODE_FOUND_TIME set appropriately.
54  */
55 int
56 check_for_temporal_macros(const char *str, size_t len)
57 {
58         int result = 0;
59
60         /*
61          * We're using the Boyer-Moore-Horspool algorithm, which searches starting
62          * from the *end* of the needle. Our needles are 8 characters long, so i
63          * starts at 7.
64          */
65         size_t i = 7;
66
67         while (i < len) {
68                 /*
69                  * Check whether the substring ending at str[i] has the form "__...E__". On
70                  * the assumption that 'E' is less common in source than '_', we check
71                  * str[i-2] first.
72                  */
73                 if (str[i - 2] == 'E' &&
74                     str[i - 0] == '_' &&
75                     str[i - 7] == '_' &&
76                     str[i - 1] == '_' &&
77                     str[i - 6] == '_') {
78                         /*
79                          * Check the remaining characters to see if the substring is "__DATE__"
80                          * or "__TIME__".
81                          */
82                         if (str[i - 5] == 'D' && str[i - 4] == 'A' &&
83                             str[i - 3] == 'T') {
84                                 result |= HASH_SOURCE_CODE_FOUND_DATE;
85                         } else if (str[i - 5] == 'T' && str[i - 4] == 'I' &&
86                                    str[i - 3] == 'M') {
87                                 result |= HASH_SOURCE_CODE_FOUND_TIME;
88                         }
89                 }
90
91                 /*
92                  * macro_skip tells us how far we can skip forward upon seeing str[i] at
93                  * the end of a substring.
94                  */
95                 i += macro_skip[(uint8_t)str[i]];
96         }
97
98         return result;
99 }
100
101 /*
102  * Hash a string. Returns a bitmask of HASH_SOURCE_CODE_* results.
103  */
104 int
105 hash_source_code_string(
106   struct conf *conf, struct mdfour *hash, const char *str, size_t len,
107   const char *path)
108 {
109         int result = HASH_SOURCE_CODE_OK;
110
111         /*
112          * Check for __DATE__ and __TIME__ if the sloppiness configuration tells us
113          * we should.
114          */
115         if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
116                 result |= check_for_temporal_macros(str, len);
117         }
118
119         /*
120          * Hash the source string.
121          */
122         hash_buffer(hash, str, len);
123
124         if (result & HASH_SOURCE_CODE_FOUND_DATE) {
125                 /*
126                  * Make sure that the hash sum changes if the (potential) expansion of
127                  * __DATE__ changes.
128                  */
129                 time_t t = time(NULL);
130                 struct tm *now = localtime(&t);
131                 cc_log("Found __DATE__ in %s", path);
132                 hash_delimiter(hash, "date");
133                 hash_buffer(hash, &now->tm_year, sizeof(now->tm_year));
134                 hash_buffer(hash, &now->tm_mon, sizeof(now->tm_mon));
135                 hash_buffer(hash, &now->tm_mday, sizeof(now->tm_mday));
136         }
137         if (result & HASH_SOURCE_CODE_FOUND_TIME) {
138                 /*
139                  * We don't know for sure that the program actually uses the __TIME__
140                  * macro, but we have to assume it anyway and hash the time stamp. However,
141                  * that's not very useful since the chance that we get a cache hit later
142                  * the same second should be quite slim... So, just signal back to the
143                  * caller that __TIME__ has been found so that the direct mode can be
144                  * disabled.
145                  */
146                 cc_log("Found __TIME__ in %s", path);
147         }
148
149         return result;
150 }
151
152 /*
153  * Hash a file ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
154  * results.
155  */
156 int
157 hash_source_code_file(struct conf *conf, struct mdfour *hash, const char *path)
158 {
159         char *data;
160         size_t size;
161
162         if (is_precompiled_header(path)) {
163                 if (hash_file(hash, path)) {
164                         return HASH_SOURCE_CODE_OK;
165                 } else {
166                         return HASH_SOURCE_CODE_ERROR;
167                 }
168         } else {
169                 int result;
170
171                 if (!read_file(path, 0, &data, &size)) {
172                         return HASH_SOURCE_CODE_ERROR;
173                 }
174                 result = hash_source_code_string(conf, hash, data, size, path);
175                 free(data);
176                 return result;
177         }
178 }
179
180 bool
181 hash_command_output(struct mdfour *hash, const char *command,
182                     const char *compiler)
183 {
184 #ifdef _WIN32
185         SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
186         HANDLE pipe_out[2];
187         PROCESS_INFORMATION pi;
188         STARTUPINFO si;
189         DWORD exitcode;
190         char *sh = NULL;
191         char *win32args;
192         char *path;
193         BOOL ret;
194         bool ok;
195         int fd;
196 #else
197         pid_t pid;
198         int pipefd[2];
199 #endif
200
201         struct args *args = args_init_from_string(command);
202         int i;
203         for (i = 0; i < args->argc; i++) {
204                 if (str_eq(args->argv[i], "%compiler%")) {
205                         args_set(args, i, compiler);
206                 }
207         }
208         cc_log_argv("Executing compiler check command ", args->argv);
209
210 #ifdef _WIN32
211         memset(&pi, 0x00, sizeof(pi));
212         memset(&si, 0x00, sizeof(si));
213
214         path = find_executable(args->argv[0], NULL);
215         if (!path)
216                 path = args->argv[0];
217         sh = win32getshell(path);
218         if (sh)
219                 path = sh;
220
221         si.cb = sizeof(STARTUPINFO);
222         CreatePipe(&pipe_out[0], &pipe_out[1], &sa, 0);
223         SetHandleInformation(pipe_out[0], HANDLE_FLAG_INHERIT, 0);
224         si.hStdOutput = pipe_out[1];
225         si.hStdError = pipe_out[1];
226         si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
227         si.dwFlags = STARTF_USESTDHANDLES;
228         win32args = win32argvtos(sh, args->argv);
229         ret = CreateProcess(path, win32args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
230         CloseHandle(pipe_out[1]);
231         args_free(args);
232         free(win32args);
233         if (ret == 0) {
234                 stats_update(STATS_COMPCHECK);
235                 return false;
236         }
237         fd = _open_osfhandle((intptr_t) pipe_out[0], O_BINARY);
238         ok = hash_fd(hash, fd);
239         if (!ok) {
240                 cc_log("Error hashing compiler check command output: %s", strerror(errno));
241                 stats_update(STATS_COMPCHECK);
242         }
243         WaitForSingleObject(pi.hProcess, INFINITE);
244         GetExitCodeProcess(pi.hProcess, &exitcode);
245         CloseHandle(pipe_out[0]);
246         CloseHandle(pi.hProcess);
247         CloseHandle(pi.hThread);
248         if (exitcode != 0) {
249                 cc_log("Compiler check command returned %d", (int) exitcode);
250                 stats_update(STATS_COMPCHECK);
251                 return false;
252         }
253         return ok;
254 #else
255         if (pipe(pipefd) == -1) {
256                 fatal("pipe failed");
257         }
258         pid = fork();
259         if (pid == -1) {
260                 fatal("fork failed");
261         }
262
263         if (pid == 0) {
264                 /* Child. */
265                 close(pipefd[0]);
266                 close(0);
267                 dup2(pipefd[1], 1);
268                 dup2(pipefd[1], 2);
269                 _exit(execvp(args->argv[0], args->argv));
270                 return false; /* Never reached. */
271         } else {
272                 /* Parent. */
273                 int status;
274                 bool ok;
275                 args_free(args);
276                 close(pipefd[1]);
277                 ok = hash_fd(hash, pipefd[0]);
278                 if (!ok) {
279                         cc_log("Error hashing compiler check command output: %s", strerror(errno));
280                         stats_update(STATS_COMPCHECK);
281                 }
282                 close(pipefd[0]);
283                 if (waitpid(pid, &status, 0) != pid) {
284                         cc_log("waitpid failed");
285                         return false;
286                 }
287                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
288                         cc_log("Compiler check command returned %d", WEXITSTATUS(status));
289                         stats_update(STATS_COMPCHECK);
290                         return false;
291                 }
292                 return ok;
293         }
294 #endif
295 }
296
297 bool
298 hash_multicommand_output(struct mdfour *hash, const char *commands,
299                          const char *compiler)
300 {
301         char *command_string, *command, *p, *saveptr = NULL;
302         bool ok = true;
303
304         command_string = x_strdup(commands);
305         p = command_string;
306         while ((command = strtok_r(p, ";", &saveptr))) {
307                 if (!hash_command_output(hash, command, compiler)) {
308                         ok = false;
309                 }
310                 p = NULL;
311         }
312         free(command_string);
313         return ok;
314 }