bool openprocess(DWORD pid, DWORD process_access, DWORD thread_access, PROCESS_INFORMATION& pi) { closeprocess(pi); static bool ok = setdebugprivilege(); if (!ok) { return false; } pi.dwProcessId = pid; pi.hProcess = OpenProcess(process_access, FALSE, pi.dwProcessId); if (!pi.hProcess) { closeprocess(pi); return false; } pi.dwThreadId = getthreadid(pi.dwProcessId); if (!pi.dwThreadId) { closeprocess(pi); return false; } pi.hThread = OpenThread(thread_access, FALSE, pi.dwThreadId); if (!pi.hThread) { closeprocess(pi); return false; } return true; }
bool injectdll(DWORD pid, const std::wstring& x86dll, const std::wstring& x64dll) { PROCESS_INFORMATION pi = { 0 }; if (!openprocess(pid, PROCESS_ALL_ACCESS, THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME, pi)) { return false; } SuspendThread(pi.hThread); if (!injectdll(pi, x86dll, x64dll)) { ResumeThread(pi.hThread); closeprocess(pi); return false; } ResumeThread(pi.hThread); closeprocess(pi); return true; }
int main(int argc, char * const argv[]) { char *output = NULL; char buffer[1024]; unsigned long gen = 100; long seed[33]; char c; int mul = 0, seedpos = 0; FILE *out; int verbosity = 1, running = 0; struct timespec tp; char mode[] = "w"; sfmt_t sfmt; int cores = #if defined(WIN32) || defined(__WIN32) 1; #else sysconf (_SC_NPROCESSORS_CONF); #endif unsigned long blocksize; struct process *process; #ifdef _WIN32 seed[0] = time(NULL); #else clock_gettime(CLOCK_REALTIME, &tp); seed[0] = tp.tv_nsec + tp.tv_sec * 1000 * 1000 * 1000; #endif while ((c = getopt(argc, argv, "hc:a:o:s:g:qv")) != -1) { switch (c) { default: usage(argv[0], 1, "ERROR: Unknown parameter %c\n", c); break; case 'h': case '?': usage(argv[0], 0, NULL); break; case 'g': gen = atoi(optarg); if (gen > INT_MAX) usage(argv[0], 2, "ERROR: Generate %lu is over max %d\n", gen, INT_MAX); break; case 'q': verbosity = 0; break; case 'c': cores = atoi(optarg); if (cores <= 0) usage(argv[0], 2, "ERROR: %d cores must be over zero", cores); break; case 'v': verbosity++; break; case 's': { char *startptr = optarg; /* Read seed from /dev/urandom */ if (strcmp("dev", optarg) == 0) { FILE *dev = fopen("/dev/urandom", "r"); int r; if (!dev) { perror("opening /dev/urandom"); return 12; } do { r = fread(seed, sizeof seed[0], sizeof seed/sizeof seed[0], dev); } while (r != sizeof seed/sizeof seed[0] && errno == EINTR); if (r != sizeof seed/sizeof seed[0]) { perror("fread /dev/random"); return 13; } seedpos = sizeof seed/sizeof seed[0]; fclose(dev); break; } for (; seedpos < 33; seedpos++) { char *endptr; unsigned long s; errno = 0; s = strtol(startptr, &endptr, 10); if (endptr == startptr) { break; } if (errno != 0) { perror("sttrol"); return errno; } startptr = endptr + 1; seed[seedpos] = s; if (*endptr == '\0') { seedpos++; break; } } } break; case 'a': mode[0] = 'a'; case 'o': output = strdup(optarg); break; } } freopen("/dev/null", "r", stdin); process = alloca(sizeof(process[0])*cores); memset(process, 0, sizeof(process[0])*cores); /* Calculate reasonable block sizes for the cpu configuration */ blocksize = gen; for (mul = 20; mul > 0; mul--) { long high = 1 << mul; if (gen >= (unsigned)(cores*high)) { blocksize = gen / (cores * mul); break; } } if (blocksize >= (1 << 20)/20) blocksize = (1 << 20)/20; int i, pos = 0; sfmt_init_by_array(&sfmt, (uint32_t*)&seed[0], (seedpos - 1)*(sizeof(seed[0])/sizeof(uint32_t))); pos = sprintf(buffer, "%ld", seed[0]); for (i = 1; i < seedpos; i++) pos += sprintf(&buffer[pos], ",%ld", seed[i]); if (verbosity >= 1) fprintf(stderr, "Generating %ld deals with %s seed. Each subprocess does %ld deals.\n", gen, buffer, blocksize); out = output ? fopen(output, mode) : stdout; if (!out) { perror("Can't open output file"); return 10; } { int fd, maxfd = -1; int c; fd_set fds; fd_set rfds; FD_ZERO(&fds); for (c = 0; c < cores; c++) { unsigned long b = gen - scheduled > blocksize ? blocksize : gen - scheduled; if (b == 0) break; if ((fd = makeprocess(&process[c], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0) return 20; running++; if (fd > maxfd) maxfd = fd; FD_SET(fd, &fds); } /* Loop as long as we have subprocess active */ while (running > 0) { int active; int p = 0; rfds = fds; /* Wait for any input */ do { active = select(maxfd + 1, &rfds, NULL, NULL, NULL); } while (active == -1 && errno == EINTR); if (active <= 0) { perror("select"); return 60; } /* Check which subprocess provided the input */ for (; p < cores; p++) { if (FD_ISSET(process[p].fd, &rfds)) { errno = 0; while (fgets(buffer, sizeof buffer, process[p].f)) parseLine(buffer, out, &process[p], gen, verbosity); /* Has the process exited? */ if (feof(process[p].f)) { FD_CLR(process[p].fd, &fds); closeprocess(&process[p]); running--; /* Is there more blocks to shedule? */ if (scheduled < gen) { unsigned long b = gen - scheduled > blocksize ? blocksize : gen - scheduled; if ((fd = makeprocess(&process[p], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0) return 20; running++; if (fd > maxfd) maxfd = fd; FD_SET(fd, &fds); } continue; } /* EWOULDBLOCK and EINTR can happen during normal operation */ if (errno != EWOULDBLOCK && errno != EINTR) { perror("fgets"); return 50; } } } } } fclose(out); return 0; }