Skip to content

Conversation

@Tim---
Copy link
Contributor

@Tim--- Tim--- commented Feb 25, 2024

Hi,

I'm trying to add support for writing memory related to #324.

For now, the changes for the Python API are an optional write_fn parameter to Program.add_memory_segment, and the method Program.write.
Before I try to tackle the missing pieces (writable objects, and optional write support in the Program constructors), I wanted to make sure I'm on the right track.

Note that on the C-side, instead of adding *_write functions as counterparts to the *_read functions, I tried to maintain as much as possible a unique *_rw(bool is_write, void *buf, size_t count) kind of functions.
This has the advantage of reusing the internal logic of some functions for both cases (eg: drgn_program_read_rw).
However, I'm now seeing that maybe the buf parameter should be const void * for the write functions, or I need to make some dirty casts :/.

Also, the naming choices are maybe not the best (memory_interface is meh, and if we keep the *_rw functions, maybe *_transfer would be prettier).

Anyway, feel free to review the changes and tell me if I made some mistakes, I'll need to rebase it anyway to add the Signed-Off-By that I forgot in the commits :).

@Tim---
Copy link
Contributor Author

Tim--- commented Feb 29, 2024

I tried to go a bit further and started implementing __setattr__ and __setitem__ methods for the Object class.
For now I just try to implement it for "value" objects, but I seem to have reached a rather annoying problem:

If I assign point.x = 123, I can read point.x with the new value. But if I so the same with a nested structure like segment.a.x = 123, the value is not changed because segment.a seems to return a copy of the point object.

Do you have an idea of how this could be handled for writing objects ?

@osandov
Copy link
Owner

osandov commented Oct 1, 2024

Hi, sorry for the ridiculously late response. I recently added a couple of experimental helpers for writing to kernel memory/objects: https://drgn.readthedocs.io/en/latest/helpers.html#drgn.helpers.experimental.kmodify.write_memory. That renewed my interest in having this more generic interface, since it would be possible to plug the write_memory() helper into a memory writer. Let me know if this is still something that you're interested in and I can give this PR a look.

For now I just try to implement it for "value" objects, but I seem to have reached a rather annoying problem

I have conflicting feelings about allowing writes to value objects in general. In the Python API, Objects are currently immutable. Allowing writes could break some expectations there. But, it is potentially useful. Did you have any particular use cases in mind?

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.

2 participants