From f9d9674e616fea9c2f2a044633baf79bb30471e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 20:31:35 +0100 Subject: [PATCH 1/6] simp_compose_and_mask improved --- miasm/expression/simplifications_common.py | 37 ++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index af4092862..48affd8c1 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1741,19 +1741,38 @@ def simp_compose_and_mask(_, expr): if not arg2.is_int(): return expr int2 = int(arg2) - if (int2 + 1) & int2 != 0: - return expr - mask_size = int2.bit_length() + 7 // 8 + mask_size = (int2.bit_length() + 7) // 8 * 8 + if int2 == int(arg1.mask): + return arg1 out = [] + mask_needed = False for offset, arg in arg1.iter_args(): if offset == mask_size: - return ExprCompose(*out).zeroExtend(expr.size) - elif mask_size > offset and mask_size < offset+arg.size and arg.is_int(): - out.append(ExprSlice(arg, 0, mask_size-offset)) - return ExprCompose(*out).zeroExtend(expr.size) + break else: - out.append(arg) - return expr + if offset < mask_size < offset+arg.size: + arg = ExprSlice(arg, 0, mask_size-offset) + + arg_mask = (int(arg.mask) << offset) + if int2 & arg_mask != 0: + out.append(arg) + if int2 & arg_mask != arg_mask: + mask_needed = True + elif mask_size > offset + arg.size: + out.append(ExprInt(0, arg.size)) + + if mask_size <= offset + arg.size: + break + + if len(out) == 0: + return ExprInt(0, expr.size) + else: + result = out[0] if len(out) == 1 else ExprCompose(*out) + if result.size != expr.size: + result = result.zeroExtend(expr.size) + if mask_needed: + result = result & arg2 + return result def simp_bcdadd_cf(_, expr): """bcdadd(const, const) => decimal""" From 2e10c94a96820963d493eed5d23836ca7e0949d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 21:11:44 +0100 Subject: [PATCH 2/6] Revert "simp_compose_and_mask improved" This reverts commit f9d9674e --- miasm/expression/simplifications_common.py | 37 ++++++---------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index 0982f0df8..2ceb507c9 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1753,38 +1753,19 @@ def simp_compose_and_mask(_, expr): if not arg2.is_int(): return expr int2 = int(arg2) - mask_size = (int2.bit_length() + 7) // 8 * 8 - if int2 == int(arg1.mask): - return arg1 + if (int2 + 1) & int2 != 0: + return expr + mask_size = int2.bit_length() + 7 // 8 out = [] - mask_needed = False for offset, arg in arg1.iter_args(): if offset == mask_size: - break + return ExprCompose(*out).zeroExtend(expr.size) + elif mask_size > offset and mask_size < offset+arg.size and arg.is_int(): + out.append(ExprSlice(arg, 0, mask_size-offset)) + return ExprCompose(*out).zeroExtend(expr.size) else: - if offset < mask_size < offset+arg.size: - arg = ExprSlice(arg, 0, mask_size-offset) - - arg_mask = (int(arg.mask) << offset) - if int2 & arg_mask != 0: - out.append(arg) - if int2 & arg_mask != arg_mask: - mask_needed = True - elif mask_size > offset + arg.size: - out.append(ExprInt(0, arg.size)) - - if mask_size <= offset + arg.size: - break - - if len(out) == 0: - return ExprInt(0, expr.size) - else: - result = out[0] if len(out) == 1 else ExprCompose(*out) - if result.size != expr.size: - result = result.zeroExtend(expr.size) - if mask_needed: - result = result & arg2 - return result + out.append(arg) + return expr def simp_bcdadd_cf(_, expr): """bcdadd(const, const) => decimal""" From a43d4a38920ef50808d775569bd5c25c6a580559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 20:31:35 +0100 Subject: [PATCH 3/6] simp_compose_and_mask improved --- miasm/expression/simplifications_common.py | 37 ++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index 2ceb507c9..0982f0df8 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1753,19 +1753,38 @@ def simp_compose_and_mask(_, expr): if not arg2.is_int(): return expr int2 = int(arg2) - if (int2 + 1) & int2 != 0: - return expr - mask_size = int2.bit_length() + 7 // 8 + mask_size = (int2.bit_length() + 7) // 8 * 8 + if int2 == int(arg1.mask): + return arg1 out = [] + mask_needed = False for offset, arg in arg1.iter_args(): if offset == mask_size: - return ExprCompose(*out).zeroExtend(expr.size) - elif mask_size > offset and mask_size < offset+arg.size and arg.is_int(): - out.append(ExprSlice(arg, 0, mask_size-offset)) - return ExprCompose(*out).zeroExtend(expr.size) + break else: - out.append(arg) - return expr + if offset < mask_size < offset+arg.size: + arg = ExprSlice(arg, 0, mask_size-offset) + + arg_mask = (int(arg.mask) << offset) + if int2 & arg_mask != 0: + out.append(arg) + if int2 & arg_mask != arg_mask: + mask_needed = True + elif mask_size > offset + arg.size: + out.append(ExprInt(0, arg.size)) + + if mask_size <= offset + arg.size: + break + + if len(out) == 0: + return ExprInt(0, expr.size) + else: + result = out[0] if len(out) == 1 else ExprCompose(*out) + if result.size != expr.size: + result = result.zeroExtend(expr.size) + if mask_needed: + result = result & arg2 + return result def simp_bcdadd_cf(_, expr): """bcdadd(const, const) => decimal""" From 9a0528bd825e3df68c287078e13e489e730503f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 21:10:34 +0100 Subject: [PATCH 4/6] simp_compose_and_mask update --- miasm/expression/simplifications_common.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index 0982f0df8..0d010d468 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1762,7 +1762,7 @@ def simp_compose_and_mask(_, expr): if offset == mask_size: break else: - if offset < mask_size < offset+arg.size: + if arg.is_int() and offset < mask_size < offset+arg.size: arg = ExprSlice(arg, 0, mask_size-offset) arg_mask = (int(arg.mask) << offset) @@ -1779,9 +1779,15 @@ def simp_compose_and_mask(_, expr): if len(out) == 0: return ExprInt(0, expr.size) else: - result = out[0] if len(out) == 1 else ExprCompose(*out) - if result.size != expr.size: - result = result.zeroExtend(expr.size) + if len(out) == 1: + result = out[0] + if result.size != expr.size: + result = result.zeroExtend(expr.size) + else: + size = sum(arg.size for arg in out) + if size != expr.size: + out.append(ExprInt(0, expr.size - size)) + result = ExprCompose(*out) if mask_needed: result = result & arg2 return result From 1736a6a68b0bd5392a82730f206c93c73f0ce062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 21:23:12 +0100 Subject: [PATCH 5/6] new exprcompose tests --- test/expression/simplifications.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/expression/simplifications.py b/test/expression/simplifications.py index 96ab8a59e..c469d1c2f 100644 --- a/test/expression/simplifications.py +++ b/test/expression/simplifications.py @@ -368,6 +368,19 @@ def check(expr_in, expr_out): ExprInt(0x1, 32), ExprInt(0x0, 32)) ), + (ExprCompose(a[:8],b[:8],c[:8],d[:8]) + & + ExprInt(0xA000B000, 32), + ExprCompose(ExprInt(0,8), b[:8], ExprInt(0,8), d[:8]) & + ExprInt(0xA000B000, 32) + ), + + (ExprCompose(a[:8],b[:8],c[:8],d[:8]) + & + ExprInt(0xFF00FF00, 32), + ExprCompose(ExprInt(0,8), b[:8], ExprInt(0,8), d[:8]) + ), + (ExprCompose(a[:16], b[:16])[8:32], ExprCompose(a[8:16], b[:16])), ((a >> ExprInt(16, 32))[:16], From 9d513c340ff456041373a98cdfba66408d1f502b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilman=20Ko=C3=9F?= Date: Sat, 9 Jan 2021 21:23:34 +0100 Subject: [PATCH 6/6] simp_compose_and_mask bugfix --- miasm/expression/simplifications_common.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index 0d010d468..357ea4eca 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1779,15 +1779,10 @@ def simp_compose_and_mask(_, expr): if len(out) == 0: return ExprInt(0, expr.size) else: - if len(out) == 1: - result = out[0] - if result.size != expr.size: - result = result.zeroExtend(expr.size) - else: - size = sum(arg.size for arg in out) - if size != expr.size: - out.append(ExprInt(0, expr.size - size)) - result = ExprCompose(*out) + size = sum(arg.size for arg in out) + if size != expr.size: + out.append(ExprInt(0, expr.size - size)) + result = ExprCompose(*out) if mask_needed: result = result & arg2 return result