about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlorent Monjalet <florent.monjalet@gmail.com>2015-11-29 22:07:54 +0100
committerFlorent Monjalet <florent.monjalet@gmail.com>2016-01-18 14:02:31 +0100
commit0682ca8d6e82626bb0325d96d19342fbede33e21 (patch)
tree1831e9720e5a4f8991d6b69e0b9f13df0b29e898
parentd19f4c1dbdd2f1f451d03551abb0e5ebf4d455be (diff)
downloadmiasm-0682ca8d6e82626bb0325d96d19342fbede33e21.tar.gz
miasm-0682ca8d6e82626bb0325d96d19342fbede33e21.zip
MemStruct: No more 'pin' and 'mem_sized_arraytype' functions
Diffstat (limited to '')
-rw-r--r--miasm2/analysis/mem.py58
-rw-r--r--test/analysis/mem.py10
2 files changed, 13 insertions, 55 deletions
diff --git a/miasm2/analysis/mem.py b/miasm2/analysis/mem.py
index 2b2b3a72..ce742dcc 100644
--- a/miasm2/analysis/mem.py
+++ b/miasm2/analysis/mem.py
@@ -71,8 +71,6 @@ of PinnedType with a custom implementation:
       in C. It cannot be allocated automatically, since it has no known size
     - PinnedSizedArray: a sized PinnedArray, can be automatically allocated in memory
       and allows more operations than PinnedArray
-    - pin: a function that dynamically generates a PinnedStruct subclass from a
-      Type. This class has only one field named "val".
 
 A PinnedType do not always have a static size (cls.sizeof()) nor a dynamic size
 (self.get_size()).
@@ -167,28 +165,10 @@ def set_str_utf16(vm, addr, s):
     vm.set_mem(addr, s)
 
 
-# Type to PinnedType helper
-
-def pin(field):
-    """Generate a PinnedStruct subclass from a field. The field's value can
-    be accessed through self.val or self.deref_val if field is a Ptr.
-
-    @field: a Type instance.
-    """
-    if field in DYN_MEM_STRUCT_CACHE:
-        return DYN_MEM_STRUCT_CACHE[field]
-
-    fields = [("val", field)]
-    # Build a type to contain the field type
-    mem_type = type("Pinned%r" % field, (PinnedStruct,), {'fields': fields})
-    DYN_MEM_STRUCT_CACHE[field] = mem_type
-    return mem_type
-
-
 # Type classes
 
 class Type(object):
-    """Base class to provide methods to set and get fields from virtual pin.
+    """Base class to provide methods to set and get fields from virtual mem.
 
     Subclasses can either override _pack and _unpack, or get and set if data
     serialization requires more work (see Inline implementation for an example).
@@ -330,7 +310,7 @@ class Ptr(Num):
             in memory
         @dst_type: (PinnedType or Type) the PinnedType this Ptr points to.
             If a Type is given, it is transformed into a PinnedType with
-            pin(TheType).
+            TheType.pinned.
         *type_args, **type_kwargs: arguments to pass to the the pointed
             PinnedType when instanciating it (e.g. for PinnedStr encoding or
             PinnedArray field_type).
@@ -348,10 +328,10 @@ class Ptr(Num):
             dst_type._get_self_type = lambda: self._get_self_type()
             # dst_type cannot be patched here, since _get_self_type of the outer
             # class has not yet been set. Patching dst_type involves calling
-            # pin(dst_type), which will only return a type that does not point
+            # dst_type.pinned, which will only return a type that does not point
             # on PinnedSelf but on the right class only when _get_self_type of the
             # outer class has been replaced by _MetaPinnedStruct.
-            # In short, dst_type = pin(dst_type) is not valid here, it is done
+            # In short, dst_type = dst_type.pinned is not valid here, it is done
             # lazily in _fix_dst_type
         self._dst_type = dst_type
         self._type_args = type_args
@@ -1307,8 +1287,8 @@ class PinnedSizedArray(PinnedArray):
     """A fixed size PinnedArray. Its additional arg represents the @array_len (in
     number of elements) of this array.
 
-    This type is dynamically sized. Use mem_sized_array_type to generate a
-    fixed @field_type and @array_len array which has a static size.
+    This type is dynamically sized. Generate a fixed @field_type and @array_len
+    array which has a static size by using Array(type, size).pinned.
     """
     _array_len = None
 
@@ -1321,7 +1301,7 @@ class PinnedSizedArray(PinnedArray):
         if self._array_len is None or self._field_type is None:
             raise NotImplementedError(
                 "Provide field_type and array_len to instanciate this class, "
-                "or generate a subclass with mem_sized_array_type.")
+                "or generate a subclass with Array(type, size).pinned.")
 
     @property
     def array_len(self):
@@ -1330,7 +1310,7 @@ class PinnedSizedArray(PinnedArray):
 
     def sizeof(cls):
         raise ValueError("PinnedSizedArray is not statically sized. Use "
-                         "mem_sized_array_type to generate a type that is.")
+                         "Array(type, size).pinned to generate a type that is.")
 
     def get_size(self):
         return self._array_len * self._field_type.size()
@@ -1384,25 +1364,3 @@ def mem_array_type(field_type):
     DYN_MEM_STRUCT_CACHE[cache_key] = array_type
     return array_type
 
-
-def mem_sized_array_type(field_type, array_len):
-    """Generate a PinnedSizedArray subclass that has a fixed @field_type and a
-    fixed @array_len. This allows to instanciate the returned type with only
-    the vm and addr arguments, as are standard PinnedTypes.
-    """
-    cache_key = (field_type, array_len)
-    if cache_key in DYN_MEM_STRUCT_CACHE:
-        return DYN_MEM_STRUCT_CACHE[cache_key]
-
-    @classmethod
-    def sizeof(cls):
-        return cls._field_type.size() * cls._array_len
-
-    array_type = type('PinnedSizedArray_%r_%s' % (field_type, array_len),
-                      (PinnedSizedArray,),
-                      {'_array_len': array_len,
-                       '_field_type': field_type,
-                       'sizeof': sizeof})
-    DYN_MEM_STRUCT_CACHE[cache_key] = array_type
-    return array_type
-
diff --git a/test/analysis/mem.py b/test/analysis/mem.py
index dca3346b..60d9c569 100644
--- a/test/analysis/mem.py
+++ b/test/analysis/mem.py
@@ -7,9 +7,9 @@ import struct
 from miasm2.analysis.machine import Machine
 from miasm2.analysis.mem import PinnedStruct, Num, Ptr, PinnedStr, PinnedArray,\
                                 PinnedSizedArray, Array, mem_array_type,\
-                                mem_sized_array_type, RawStruct, pin,\
-                                Union, BitField, PinnedSelf, PinnedVoid, Bits, \
-                                set_allocator, PinnedUnion, Struct
+                                RawStruct, Union, BitField, PinnedSelf, \
+                                PinnedVoid, Bits, set_allocator, PinnedUnion, \
+                                Struct
 from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
 from miasm2.os_dep.common import heap
 
@@ -198,8 +198,8 @@ except ValueError:
 # PinnedSizedArray tests
 memsarray = PinnedSizedArray(jitter.vm, None, Num("I"), 10)
 # This also works:
-_memsarray = mem_sized_array_type(Num("I"), 10)(jitter.vm)
-# And mem_sized_array_type generates statically sized types
+_memsarray = Array(Num("I"), 10).pinned(jitter.vm)
+# And Array(type, size).pinned generates statically sized types
 assert _memsarray.sizeof() == len(memsarray)
 memsarray.memset('\xcc')
 assert memsarray[0] == 0xcccccccc