question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement a handle manager

See original GitHub issue

Handles are currently hacked in. Add a handle manager that allows something like this:

def ZwCreateFile_syscall(...):
    handle_data = (...)
    hFile = dp.handles.new(data)
    return hFile

def ZwReadFile_syscall(hFile: HANDLE, ...):
    handle_data = dp.handles.get(hFile, None)
    return

def ZwCloseHandle(hFile):
    dp.handles.close(hFile)

It should also support duplication (every handle points to refcounted data).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mrexodiacommented, Sep 6, 2022

Just had another look, saw you renamed the member to handles already, thanks!

For __find_free_handle I guess you could just implement it without reusing handles to keep things simple. Another approach would be to keep a free list and only increase the handle count when there is nothing available in the free list. This is O(1).

For the get function I think you have to & ~3 (or similar) because some lower bits of the handle value are ignored (don’t have a reference right now, but I can test later).

The storage of a dict I think is fine to start with, but eventually I suspect it could turn into a mess. An alternative would be to have a typed API, something like:

from typing import Any, Type, TypeVar

class FileHandle:
	def __init__(self, path):
		self.path = path
		
	def __str__(self):
	    return f"{type(self).__name__}(path: {self.path})"
		
class SpecialFileHandle(FileHandle):
	def __init__(self, path, special):
		super().__init__(path)
		self.special = special
		
fh = FileHandle('c:/blah.txt')
sfh = SpecialFileHandle('c:/foo.txt', 1337)

handles = {0x1C: fh, 0x2C: sfh}

# HandleManager
def _get_internal(handle_value: int) -> Any:
    return handles.get(handle_value & ~3, None)

T = TypeVar('T')

def get(handle_value: int, handle_type: Type[T]) -> T:
    data = _get_internal(handle_value)
    if data is None:
        return None
    if handle_type is not None and not isinstance(data, handle_type):
        raise TypeError(f"Expected {handle_type.__name__} got {type(data).__name__}")
    return data

# OK
print(get(0x1C, FileHandle))
print(get(0x1D, FileHandle))

# OK
print(get(0x2C, FileHandle))

# Type error
print(get(0x1C, SpecialFileHandle))

This would allow for full flexibility. Optionally there could be a HandleData base class that stores metadata like creation time, call stack (python and/or native) and tracing if desired.

0reactions
mrexodiacommented, Sep 7, 2022

I guess an exception could be used to generically handle this in syscalls? I think I confused the behavior with process IDs…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Be a Good Manager - Business News Daily
A good leader sets a positive example and knows how to use their strengths to help their team achieve goals. Successful managers get...
Read more >
4 Strategies Good Managers Use to Handle Their Problem ...
1. Prepare ahead. Great managers will analyze the problem first to understand all perspectives. · 2. Know the right timing and approach ·...
Read more >
3 Steps to Better Handle Conflict with Your Boss | LSA Global
Handling conflict with your boss as a new manager is not easy. Managing your team is hard enough. You need your boss on...
Read more >
6 Tips to Improve Manager Effectiveness at Your Company
They trust managers because they believe them to be competent, honest and reliable. You can instill trust for your leadership in three ways:...
Read more >
How To Address Management Issues at Work: 6 Steps To Take
Strategies for managing issues at work. You can use the following strategies to handle issues at work: Develop your management skills. Here are ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found