2021-11-21 23:59:44 +00:00
|
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2021-04-03 09:10:35 +01:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-07 22:28:01 +00:00
|
|
|
from tests import TestCase
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
from tests.libdrgn import deserialize_bits, serialize_bits
|
|
|
|
|
|
|
|
VALUE = 12345678912345678989
|
|
|
|
|
|
|
|
|
|
|
|
def py_serialize_bits(value, bit_offset, bit_size, little_endian):
|
|
|
|
bits = bit_offset + bit_size
|
|
|
|
size = (bits + 7) // 8
|
|
|
|
if little_endian:
|
|
|
|
tmp = value << bit_offset
|
|
|
|
else:
|
|
|
|
tmp = value << -bits % 8
|
|
|
|
# Buffer with unused bits set to zero.
|
|
|
|
buf0 = tmp.to_bytes(size, "little" if little_endian else "big")
|
|
|
|
|
|
|
|
# Buffer with unused bits set to one.
|
|
|
|
buf1 = bytearray(buf0)
|
|
|
|
if little_endian:
|
|
|
|
# bit_offset least significant bits.
|
|
|
|
buf1[0] |= (1 << bit_offset) - 1
|
|
|
|
# 8 - (bit_offset + bit_size) % 8 most significant bits.
|
|
|
|
buf1[-1] |= (0xFF00 >> -bits % 8) & 0xFF
|
|
|
|
else:
|
|
|
|
# bit_offset most significant bits.
|
|
|
|
buf1[0] |= (0xFF00 >> bit_offset) & 0xFF
|
|
|
|
# 8 - (bit_offset + bit_size) % 8 least significant bits.
|
|
|
|
buf1[-1] |= (1 << -bits % 8) - 1
|
|
|
|
|
|
|
|
return buf0, buf1
|
|
|
|
|
|
|
|
|
2022-01-07 22:28:01 +00:00
|
|
|
class TestSerialize(TestCase):
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
def test_deserialize(self):
|
|
|
|
for bit_size in range(1, 65):
|
|
|
|
expected = VALUE & ((1 << bit_size) - 1)
|
|
|
|
for bit_offset in range(8):
|
|
|
|
for little_endian in [True, False]:
|
|
|
|
for buf in py_serialize_bits(
|
|
|
|
expected, bit_offset, bit_size, little_endian
|
|
|
|
):
|
|
|
|
value = deserialize_bits(
|
|
|
|
buf, bit_offset, bit_size, little_endian
|
|
|
|
)
|
|
|
|
self.assertEqual(value, expected)
|
|
|
|
|
|
|
|
def test_serialize(self):
|
|
|
|
for bit_size in range(1, 65):
|
|
|
|
value = VALUE & ((1 << bit_size) - 1)
|
|
|
|
for bit_offset in range(8):
|
|
|
|
for little_endian in [True, False]:
|
|
|
|
expected0, expected1 = py_serialize_bits(
|
|
|
|
value, bit_offset, bit_size, little_endian
|
|
|
|
)
|
|
|
|
buf = bytearray(len(expected0))
|
|
|
|
serialize_bits(buf, bit_offset, value, bit_size, little_endian)
|
|
|
|
self.assertEqual(buf, expected0)
|
|
|
|
|
|
|
|
buf = bytearray([0xFF] * len(expected1))
|
|
|
|
serialize_bits(buf, bit_offset, value, bit_size, little_endian)
|
|
|
|
self.assertEqual(buf, expected1)
|