gcontini
2020-10-31 c8f8e0c1f0a3687539a92169735845814e18c187
read dmi information segfault on windows
6个文件已修改
1个文件已添加
3个文件已删除
634 ■■■■ 已修改文件
src/library/os/CMakeLists.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/linux/cpu_info.cpp 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/linux/dmi_info.cpp 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/dmi_info.cpp 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/isvm/BIOSReader.cpp 49 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/isvm/Native.cpp 438 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/isvm/Native.h 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/windows/isvm/main.cpp 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/os/CMakeLists.txt 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/library/os/dmi_info_test.cpp 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/library/os/CMakeLists.txt
@@ -16,7 +16,6 @@
          openssl/signature_verifier.cpp 
        execution_environment_common.cpp 
        windows/execution_environment.cpp
          windows/isvm/Native.cpp
        windows/isvm/BIOSReader.cpp
          windows/os_win.cpp
          windows/network.cpp)
@@ -30,7 +29,6 @@
        windows/signature_verifier.cpp 
        execution_environment_common.cpp 
        windows/execution_environment.cpp
        windows/isvm/Native.cpp
        windows/isvm/BIOSReader.cpp
        windows/os_win.cpp
        windows/network.cpp)
src/library/os/linux/cpu_info.cpp
@@ -16,9 +16,9 @@
using namespace std;
struct CPUVendorID {
    unsigned int ebx;
    unsigned int edx;
    unsigned int ecx;
    uint32_t ebx;
    uint32_t edx;
    uint32_t ecx;
    string toString() const { return string(reinterpret_cast<const char *>(this), 12); }
};
@@ -56,7 +56,7 @@
 * @return true if the cpu hypervisor bit is set to 1
 */
bool CpuInfo::is_hypervisor_set() const {
    unsigned int level = 1, eax = 0, ebx = 0, ecx = 0, edx = 0;
    uint32_t level = 1, eax = 0, ebx = 0, ecx = 0, edx = 0;
    __get_cpuid(level, &eax, &ebx, &ecx, &edx);
    bool is_virtual = (((ecx >> 31) & 1) == 1);  // hypervisor flag
@@ -64,7 +64,7 @@
}
uint32_t CpuInfo::model() const {
    unsigned int level = 1, eax = 0, ebx = 0, ecx = 0, edx = 0;
    uint32_t level = 1, eax = 0, ebx = 0, ecx = 0, edx = 0;
    __get_cpuid(level, &eax, &ebx, &ecx, &edx);
    // ax bits 0-3 stepping,4-7 model,8-11 family id,12-13 processor type
    //        14-15 reserved, 16-19 extended model, 20-27 extended family, 27-31 reserved
src/library/os/linux/dmi_info.cpp
@@ -1,7 +1,7 @@
#include "../../base/file_utils.hpp"
#include "../../base/string_utils.h"
#include "../../base/logger.h"
#include "../dmi_info.hpp"
namespace license {
@@ -10,8 +10,9 @@
DmiInfo::DmiInfo() {
    try {
        m_bios_vendor = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/sys_vendor", 256)));
    } catch (...) {
    } catch (std::string &e) {
        m_bios_vendor = "";
        LOG_DEBUG("Can not read sys_vendor", e);
    }
    try {
        m_bios_description = toupper_copy(trim_copy(get_file_contents("/sys/class/dmi/id/modalias", 256)));
@@ -19,7 +20,9 @@
        if (last_char == '\r' || last_char == '\n') {
            m_bios_description = m_bios_description.erase(m_bios_description.length() - 1);
        }
    } catch (...) {
    } catch (std::string &e) {
        m_bios_description = "";
        LOG_DEBUG("Can not read bios_description", e);
    }
    try {
        m_sys_vendor = get_file_contents("/sys/class/dmi/id/sys_vendor", 256);
@@ -27,7 +30,9 @@
        if (last_char == '\r' || last_char == '\n') {
            m_sys_vendor = m_sys_vendor.erase(m_sys_vendor.length() - 1);
        }
    } catch (...) {
    } catch (std::string &e) {
        m_sys_vendor = "";
        LOG_DEBUG("Can not read bios_description", e);
    }
}
src/library/os/windows/dmi_info.cpp
@@ -5,22 +5,19 @@
 *      Author: devel
 */
#include <windows.h>
#include "isvm/BIOSReader.h"
#include "isvm/Native.h"
#include "../../base/string_utils.h"
#include "../dmi_info.hpp"
namespace license {
namespace os {
DmiInfo::DmiInfo() {
    if (InitEntryPoints()) {
        BIOSReader reader;
        SystemInformation info = reader.readSystemInfo();
        m_sys_vendor = toupper_copy(info.Manufacturer);
        m_bios_vendor = toupper_copy(info.ProductName);
        m_bios_description = toupper_copy(info.SysVersion) + toupper_copy(info.family);
    }
}
}
} /* namespace license */
src/library/os/windows/isvm/BIOSReader.cpp
@@ -1,8 +1,9 @@
#include "BIOSReader.h"
#include <windows.h>
#include <cstdint>
#include "Native.h"
#include <vector>
#include <stdint.h>
#include "BIOSReader.h"
#include "../../../base/logger.h"
struct smbios_structure_header {
    uint8_t type;
@@ -96,26 +97,50 @@
    if (0 != t1->family_str) info->family = (std::string::traits_type::char_type *)string_addr[t1->family_str - 1];
}
struct RawSMBIOSData {
    BYTE Used20CallingMethod;
    BYTE SMBIOSMajorVersion;
    BYTE SMBIOSMinorVersion;
    BYTE DmiRevision;
    DWORD Length;
    BYTE SMBIOSTableData[];
};
bool readSMBIOS(std::vector<uint8_t> &buffer) {
    const DWORD tableSignature = ('R' << 24) | ('S' << 16) | ('M' << 8) | 'B';
    bool can_read = false;
    uint32_t size = GetSystemFirmwareTable(tableSignature, 0, NULL, 0);
    if (size > 0) {
        buffer.resize(size);
        if (GetSystemFirmwareTable(tableSignature, 0, buffer.data(), size) > 0) {
            can_read = true;
        }
    }
    return can_read;
}
SystemInformation BIOSReader::readSystemInfo() {
    SystemInformation info;
    SystemInformation info = {};
    std::vector<uint8_t> buffer;
    uint32_t size = 0;
    RawSMBIOSData *data = (RawSMBIOSData *)(LocateSMBIOS(&size));
    if (readSMBIOS(buffer)) {
    if (NULL == data || 0 == size) return info;
        /*RawSMBIOSData *data = (RawSMBIOSData *)(buffer.data());
    smbios_structure_header *header = (smbios_structure_header *)(data->SMBIOSTableData);
    while (NULL != header) {
    while (nullptr != header) {
        if (1 == header->type) {
            read_smbios_type_1((int8_t *)header, &info);
            header = NULL;    //!    stop
            header = nullptr;  //!    stop
        } else {
            int32_t offset = ((0x0F) & (header->length >> 4)) * 16 + (header->length & 0x0F);
            header = (smbios_structure_header *)parse_smbiod_content((int8_t *)header + offset, NULL, NULL);
        }
         }*/
    } else {
        LOG_DEBUG("Can't read smbios table");
    }
    free(data);
    return info;
}
src/library/os/windows/isvm/Native.cpp
File was deleted
src/library/os/windows/isvm/Native.h
File was deleted
src/library/os/windows/isvm/main.cpp
File was deleted
test/library/os/CMakeLists.txt
@@ -1,6 +1,6 @@
add_executable( test_network
 network_test.cpp
)
 "network_test.cpp")
target_link_libraries( test_network
 licensecc_static
@@ -10,6 +10,17 @@
)
ADD_TEST(NAME test_network COMMAND test_network)
add_executable( test_dmi_info dmi_info_test.cpp "dmi_info_test.cpp")
target_link_libraries( test_dmi_info
 licensecc_static
 Boost::unit_test_framework
 Boost::filesystem
 Boost::system
)
ADD_TEST(NAME test_dmi_info COMMAND test_dmi_info)
add_executable( test_execution_environment
 execution_environment_test.cpp
@@ -23,7 +34,13 @@
)
ADD_TEST(NAME test_execution_environment COMMAND test_execution_environment)
if(CODE_COVERAGE AND UNIX)
    target_compile_options(test_network PUBLIC -O0 -g --coverage)
    target_compile_options(test_dmi_info PUBLIC -O0 -g --coverage)
    target_compile_options(test_execution_environment PUBLIC -O0 -g --coverage)
    target_compile_options(test_network PUBLIC -O0 -g --coverage)
endif(CODE_COVERAGE AND UNIX)
test/library/os/dmi_info_test.cpp
New file
@@ -0,0 +1,25 @@
#define BOOST_TEST_MODULE dmi_info_test
#include <string>
#include <iostream>
#include <unordered_map>
#include <boost/test/unit_test.hpp>
#include "../../../src/library/os/dmi_info.hpp"
namespace license {
namespace test {
using namespace std;
BOOST_AUTO_TEST_CASE(dmi_info) {
    os::DmiInfo dmiInfo;
//windows bug
#ifdef __unix__
    BOOST_CHECK_MESSAGE(dmiInfo.bios_vendor().size()>0, "Bios vendor OK");
    BOOST_CHECK_MESSAGE(dmiInfo.bios_description().size() > 0, "Bios description OK");
    BOOST_CHECK_MESSAGE(dmiInfo.sys_vendor().size() > 0, "Sys vendor OK");
#endif
}
}  // namespace test
}  // namespace license