Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
347 lines
13 KiB
C++
347 lines
13 KiB
C++
// Isolated, read-only ABI sanity probe for Mentor EE 7.9 CLibServer.
|
|
//
|
|
// This program intentionally does not call VerifyRCCells, SetVerifyRCCellsFlag,
|
|
// Save, VerifyReuseBlock, or UnVerifyReuseBlock. It only reproduces the
|
|
// constructor/Init/Load/Close/destructor lifecycle observed in Mentor's own
|
|
// x86 RFSymMergeUtility.exe caller.
|
|
|
|
#include <windows.h>
|
|
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
constexpr std::size_t kCLibServerObjectSize = 0x0c;
|
|
|
|
using CtorFn = void(__thiscall*)(void*, const char*);
|
|
using InitFn = long(__thiscall*)(void*);
|
|
using LoadFn = long(__thiscall*)(void*, const char*, int);
|
|
using CloseFn = void(__thiscall*)(void*);
|
|
using DtorFn = void(__thiscall*)(void*);
|
|
using SetVerifyFlagFn = void(__thiscall*)(void*, int);
|
|
using SaveFn = long(__thiscall*)(void*, const char*, int, int);
|
|
|
|
struct Functions {
|
|
CtorFn ctor = nullptr;
|
|
InitFn init = nullptr;
|
|
LoadFn load = nullptr;
|
|
CloseFn close = nullptr;
|
|
DtorFn dtor = nullptr;
|
|
SetVerifyFlagFn setVerifyFlag = nullptr;
|
|
SaveFn save = nullptr;
|
|
};
|
|
|
|
struct LifecycleResult {
|
|
long initReturn;
|
|
long loadReturn;
|
|
int constructed;
|
|
int closed;
|
|
int destroyed;
|
|
DWORD exceptionCode;
|
|
};
|
|
|
|
// Keep the SEH boundary free of C++ objects. MSVC rejects __try in a function
|
|
// that would need automatic object unwinding.
|
|
int invokeLifecycle(const Functions* fn, void* object, const char* path,
|
|
LifecycleResult* result) {
|
|
result->initReturn = -1;
|
|
result->loadReturn = -1;
|
|
result->constructed = 0;
|
|
result->closed = 0;
|
|
result->destroyed = 0;
|
|
result->exceptionCode = ERROR_SUCCESS;
|
|
__try {
|
|
fn->ctor(object, path);
|
|
result->constructed = 1;
|
|
result->initReturn = fn->init(object);
|
|
if (result->initReturn == 0) {
|
|
// The observed Mentor caller pushes mode 1 for Load(path, mode).
|
|
result->loadReturn = fn->load(object, path, 1);
|
|
}
|
|
fn->close(object);
|
|
result->closed = 1;
|
|
fn->dtor(object);
|
|
result->destroyed = 1;
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
|
result->exceptionCode = GetExceptionCode();
|
|
}
|
|
return result->exceptionCode == ERROR_SUCCESS ? 0 : 1;
|
|
}
|
|
|
|
struct WriteResult {
|
|
long initReturn;
|
|
long loadReturn;
|
|
long saveReturn;
|
|
int constructed;
|
|
int flagSet;
|
|
int closed;
|
|
int destroyed;
|
|
DWORD exceptionCode;
|
|
};
|
|
|
|
int invokeWriteLifecycle(const Functions* fn, void* object, const char* path,
|
|
int verifyFlag, WriteResult* result) {
|
|
result->initReturn = -1;
|
|
result->loadReturn = -1;
|
|
result->saveReturn = -1;
|
|
result->constructed = 0;
|
|
result->flagSet = 0;
|
|
result->closed = 0;
|
|
result->destroyed = 0;
|
|
result->exceptionCode = ERROR_SUCCESS;
|
|
__try {
|
|
fn->ctor(object, path);
|
|
result->constructed = 1;
|
|
result->initReturn = fn->init(object);
|
|
if (result->initReturn == 0) {
|
|
result->loadReturn = fn->load(object, path, 1);
|
|
}
|
|
if (result->loadReturn == 0) {
|
|
fn->setVerifyFlag(object, verifyFlag);
|
|
result->flagSet = 1;
|
|
// RFSymMergeUtility.exe uses Save(NULL, 0, 0). The verify flag
|
|
// makes CLibServer::Save enter its internal VerifyRCCells path.
|
|
result->saveReturn = fn->save(object, nullptr, 0, 0);
|
|
}
|
|
fn->close(object);
|
|
result->closed = 1;
|
|
fn->dtor(object);
|
|
result->destroyed = 1;
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
|
result->exceptionCode = GetExceptionCode();
|
|
}
|
|
return result->exceptionCode == ERROR_SUCCESS ? 0 : 1;
|
|
}
|
|
|
|
std::string envValue(const char* key) {
|
|
const char* value = std::getenv(key);
|
|
return value && *value ? value : std::string();
|
|
}
|
|
|
|
std::wstring widen(const std::string& value) {
|
|
if (value.empty()) return {};
|
|
const int count = MultiByteToWideChar(CP_UTF8, 0, value.data(),
|
|
static_cast<int>(value.size()), nullptr, 0);
|
|
if (count <= 0) return {};
|
|
std::wstring result(static_cast<std::size_t>(count), L'\0');
|
|
MultiByteToWideChar(CP_UTF8, 0, value.data(), static_cast<int>(value.size()),
|
|
result.data(), count);
|
|
return result;
|
|
}
|
|
|
|
std::string jsonEscape(const std::string& value) {
|
|
std::ostringstream out;
|
|
for (const char ch : value) {
|
|
switch (ch) {
|
|
case '\\': out << "\\\\"; break;
|
|
case '"': out << "\\\""; break;
|
|
case '\n': out << "\\n"; break;
|
|
case '\r': out << "\\r"; break;
|
|
default: out << ch; break;
|
|
}
|
|
}
|
|
return out.str();
|
|
}
|
|
|
|
void fail(const std::string& message, DWORD error = ERROR_SUCCESS) {
|
|
std::cout << "{\"ok\":false,\"mode\":\"read-only\",\"error\":\""
|
|
<< jsonEscape(message) << "\"";
|
|
if (error != ERROR_SUCCESS) std::cout << ",\"win32_error\":" << error;
|
|
std::cout << "}" << std::endl;
|
|
}
|
|
|
|
FARPROC required(HMODULE module, const char* name) {
|
|
FARPROC address = GetProcAddress(module, name);
|
|
return address;
|
|
}
|
|
|
|
int run(const std::string& lmc) {
|
|
const std::string sddHome = envValue("SDD_HOME");
|
|
const std::string configured = envValue("LIBMANAGERDBPI_PATH");
|
|
const std::string dbPath = !configured.empty()
|
|
? configured
|
|
: (sddHome.empty() ? "LibManagerDBPI.dll"
|
|
: sddHome + "\\common\\win32\\lib\\LibManagerDBPI.dll");
|
|
|
|
HMODULE module = LoadLibraryW(widen(dbPath).c_str());
|
|
if (!module) {
|
|
fail("无法加载 LibManagerDBPI.dll: " + dbPath, GetLastError());
|
|
return 2;
|
|
}
|
|
|
|
const char* ctorName = "??0CLibServer@@QAE@PBD@Z";
|
|
const char* initName = "?Init@CLibServer@@QAEJXZ";
|
|
const char* loadName = "?Load@CLibServer@@QAEJPBDH@Z";
|
|
const char* closeName = "?Close@CLibServer@@QAEXXZ";
|
|
const char* dtorName = "??1CLibServer@@QAE@XZ";
|
|
|
|
Functions fn{
|
|
reinterpret_cast<CtorFn>(required(module, ctorName)),
|
|
reinterpret_cast<InitFn>(required(module, initName)),
|
|
reinterpret_cast<LoadFn>(required(module, loadName)),
|
|
reinterpret_cast<CloseFn>(required(module, closeName)),
|
|
reinterpret_cast<DtorFn>(required(module, dtorName)),
|
|
};
|
|
if (!fn.ctor || !fn.init || !fn.load || !fn.close || !fn.dtor) {
|
|
fail("CLibServer lifecycle export is incomplete");
|
|
return 3;
|
|
}
|
|
|
|
// RFSymMergeUtility.exe calls operator new(0xC) before the constructor.
|
|
void* object = VirtualAlloc(nullptr, kCLibServerObjectSize,
|
|
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
|
if (!object) {
|
|
fail("无法分配 CLibServer 对象缓冲区", GetLastError());
|
|
return 4;
|
|
}
|
|
|
|
LifecycleResult lifecycle{};
|
|
invokeLifecycle(&fn, object, lmc.c_str(), &lifecycle);
|
|
|
|
VirtualFree(object, 0, MEM_RELEASE);
|
|
// Deliberately do not FreeLibrary(module): Mentor's MFC/DBPI module graph
|
|
// is process-scoped and the OS reclaims it safely at process exit.
|
|
|
|
std::cout << "{\"ok\":" << (lifecycle.exceptionCode == ERROR_SUCCESS ? "true" : "false")
|
|
<< ",\"mode\":\"read-only\",\"architecture\":\"x86\""
|
|
<< ",\"module\":\"" << jsonEscape(dbPath) << "\""
|
|
<< ",\"object_size\":" << kCLibServerObjectSize
|
|
<< ",\"init_return\":" << lifecycle.initReturn
|
|
<< ",\"load_return\":" << lifecycle.loadReturn
|
|
<< ",\"load_mode\":1"
|
|
<< ",\"constructed\":" << (lifecycle.constructed ? "true" : "false")
|
|
<< ",\"closed\":" << (lifecycle.closed ? "true" : "false")
|
|
<< ",\"destroyed\":" << (lifecycle.destroyed ? "true" : "false")
|
|
<< ",\"write_call_enabled\":false";
|
|
if (lifecycle.exceptionCode != ERROR_SUCCESS) {
|
|
std::cout << ",\"exception_code\":" << lifecycle.exceptionCode;
|
|
}
|
|
std::cout << "}" << std::endl;
|
|
return lifecycle.exceptionCode == ERROR_SUCCESS && lifecycle.initReturn == 0 &&
|
|
lifecycle.loadReturn == 0
|
|
? 0
|
|
: 5;
|
|
}
|
|
|
|
std::string fullPath(const std::string& path) {
|
|
char buffer[MAX_PATH * 4] = {};
|
|
const DWORD length = GetFullPathNameA(path.c_str(), sizeof(buffer), buffer, nullptr);
|
|
return length == 0 || length >= sizeof(buffer) ? std::string() : std::string(buffer, length);
|
|
}
|
|
|
|
bool pathWithin(const std::string& root, const std::string& candidate) {
|
|
const std::string rootFull = fullPath(root);
|
|
const std::string candidateFull = fullPath(candidate);
|
|
if (rootFull.empty() || candidateFull.empty()) return false;
|
|
if (_strnicmp(rootFull.c_str(), candidateFull.c_str(), rootFull.size()) != 0) return false;
|
|
if (candidateFull.size() == rootFull.size()) return false;
|
|
return candidateFull[rootFull.size()] == '\\';
|
|
}
|
|
|
|
int runWrite(const std::string& fixtureRoot, const std::string& lmc, int verifyFlag) {
|
|
if (!pathWithin(fixtureRoot, lmc)) {
|
|
std::cout << "{\"ok\":false,\"mode\":\"write-isolated-copy\",\"error\":\"path_guard\",\"fixture_root\":\""
|
|
<< jsonEscape(fullPath(fixtureRoot)) << "\",\"lmc\":\""
|
|
<< jsonEscape(fullPath(lmc)) << "\"}" << std::endl;
|
|
return 10;
|
|
}
|
|
|
|
const std::string sddHome = envValue("SDD_HOME");
|
|
const std::string configured = envValue("LIBMANAGERDBPI_PATH");
|
|
const std::string dbPath = !configured.empty()
|
|
? configured
|
|
: (sddHome.empty() ? "LibManagerDBPI.dll"
|
|
: sddHome + "\\common\\win32\\lib\\LibManagerDBPI.dll");
|
|
HMODULE module = LoadLibraryW(widen(dbPath).c_str());
|
|
if (!module) {
|
|
fail("无法加载 LibManagerDBPI.dll: " + dbPath, GetLastError());
|
|
return 11;
|
|
}
|
|
|
|
Functions fn{
|
|
reinterpret_cast<CtorFn>(required(module, "??0CLibServer@@QAE@PBD@Z")),
|
|
reinterpret_cast<InitFn>(required(module, "?Init@CLibServer@@QAEJXZ")),
|
|
reinterpret_cast<LoadFn>(required(module, "?Load@CLibServer@@QAEJPBDH@Z")),
|
|
reinterpret_cast<CloseFn>(required(module, "?Close@CLibServer@@QAEXXZ")),
|
|
reinterpret_cast<DtorFn>(required(module, "??1CLibServer@@QAE@XZ")),
|
|
reinterpret_cast<SetVerifyFlagFn>(required(module, "?SetVerifyRCCellsFlag@CLibServer@@QAEXH@Z")),
|
|
reinterpret_cast<SaveFn>(required(module, "?Save@CLibServer@@QAEJPBDHH@Z")),
|
|
};
|
|
if (!fn.ctor || !fn.init || !fn.load || !fn.close || !fn.dtor ||
|
|
!fn.setVerifyFlag || !fn.save) {
|
|
fail("CLibServer write lifecycle export is incomplete");
|
|
return 12;
|
|
}
|
|
|
|
void* object = VirtualAlloc(nullptr, kCLibServerObjectSize,
|
|
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
|
if (!object) {
|
|
fail("无法分配 CLibServer 对象缓冲区", GetLastError());
|
|
return 13;
|
|
}
|
|
WriteResult result{};
|
|
invokeWriteLifecycle(&fn, object, lmc.c_str(), verifyFlag, &result);
|
|
VirtualFree(object, 0, MEM_RELEASE);
|
|
|
|
std::cout << "{\"ok\":"
|
|
<< (result.exceptionCode == ERROR_SUCCESS ? "true" : "false")
|
|
<< ",\"mode\":\"write-isolated-copy\",\"architecture\":\"x86\""
|
|
<< ",\"fixture_root\":\"" << jsonEscape(fullPath(fixtureRoot)) << "\""
|
|
<< ",\"lmc\":\"" << jsonEscape(fullPath(lmc)) << "\""
|
|
<< ",\"object_size\":" << kCLibServerObjectSize
|
|
<< ",\"load_mode\":1"
|
|
<< ",\"verify_flag\":" << verifyFlag
|
|
<< ",\"init_return\":" << result.initReturn
|
|
<< ",\"load_return\":" << result.loadReturn
|
|
<< ",\"save_return\":" << result.saveReturn
|
|
<< ",\"constructed\":" << (result.constructed ? "true" : "false")
|
|
<< ",\"flag_set\":" << (result.flagSet ? "true" : "false")
|
|
<< ",\"closed\":" << (result.closed ? "true" : "false")
|
|
<< ",\"destroyed\":" << (result.destroyed ? "true" : "false")
|
|
<< ",\"write_call_enabled\":true";
|
|
if (result.exceptionCode != ERROR_SUCCESS) {
|
|
std::cout << ",\"exception_code\":" << result.exceptionCode;
|
|
}
|
|
std::cout << "}" << std::endl;
|
|
return result.exceptionCode == ERROR_SUCCESS ? 0 : 14;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc == 8 && std::string(argv[1]) == "--write-isolated-copy" &&
|
|
std::string(argv[2]) == "--fixture-root" &&
|
|
std::string(argv[4]) == "--lmc" &&
|
|
std::string(argv[6]) == "--set-verify-flag") {
|
|
const std::string fixtureRoot(argv[3]);
|
|
const std::string lmc(argv[5]);
|
|
const int verifyFlag = std::atoi(argv[7]);
|
|
if (verifyFlag != 1) {
|
|
fail("写 probe 只允许 verify flag=1");
|
|
return 1;
|
|
}
|
|
if (GetFileAttributesA(lmc.c_str()) == INVALID_FILE_ATTRIBUTES) {
|
|
fail("LMC 文件不存在: " + lmc, GetLastError());
|
|
return 1;
|
|
}
|
|
return runWrite(fixtureRoot, lmc, verifyFlag);
|
|
}
|
|
if (argc != 4 || std::string(argv[1]) != "--read-only" ||
|
|
std::string(argv[2]) != "--lmc" || argv[3][0] == '\0') {
|
|
fail("仅支持 --read-only --lmc <isolated-lmc>");
|
|
return 1;
|
|
}
|
|
const std::string lmc(argv[3]);
|
|
if (GetFileAttributesA(lmc.c_str()) == INVALID_FILE_ATTRIBUTES) {
|
|
fail("LMC 文件不存在: " + lmc, GetLastError());
|
|
return 1;
|
|
}
|
|
return run(lmc);
|
|
}
|