Skip to content

Commit

Permalink
Allow to disable caching (#2484)
Browse files Browse the repository at this point in the history
* Allow to disable caching

By setting `context.cache_dir = None`. Generate default cache dir again by setting `context.cache_dir = True`.

* Disable cache in `asm` caching test

* Update CHANGELOG

* Fix asm cache test
  • Loading branch information
peace-maker authored Oct 24, 2024
1 parent 3303ea9 commit bb7a85c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ The table below shows which release corresponds to each branch, and what date th
- [#2483][2483] Only print `checksec` output of `ELF.libc` when it was printed for the `ELF` already
- [#2482][2482] Throw error when using `sni` and setting `server_hostname` manually in `remote`
- [#2478][2478] libcdb-cli: add `--offline-only`, refactor unstrip and add fetch parser for download libc-database
- [#2484][2484] Allow to disable caching

[2471]: https://github.com/Gallopsled/pwntools/pull/2471
[2358]: https://github.com/Gallopsled/pwntools/pull/2358
Expand All @@ -94,6 +95,7 @@ The table below shows which release corresponds to each branch, and what date th
[2483]: https://github.com/Gallopsled/pwntools/pull/2483
[2482]: https://github.com/Gallopsled/pwntools/pull/2482
[2478]: https://github.com/Gallopsled/pwntools/pull/2478
[2484]: https://github.com/Gallopsled/pwntools/pull/2484

## 4.14.0 (`beta`)

Expand Down
4 changes: 3 additions & 1 deletion pwnlib/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,11 @@ def asm(shellcode, vma = 0, extract = True, shared = False):
The output is cached:
>>> start = time.time()
>>> asm("lea rax, [rip+0]", arch = 'amd64')
>>> asm("lea rax, [rip+0]", arch = 'amd64', cache_dir = None) # force uncached time
b'H\x8d\x05\x00\x00\x00\x00'
>>> uncached_time = time.time() - start
>>> asm("lea rax, [rip+0]", arch = 'amd64') # cache it
b'H\x8d\x05\x00\x00\x00\x00'
>>> start = time.time()
>>> asm("lea rax, [rip+0]", arch = 'amd64')
b'H\x8d\x05\x00\x00\x00\x00'
Expand Down
19 changes: 15 additions & 4 deletions pwnlib/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,9 +1377,9 @@ def buffer_size(self, size):
def cache_dir_base(self, new_base):
"""Base directory to use for caching content.
Changing this to a different value will clear the `cache_dir` path
Changing this to a different value will clear the :attr:`cache_dir` path
stored in TLS since a new path will need to be generated to respect the
new `cache_dir_base` value.
new :attr:`cache_dir_base` value.
"""

if new_base != self.cache_dir_base:
Expand All @@ -1394,19 +1394,28 @@ def cache_dir(self):
Note:
May be either a path string, or :const:`None`.
Set to :const:`None` to disable caching.
Set to :const:`True` to generate the default cache directory path
based on :attr:`cache_dir_base` again.
Example:
>>> cache_dir = context.cache_dir
>>> cache_dir is not None
True
>>> os.chmod(cache_dir, 0o000)
>>> del context._tls['cache_dir']
>>> context.cache_dir = True
>>> context.cache_dir is None
True
>>> os.chmod(cache_dir, 0o755)
>>> cache_dir == context.cache_dir
True
>>> context.cache_dir = None
>>> context.cache_dir is None
True
>>> context.cache_dir = True
>>> context.cache_dir is not None
True
"""
try:
# If the TLS already has a cache directory path, we return it
Expand Down Expand Up @@ -1451,7 +1460,9 @@ def cache_dir(self):

@cache_dir.setter
def cache_dir(self, v):
if os.access(v, os.W_OK):
if v is True:
del self._tls["cache_dir"]
elif v is None or os.access(v, os.W_OK):
# Stash this in TLS for later reuse
self._tls["cache_dir"] = v

Expand Down

0 comments on commit bb7a85c

Please sign in to comment.