Main HEAP and Thread HEAP

2017. 10. 30. 21:320x04 pwnable

728x90

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>


//this Function pronounced, return value is void Pointer

// use of one arguments is void pointer arg


// void pointer means freedom return value.

void* threadFunc(void *arg);


int main(int argc, char*argv[])

{

// thread 

pthread_t t1;


void *s;

int ret;

char *addr;

// GET process ID

printf("W3lcome to per thread arena example : %d\n",getpid() );


printf("Before malloc in main thread\n");

getchar();

// malloc 1000 byte

addr = (char*)malloc(1000);

printf("After malloc and before free in main thread\n");

getchar();


// free memory

free(addr); // create freelist related in main

printf("After free in main thread\n");

getchar();


// return value => pthread_create 



// second arguments NULL means use of basic characteristic related thread.

// third arguments -> threadFunc is use of Function by Thread 


ret = pthread_create(&t1, NULL, threadFunc, NULL);

// Let's Create threadFunc in related Thread.

// IF return of ret is Zero -> this thread create succuess 

// But, instead of Zero this thread cause by Error.

if(ret)

{

printf("Thread Create Error Sorry!\n");

return -1;

}


// pthread_join means wait for Terminated about another thread. 

ret = pthread_join(t1, &s);

if(ret)

{

printf("Thread join Error\n");

return -1;

}

return 0;

}


void * threadFunc(void *arg)

{

// before, just malloc in main.

// but now, malloc in Thread.

printf("Before Malloc in thread\n");

getchar();

char *addr = (char*)malloc(1000);

printf("After malloc and before free in Thread 1 \n");

getchar();


free(addr); // create freelist related in Thread 1

printf("After free in Thread 1 \n");


getchar();


오늘 알게 된 것 : pthread를 컴파일 할 때는 (32비트 기준)

gcc -o 바이너리 ./바이너리.c -m32 -lpthread


'0x04 pwnable' 카테고리의 다른 글

rtl 공부 2  (0) 2017.11.27
rtl 공부  (0) 2017.11.27
공부 내용  (0) 2017.10.23
Lord of Bof 2번 문제  (0) 2017.07.07
overflow_example  (0) 2017.07.06