2 * Copyright (C) 2009-2015 Joel Rosdahl
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)
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
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
21 #include "macroskip.h"
22 #include "murmurhashneutral2.h"
25 hash_from_string(void *str)
27 return murmurhashneutral2(str, strlen((const char *)str), 0);
33 return murmurhashneutral2(&i, sizeof(int), 0);
37 strings_equal(void *str1, void *str2)
39 return str_eq((const char *)str1, (const char *)str2);
43 file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2)
45 return memcmp(fh1->hash, fh2->hash, 16) == 0
46 && fh1->size == fh2->size;
50 * Search for the strings "__DATE__" and "__TIME__" in str.
52 * Returns a bitmask with HASH_SOURCE_CODE_FOUND_DATE and
53 * HASH_SOURCE_CODE_FOUND_TIME set appropriately.
56 check_for_temporal_macros(const char *str, size_t len)
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
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
73 if (str[i - 2] == 'E' &&
79 * Check the remaining characters to see if the substring is "__DATE__"
82 if (str[i - 5] == 'D' && str[i - 4] == 'A' &&
84 result |= HASH_SOURCE_CODE_FOUND_DATE;
85 } else if (str[i - 5] == 'T' && str[i - 4] == 'I' &&
87 result |= HASH_SOURCE_CODE_FOUND_TIME;
92 * macro_skip tells us how far we can skip forward upon seeing str[i] at
93 * the end of a substring.
95 i += macro_skip[(uint8_t)str[i]];
102 * Hash a string. Returns a bitmask of HASH_SOURCE_CODE_* results.
105 hash_source_code_string(
106 struct conf *conf, struct mdfour *hash, const char *str, size_t len,
109 int result = HASH_SOURCE_CODE_OK;
112 * Check for __DATE__ and __TIME__ if the sloppiness configuration tells us
115 if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
116 result |= check_for_temporal_macros(str, len);
120 * Hash the source string.
122 hash_buffer(hash, str, len);
124 if (result & HASH_SOURCE_CODE_FOUND_DATE) {
126 * Make sure that the hash sum changes if the (potential) expansion of
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));
137 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
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
146 cc_log("Found __TIME__ in %s", path);
153 * Hash a file ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
157 hash_source_code_file(struct conf *conf, struct mdfour *hash, const char *path)
162 if (is_precompiled_header(path)) {
163 if (hash_file(hash, path)) {
164 return HASH_SOURCE_CODE_OK;
166 return HASH_SOURCE_CODE_ERROR;
171 if (!read_file(path, 0, &data, &size)) {
172 return HASH_SOURCE_CODE_ERROR;
174 result = hash_source_code_string(conf, hash, data, size, path);
181 hash_command_output(struct mdfour *hash, const char *command,
182 const char *compiler)
185 SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
187 PROCESS_INFORMATION pi;
201 struct args *args = args_init_from_string(command);
203 for (i = 0; i < args->argc; i++) {
204 if (str_eq(args->argv[i], "%compiler%")) {
205 args_set(args, i, compiler);
208 cc_log_argv("Executing compiler check command ", args->argv);
211 memset(&pi, 0x00, sizeof(pi));
212 memset(&si, 0x00, sizeof(si));
214 path = find_executable(args->argv[0], NULL);
216 path = args->argv[0];
217 sh = win32getshell(path);
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]);
234 stats_update(STATS_COMPCHECK);
237 fd = _open_osfhandle((intptr_t) pipe_out[0], O_BINARY);
238 ok = hash_fd(hash, fd);
240 cc_log("Error hashing compiler check command output: %s", strerror(errno));
241 stats_update(STATS_COMPCHECK);
243 WaitForSingleObject(pi.hProcess, INFINITE);
244 GetExitCodeProcess(pi.hProcess, &exitcode);
245 CloseHandle(pipe_out[0]);
246 CloseHandle(pi.hProcess);
247 CloseHandle(pi.hThread);
249 cc_log("Compiler check command returned %d", (int) exitcode);
250 stats_update(STATS_COMPCHECK);
255 if (pipe(pipefd) == -1) {
256 fatal("pipe failed");
260 fatal("fork failed");
269 _exit(execvp(args->argv[0], args->argv));
270 return false; /* Never reached. */
277 ok = hash_fd(hash, pipefd[0]);
279 cc_log("Error hashing compiler check command output: %s", strerror(errno));
280 stats_update(STATS_COMPCHECK);
283 if (waitpid(pid, &status, 0) != pid) {
284 cc_log("waitpid failed");
287 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
288 cc_log("Compiler check command returned %d", WEXITSTATUS(status));
289 stats_update(STATS_COMPCHECK);
298 hash_multicommand_output(struct mdfour *hash, const char *commands,
299 const char *compiler)
301 char *command_string, *command, *p, *saveptr = NULL;
304 command_string = x_strdup(commands);
306 while ((command = strtok_r(p, ";", &saveptr))) {
307 if (!hash_command_output(hash, command, compiler)) {
312 free(command_string);