From 88f37299e9fd3ca6fdb4d788eb43105f7fc73ae5 Mon Sep 17 00:00:00 2001 From: Luke Craig Date: Wed, 8 Apr 2026 10:35:37 -0400 Subject: [PATCH 1/4] instances: add ops --- src/dwarffi/instances.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/dwarffi/instances.py b/src/dwarffi/instances.py index a5795e5..3d62f7c 100644 --- a/src/dwarffi/instances.py +++ b/src/dwarffi/instances.py @@ -1156,6 +1156,36 @@ def __gt__(self, other: Any) -> bool: def __ge__(self, other: Any) -> bool: return self.address >= (other.address if isinstance(other, Ptr) else other) + + def __and__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return self.address & other + + def __rand__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return other & self.address + + def __or__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return self.address | other + + def __ror__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return other | self.address + + def __xor__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return self.address ^ other + + def __rxor__(self, other: int) -> int: + if not isinstance(other, int): + return NotImplemented + return other ^ self.address class EnumInstance: From 20e7880d741d055883c8de4d891e411e3edbd413 Mon Sep 17 00:00:00 2001 From: Luke Craig Date: Wed, 8 Apr 2026 10:39:47 -0400 Subject: [PATCH 2/4] instances: fixup --- src/dwarffi/instances.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dwarffi/instances.py b/src/dwarffi/instances.py index 3d62f7c..bcae949 100644 --- a/src/dwarffi/instances.py +++ b/src/dwarffi/instances.py @@ -1156,7 +1156,7 @@ def __gt__(self, other: Any) -> bool: def __ge__(self, other: Any) -> bool: return self.address >= (other.address if isinstance(other, Ptr) else other) - + def __and__(self, other: int) -> int: if not isinstance(other, int): return NotImplemented From 60071920ac4f1838eb33657bf024ca1175a8fbcf Mon Sep 17 00:00:00 2001 From: Luke Craig Date: Wed, 8 Apr 2026 10:55:38 -0400 Subject: [PATCH 3/4] fixup tests --- tests/test_magic_math.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/test_magic_math.py b/tests/test_magic_math.py index dbd2f61..a2691a3 100644 --- a/tests/test_magic_math.py +++ b/tests/test_magic_math.py @@ -1,6 +1,6 @@ import pytest -from dwarffi import DFFI +from dwarffi import DFFI, Ptr @pytest.fixture @@ -72,3 +72,33 @@ def test_primitive_math(ffi_env: DFFI): assert val > 50 assert val <= 100 assert val == 100 + +def test_ptr_tagged_and_page_alignment(ffi_env: DFFI): + """ + Simulate AArch64 tagged pointers and OS page alignment masking. + Verifies that complex bitwise logic chains evaluate correctly. + """ + d = ffi_env + # 64-bit pointer with a metadata tag in the top 8 bits + # Tag: 0xA5, True Address: 0x00007FFF80001234 + tagged_addr = 0xA5007FFF80001234 + ptr = d.t.int.ptr(tagged_addr) + + # 1. Extract the tag (shift right 56 bits) + tag = (ptr & 0xFF00000000000000) >> 56 + assert tag == 0xA5 + + # 2. Clear the tag to get the routable address + actual_addr = ptr & 0x00FFFFFFFFFFFFFF + assert actual_addr == 0x00007FFF80001234 + + # 3. Calculate 4KB Page alignment (Clear the bottom 12 bits) + # Python bitwise NOT on standard ints can be tricky with signs, + # so we explicitly mask the bits we want. + PAGE_MASK = 0xFFFFFFFFFFFFF000 + page_base = actual_addr & PAGE_MASK + assert page_base == 0x00007FFF80001000 + + # 4. Isolate the offset within the page + page_offset = actual_addr & 0xFFF + assert page_offset == 0x234 From ccf10599797ddec16da0ef381fc486934610ff8e Mon Sep 17 00:00:00 2001 From: Luke Craig Date: Wed, 8 Apr 2026 10:57:41 -0400 Subject: [PATCH 4/4] ruff --- tests/test_magic_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_magic_math.py b/tests/test_magic_math.py index a2691a3..0157931 100644 --- a/tests/test_magic_math.py +++ b/tests/test_magic_math.py @@ -1,6 +1,6 @@ import pytest -from dwarffi import DFFI, Ptr +from dwarffi import DFFI @pytest.fixture