Skip to content

Instantly share code, notes, and snippets.

@KunYi
Created May 31, 2018 09:00
Show Gist options
  • Save KunYi/b508d572007ae82ab570b57092dd1a9a to your computer and use it in GitHub Desktop.
Save KunYi/b508d572007ae82ab570b57092dd1a9a to your computer and use it in GitHub Desktop.
sub 4K sp for x86_64
-----------------------------------------
; sub 4K on Stack pointer register
-----------------------------------------
.text
.globl DecSp4K
.type DecSp4K, @function
DecSp4K:
MOVQ %rsp, %rax
SUB $(4096-8), %rsp
SUB $8, %rax
JMP *8(%rax)
------------------------------------------
test.c
------------------------------------------
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <alloca.h>
extern unsigned long long getSP(void);
extern void DecSp4K(void);
long long i;
int main(int argc, char** argv) {
char t;
char a;
struct rlimit rlim;
for (;;) {
getrlimit(RLIMIT_STACK, &rlim);
printf("soft=%dK, hard=%dK\n",
(int)rlim.rlim_cur/1024,
(int)rlim.rlim_max/1024);
printf("t address: 0x%08llx\n", &t);
printf("pid = %d", getpid());
a = getchar();
if (a == 'e') break;
for (i=0; i< 8192*(1024/4) ; i++ ) {
DecSp4K();
printf("%8lld kb, sp: 0x%08llx\n",i*4, getSP());
}
printf("sucess, i = %lld\n", i);
}
@KunYi
Copy link
Author

KunYi commented May 31, 2018

`~/tmp$ cat decSP4K.s

.text
.globl DecSp4K
.type DecSp4K, @function

DecSp4K:

DecSp4K:

MOVQ %rsp, %rax
SUB $(4096-8), %rsp
SUB $8, %rax
JMP *8(%rax)
~/tmp$ cat getSP.s

.text
.globl getSP
.type getSP, @function

getSP:
MOVQ %rsp, %rax
RET
~/tmp$ cat test1.c
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <alloca.h>

extern unsigned long long getSP(void);
extern void DecSp4K(void);

long long i;
int main(int argc, char** argv) {
char t;
char a;

struct rlimit rlim;
for (;;) {
getrlimit(RLIMIT_STACK, &rlim);
printf("soft=%dK, hard=%dK\n",
	(int)rlim.rlim_cur/1024, 
	(int)rlim.rlim_max/1024);
printf("t address: 0x%08llx\n", &t);
printf("pid = %d", getpid());
a = getchar();
if (a == 'e') break;	
for (i=0; i< 8192*(1024/4) ; i++ ) {
	DecSp4K();
	printf("%8lld kb, sp: 0x%08llx\n",i*4, getSP());
}
printf("sucess, i = %lld\n", i);
}

}

~/tmp$ gcc decSP4K.s getSP.s test1.c -o test1
test1.c: In function ‘main’:
test1.c:20:2: warning: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]
printf("t address: 0x%08llx\n", &t);
^

`

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