From 90540c2d5ace46a1e9789c75fde0b1f7dbb12a9b Mon Sep 17 00:00:00 2001 From: Mans Rullgard Date: Mon, 13 Aug 2012 00:53:05 +0100 Subject: x86: swscale: fix fragile memory accesses To access data at multiple fixed offsets from a base address, this code uses a single "m" operand and code of the form "32%0", relying on the memory operand instantiation having no displacement, giving a final result of the form "32(%rax)". If the compiler uses a register and displacement, e.g. "64(%rax)", the end result becomes "3264(%rax)", which obviously does not work. Replacing the "m" operands with "r" operands allows safe addition of a displacement. In theory, multiple memory operands could use a shared base register with different index registers, "(%rax,%rbx)", potentially making more efficient use of registers. In the cases at hand, no such sharing is possible since the addresses involved are entirely unrelated. After this change, the code somewhat rudely accesses memory without using a corresponding memory operand, which in some cases can lead to unwanted "optimisations" of surrounding code. However, the original code also accesses memory not covered by a memory operand, so this is not adding any defect not already present. It is also hightly unlikely that any such optimisations could be performed here since the memory locations in questions are not accessed elsewhere in the same functions. This fixes crashes with suncc. Signed-off-by: Mans Rullgard --- libswscale/x86/rgb2rgb_template.c | 284 ++++++++++++++++++-------------------- 1 file changed, 137 insertions(+), 147 deletions(-) (limited to 'libswscale/x86') diff --git a/libswscale/x86/rgb2rgb_template.c b/libswscale/x86/rgb2rgb_template.c index 0f2bfd0581..7a641e1814 100644 --- a/libswscale/x86/rgb2rgb_template.c +++ b/libswscale/x86/rgb2rgb_template.c @@ -73,25 +73,24 @@ static inline void RENAME(rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int sr __asm__ volatile("movq %0, %%mm7"::"m"(mask32a):"memory"); while (s < mm_end) { __asm__ volatile( - PREFETCH" 32%1 \n\t" - "movd %1, %%mm0 \n\t" - "punpckldq 3%1, %%mm0 \n\t" - "movd 6%1, %%mm1 \n\t" - "punpckldq 9%1, %%mm1 \n\t" - "movd 12%1, %%mm2 \n\t" - "punpckldq 15%1, %%mm2 \n\t" - "movd 18%1, %%mm3 \n\t" - "punpckldq 21%1, %%mm3 \n\t" + PREFETCH" 32(%1) \n\t" + "movd (%1), %%mm0 \n\t" + "punpckldq 3(%1), %%mm0 \n\t" + "movd 6(%1), %%mm1 \n\t" + "punpckldq 9(%1), %%mm1 \n\t" + "movd 12(%1), %%mm2 \n\t" + "punpckldq 15(%1), %%mm2 \n\t" + "movd 18(%1), %%mm3 \n\t" + "punpckldq 21(%1), %%mm3 \n\t" "por %%mm7, %%mm0 \n\t" "por %%mm7, %%mm1 \n\t" "por %%mm7, %%mm2 \n\t" "por %%mm7, %%mm3 \n\t" - MOVNTQ" %%mm0, %0 \n\t" - MOVNTQ" %%mm1, 8%0 \n\t" - MOVNTQ" %%mm2, 16%0 \n\t" - MOVNTQ" %%mm3, 24%0" - :"=m"(*dest) - :"m"(*s) + MOVNTQ" %%mm0, (%0) \n\t" + MOVNTQ" %%mm1, 8(%0) \n\t" + MOVNTQ" %%mm2, 16(%0) \n\t" + MOVNTQ" %%mm3, 24(%0)" + :: "r"(dest), "r"(s) :"memory"); dest += 32; s += 24; @@ -138,9 +137,9 @@ static inline void RENAME(rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int sr "pand "MANGLE(mask24hhhh)", %%mm5\n\t" \ "por %%mm5, %%mm4 \n\t" \ \ - MOVNTQ" %%mm0, %0 \n\t" \ - MOVNTQ" %%mm1, 8%0 \n\t" \ - MOVNTQ" %%mm4, 16%0" + MOVNTQ" %%mm0, (%0) \n\t" \ + MOVNTQ" %%mm1, 8(%0) \n\t" \ + MOVNTQ" %%mm4, 16(%0)" static inline void RENAME(rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int src_size) @@ -154,18 +153,17 @@ static inline void RENAME(rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int sr mm_end = end - 31; while (s < mm_end) { __asm__ volatile( - PREFETCH" 32%1 \n\t" - "movq %1, %%mm0 \n\t" - "movq 8%1, %%mm1 \n\t" - "movq 16%1, %%mm4 \n\t" - "movq 24%1, %%mm5 \n\t" + PREFETCH" 32(%1) \n\t" + "movq (%1), %%mm0 \n\t" + "movq 8(%1), %%mm1 \n\t" + "movq 16(%1), %%mm4 \n\t" + "movq 24(%1), %%mm5 \n\t" "movq %%mm0, %%mm2 \n\t" "movq %%mm1, %%mm3 \n\t" "movq %%mm4, %%mm6 \n\t" "movq %%mm5, %%mm7 \n\t" STORE_BGR24_MMX - :"=m"(*dest) - :"m"(*s) + :: "r"(dest), "r"(s) :"memory"); dest += 24; s += 32; @@ -198,19 +196,18 @@ static inline void RENAME(rgb15to16)(const uint8_t *src, uint8_t *dst, int src_s mm_end = end - 15; while (s