Skip to content

Fix access violation when loading script files - #1

Open
crimsonskylark wants to merge 1 commit into
ivellioscolin:developfrom
crimsonskylark:develop
Open

Fix access violation when loading script files#1
crimsonskylark wants to merge 1 commit into
ivellioscolin:developfrom
crimsonskylark:develop

Conversation

@crimsonskylark

Copy link
Copy Markdown

This PR addresses the "Access violation" reported in ivellioscolin/pykd#6.

The exception was caused by an unconditional usage of the _Py_fopen API which is no longer exported as of Python 3.10-3.13. According to python/cpython#127350 (comment) the API will return for Python 3.14 (and presumably future versions). My solution instead relies on the standard C++ file I/O APIs to read the file into memory and execute via PyRun_String in lieu of the previous PyRun_FileExFlags.

Finally, during the course of my investigation I found other APIs no longer exported by CPython (as of 3.13.5) also in-use by PyKD. As I cannot commit time to a bigger PR at this time I've left their usage intact, but the below list provides their names for future reference.

PyString_Type
_PyThreadState_Current
PyString_FromString
PyString_AsString
PyImport_Cleanup
PyClass_New
PyInstance_New
PyFile_FromString
PyFile_AsFile

Utilize C++ standard APIs for opening script files in lieu of C `fopen`.
@crimsonskylark

Copy link
Copy Markdown
Author

I've also found other issues, e.g., when setting breakpoints via the API. I will leave this PR as-is since it resolves the original issue and open new PRs for the others.

@gerhart01

Copy link
Copy Markdown

Hello.
There are errors, when executing python scripts, which are working with Windows kernel.
Do that errors can be fixed in pykd-ext.dll only, or pykd.pyd had to be fixed too?

For example script for listing afd.sys driver endpoints: https://github.com/gerhart01/Hyper-V-scripts/blob/master/ParseAfdEndpointListHead.py

************* Path validation summary **************
Response                         Time (ms)     Location
Deferred                                       SRV*C:\Symbols*https://msdl.microsoft.com/download/symbols
Symbol search path is: SRV*C:\Symbols*https://msdl.microsoft.com/download/symbols
Executable search path is: 
Loading symbols for fffff806`7d800000     ntkrnlmp.exe ->   ntkrnlmp.exe
ModLoad: fffff806`7d800000 fffff806`7ec59000   ntkrnlmp.exe
Windows 10 Kernel Version 27975 MP (1 procs) Free x64
Product: WinNt, suite: TerminalServer SingleUserTS
Edition build lab: 27975.984.amd64fre.br_release_svc_prod3.251020-1700
Kernel base = 0xfffff806`7d800000 PsLoadedModuleList = 0xfffff806`7e6f9600
Debug session time: Tue Nov 11 10:24:20.253 2025 (UTC - 8:00)
System Uptime: 0 days 0:05:16.894
Loaded dbghelp extension DLL
Loaded exts extension DLL
Loaded kext extension DLL
Loaded kdexts extension DLL
nt!PpmIdleGuestExecute+0x1d:
fffff806`7dc9960d 48c1e220        shl     rdx,20h
kd> .load @"C:\scripts\pykd.dll"
Loaded C:\scripts\pykd.dll extension DLL
kd> !info

pykd bootstrapper version: 2.0.0.25

Installed python:

Version:        Status:     Image:
------------------------------------------------------------------------------
* 3.13 x86-64   Unloaded    C:\Python313\python313.dll


kd> !py @"C:\scripts\hello.py"
Hello
kd> !info

pykd bootstrapper version: 2.0.0.25

Installed python:

Version:        Status:     Image:
------------------------------------------------------------------------------
* 3.13 x86-64   Loaded      C:\Python313\python313.dll


kd> !py @"C:\scripts\ParseAfdEndpointListHead.py"
Script for AfdEndpointList parsing
Executing .reload command ...
c0000005 Exception in C:\scripts\pykd.dll.py debugger extension.
      PC: 00007ffa`e7808a19  VA: 00000000`00000010  R/W: 0  Parameter: 00000000`00000000

@crimsonskylark

Copy link
Copy Markdown
Author

@gerhart01 Did you compile my branch or is this the official pykd release? If the former, my guess is you ran into one of the other problems I mentioned in my comment above.

I'm still planning on coming back to this and fix everything once and for all but haven't had the time.

@gerhart01

Copy link
Copy Markdown

Yes, i downloaded your compiled version from ivellioscolin/pykd#6 (comment).
Ok, understood - thanks for explanation.

@gerhart01

Copy link
Copy Markdown

It's working, if you replace getAttrByName in pykd\pymodule.h on that variant :

static python::object getAttrByName(kdlib::Module& module, const std::wstring& symbolName)
    {
        unsigned __int64 symbolVa = 0;
        kdlib::TypeInfoPtr typeInfo;
        bool symbolFound = false;
        bool typeFound = false;

        // Scope for AutoRestorePyState
        {
#if PY_VERSION_HEX < 0x030D0000  // Python < 3.13
            AutoRestorePyState  pystate;  
#endif

            try {
                symbolVa = module.getSymbolVa(symbolName);
                symbolFound = true;
            }
            catch (kdlib::DbgException&)
            {
            }

            if (!symbolFound)
            {
                try {
                    typeInfo = module.getTypeByName(symbolName);
                    typeFound = true;
                }
                catch (kdlib::DbgException&)
                {
                }
            }
        }

        if (symbolFound)
        {
            return python::object(symbolVa);
        }

        if (typeFound)
        {
            return python::object(typeInfo);
        }

        std::wstringstream sstr;
        sstr << L'\'' << module.getName() << L'\''
            << L" module has not a symbol "
            << L'\'' << symbolName << L'\'';
        throw AttributeException(std::string(_bstr_t(sstr.str().c_str())).c_str());
    }
image

@crimsonskylark

Copy link
Copy Markdown
Author

@gerhart01 I'll include it in my PR with proper attribution, of course. Thanks.

@ivellioscolin

Copy link
Copy Markdown
Owner

Thank you all for your feedback.

If the access violation is due to unstable API _Py_fopen, it should been already fixed by commit
7c0c237

However the binary on release page https://github.com/ivellioscolin/pykd-ext/releases points to the original HEAD 857b62e, which doesn't include all fixes I made after. I should have put a HEAD build just as did for pykd.

I'm trying to make sometime to finish some other pending issues, and will put a pykd-ext nightly build on release page as well.

@gerhart01

Copy link
Copy Markdown

Fixes for pykd Python 3.14 supporting (partitially or full). Tested on WinDBG scripts for Kernel Mode:
https://github.com/gerhart01/pykd
https://github.com/gerhart01/pykd-ext
Probably, you can use them for patches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants