From 555b489b65abe4f1a0e5ce37374ae6ea57653269 Mon Sep 17 00:00:00 2001 From: chen_yong_jian Date: Tue, 10 Dec 2019 14:06:03 +0800 Subject: [PATCH] Enable mapledriver to execute dex2mpl and mplipa on windows --- src/maple_driver/include/safe_exe.h | 60 ++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/src/maple_driver/include/safe_exe.h b/src/maple_driver/include/safe_exe.h index c8a3903083..ccb2fccdfb 100644 --- a/src/maple_driver/include/safe_exe.h +++ b/src/maple_driver/include/safe_exe.h @@ -15,13 +15,20 @@ #ifndef MAPLE_DRIVER_INCLUDE_SAFE_EXE_H #define MAPLE_DRIVER_INCLUDE_SAFE_EXE_H -#if __linux__ or __linux +/* To start a new process for dex2mpl/mplipa, we need sys/wait on unix-like systems to + * make it complete. However, there is not a sys/wait.h for mingw, so we used createprocess + * in windows.h instead + */ +#ifdef _WIN32 +#include +#else +#include +#endif + #include #include -#include #include #include "error_code.h" -#endif #include "mpl_logging.h" #include "string_utils.h" #include "securec.h" @@ -29,8 +36,7 @@ namespace maple { class SafeExe { public: - // Current tool is for linux only -#if __linux__ or __linux +#ifndef _WIN32 static ErrorCode HandleCommand(const std::string &cmd, const std::string &args) { std::vector vectorArgs = ParseArgsVector(cmd, args); // extra space for exe name and args @@ -69,12 +75,50 @@ class SafeExe { ret = ErrorCode::kErrorCompileFail; } } + for (int j = 0; j < vectorArgs.size(); ++j) { delete [] argv[j]; } delete [] argv; return ret; } +#else + static ErrorCode HandleCommand(const std::string &cmd, const std::string &args) { + ErrorCode ret = ErrorCode::kErrorNoError; + + STARTUPINFO startInfo; + PROCESS_INFORMATION pInfo; + DWORD exitCode; + + errno_t retSafe = memset_s(&startInfo, sizeof(STARTUPINFO), 0, sizeof(STARTUPINFO)); + CHECK_FATAL(retSafe == EOK, "memset_s for StartUpInfo failed when HandleComand"); + + startInfo.cb = sizeof(STARTUPINFO); + + char* appName = strdup(cmd.c_str()); + char* cmdLine = strdup(args.c_str()); + CHECK_FATAL(appName != nullptr, "strdup for appName failed"); + CHECK_FATAL(cmdLine != nullptr, "strdup for cmdLine failed"); + + bool success = CreateProcess(appName, cmdLine, NULL, NULL, FALSE, + NORMAL_PRIORITY_CLASS, NULL, NULL, &startInfo, &pInfo); + CHECK_FATAL(success != 0, "CreateProcess failed when HandleCommond"); + + WaitForSingleObject(pInfo.hProcess, INFINITE); + GetExitCodeProcess(pInfo.hProcess, &exitCode); + + if (exitCode != 0) { + LogInfo::MapleLogger() << "Error while Exe, cmd: " << cmd << " args: " << args + << " exitCode: " << exitCode << '\n'; + ret = ErrorCode::kErrorCompileFail; + } + + free(appName); + free(cmdLine); + appName = nullptr; + cmdLine = nullptr; + return ret; + } #endif static ErrorCode Exe(const std::string &cmd, const std::string &args) { @@ -83,14 +127,10 @@ class SafeExe { LogInfo::MapleLogger() << "Error while Exe, cmd: " << cmd << " args: " << args << '\n'; return ErrorCode::kErrorCompileFail; } - -#if __linux__ or __linux ErrorCode ret = HandleCommand(cmd, args); return ret; -#else - return ErrorCode::kErrorNotImplement; -#endif } + private: static std::vector ParseArgsVector(const std::string &cmd, const std::string &args) { std::vector tmpArgs; -- Gitee