Skip to content

erasure_code/aarch64: limit SVE dot product table loads to 16 bytes - #479

Open
Shakhtar-Sankur wants to merge 3 commits into
intel:masterfrom
Shakhtar-Sankur:sve-dot-prod-table-overread
Open

Shakhtar-Sankur wants to merge 3 commits into
intel:masterfrom
Shakhtar-Sankur:sve-dot-prod-table-overread

Conversation

@Shakhtar-Sankur

Copy link
Copy Markdown

Fixes #476

gf_nvect_dot_prod_sve_unrolled() loads the two 16-byte halves of each 32-byte coefficient table with svptrue_b8(), so each load reads a full SVE vector. svtbl_u8() only indexes lanes 0-15, so the extra bytes are never used, but for the last coefficient the second load reads VL/8 - 16 bytes past the end of gftbls: 16 bytes at 256-bit VL (Neoverse V1), 48 at 512-bit (A64FX), up to 240 at 2048-bit. When the table ends at the end of a mapping, ec_encode_data() and gf_vect_dot_prod() fault. All of the gf_*vect_dot_prod_sve and _sve2 kernels are built from this function.

The fix loads the table halves with a 16-lane predicate (svwhilelt_b8_s32(0, 16)). Inactive lanes are not read and svtbl_u8() never uses them, so the results are unchanged.

As suggested in the issue, erasure_code_test now also runs ec_encode_data() and gf_vect_dot_prod() with a table of exactly 32 * k * rows bytes that ends at an inaccessible guard page (k from 1 to 32, 1 to 10 rows), and compares the parity with ec_encode_data_base(). It follows the guard-page test in mem/mem_zero_detect_test.c, including the Windows path.

Testing: cross-compiled with GCC 14 for aarch64 and run under qemu-aarch64 8.2 user mode.

CPU master + new test this PR
max, SVE2, VL 128 pass pass
max, SVE2, VL 256 SIGSEGV pass
max, SVE2, VL 512 SIGSEGV pass
max, SVE2, VL 2048 SIGSEGV pass
neoverse-v1 (SVE, 256) SIGSEGV pass
a64fx (SVE, 512) SIGSEGV pass
max, sve=off (NEON) pass pass

The reproducer from #476 gives the same pattern. With this PR, erasure_code_test, erasure_code_update_test, gf_vect_mul_test, gf_inverse_test and gf_vect_dot_prod_test pass on all seven CPU configurations, and native x86-64 make check passes (18/18). The changed files are clean with clang-format 18 (the repo's .clang-format).

gf_nvect_dot_prod_sve_unrolled() loads the two 16-byte halves of each
32-byte coefficient table with svptrue_b8(), so each load reads a full
SVE vector. svtbl_u8() only indexes lanes 0-15, so the extra bytes are
never used, but for the last coefficient the second load reads VL/8 - 16
bytes past the end of gftbls: 16 bytes at 256-bit VL (Neoverse V1), 48
at 512-bit (A64FX), up to 240 at 2048-bit. If the table ends at the end
of a mapping, ec_encode_data() and gf_vect_dot_prod() fault. All of the
gf_*vect_dot_prod_sve and _sve2 kernels are built from this function.

Load the table halves with a 16-lane predicate instead. Inactive lanes
are not read, and svtbl_u8() never uses them, so the results are
unchanged.

Add a check to erasure_code_test that runs ec_encode_data() and
gf_vect_dot_prod() with a table of exactly 32 * k * rows bytes that ends
at an inaccessible guard page, and compares the parity with
ec_encode_data_base().

Fixes intel#476

Signed-off-by: Sankur Kundu <sankur.kundu.tw@gmail.com>
@liuqinfei

Copy link
Copy Markdown

The kernel fix looks correct.

Please fix two test issues before merging:

  1. The helper assumes 42 buffers, but TEST_SOURCES is configurable. With TEST_SOURCES=32, the previous test passes and the new test aborts; disabling assertions leaves out-of-bounds accesses. Please allocate dedicated buffers or limit the combinations to the available capacity.
  2. gf_vect_dot_prod() reuses output already computed by ec_encode_data(), so even a no-op passes the comparison. Please invalidate the destination before the call, for example by filling it with the reference bytes XOR 0xff.

The guard page test used up to 42 of the shared test buffers, which
breaks when TEST_SOURCES is set lower. Allocate its own buffers instead.

Also check the reference against tables from ec_init_tables_base(), since
ec_init_tables() produces a different format on GFNI capable CPUs, and
overwrite the output before calling gf_vect_dot_prod() so the check cannot
pass on the data written by ec_encode_data().

Signed-off-by: Sankur Kundu <sankur.kundu.tw@gmail.com>
@Shakhtar-Sankur

Copy link
Copy Markdown
Author

Thanks @liuqinfei, both fixed in d0a142e:

  1. The guard page test now allocates its own buffers (32 sources, 10 outputs and 10 references), so it no longer depends on TEST_SOURCES. With TEST_SOURCES=32 it passes, also with assertions disabled under AddressSanitizer.
  2. The output is filled with the reference bytes XOR 0xff before gf_vect_dot_prod(). With gf_vect_dot_prod() replaced by a no-op, the test now fails.

While testing on an x86 CPU with GFNI I also found that the reference was computed with ec_encode_data_base() on tables from ec_init_tables(), which uses the GFNI table format there, so the test failed on GFNI machines. The reference now uses its own tables from ec_init_tables_base().

Under qemu, the test still segfaults on the guard page without the kernel fix at SVE vector lengths of 256, 512 and 2048 bits (and on neoverse-v1 and a64fx), and passes with it, including 128-bit SVE and NEON only.

@liuqinfei

Copy link
Copy Markdown

Thanks, d0a142e addresses both requested test fixes, and the separate base-format reference tables are correct. I rechecked TEST_SOURCES=32, all behave as expected. The kernel fix still looks good.

One remaining test issue: the new source/output buffers use malloc(), which does not guarantee the 64-byte alignment used by the original test. The supported EC_ALIGNED_ADDR configurations in tools/test_extended.sh use aligned AVX2/AVX-512 loads and stores, so these buffers may cause alignment faults.

Please allocate buffs[] with posix_memalign(..., 64, len) and release them with aligned_free(), using the existing portable helpers in test.h.

With EC_ALIGNED_ADDR the encode functions use aligned loads and stores,
so allocate the guard page test buffers with posix_memalign() like the
rest of the test, and release them with aligned_free().

Signed-off-by: Sankur Kundu <sankur.kundu.tw@gmail.com>
@Shakhtar-Sankur

Copy link
Copy Markdown
Author

Thanks @liuqinfei, done in 8c87417: the guard page test buffers are now allocated with posix_memalign(..., 64, len) and released with aligned_free().

With the library built with D="EC_ALIGNED_ADDR", the guard page test on its own segfaults with the previous malloc() buffers and passes now (x86-64 with AVX-512 and GFNI). The aarch64 SVE results are unchanged.

For information: in the same EC_ALIGNED_ADDR build on that machine, erasure_code_test on master (af6a939) also segfaults, earlier, in the efence encode loop (vmovntdqa in the AVX-512 kernel on 16-byte aligned efence buffers). That is separate from this PR, so I haven't changed it here.

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.

aarch64 SVE: gf_nvect_dot_prod_sve reads VL−16 bytes past the end of gftbls (SIGSEGV in ec_encode_data at VL > 128)

2 participants