memflow / memflow-py

Python support for memflow
https://pypi.org/project/memflow/
MIT License
13 stars 4 forks source link

Nested offset attributes not set #18

Closed emesare closed 1 year ago

emesare commented 1 year ago

The below snippet fails with AttributeError: 'TEST_OFFSETS' object has no attribute 'two_offset'.

from memflow import *
from ctypes import *

class TEST_OFFSETS(Structure):
    _fields_ = []
    _offsets_ = [(0x8, "two_offset", c_int64)]

class NESTED_OFFSET_TEST(Structure):
    _fields_ = [("inner", TEST_OFFSETS)]

def test_nested_offsets():
    my_os = dummy.os()

    # Test writing new `TEST` structure.
    test_struct = TEST_OFFSETS((1, 2), 2, two_offset=2)
    my_os.phys_write(0, TEST_OFFSETS, test_struct)

    # Test reading a nested structure with offsets.
    test_works = my_os.phys_read(0, NESTED_OFFSET_TEST).inner
    assert test_works.two_offset == 2

The root cause of this problem I think is the fact that the Structure has special constructor that overrides the __dict__ member with StgDict containing the fields ONLY in _fields_, not in _offsets_ (see: 1, 2, 3).

emesare commented 1 year ago

A hack-solution for now is to use _offsets_ for every nested offset structure.

class NESTED_OFFSET_TEST(Structure):
    _fields_ = [("inner", TEST_OFFSETS)]
    _offsets_ = [(0x0, "inner_offset", TEST_OFFSETS)] # use this in the mean time