VirtualBox

Ignore:
Timestamp:
Jan 27, 2017 10:00:44 AM (8 years ago)
Author:
vboxsync
Message:

BugReportTool: rudimentary filtering support for Windows registry files, memory leak fixes (bugref:8169)

Location:
trunk/src/VBox/Frontends/VBoxBugReport
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxBugReport/VBoxBugReport.cpp

    r65456 r65478  
    146146};
    147147
     148
     149BugReportFilter::BugReportFilter() : m_pvBuffer(0), m_cbBuffer(0)
     150{
     151}
     152
     153BugReportFilter::~BugReportFilter()
     154{
     155    if (m_pvBuffer)
     156        RTMemFree(m_pvBuffer);
     157}
     158
     159void *BugReportFilter::allocateBuffer(size_t cbNeeded)
     160{
     161    if (m_pvBuffer)
     162    {
     163        if (cbNeeded > m_cbBuffer)
     164            RTMemFree(m_pvBuffer);
     165        else
     166            return m_pvBuffer;
     167    }
     168    m_pvBuffer = RTMemAlloc(cbNeeded);
     169    if (!m_pvBuffer)
     170        throw RTCError(com::Utf8StrFmt("Failed to allocate %ld bytes\n", cbNeeded));
     171    m_cbBuffer = cbNeeded;
     172    return m_pvBuffer;
     173}
     174
     175
    148176/*
    149177 * An abstract class serving as the root of the bug report item tree.
     
    152180{
    153181    m_pszTitle = RTStrDup(pszTitle);
     182    m_filter = 0;
    154183}
    155184
    156185BugReportItem::~BugReportItem()
    157186{
     187    if (m_filter)
     188        delete m_filter;
    158189    RTStrFree(m_pszTitle);
    159190}
    160191
     192void BugReportItem::addFilter(BugReportFilter *filter)
     193{
     194    m_filter = filter;
     195}
     196
     197void *BugReportItem::applyFilter(void *pvSource, size_t *pcbInOut)
     198{
     199    if (m_filter)
     200        return m_filter->apply(pvSource, pcbInOut);
     201    return pvSource;
     202}
     203
    161204const char * BugReportItem::getTitle(void)
    162205{
     
    184227}
    185228
    186 void BugReport::addItem(BugReportItem* item)
    187 {
     229void BugReport::addItem(BugReportItem* item, BugReportFilter *filter)
     230{
     231    if (filter)
     232        item->addFilter(filter);
    188233    if (item)
    189234        m_Items.append(item);
     
    199244    }
    200245    RTPrintf("100%% - compressing...\n\n");
     246}
     247
     248void *BugReport::applyFilters(BugReportItem* item, void *pvSource, size_t *pcbInOut)
     249{
     250    return item->applyFilter(pvSource, pcbInOut);
    201251}
    202252
     
    447497        while (RT_SUCCESS(rc = RTStrmReadEx(strmIn, buf, sizeof(buf), &cbRead)) && cbRead)
    448498        {
    449             rc = RTStrmWriteEx(m_StrmTxt, buf, cbRead, &cbWritten);
     499            rc = RTStrmWriteEx(m_StrmTxt, applyFilters(item, buf, &cbRead), cbRead, &cbWritten);
    450500            if (RT_FAILURE(rc) || cbRead != cbWritten)
    451501                throw RTCError(com::Utf8StrFmt("Write failure (rc=%d, cbRead=%lu, cbWritten=%lu)\n",
     
    522572             offset += cbRead)
    523573        {
    524             handleRtError(RTTarFileWriteAt(m_hTarFile, offset, buf, cbRead, NULL),
     574            handleRtError(RTTarFileWriteAt(m_hTarFile, offset, applyFilters(item, buf, &cbRead), cbRead, NULL),
    525575                          "Failed to write %u bytes to TAR", cbRead);
    526576        }
  • trunk/src/VBox/Frontends/VBoxBugReport/VBoxBugReport.h

    r65456 r65478  
    8686
    8787/*
     88 * An abstract class serving as the root of the bug report filter tree.
     89 * A child provides an implementation of the 'apply' method. A child
     90 * should modify the input buffer (provided via pvSource) in place, or
     91 * allocate a new buffer via 'allocateBuffer'. Allocated buffers are
     92 * released automatically when another buffer is allocated, which means
     93 * that NEXT CALL TO 'APPLY' INVALIDATES BUFFERS RETURNED IN PREVIOUS
     94 * CALLS!
     95 */
     96class BugReportFilter
     97{
     98public:
     99    BugReportFilter();
     100    virtual ~BugReportFilter();
     101    virtual void *apply(void *pvSource, size_t *pcbInOut) = 0;
     102protected:
     103    void *allocateBuffer(size_t cbNeeded);
     104private:
     105    void *m_pvBuffer;
     106    size_t m_cbBuffer;
     107};
     108
     109
     110/*
    88111 * An abstract class serving as the root of the bug report item tree.
    89112 */
     
    95118    virtual const char *getTitle(void);
    96119    virtual PRTSTREAM getStream(void) = 0;
     120    void addFilter(BugReportFilter *filter);
     121    void *applyFilter(void *pvSource, size_t *pcbInOut);
    97122private:
    98123    char *m_pszTitle;
     124    BugReportFilter *m_filter;
    99125};
    100126
     
    108134    virtual ~BugReport();
    109135
    110     void addItem(BugReportItem* item);
     136    void addItem(BugReportItem* item, BugReportFilter *filter = 0);
    111137    int  getItemCount(void);
    112138    void process();
     139    void *applyFilters(BugReportItem* item, void *pvSource, size_t *pcbInOut);
    113140
    114141    virtual void processItem(BugReportItem* item) = 0;
  • trunk/src/VBox/Frontends/VBoxBugReport/VBoxBugReportWin.cpp

    r65456 r65478  
    534534    for (int i = 0; SetupDiEnumDeviceInfo(m_hDevInfo, i, &deviceInfoData); ++i)
    535535    {
     536        if (m_hHostCtrlDev != INVALID_HANDLE_VALUE)
     537            CloseHandle(m_hHostCtrlDev);
     538        if (m_pDetailData)
     539            RTMemFree(m_pDetailData);
     540
    536541        SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    537542        deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     
    694699
    695700
     701class BugReportFilterRegistryWin : public BugReportFilter
     702{
     703public:
     704    BugReportFilterRegistryWin() {};
     705    virtual ~BugReportFilterRegistryWin() {};
     706    virtual void *apply(void *pvSource, size_t *pcbInOut);
     707};
     708
     709void *BugReportFilterRegistryWin::apply(void *pvSource, size_t *pcbInOut)
     710{
     711    /*
     712     * The following implementation is not optimal by any means. It serves to
     713     * illustrate and test the case when filter's output is longer than its
     714     * input.
     715     */
     716    RT_NOREF(pcbInOut);
     717    /* Registry export files are encoded in UTF-16 (little endian on Intel x86). */
     718    void *pvDest = pvSource;
     719    uint16_t *pwsSource = (uint16_t *)pvSource;
     720    if (*pwsSource++ == 0xFEFF && *pcbInOut > 48)
     721    {
     722        if (!memcmp(pwsSource, L"Windows Registry Editor", 46))
     723        {
     724            *pcbInOut += 2;
     725            pvDest = allocateBuffer(*pcbInOut);
     726            uint16_t *pwsDest = (uint16_t *)pvDest;
     727            *pwsDest++ = 0xFEFF;
     728            *pwsDest++ = '#';
     729            /* Leave space for 0xFEFF and '#' */
     730            memcpy(pwsDest, pwsSource, *pcbInOut - 4);
     731        }
     732    }
     733    return pvDest;
     734}
     735
    696736
    697737void createBugReportOsSpecific(BugReport* report, const char *pszHome)
     
    721761    report->addItem(new BugReportCommand("DriverStore", PathJoin(WinSysDir.c_str(), "pnputil.exe"), "-e", NULL));
    722762    report->addItem(new BugReportCommandTemp("RegDevKeys", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
    723         "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\Root\\NET", NULL));
     763                                             "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\Root\\NET", NULL),
     764                    new BugReportFilterRegistryWin());
    724765    report->addItem(new BugReportCommandTemp("RegDrvKeys", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
    725         "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", NULL));
     766        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", NULL),
     767                    new BugReportFilterRegistryWin());
    726768    report->addItem(new BugReportCommandTemp("RegNetwork", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
    727         "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}", NULL));
     769        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}", NULL),
     770                    new BugReportFilterRegistryWin());
    728771    report->addItem(new BugReportUsbTreeWin);
    729772    report->addItem(new BugReportDriversWin);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette