Skip to content

Commit

Permalink
crc: Fix for small buffer readover in iscsi crc
Browse files Browse the repository at this point in the history
Change-Id: Ib4d7e2c6838d490a539a0174b8eb128e4fb49bba
Signed-off-by: Greg Tucker <greg.b.tucker@intel.com>
  • Loading branch information
gbtucker committed Aug 17, 2018
1 parent bad3a0a commit 105eeb9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
10 changes: 8 additions & 2 deletions crc/crc32_funcs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif

#define MAX_BUF 512
#define TEST_SIZE 20
#define TEST_SIZE 32

typedef uint32_t(*crc32_func_t) (uint32_t, const uint8_t *, uint64_t);
typedef uint32_t(*crc32_func_t_base) (uint32_t, uint8_t *, uint64_t);
Expand Down Expand Up @@ -248,9 +248,15 @@ int eob_test(func_case_t * test_func)
int i;
unsigned char *buf = NULL;

// Null test
if (0 != test_func->crc32_func_call(0, NULL, 0)) {
fail++;
printf("crc null test fail\n");
}

buf = (unsigned char *)buf_alloc; //reset buf
buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end
for (i = 0; i < TEST_SIZE; i++) {
for (i = 0; i <= TEST_SIZE; i++) {
crc = test_func->crc32_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
crc_ref = test_func->crc32_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
if (crc != crc_ref)
Expand Down
18 changes: 9 additions & 9 deletions crc/crc32_iscsi_00.asm
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ crc32_iscsi_00:

mov rax, crc_init ;; rax = crc_init;

cmp len, 8
jb less_than_8

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1) ALIGN: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand All @@ -203,9 +206,6 @@ crc32_iscsi_00:
;; amount of the address
je proc_block ;; Skip if aligned

cmp len, 8
jb less_than_8

;;;; Calculate CRC of unaligned bytes of the buffer (if any) ;;;;
mov rbx, [bufptmp] ;; load a quadword from the buffer
add bufptmp, bufp ;; align buffer pointer for
Expand Down Expand Up @@ -312,19 +312,19 @@ do_return:
less_than_8:
test len,4
jz less_than_4
crc32 eax, dword[bufptmp]
add bufptmp,4
crc32 eax, dword[bufp]
add bufp,4
less_than_4:
test len,2
jz less_than_2
crc32 eax, word[bufptmp]
add bufptmp,2
crc32 eax, word[bufp]
add bufp,2
less_than_2:
test len,1
jz do_return
crc32 rax, byte[bufptmp]
crc32 rax, byte[bufp]
pop rbx
pop bufptmp
pop bufp
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
20 changes: 10 additions & 10 deletions crc/crc32_iscsi_01.asm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ crc32_iscsi_01:
mov crc_init, crc_init_arg
%endif

;; If len is less than 8 we need to jump to special code to avoid
;; reading beyond the end of the buffer
cmp len, 8
jb less_than_8

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1) ALIGN: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand All @@ -108,11 +113,6 @@ crc32_iscsi_01:
;; the address
je proc_block ;; Skip if aligned

;; If len is less than 8 and we're unaligned, we need to jump
;; to special code to avoid reading beyond the end of the buffer
cmp len, 8
jb less_than_8

;;;; Calculate CRC of unaligned bytes of the buffer (if any) ;;;
mov tmp, [bufptmp] ;; load a quadword from the buffer
add bufptmp, bufp ;; align buffer pointer for quadword
Expand Down Expand Up @@ -334,17 +334,17 @@ do_16:
less_than_8:
test len,4
jz less_than_4
crc32 crc_init_dw, dword[bufptmp]
add bufptmp,4
crc32 crc_init_dw, dword[bufp]
add bufp,4
less_than_4:
test len,2
jz less_than_2
crc32 crc_init_dw, word[bufptmp]
add bufptmp,2
crc32 crc_init_dw, word[bufp]
add bufp,2
less_than_2:
test len,1
jz do_return
crc32 crc_init_dw, byte[bufptmp]
crc32 crc_init_dw, byte[bufp]
mov rax, crc_init
pop rsi
pop rdi
Expand Down
10 changes: 8 additions & 2 deletions crc/crc64_funcs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif

#define MAX_BUF 512
#define TEST_SIZE 20
#define TEST_SIZE 32

typedef uint64_t u64;
typedef uint32_t u32;
Expand Down Expand Up @@ -243,9 +243,15 @@ int eob_test(func_case_t * test_func)
int i;
unsigned char *buf = NULL;

// Null test
if (0 != test_func->crc64_func_call(0, NULL, 0)) {
fail++;
printf("crc null test fail\n");
}

buf = (unsigned char *)buf_alloc; //reset buf
buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end
for (i = 0; i < TEST_SIZE; i++) {
for (i = 0; i <= TEST_SIZE; i++) {
crc = test_func->crc64_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
crc_ref = test_func->crc64_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
if (crc != crc_ref)
Expand Down

0 comments on commit 105eeb9

Please sign in to comment.