[GH-ISSUE #1735] Large alignments on stack (eg. AVX2) #1149

Closed
opened 2026-10-14 03:36:38 +07:00 by bsakaguchi · 17 comments
Owner

Originally created by @magnumripper on GitHub (Sep 6, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1735

Originally assigned to: @magnumripper on GitHub.

See also #1731 and http://www.openwall.com/lists/john-dev/2015/09/06/5

Perhaps write an autoconf test that can safely detect whether large stack alignments (i.e. MEM_ALIGN_SIMD for AVX2 and above) work, and if so set SAFE_ALIGN_SIMD or whatever macro. This means we can optimize things currently always using mem_align(), especially in pseudo_intrinsics.h and simd-intrinsics.c.

Actually we could already set that macro for any build where sizeof(vtype) <= 16.

Originally created by @magnumripper on GitHub (Sep 6, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1735 Originally assigned to: @magnumripper on GitHub. See also #1731 and http://www.openwall.com/lists/john-dev/2015/09/06/5 Perhaps write an autoconf test that can safely detect whether large stack alignments (i.e. MEM_ALIGN_SIMD for AVX2 and above) work, and if so set SAFE_ALIGN_SIMD or whatever macro. This means we can optimize things currently always using `mem_align()`, especially in pseudo_intrinsics.h and simd-intrinsics.c. Actually we could already set that macro for any build where sizeof(vtype) <= 16.
bsakaguchi 2026-10-14 03:36:38 +07:00
Author
Owner

@magnumripper commented on GitHub (Sep 6, 2015):

The AC test need to be very thorough though. I think we've sometimes seen problems surface only under OMP.

<!-- gh-comment-id:138089095 --> @magnumripper commented on GitHub (Sep 6, 2015): The AC test need to be very thorough though. I think we've sometimes seen problems surface only under OMP.
Author
Owner

@jfoug commented on GitHub (Sep 6, 2015):

This was posted as a line comment

github.com/magnumripper/JohnTheRipper@1bd8d9dc2c (commitcomment-13086395)

This is the way to go, not to probe.

#define mem_align(a,b) (void*)((a)+(((b)-1)-(((size_t)(a)-1)&((b)-1))))

However, I have to get it working being integrated into john's code. I have stand alone samples showing it is 100% correct. But I am having issues with john..

What this does is simply add the appropriate value to the memory pointer, based upon it's address. All done without writing anything to any memory location or calling a function.

<!-- gh-comment-id:138091482 --> @jfoug commented on GitHub (Sep 6, 2015): This was posted as a line comment https://github.com/magnumripper/JohnTheRipper/commit/1bd8d9dc2ccad29db96f424a89e3a51153972fb5#commitcomment-13086395 This is the way to go, not to probe. ``` C #define mem_align(a,b) (void*)((a)+(((b)-1)-(((size_t)(a)-1)&((b)-1)))) ``` However, I have to get it working being integrated into john's code. I have stand alone samples showing it is 100% correct. But I am having issues with john.. What this does is simply add the appropriate value to the memory pointer, based upon it's address. All done without writing anything to any memory location or calling a function.
Author
Owner

@jfoug commented on GitHub (Sep 6, 2015):

Think I found the problem. There was a #undef mem_align at bottom of pseudo_intrinsics I missed.

That was it. Building now, and no warnings. That #undef was causing my define to get lost for any SIMD format.

Let me get build done, test it, and check in. Once done, this should close this issue once and for all. For aligned stack values, we build a buffer large enough + the SIMD size (or whatever alignment we are using), then use mem_align(a,b) like always, but it will simply be a simple mathematical adjustment to the pointer, to move it to the next properly aligned location (which may be the actual original starting pointer).

No need to probe or anything else, and have things get missed.

<!-- gh-comment-id:138092398 --> @jfoug commented on GitHub (Sep 6, 2015): Think I found the problem. There was a #undef mem_align at bottom of pseudo_intrinsics I missed. That was it. Building now, and no warnings. That #undef was causing my define to get lost for any SIMD format. Let me get build done, test it, and check in. Once done, this should close this issue once and for all. For aligned stack values, we build a buffer large enough + the SIMD size (or whatever alignment we are using), then use mem_align(a,b) like always, but it will simply be a simple mathematical adjustment to the pointer, to move it to the next properly aligned location (which may be the actual original starting pointer). No need to probe or anything else, and have things get missed.
Author
Owner

@jfoug commented on GitHub (Sep 6, 2015):

Think I found the problem, I was getting crashes. I have to typecast a to unsigned char before doing the -1

Finally got it (glad I wrote a 'test' app, lol)

<!-- gh-comment-id:138095536 --> @jfoug commented on GitHub (Sep 6, 2015): Think I found the problem, I was getting crashes. I have to typecast a to unsigned char before doing the -1 Finally got it (glad I wrote a 'test' app, lol)
Author
Owner

@jfoug commented on GitHub (Sep 6, 2015):

here is the test:

#include <stdio.h>
void *mem_align(void *a, int b) {
    char *al = (char*)a;
    al += (b-1);
    al -= (size_t)al&(b-1);
    return al;
}
#define mem_align_define(a,b) (void*)( ((char*)(a))+( ((b)-1) - (((size_t)((char*)(a))-1)&((b)-1))))

int test1(), test2(), test3();
int main() {
    if (test1() && test2() && test3())
        printf ("success\n");
}
int test1() {
    unsigned char buf[256];
    int i;
    for (i = 0; i < 16; ++i) {
        if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 32; ++i) {
        if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 64; ++i) {
        if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    return 1;
}
int test2() {
    unsigned long buf[256];
    int i;
    for (i = 0; i < 16; ++i) {
        if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 32; ++i) {
        if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 64; ++i) {
        if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    return 1;
}
int test3() {
    long double buf[256];
    int i;
    for (i = 0; i < 16; ++i) {
        if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 32; ++i) {
        if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    for (i = 0; i < 64; ++i) {
        if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) {
            printf ("Failure\n");
            return 0;
        }
    }
    return 1;
}

It tests different sized buffers, testing each for 16, 32 and 64 byte offsets. This test was against the functional version.

<!-- gh-comment-id:138097329 --> @jfoug commented on GitHub (Sep 6, 2015): here is the test: ``` #include <stdio.h> void *mem_align(void *a, int b) { char *al = (char*)a; al += (b-1); al -= (size_t)al&(b-1); return al; } #define mem_align_define(a,b) (void*)( ((char*)(a))+( ((b)-1) - (((size_t)((char*)(a))-1)&((b)-1)))) int test1(), test2(), test3(); int main() { if (test1() && test2() && test3()) printf ("success\n"); } int test1() { unsigned char buf[256]; int i; for (i = 0; i < 16; ++i) { if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 32; ++i) { if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 64; ++i) { if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } return 1; } int test2() { unsigned long buf[256]; int i; for (i = 0; i < 16; ++i) { if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 32; ++i) { if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 64; ++i) { if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } return 1; } int test3() { long double buf[256]; int i; for (i = 0; i < 16; ++i) { if (mem_align(&buf[i], 16)-(void*)&buf[i] != mem_align_define(&buf[i], 16)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 32; ++i) { if (mem_align(&buf[i], 32)-(void*)&buf[i] != mem_align_define(&buf[i], 32)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } for (i = 0; i < 64; ++i) { if (mem_align(&buf[i], 64)-(void*)&buf[i] != mem_align_define(&buf[i], 64)-(void*)&buf[i]) { printf ("Failure\n"); return 0; } } return 1; } ``` It tests different sized buffers, testing each for 16, 32 and 64 byte offsets. This test was against the functional version.
Author
Owner

@jfoug commented on GitHub (Sep 6, 2015):

Checked in here. This issue should now be moot.

<!-- gh-comment-id:138098881 --> @jfoug commented on GitHub (Sep 6, 2015): Checked in here. This issue should now be moot.
Author
Owner

@magnumripper commented on GitHub (Sep 7, 2015):

The macro is much better.
Solar described more problems though: http://www.openwall.com/lists/john-dev/2015/09/06/5

Re-opening for evaluation.

<!-- gh-comment-id:138218305 --> @magnumripper commented on GitHub (Sep 7, 2015): The macro is much better. Solar described more problems though: http://www.openwall.com/lists/john-dev/2015/09/06/5 Re-opening for evaluation.
Author
Owner

@jfoug commented on GitHub (Sep 7, 2015):

If you dig more into that thread, Solar and I did talk about bit more about some of these issues. Put no code into play, but there is more ideas bouncing around, good and bad.

<!-- gh-comment-id:138227433 --> @jfoug commented on GitHub (Sep 7, 2015): If you dig more into that thread, Solar and I did talk about bit more about some of these issues. Put no code into play, but there is more ideas bouncing around, good and bad.
Author
Owner

@magnumripper commented on GitHub (Sep 7, 2015):

What you suggested in http://www.openwall.com/lists/john-dev/2015/09/06/9 is pretty slick. I'll experiment a little with that.

I wonder though, why we've only seen probems for real in a few formats and only under OpenMP. As far as I know we've never seen problems with explicit vector declarations that ends up in memory due to register spill. Maybe gcc actually handles that properly. If we can confirm that, we can use it:

Instead of:

        char _b[8 * 64 * MD5_SSE_NUM_KEYS + MEM_ALIGN_SIMD] = { 0 };
        unsigned char (*buffers)[64*MD5_SSE_NUM_KEYS] =
                mem_align(_b, MEM_ALIGN_SIMD);

We'd just do:

        vtype _b[8 * 64 * MD5_SSE_NUM_KEYS / sizeof(vtype)] = { 0 };
        unsigned char (*buffers)[64*MD5_SSE_NUM_KEYS] = (unsigned char*)_b;
<!-- gh-comment-id:138376861 --> @magnumripper commented on GitHub (Sep 7, 2015): What you suggested in http://www.openwall.com/lists/john-dev/2015/09/06/9 is pretty slick. I'll experiment a little with that. I wonder though, why we've only seen probems for real in a few formats and only under OpenMP. As far as I know we've **never** seen problems with explicit vector declarations that ends up in memory due to register spill. Maybe gcc actually handles that properly. If we can confirm that, we can use it: Instead of: ``` c char _b[8 * 64 * MD5_SSE_NUM_KEYS + MEM_ALIGN_SIMD] = { 0 }; unsigned char (*buffers)[64*MD5_SSE_NUM_KEYS] = mem_align(_b, MEM_ALIGN_SIMD); ``` We'd just do: ``` c vtype _b[8 * 64 * MD5_SSE_NUM_KEYS / sizeof(vtype)] = { 0 }; unsigned char (*buffers)[64*MD5_SSE_NUM_KEYS] = (unsigned char*)_b; ```
Author
Owner

@magnumripper commented on GitHub (Sep 8, 2015):

This might be part of a canonical solution:

diff --git a/src/configure.ac b/src/configure.ac
index 0f27922..f45cd56 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -225,6 +225,8 @@ if test "x$asan" = xyes || test "x$ubsan" = xyes ; then
 else
    JTR_FLAG_CHECK([-fomit-frame-pointer], 1)
 fi
+   # AVX2 alignment
+   JTR_FLAG_CHECK([-mpreferred-stack-boundary=5], 1)
    # gcc bug workaround, see issue 632
    JTR_FLAG_CHECK([--param allow-store-data-races=0], 1)
    # Silly OSX warnings

...but there's more to it than the above. First, we should only do it for AVX2 or higher. Second, it should not always be "5" - it should be the log2 of sizeof(vtype).

Third, this might not help under Windows (at least not MinGW64) unless they fixed that now. So we'd still need alternative code paths like the current mem_align stuff.

<!-- gh-comment-id:138399455 --> @magnumripper commented on GitHub (Sep 8, 2015): This might be part of a canonical solution: ``` diff diff --git a/src/configure.ac b/src/configure.ac index 0f27922..f45cd56 100644 --- a/src/configure.ac +++ b/src/configure.ac @@ -225,6 +225,8 @@ if test "x$asan" = xyes || test "x$ubsan" = xyes ; then else JTR_FLAG_CHECK([-fomit-frame-pointer], 1) fi + # AVX2 alignment + JTR_FLAG_CHECK([-mpreferred-stack-boundary=5], 1) # gcc bug workaround, see issue 632 JTR_FLAG_CHECK([--param allow-store-data-races=0], 1) # Silly OSX warnings ``` ...but there's more to it than the above. First, we should only do it for AVX2 or higher. Second, it should not always be "5" - it should be the log2 of `sizeof(vtype)`. Third, this might not help under Windows (at least not MinGW64) unless they fixed that now. So we'd still need alternative code paths like the current mem_align stuff.
Author
Owner

@magnumripper commented on GitHub (Sep 8, 2015):

-mpreferred-stack-boundary doesn't seem to help at all, but there are other ways around it:
http://www.openwall.com/lists/john-dev/2015/09/08/14

Oh, and the -mincoming-stack-boundary option isn't even present in gcc-4.9 and later.

<!-- gh-comment-id:138680762 --> @magnumripper commented on GitHub (Sep 8, 2015): -mpreferred-stack-boundary doesn't seem to help at all, but there are other ways around it: http://www.openwall.com/lists/john-dev/2015/09/08/14 Oh, and the -mincoming-stack-boundary option isn't even present in gcc-4.9 and later.
Author
Owner

@magnumripper commented on GitHub (Sep 8, 2015):

I currently believe this is all a red herring except the case (that we've actually seen IRL) where we declare variables local to an OMP parallel block, and try to use JTR_ALIGN() (a.k.a __attribute__ ((aligned()))). Unless my theory is debunked we should revert most of 1bd8d9d, especially the change to md5cryptsse().

<!-- gh-comment-id:138708993 --> @magnumripper commented on GitHub (Sep 8, 2015): I currently believe this is all a red herring **except** the case (that we've actually seen IRL) where we declare variables local to an OMP parallel block, and try to use `JTR_ALIGN()` (a.k.a `__attribute__ ((aligned()))`). Unless my theory is debunked we should revert most of 1bd8d9d, especially the change to `md5cryptsse()`.
Author
Owner

@magnumripper commented on GitHub (Sep 9, 2015):

@jfoug I believe your mem_align macro might actually be optimized away when needed the most:
http://www.openwall.com/lists/john-dev/2015/09/09/3

We'll see - let's remember this in case we see impossibe things happen.

<!-- gh-comment-id:138862814 --> @magnumripper commented on GitHub (Sep 9, 2015): @jfoug I believe your mem_align macro might actually be optimized away when needed the most: http://www.openwall.com/lists/john-dev/2015/09/09/3 We'll see - let's remember this in case we see impossibe things happen.
Author
Owner

@jfoug commented on GitHub (Sep 9, 2015):

Remembered. However, I fail to see how this ever could be optimized away. It 'may' be when the constants used are known by the compiler to be what the stack already is aligned to, however, since all usages are against char* or unsigned* arrays, the compiler will only be aligning stack to 4 or 8 bytes, so there should be no chance the compiler runs the math, completing the expression and finding that no work is ever being done. If it does optimize it away, it would only be done by bugs in the optimizer, or if we tried to align to 4 bytes or something.

<!-- gh-comment-id:138896559 --> @jfoug commented on GitHub (Sep 9, 2015): Remembered. However, I fail to see how this ever could be optimized away. It 'may' be when the constants used are known by the compiler to be what the stack already is aligned to, however, since all usages are against char\* or unsigned\* arrays, the compiler will only be aligning stack to 4 or 8 bytes, so there should be no chance the compiler runs the math, completing the expression and finding that no work is ever being done. If it does optimize it away, it would only be done by bugs in the optimizer, or if we tried to align to 4 bytes or something.
Author
Owner

@jfoug commented on GitHub (Sep 9, 2015):

One other huge difference in using the macro is that the way the macro is used, there is always an lparam expression, where your expression was a simple read test that affected nothing in the running environment other than conditional logic.

I am a bit surprised yours was optimized away, but I do understand the optimizer wanting to optimize it away. I would just think it should not. I bet it was optimized away, because it knew that it was always aligning stuff 32 bytes. But then comes along a library that does the stack work, not the compiler, and bam, that 'knowledge' that the compiler had is no longer correct.

I could be wrong however, and it is only due to my expression being complex enough that the current optimizer does not recognize it, and that when newer versions of the compiler are released, they improve the amount of code run simulation being done, and again think there is no reason to emit any code for this macro.

<!-- gh-comment-id:138899786 --> @jfoug commented on GitHub (Sep 9, 2015): One other huge difference in using the macro is that the way the macro is used, there is always an lparam expression, where your expression was a simple read test that affected nothing in the running environment other than conditional logic. I am a bit surprised yours was optimized away, but I do understand the optimizer wanting to optimize it away. I would just think it should not. I bet it was optimized away, because it knew that it was always aligning stuff 32 bytes. But then comes along a library that does the stack work, not the compiler, and bam, that 'knowledge' that the compiler had is no longer correct. I could be wrong however, and it is only due to my expression being complex enough that the current optimizer does not recognize it, and that when newer versions of the compiler are released, they improve the amount of code run simulation being done, and again think there is no reason to emit any code for this macro.
Author
Owner

@magnumripper commented on GitHub (Sep 9, 2015):

Did some more tests today. I can reliably see gcc-4.8 optimize away that test (when not in a separate function) but I have yet to see it do so with the macro. So I think we're safe.

<!-- gh-comment-id:138959656 --> @magnumripper commented on GitHub (Sep 9, 2015): Did some more tests today. I can reliably see gcc-4.8 optimize away that test (when not in a separate function) but I have yet to see it do so with the macro. So I think we're safe.
Author
Owner

@jfoug commented on GitHub (Sep 9, 2015):

I honestly think it is NOT going to reduce the expression all the way out, simply because it is an expression that can not be fully expressed at compile time. Now, if the compiler really got smart, and realized just what the expression was trying to do, and then stripped the emiited code down to

buf = _buf;

then we have problems. I just do not see that, since I have mixed up 2 or 3 pointers, some conversion into an integer (which can NOT be known during compile), and some other stuff. Even though the end result is a simple expression that ends up doing

buf = _buf + ((_buf+31)%32;

I just do not see the compiler stripping off the last part.

<!-- gh-comment-id:138997623 --> @jfoug commented on GitHub (Sep 9, 2015): I honestly think it is NOT going to reduce the expression all the way out, simply because it is an expression that can not be fully expressed at compile time. Now, if the compiler really got smart, and realized just what the expression was trying to do, and then stripped the emiited code down to buf = _buf; then we have problems. I just do not see that, since I have mixed up 2 or 3 pointers, some conversion into an integer (which can NOT be known during compile), and some other stuff. Even though the end result is a simple expression that ends up doing ``` C buf = _buf + ((_buf+31)%32; ``` I just do not see the compiler stripping off the last part.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1149
No description provided.