NOISSUE Move NtStatusGen to external repository

This commit is contained in:
Janrupf 2022-05-20 19:59:09 +02:00
parent 918090e02a
commit cbe6d0dbfd
9 changed files with 2616 additions and 257 deletions

View File

@ -105,7 +105,7 @@ void LoggedProcess::on_exit(int exit_code, QProcess::ExitStatus status)
}
emit log({tr("Please not that usually neither exit code nor its description are enough to diagnose issues!")}, MessageLevel::Launcher);
emit log({tr("Always post the entire log and not just the exit code.")}, MessageLevel::Launcher);
emit log({tr("Always upload the entire log and not just the exit code.")}, MessageLevel::Launcher);
}
else
{

View File

@ -12,46 +12,7 @@ set(systeminfo_INCLUDE_DIRS
${CMAKE_CURRENT_LIST_DIR}/include)
if (WIN32)
set(systeminfo_BUILD_SOURCES
build-src/win32/main.cpp)
set(systeminfo_PREPROCESSOR_INPUT
build-src/win32/ntstatus.preprocess.h)
set(systeminfo_PREPROCESSOR_OUT
${CMAKE_CURRENT_BINARY_DIR}/ntstatus.preprocess.h.target)
get_filename_component(systeminfo_PREPROCESSOR_INPUT_FULL
${systeminfo_PREPROCESSOR_INPUT}
REALPATH BASE_DIR ${CMAKE_CURRENT_LIST_DIR}
)
add_custom_target(systeminfo-preprocess-nstatus)
add_custom_command(
COMMAND ${CMAKE_C_COMPILER} -MF ${systeminfo_PREPROCESSOR_OUT} -M ${systeminfo_PREPROCESSOR_INPUT_FULL}
DEPENDS ${systeminfo_PREPROCESSOR_INPUT}
COMMENT "Generating path to ntstatus.h"
TARGET "systeminfo-preprocess-nstatus"
)
add_executable(systeminfo-ntstatus-gen ${systeminfo_BUILD_SOURCES})
add_dependencies(systeminfo-ntstatus-gen systeminfo-preprocess-nstatus)
target_compile_definitions(systeminfo-ntstatus-gen PRIVATE
NTSTATUS_PREPROCESSOR_OUT=\"${systeminfo_PREPROCESSOR_OUT}\")
set(NTSTATUS_GEN_HEADER "${CMAKE_CURRENT_BINARY_DIR}/NtStatusGen.h")
set(NTSTATUS_GEN_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/NtStatusGen.cpp")
add_custom_command(
COMMAND systeminfo-ntstatus-gen ${NTSTATUS_GEN_HEADER} ${NTSTATUS_GEN_SOURCE}
COMMENT "Generating NTSTATUS lookup table"
OUTPUT ${NTSTATUS_GEN_HEADER} ${NTSTATUS_GEN_SOURCE}
)
set_property(SOURCE ${NTSTATUS_GEN_HEADER} PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE ${NTSTATUS_GEN_SOURCE} PROPERTY SKIP_AUTOMOC ON)
list(APPEND systeminfo_SOURCES src/sys_win32.cpp ${NTSTATUS_GEN_SOURCE} ${NTSTATUS_GEN_HEADER})
list(APPEND systeminfo_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND systeminfo_SOURCES src/sys_win32.cpp src/ntstatus/NtStatusNames.cpp)
elseif (UNIX)
if(APPLE)
list(APPEND systeminfo_SOURCES src/sys_apple.cpp)

View File

@ -1,208 +0,0 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <vector>
static void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](char c) {
return !std::isspace(c);
}));
}
static void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](char c) {
return !std::isspace(c);
}).base(), s.end());
}
static void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
static bool startsWith(const std::string &s, const std::string &start) {
return s.rfind(start, 0) == 0;
}
static bool endsWith(const std::string &s, const std::string &end) {
if (s.length() >= end.length()) {
return s.compare(s.length() - end.length(), end.length(), end) == 0;
} else {
return false;
}
}
static bool extractNumber(std::string macroValue, uint64_t &output) {
while (startsWith(macroValue, "(") && endsWith(macroValue, ")")) {
macroValue = macroValue.substr(1, macroValue.length() - 2);
}
if (startsWith(macroValue, "(NTSTATUS)")) {
macroValue = macroValue.substr(10);
ltrim(macroValue);
}
errno = 0;
auto value = std::strtoull(&macroValue[0], nullptr, 0);
if (errno != 0) {
return false;
}
output = value;
return true;
}
struct NtStatusCode {
explicit NtStatusCode() = default;
explicit NtStatusCode(std::string name, uint64_t value) : name(std::move(name)), value(value) {}
std::string name;
uint64_t value = 0;
};
static std::vector<NtStatusCode> predefinedCodes() {
// Some codes are not in ntstatus.h for some reason...
// https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/specific-exceptions
return {
NtStatusCode{"STATUS_APPLICATION_HANG", 0xCFFFFFFF},
NtStatusCode{"STATUS_CPP_EH_EXCEPTION", 0xE06D7363},
NtStatusCode{"STATUS_CLR_EXCEPTION", 0xE0434f4D},
};
}
int main(int argc, const char **argv) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <header-out> <source-out>" << std::endl;
return 1;
}
std::ifstream ntstatusTarget(NTSTATUS_PREPROCESSOR_OUT);
if (!ntstatusTarget.is_open()) {
std::cerr << "Failed to open preprocessor output at " << NTSTATUS_PREPROCESSOR_OUT << std::endl;
return 1;
}
std::string ntstatusPath;
std::string line;
while (std::getline(ntstatusTarget, line)) {
trim(line);
if (endsWith(line, "ntstatus.h")) {
ntstatusPath = line;
break;
}
}
ntstatusTarget.close();
if (ntstatusPath.empty()) {
std::cerr << "Failed to find path to ntstatus.h in generated preprocessor output" << std::endl;
return 1;
}
std::cout << "nstatus.h at " << ntstatusPath << std::endl;
std::ifstream ntstatusHeader(ntstatusPath);
if (!ntstatusHeader.is_open()) {
std::cerr << "Failed to open ntstatus.h" << std::endl;
return 1;
}
std::vector<NtStatusCode> codes = predefinedCodes();
while (std::getline(ntstatusHeader, line)) {
trim(line);
if (startsWith(line, "#define") && line.find("NTSTATUS") != std::string::npos) {
line = line.substr(7);
ltrim(line);
auto space = line.find(' ');
if (space == std::string::npos) {
std::cerr << "Skipping #define " << line << " as the macro has no value" << std::endl;
continue;
}
auto name = line.substr(0, space);
auto value = line.substr(space + 1);
ltrim(value);
NtStatusCode code;
code.name = name;
if (!extractNumber(value, code.value)) {
std::cerr << "Skipping #define " << line << " because its value couldn't be parsed" << std::endl;
} else {
codes.emplace_back(std::move(code));
}
}
}
std::cout << "Found " << codes.size() << " NTSTATUS codes" << std::endl;
std::ofstream outputHeader(argv[1]);
if (!outputHeader.is_open()) {
std::cerr << "Failed to open header output file " << argv[1] << std::endl;
return 1;
}
outputHeader << "// AUTO GENERATED FILE, DO NOT EDIT!" << std::endl;
outputHeader << "// This file has been generated by nstatus-gen from the systeminfo library" << std::endl;
outputHeader << std::endl;
outputHeader << "#pragma once" << std::endl;
outputHeader << std::endl;
outputHeader << "#include <cstdint>" << std::endl;
outputHeader << "#include <string>" << std::endl;
outputHeader << std::endl;
outputHeader << "namespace Sys {" << std::endl;
outputHeader << "namespace Win32 {" << std::endl;
outputHeader << "bool lookupNtStatusCodeName(uint64_t code, std::string &nameOut);" << std::endl;
outputHeader << "}" << std::endl;
outputHeader << "}" << std::endl;
std::ofstream outputSource(argv[2]);
if (!outputSource.is_open()) {
std::cerr << "Failed to open source output file " << argv[2] << std::endl;
return 1;
}
outputSource << "// AUTO GENERATED FILE, DO NOT EDIT!" << std::endl;
outputSource << "// This file has been generated by nstatus-gen from the systeminfo library" << std::endl;
outputSource << std::endl;
outputSource << "#include <unordered_map>" << std::endl;
outputSource << "#include <cstdint>" << std::endl;
outputSource << "#include <string>" << std::endl;
outputSource << std::endl;
outputSource << "namespace Sys {" << std::endl;
outputSource << "namespace Win32 {" << std::endl;
outputSource << "static std::unordered_map<uint64_t, std::string> NTSTATUS_CODES = {" << std::endl;
bool first = true;
for (const auto &status: codes) {
if (first) {
first = false;
} else {
outputSource << "," << std::endl;
}
outputSource << " {0x" << std::hex << status.value << std::dec << ", \"" << status.name << "\"}";
}
outputSource << std::endl;
outputSource << "};" << std::endl;
outputSource << "bool lookupNtStatusCodeName(uint64_t code, std::string &nameOut) {" << std::endl;
outputSource << " auto it = NTSTATUS_CODES.find(code);" << std::endl;
outputSource << " if(it != NTSTATUS_CODES.end()) {" << std::endl;
outputSource << " nameOut = it->second;" << std::endl;
outputSource << " return true;" << std::endl;
outputSource << " }" << std::endl;
outputSource << " return false;" << std::endl;
outputSource << "}" << std::endl;
outputSource << "}" << std::endl;
outputSource << "}" << std::endl;
return 0;
}

View File

@ -1,5 +0,0 @@
// This file exists so that CMake can run the compiler in preprocessor only mode over it
// in order to generate a preprocesses ntstatus header, which then can be read by status
// generator.
#include <ntstatus.h>

View File

@ -0,0 +1,14 @@
// AUTO GENERATED FILE, DO NOT EDIT!
// This file has been generated by nt-status-gen from https://github.com/Janrupf/nt-status-gen
//
// Used compiler: MSVC version 19.28.29337.0
// Targeted OS: Windows version 10.0.19044
#pragma once
#include <cstdint>
#include <string>
namespace NtStatus {
bool lookupNtStatusCodeName(uint64_t code, std::string &nameOut);
}

View File

@ -0,0 +1,2 @@
Source files in this directory have been generated using
https://github.com/Janrupf/nt-status-gen

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
Source files in this directory have been generated using
https://github.com/Janrupf/nt-status-gen

View File

@ -3,7 +3,7 @@
#include <windows.h>
#include <QDebug>
#include "NtStatusGen.h"
#include "ntstatus/NtStatusNames.hpp"
Sys::KernelInfo Sys::getKernelInfo()
{
@ -60,7 +60,7 @@ Sys::DistributionInfo Sys::getDistributionInfo()
bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description)
{
bool hasCodeName = Win32::lookupNtStatusCodeName(code, name);
bool hasCodeName = NtStatus::lookupNtStatusCodeName(code, name);
PSTR messageBuffer = nullptr;
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
@ -72,7 +72,7 @@ bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &
}
auto messageSize = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
ntdll,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),