Skip to content

Commit

Permalink
Support free the stack, #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 8, 2024
1 parent 843e74b commit b019860
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ endif
#
# make EXTRA_CFLAGS=-DDEBUG_STATS
#
# or cache the stack and reuse it:
# make EXTRA_CFLAGS=-DMD_CACHE_STACK
#
# or enable the coverage for utest:
# make UTEST_FLAGS="-fprofile-arcs -ftest-coverage"
#
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) was patched an
- [x] Check capability for backtrack.
- [x] Support set specifics for any thread.
- [x] Support st_destroy to free resources for asan.
- [x] Support free the stack, [#38](https://github.com/ossrs/state-threads/issues/38).
- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12).

## GDB Tools
Expand Down
35 changes: 29 additions & 6 deletions stk.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ __thread int _st_num_free_stacks = 0;
__thread int _st_randomize_stacks = 0;

static char *_st_new_stk_segment(int size);
static void _st_delete_stk_segment(char *vaddr, int size);

_st_stack_t *_st_stack_new(int stack_size)
{
_st_clist_t *qp;
_st_stack_t *ts;
int extra;


/* If cache stack, we try to use stack from the cache list. */
#ifdef MD_CACHE_STACK
for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) {
ts = _ST_THREAD_STACK_PTR(qp);
if (ts->stk_size >= stack_size) {
Expand All @@ -75,11 +78,34 @@ _st_stack_t *_st_stack_new(int stack_size)
return ts;
}
}
#endif

extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
/* If not cache stack, we will free all stack in the list, which contains the stack to be freed.
* Note that we should never directly free it at _st_stack_free, because it is still be used,
* and will cause crash. */
#ifndef MD_CACHE_STACK
for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) {
ts = _ST_THREAD_STACK_PTR(qp);
/* Before qp is freed, move to next one, because the qp will be freed when free the ts. */
qp = qp->next;

ST_REMOVE_LINK(&ts->links);
_st_num_free_stacks--;

#if defined(DEBUG) && !defined(MD_NO_PROTECT)
mprotect(ts->vaddr, REDZONE, PROT_READ | PROT_WRITE);
mprotect(ts->stk_top + extra, REDZONE, PROT_READ | PROT_WRITE);
#endif

_st_delete_stk_segment(ts->vaddr, ts->vaddr_size);
free(ts);
}
#endif

/* Make a new thread stack object. */
if ((ts = (_st_stack_t *)calloc(1, sizeof(_st_stack_t))) == NULL)
return NULL;
extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
ts->vaddr_size = stack_size + 2*REDZONE + extra;
ts->vaddr = _st_new_stk_segment(ts->vaddr_size);
if (!ts->vaddr) {
Expand Down Expand Up @@ -114,7 +140,7 @@ void _st_stack_free(_st_stack_t *ts)
{
if (!ts)
return;

/* Put the stack on the free list */
ST_APPEND_LINK(&ts->links, _st_free_stacks.prev);
_st_num_free_stacks++;
Expand Down Expand Up @@ -152,8 +178,6 @@ static char *_st_new_stk_segment(int size)
}


/* Not used */
#if 0
void _st_delete_stk_segment(char *vaddr, int size)
{
#ifdef MALLOC_STACK
Expand All @@ -162,7 +186,6 @@ void _st_delete_stk_segment(char *vaddr, int size)
(void) munmap(vaddr, size);
#endif
}
#endif

int st_randomize_stacks(int on)
{
Expand Down
2 changes: 1 addition & 1 deletion tools/backtrace/backtrace.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#ifdef __linux__
#define _GNU_SOURCE
Expand Down
2 changes: 1 addition & 1 deletion tools/helloworld/helloworld.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <stdio.h>

Expand Down
2 changes: 1 addition & 1 deletion tools/porting/porting.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <stdio.h>
#include <setjmp.h>
Expand Down
2 changes: 1 addition & 1 deletion tools/verify/verify.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <stdio.h>

Expand Down
2 changes: 1 addition & 1 deletion utest/st_utest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <st_utest.hpp>

Expand Down
2 changes: 1 addition & 1 deletion utest/st_utest.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#ifndef ST_UTEST_PUBLIC_HPP
#define ST_UTEST_PUBLIC_HPP
Expand Down
2 changes: 1 addition & 1 deletion utest/st_utest_coroutines.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <st_utest.hpp>

Expand Down
2 changes: 1 addition & 1 deletion utest/st_utest_tcp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2013-2022 Winlin */
/* Copyright (c) 2013-2024 The SRS Authors */

#include <st_utest.hpp>

Expand Down

1 comment on commit b019860

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.