mirror of
https://github.com/mandiant/capa.git
synced 2025-12-23 07:28:34 -08:00
use weakrefs for Scopes instantiation; fix test_rules()
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
import json
|
||||
import inspect
|
||||
import logging
|
||||
import weakref
|
||||
import functools
|
||||
import contextlib
|
||||
import importlib.util
|
||||
from typing import NoReturn
|
||||
@@ -130,6 +132,25 @@ def redirecting_print_to_tqdm(disable_progress):
|
||||
inspect.builtins.print = old_print # type: ignore
|
||||
|
||||
|
||||
def weak_lru(maxsize=128, typed=False):
|
||||
"""
|
||||
LRU Cache decorator that keeps a weak reference to 'self'
|
||||
"""
|
||||
|
||||
def wrapper(func):
|
||||
@functools.lru_cache(maxsize, typed)
|
||||
def _func(_self, *args, **kwargs):
|
||||
return func(_self(), *args, **kwargs)
|
||||
|
||||
@functools.wraps(func)
|
||||
def inner(self, *args, **kwargs):
|
||||
return _func(weakref.ref(self), *args, **kwargs)
|
||||
|
||||
return inner
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def log_unsupported_format_error():
|
||||
logger.error("-" * 80)
|
||||
logger.error(" Input file does not appear to be a PE or ELF file.")
|
||||
|
||||
@@ -16,7 +16,7 @@ import collections
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
from capa.helpers import assert_never
|
||||
from capa.helpers import weak_lru, assert_never
|
||||
|
||||
try:
|
||||
from functools import lru_cache
|
||||
@@ -115,7 +115,7 @@ class Scopes:
|
||||
static: Optional[str] = None
|
||||
dynamic: Optional[str] = None
|
||||
|
||||
@lru_cache() # type: ignore
|
||||
@weak_lru()
|
||||
def __new__(cls, *args, **kwargs):
|
||||
return super().__new__(cls)
|
||||
|
||||
|
||||
@@ -357,7 +357,7 @@ def test_rules_flavor_filtering():
|
||||
),
|
||||
]
|
||||
|
||||
static_rules = capa.rules.RuleSet(rules, rules_filter_func=lambda rule: rule.scopes.static)
|
||||
static_rules = capa.rules.RuleSet(rules.copy(), rules_filter_func=lambda rule: rule.scopes.static)
|
||||
dynamic_rules = capa.rules.RuleSet(rules, rules_filter_func=lambda rule: rule.scopes.dynamic)
|
||||
|
||||
# only static rule
|
||||
|
||||
Reference in New Issue
Block a user