Skip to content

Commit bb7a85c

Browse files
authored
Allow to disable caching (#2484)
* 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
1 parent 3303ea9 commit bb7a85c

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The table below shows which release corresponds to each branch, and what date th
8383
- [#2483][2483] Only print `checksec` output of `ELF.libc` when it was printed for the `ELF` already
8484
- [#2482][2482] Throw error when using `sni` and setting `server_hostname` manually in `remote`
8585
- [#2478][2478] libcdb-cli: add `--offline-only`, refactor unstrip and add fetch parser for download libc-database
86+
- [#2484][2484] Allow to disable caching
8687

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

98100
## 4.14.0 (`beta`)
99101

pwnlib/asm.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,11 @@ def asm(shellcode, vma = 0, extract = True, shared = False):
765765
The output is cached:
766766
767767
>>> start = time.time()
768-
>>> asm("lea rax, [rip+0]", arch = 'amd64')
768+
>>> asm("lea rax, [rip+0]", arch = 'amd64', cache_dir = None) # force uncached time
769769
b'H\x8d\x05\x00\x00\x00\x00'
770770
>>> uncached_time = time.time() - start
771+
>>> asm("lea rax, [rip+0]", arch = 'amd64') # cache it
772+
b'H\x8d\x05\x00\x00\x00\x00'
771773
>>> start = time.time()
772774
>>> asm("lea rax, [rip+0]", arch = 'amd64')
773775
b'H\x8d\x05\x00\x00\x00\x00'

pwnlib/context/__init__.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1377,9 +1377,9 @@ def buffer_size(self, size):
13771377
def cache_dir_base(self, new_base):
13781378
"""Base directory to use for caching content.
13791379
1380-
Changing this to a different value will clear the `cache_dir` path
1380+
Changing this to a different value will clear the :attr:`cache_dir` path
13811381
stored in TLS since a new path will need to be generated to respect the
1382-
new `cache_dir_base` value.
1382+
new :attr:`cache_dir_base` value.
13831383
"""
13841384

13851385
if new_base != self.cache_dir_base:
@@ -1394,19 +1394,28 @@ def cache_dir(self):
13941394
13951395
Note:
13961396
May be either a path string, or :const:`None`.
1397+
Set to :const:`None` to disable caching.
1398+
Set to :const:`True` to generate the default cache directory path
1399+
based on :attr:`cache_dir_base` again.
13971400
13981401
Example:
13991402
14001403
>>> cache_dir = context.cache_dir
14011404
>>> cache_dir is not None
14021405
True
14031406
>>> os.chmod(cache_dir, 0o000)
1404-
>>> del context._tls['cache_dir']
1407+
>>> context.cache_dir = True
14051408
>>> context.cache_dir is None
14061409
True
14071410
>>> os.chmod(cache_dir, 0o755)
14081411
>>> cache_dir == context.cache_dir
14091412
True
1413+
>>> context.cache_dir = None
1414+
>>> context.cache_dir is None
1415+
True
1416+
>>> context.cache_dir = True
1417+
>>> context.cache_dir is not None
1418+
True
14101419
"""
14111420
try:
14121421
# If the TLS already has a cache directory path, we return it
@@ -1451,7 +1460,9 @@ def cache_dir(self):
14511460

14521461
@cache_dir.setter
14531462
def cache_dir(self, v):
1454-
if os.access(v, os.W_OK):
1463+
if v is True:
1464+
del self._tls["cache_dir"]
1465+
elif v is None or os.access(v, os.W_OK):
14551466
# Stash this in TLS for later reuse
14561467
self._tls["cache_dir"] = v
14571468

0 commit comments

Comments
 (0)