Skip to content

Imgproc: integral OOB access (2020-03-01) #16708

Closed

Description

@alalek

Relates #16556

Valgrind SSE (nightly):

[ RUN      ] Imgproc_Integral.accuracy
==13847== Invalid read of size 16
==13847==    at 0x5695276: _mm_blendv_epi8 (smmintrin.h:181)
==13847==    by 0x5695276: v_load_deinterleave (intrin_sse.hpp:2089)
==13847==    by 0x5695276: operator() (sumpixels.simd.hpp:243)
==13847==    by 0x5695276: cv::hal::cpu_baseline::integral_SIMD(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.simd.hpp:1116)
==13847==    by 0x5696A94: cv::hal::integral_SIMD(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.dispatch.cpp:361)
==13847==    by 0x5696C01: cv::hal::integral(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.dispatch.cpp:378)
==13847==    by 0x569BD53: cv::integral(cv::_InputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, int, int) (sumpixels.dispatch.cpp:445)
==13847==    by 0x569D7D4: cvIntegral (sumpixels.dispatch.cpp:488)

AVX2:

[ RUN      ] Size_MatType_OutMatDepth_integral.integral/6, where GetParam() = (640x480, 8UC3, CV_32S)
==46316== Invalid read of size 16
==46316==    at 0x5208CE4: _mm256_loadu_si256 (avxintrin.h:921)
==46316==    by 0x5208CE4: cv::hal_AVX2::v_load_deinterleave(unsigned char const*, cv::hal_AVX2::v_uint8x32&, cv::hal_AVX2::v_uint8x32&, cv::hal_AVX2::v_uint8x32&) (intrin_avx.hpp:2391)
==46316==    by 0x531CBF3: cv::hal::opt_AVX2::(anonymous namespace)::Integral_SIMD<unsigned char, int, double>::operator()(unsigned char const*, unsigned long, int*, unsigned long, double*, unsigned long, int*, unsigned long, int, int, int) const (sumpixels.simd.hpp:243)
==46316==    by 0x5324374: cv::hal::opt_AVX2::integral_SIMD(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.simd.hpp:1116)
==46316==    by 0x50B6418: cv::hal::integral_SIMD(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.dispatch.cpp:361)
==46316==    by 0x50B662E: cv::hal::integral(int, int, int, unsigned char const*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, unsigned char*, unsigned long, int, int, int) (sumpixels.dispatch.cpp:378)
Details

MacOSX:

[ RUN      ] Size_MatType_OutMatDepth_integral.integral/6, where GetParam() = (640x480, 8UC3, CV_32S)
Process returned: -11

Win64:

[ RUN      ] OCL_ImgSize_TmplSize_Method_MatType_MatchTemplate.MatchTemplate/89, where GetParam() = (1280x1024, 11x11, TM_CCOEFF, 8UC3)
C:\build\3_4_noICV-win64-vc14\opencv\modules\ts\src\ts_perf.cpp(2038): error: Failed
Expected: PerfTestBody() doesn't throw an exception.
  Actual: it throws...
params    = (1280x1024, 11x11, TM_CCOEFF, 8UC3)
termination reason:  unhandled exception
bytesIn   =          0
bytesOut  =          0
samples   =          0 of 1
outliers  =          0
frequency =          0
[  FAILED  ] OCL_ImgSize_TmplSize_Method_MatType_MatchTemplate.MatchTemplate/89, where GetParam() = (1280x1024, 11x11, TM_CCOEFF, 8UC3) (32 ms)

[ RUN      ] OCL_ImgSize_TmplSize_Method_MatType_MatchTemplate.MatchTemplate/113, where GetParam() = (1280x1024, 16x16, TM_CCOEFF, 8UC3)
C:\build\3_4_noICV-win64-vc14\opencv\modules\ts\src\ts_perf.cpp(2038): error: Failed
Expected: PerfTestBody() doesn't throw an exception.
  Actual: it throws...
params    = (1280x1024, 16x16, TM_CCOEFF, 8UC3)
termination reason:  unhandled exception
bytesIn   =          0
bytesOut  =          0
samples   =          0 of 1
outliers  =          0
frequency =          0
[  FAILED  ] OCL_ImgSize_TmplSize_Method_MatType_MatchTemplate.MatchTemplate/113, where GetParam() = (1280x1024, 16x16, TM_CCOEFF, 8UC3) (47 ms)

Activity

  1. ChipKerchner commented on Mar 2, 2020

    @ChipKerchner
    Contributor

    @alalek, I think this is the problem. It is loading a whole vector (x3) but only using the lower half.

                    for ( ; j + v_uint16::nlanes * cn <= width; j += v_uint16::nlanes * cn)
                    {
                        v_uint8 v_src_row_1, v_src_row_2, v_src_row_3;
                        v_load_deinterleave(src_row + j, v_src_row_1, v_src_row_2, v_src_row_3);
                        v_int16 el8_1 = v_reinterpret_as_s16(v_expand_low(v_src_row_1));
                        v_int16 el8_2 = v_reinterpret_as_s16(v_expand_low(v_src_row_2));
                        v_int16 el8_3 = v_reinterpret_as_s16(v_expand_low(v_src_row_3));
    

    It is only an issue for 3-channels (line 240, 549 and 899 of sumpixels.simd.hpp)

    I think the solution is to change one line in each of 8UC3->S32/F32/F64 to

                    for ( ; j + v_uint16::nlanes * cn * 2 <= width; j += v_uint16::nlanes * cn)
    

    Currently I'm not set up for valgrind testing. If you could test these changes, it would be a lot easier.

  2. alalek commented on Mar 4, 2020

    @alalek
    MemberAuthor

    I have adjusted loop boundaries #16731, but looks like tail processing doesn't work well (some checks are failed - need to investigate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions