ensure afl-cc only allows available compiler modes

This commit is contained in:
vanhauser-thc
2024-01-18 09:17:25 +01:00
parent 046473acd5
commit 136febaf68

View File

@ -167,7 +167,7 @@ typedef struct aflcc_state {
u8 cmplog_mode;
u8 have_instr_env, have_gcc, have_llvm, have_gcc_plugin, have_lto,
u8 have_instr_env, have_gcc, have_clang, have_llvm, have_gcc_plugin, have_lto,
have_optimized_pcguard, have_instr_list;
u8 fortify_set, asan_set, x_set, bit_mode, preprocessor_only, have_unroll,
@ -504,13 +504,20 @@ void find_built_deps(aflcc_state_t *aflcc) {
char *ptr = NULL;
#if defined(__x86_64__)
if ((ptr = find_object(aflcc, "as")) != NULL) {
#ifndef __APPLE__
// on OSX clang masquerades as GCC
aflcc->have_gcc = 1;
#endif
aflcc->have_clang = 1;
ck_free(ptr);
}
#endif
if ((ptr = find_object(aflcc, "SanitizerCoveragePCGUARD.so")) != NULL) {
aflcc->have_optimized_pcguard = 1;
@ -604,12 +611,18 @@ void compiler_mode_by_callname(aflcc_state_t *aflcc) {
aflcc->compiler_mode = GCC_PLUGIN;
#if defined(__x86_64__)
} else if (strncmp(aflcc->callname, "afl-gcc", 7) == 0 ||
strncmp(aflcc->callname, "afl-g++", 7) == 0) {
aflcc->compiler_mode = GCC;
#endif
#if defined(__x86_64__)
} else if (strcmp(aflcc->callname, "afl-clang") == 0 ||
strcmp(aflcc->callname, "afl-clang++") == 0) {
@ -618,6 +631,8 @@ void compiler_mode_by_callname(aflcc_state_t *aflcc) {
}
#endif
}
void compiler_mode_by_environ(aflcc_state_t *aflcc) {
@ -660,14 +675,22 @@ void compiler_mode_by_environ(aflcc_state_t *aflcc) {
aflcc->compiler_mode = GCC_PLUGIN;
#if defined(__x86_64__)
} else if (strcasecmp(ptr, "GCC") == 0) {
aflcc->compiler_mode = GCC;
#endif
#if defined(__x86_64__)
} else if (strcasecmp(ptr, "CLANG") == 0) {
aflcc->compiler_mode = CLANG;
#endif
} else
FATAL("Unknown AFL_CC_COMPILER mode: %s\n", ptr);
@ -751,14 +774,22 @@ void compiler_mode_by_cmdline(aflcc_state_t *aflcc, int argc, char **argv) {
aflcc->compiler_mode = GCC_PLUGIN;
#if defined(__x86_64__)
} else if (strcasecmp(ptr, "GCC") == 0) {
aflcc->compiler_mode = GCC;
#endif
#if defined(__x86_64__)
} else if (strncasecmp(ptr, "CLANG", 5) == 0) {
aflcc->compiler_mode = CLANG;
#endif
} else
FATAL("Unknown --afl-... compiler mode: %s\n", argv[i]);
@ -929,6 +960,7 @@ static void instrument_mode_new_environ(aflcc_state_t *aflcc) {
}
#if defined(__x86_64__)
if (strcasecmp(ptr2, "gcc") == 0) {
if (!aflcc->instrument_mode || aflcc->instrument_mode == INSTRUMENT_GCC)
@ -943,6 +975,9 @@ static void instrument_mode_new_environ(aflcc_state_t *aflcc) {
}
#endif
#if defined(__x86_64__)
if (strcasecmp(ptr2, "clang") == 0) {
if (!aflcc->instrument_mode || aflcc->instrument_mode == INSTRUMENT_CLANG)
@ -957,6 +992,8 @@ static void instrument_mode_new_environ(aflcc_state_t *aflcc) {
}
#endif
if (strncasecmp(ptr2, "ctx-", strlen("ctx-")) == 0 ||
strncasecmp(ptr2, "kctx-", strlen("c-ctx-")) == 0 ||
strncasecmp(ptr2, "k-ctx-", strlen("k-ctx-")) == 0) {
@ -1130,12 +1167,9 @@ void mode_final_checkout(aflcc_state_t *aflcc, int argc, char **argv) {
else if (aflcc->have_gcc_plugin)
aflcc->compiler_mode = GCC_PLUGIN;
else if (aflcc->have_gcc)
#ifdef __APPLE__
// on OSX clang masquerades as GCC
aflcc->compiler_mode = CLANG;
#else
aflcc->compiler_mode = GCC;
#endif
else if (aflcc->have_clang)
aflcc->compiler_mode = CLANG;
else if (aflcc->have_lto)
aflcc->compiler_mode = LTO;
else
@ -1143,6 +1177,38 @@ void mode_final_checkout(aflcc_state_t *aflcc, int argc, char **argv) {
}
switch (aflcc->compiler_mode) {
case GCC:
if (!aflcc->have_gcc) FATAL("afl-gcc not available on your platform!");
break;
case CLANG:
if (!aflcc->have_clang)
FATAL("afl-clang not available on your platform!");
break;
case LLVM:
if (!aflcc->have_llvm)
FATAL(
"LLVM mode is not available, please install LLVM 13+ and recompile "
"AFL++");
break;
case GCC_PLUGIN:
if (!aflcc->have_gcc_plugin)
FATAL(
"GCC_PLUGIN mode is not available, install gcc plugin support and "
"recompile AFL++");
break;
case LTO:
if (!aflcc->have_lto)
FATAL(
"LTO mode is not available, please install LLVM 13+ and lld of the "
"same version and recompile AFL++");
break;
default:
FATAL("no compiler mode available");
}
if (aflcc->compiler_mode == GCC) { aflcc->instrument_mode = INSTRUMENT_GCC; }
if (aflcc->compiler_mode == CLANG) {
@ -1618,8 +1684,6 @@ static u8 fsanitize_fuzzer_comma(char *string) {
} while (!ende);
strcpy(string, new);
// fprintf(stderr, "string: %s\n", string);
// fprintf(stderr, "new: %s\n", new);
ck_free(tmp);
ck_free(new);
@ -2503,7 +2567,11 @@ static void maybe_usage(aflcc_state_t *aflcc, int argc, char **argv) {
aflcc->compiler_mode == LTO ? " [SELECTED]" : "",
aflcc->have_gcc_plugin ? "AVAILABLE" : "unavailable!",
aflcc->compiler_mode == GCC_PLUGIN ? " [SELECTED]" : "",
aflcc->have_gcc ? "AVAILABLE" : "unavailable!",
aflcc->have_gcc && aflcc->have_clang
? "AVAILABLE"
: (aflcc->have_gcc
? "GCC ONLY "
: (aflcc->have_clang ? "CLANG ONLY" : "unavailable!")),
(aflcc->compiler_mode == GCC || aflcc->compiler_mode == CLANG)
? " [SELECTED]"
: "");