Skip to content

Extend array index rewrite to fields#120

Merged
Medowhill merged 4 commits into
Yale-PROCTOR:masterfrom
presenthee:array-index-rewrite
Jun 1, 2026
Merged

Extend array index rewrite to fields#120
Medowhill merged 4 commits into
Yale-PROCTOR:masterfrom
presenthee:array-index-rewrite

Conversation

@presenthee

@presenthee presenthee commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Related #112

Summary

Handles two limitations of above:

  • Currently rewrites only if there is a source variable for the base pointer. To rewrite a broader range of pointers, selection criteria should be improved.
  • Currently rewriter only rewrites if base pointer offset is not zero, so some target groups are selected but not rewritten yet.

Changes

  • Extend index rewriting to struct field base pointers. (e.g. (*param).field)
  • To restrict rewrites which may increase unsafe feature counts, add gates to filter rewrite selections.

Test

Regression test compared to master

feature master array-local-rewrite delta
from_ptr 57 58 +1
from_raw_parts_mut 7530 7528 -2
offset 2698 2695 -3
test case differences with delta
Public-Tests/B01_organic/flip_horizontal_lib from_raw_parts_mut: 1 -> 0 (-1), offset: 2 -> 4 (+2)
Public-Tests/B02_organic/cJSON_lib from_ptr: 2 -> 3 (+1), from_raw_parts: 50 -> 49 (-1)
Public-Tests/B02_organic/hm_geti_lib offset: 174 -> 167 (-7)
Public-Tests/B02_organic/load_png_mem_lib offset: 60 -> 63 (+3)
Public-Tests/B02_organic/pinflate_lib offset: 13 -> 16 (+3)
Public-Tests/B02_organic/sh_geti_lib offset: 147 -> 140 (-7)
Public-Tests/B02_synthetic/arrayfunc_lib offset_from: 1 -> 0 (-1)
Public-Tests/B02_synthetic/confusion_lib from_raw_parts: 0 -> 1 (+1), from_raw_parts_mut: 1 -> 0 (-1), offset: 0 -> 3 (+3), offset_from: 1 -> 2 (+1)

Rewritten test cases (including base pointers stored in fields)

Public-Tests/B01_organic/flip_horizontal_lib
Public-Tests/B01_organic/wcscat_lib
Public-Tests/B02_organic/cJSON_lib
Public-Tests/B02_organic/helxo_lib
Public-Tests/B02_organic/hm_geti_lib
Public-Tests/B02_organic/intput_lib
Public-Tests/B02_organic/load_png_mem_lib
Public-Tests/B02_organic/pinflate_lib
Public-Tests/B02_organic/sh_geti_lib
Public-Tests/B02_organic/sh_puts_lib
Public-Tests/B02_organic/str_dups_lib
Public-Tests/B02_organic/str_put_lib
Public-Tests/B02_synthetic/arrayfunc_lib
Public-Tests/B02_synthetic/confusion_lib
Public-Tests/B02_synthetic/dataentry_lib
Public-Tests/B02_synthetic/memchra2_lib
Public-Tests/P01_sphincs_plus/005_* through Public-Tests/P01_sphincs_plus/132_*

Example transformation

// pinflate_lib
// Before
                let mut src: *const i8 =
                    __crat_borrowed_s.out.offset(-(backwards_distance as
                                    isize));
                let mut dst: *mut i8 = __crat_borrowed_s.out;
                __crat_borrowed_s.out =
                    __crat_borrowed_s.out.offset(length as isize);
                   ...
                    _ => {
                        loop {
                            let fresh0 = length;
                            length = length - 1;
                            if !(fresh0 != 0) { break; }
                            let fresh1 = *src;
                            src = src.offset(1);
                            *dst = fresh1;
                            dst = dst.offset(1);

// After
                let mut src_idx: isize =
                    out_idx + (-(backwards_distance as isize));
                let mut dst_idx: isize = out_idx;
                out_idx = out_idx + (length as isize);
                ...
                    _ => {
                     loop {
                            let fresh0 = length;
                            length = length - 1;
                            if !(fresh0 != 0) { break; }
                            let fresh1 =
                                *(__crat_borrowed_s.out.offset(src_idx) as *mut i8);
                            src_idx = src_idx + 1;
                            *(__crat_borrowed_s.out.offset(dst_idx) as *mut i8) =
                                fresh1;
                            dst_idx = dst_idx + 1;
                        }

@Medowhill Medowhill merged commit 257ad18 into Yale-PROCTOR:master Jun 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants