|
| 1 | +/* Copyright (C) 2017 Swift Navigation Inc. |
| 2 | + * Contact: Swift Navigation <[email protected]> |
| 3 | + * |
| 4 | + * This source is subject to the license found in the file 'LICENSE' which must |
| 5 | + * be be distributed together with this source. All other rights reserved. |
| 6 | + * |
| 7 | + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 8 | + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 9 | + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | + */ |
| 11 | + |
| 12 | +#include <string.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <stdbool.h> |
| 16 | +#include <unistd.h> |
| 17 | + |
| 18 | +//#define DEBUG |
| 19 | + |
| 20 | +const char* real_cpp_path = "/usr/bin/g++"; |
| 21 | +const char* new_argv_entry = "lib/libLLVMTransformUtils.a"; |
| 22 | + |
| 23 | +int main(int argc, const char* argv[]) { |
| 24 | + |
| 25 | + size_t args_str_size = 0; |
| 26 | + size_t* lengths = (size_t*) malloc(argc*sizeof(size_t)); |
| 27 | + |
| 28 | + for (int x = 0; x < argc; x++) { |
| 29 | + lengths[x] = strlen(argv[x]); |
| 30 | + args_str_size += lengths[x]; |
| 31 | + } |
| 32 | + |
| 33 | +#ifdef DEBUG |
| 34 | + printf("%zu\n", args_str_size); |
| 35 | +#endif |
| 36 | + |
| 37 | + // Make space for spaces in between argument values + null terminator |
| 38 | + args_str_size += (argc - 1) + 1; |
| 39 | + |
| 40 | + char* all_args_buf = (char*) malloc(args_str_size); |
| 41 | + char* p = all_args_buf; |
| 42 | + |
| 43 | + for (int x = 0; x < argc; x++) { |
| 44 | + memcpy(p, argv[x], lengths[x]); |
| 45 | + p += lengths[x]; |
| 46 | + *p++ = ' '; |
| 47 | + } |
| 48 | + |
| 49 | + // Null terminate the string |
| 50 | + *p = '\0'; |
| 51 | + |
| 52 | +#ifdef DEBUG |
| 53 | + printf("All args: %s\n", all_args_buf); |
| 54 | +#endif |
| 55 | + |
| 56 | + bool append_new_arg = false; |
| 57 | + |
| 58 | + if ( (strstr(all_args_buf, "-o lib/libLTO.so.4.0.1") != NULL && |
| 59 | + strstr(all_args_buf, "lib/libLLVMObfuscation.a") != NULL ) |
| 60 | + || |
| 61 | + (strstr(all_args_buf, "-o bin/llvm-lto") != NULL && |
| 62 | + strstr(all_args_buf, "lib/libLLVMObfuscation.a") != NULL ) ) |
| 63 | + { |
| 64 | + fprintf(stderr, "********************** HACK IMMINENT **************************\n"); |
| 65 | + fprintf(stderr, "********************** HACK IMMINENT **************************\n"); |
| 66 | + fprintf(stderr, "********************** HACK IMMINENT **************************\n"); |
| 67 | + |
| 68 | + append_new_arg = true; |
| 69 | + } |
| 70 | + |
| 71 | + argv[0] = real_cpp_path; |
| 72 | + |
| 73 | + if (append_new_arg) { |
| 74 | + |
| 75 | + int new_argc = argc + 1; |
| 76 | + const char** new_argv = malloc( (new_argc+1) * sizeof(char*) ); |
| 77 | + |
| 78 | + for (int x = 0; x < argc; x++) { |
| 79 | + |
| 80 | + size_t length = (lengths[x] + 1); |
| 81 | + |
| 82 | + new_argv[x] = (char*) malloc(length * sizeof(char)); |
| 83 | + memcpy((void*) new_argv[x], argv[x], length * sizeof(char)); |
| 84 | + } |
| 85 | + |
| 86 | + size_t new_argv_len = strlen(new_argv_entry) + 1; |
| 87 | + new_argv[new_argc - 1] = malloc(new_argv_len * sizeof(char)); |
| 88 | + |
| 89 | + memcpy((void*) new_argv[new_argc - 1], new_argv_entry, new_argv_len); |
| 90 | + |
| 91 | + new_argv[new_argc] = NULL; |
| 92 | + |
| 93 | + argc = new_argc; |
| 94 | + argv = new_argv; |
| 95 | + } |
| 96 | + |
| 97 | +#if 0 // This causes a heap corruption error trigger? |
| 98 | + free(lengths); |
| 99 | + free(all_args_buf); |
| 100 | +#endif |
| 101 | + |
| 102 | + int execv_ret = execv(real_cpp_path, (char*const*)argv); |
| 103 | + |
| 104 | + fprintf(stderr, "execv() failure: %d\n", execv_ret); |
| 105 | + return -42; |
| 106 | +} |
0 commit comments