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