Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: integer overflow in main thread stack base detection #7373

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions src/fiber.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require "c/sys/mman"
{% unless flag?(:win32) %}
require "c/sys/resource"
{% end %}
require "thread/linked_list"

# Load the arch-specific methods to create a context and to swap from one
Expand Down Expand Up @@ -87,28 +84,12 @@ class Fiber
end

# :nodoc:
def initialize
def initialize(@stack : Void*)
@proc = Proc(Void).new { }
@stack_top = _fiber_get_stack_top
@stack_bottom = GC.stack_bottom
@name = "main"

# Determine location of the top of the stack.
# The technique here works only for the main stack on a POSIX platform.
# TODO: implement for Windows with GetCurrentThreadStackLimits
# TODO: implement for pthreads using
# linux-glibc/musl: pthread_getattr_np
# macosx: pthread_get_stackaddr_np, pthread_get_stacksize_np
# freebsd: pthread_attr_get_np
# openbsd: pthread_stackseg_np
@stack = Pointer(Void).null
{% unless flag?(:win32) %}
if LibC.getrlimit(LibC::RLIMIT_STACK, out rlim) == 0
stack_size = rlim.rlim_cur
@stack = Pointer(Void).new(@stack_bottom.address - stack_size)
end
{% end %}

@@fibers.push(self)
end

Expand Down
3 changes: 3 additions & 0 deletions src/lib_c/aarch64-linux-gnu/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(attr : PthreadAttrT*) : Int
fun pthread_attr_getstack(addr : PthreadAttrT*, stackaddr : Void**, stacksize : SizeT*) : Int
fun pthread_condattr_destroy(attr : PthreadCondattrT*) : Int
fun pthread_condattr_init(attr : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(attr : PthreadCondattrT*, type : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(cond : PthreadCondT*, mutex : PthreadMutexT*) : Int
fun pthread_create(newthread : PthreadT*, attr : PthreadAttrT*, start_routine : Void* -> Void*, arg : Void*) : Int
fun pthread_detach(th : PthreadT) : Int
fun pthread_getattr_np(thread : PthreadT, attr : PthreadAttrT*) : Int
fun pthread_equal(thread1 : PthreadT, thread2 : PthreadT) : Int
fun pthread_join(th : PthreadT, thread_return : Void**) : Int
fun pthread_mutexattr_destroy(attr : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/aarch64-linux-gnu/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/aarch64-linux-musl/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(x0 : PthreadAttrT*) : Int
fun pthread_attr_getstack(x0 : PthreadAttrT*, x1 : Void**, x2 : SizeT*) : Int
fun pthread_condattr_destroy(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_init(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(x0 : PthreadCondattrT*, x1 : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(x0 : PthreadCondT*, x1 : PthreadMutexT*) : Int
fun pthread_create(x0 : PthreadT*, x1 : PthreadAttrT*, x2 : Void* -> Void*, x3 : Void*) : Int
fun pthread_detach(x0 : PthreadT) : Int
fun pthread_getattr_np(x0 : PthreadT, x1 : PthreadAttrT*) : Int
fun pthread_join(x0 : PthreadT, x1 : Void**) : Int
fun pthread_mutexattr_destroy(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(x0 : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/aarch64-linux-musl/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/arm-linux-gnueabihf/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(attr : PthreadAttrT*) : Int
fun pthread_attr_getstack(addr : PthreadAttrT*, stackaddr : Void**, stacksize : SizeT*) : Int
fun pthread_condattr_destroy(attr : PthreadCondattrT*) : Int
fun pthread_condattr_init(attr : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(attr : PthreadCondattrT*, type : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(cond : PthreadCondT*, mutex : PthreadMutexT*) : Int
fun pthread_create(newthread : PthreadT*, attr : PthreadAttrT*, start_routine : Void* -> Void*, arg : Void*) : Int
fun pthread_detach(th : PthreadT) : Int
fun pthread_getattr_np(thread : PthreadT, attr : PthreadAttrT*) : Int
fun pthread_equal(thread1 : PthreadT, thread2 : PthreadT) : Int
fun pthread_join(th : PthreadT, thread_return : Void**) : Int
fun pthread_mutexattr_destroy(attr : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/arm-linux-gnueabihf/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/i386-linux-gnu/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(attr : PthreadAttrT*) : Int
fun pthread_attr_getstack(addr : PthreadAttrT*, stackaddr : Void**, stacksize : SizeT*) : Int
fun pthread_condattr_destroy(attr : PthreadCondattrT*) : Int
fun pthread_condattr_init(attr : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(attr : PthreadCondattrT*, type : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(cond : PthreadCondT*, mutex : PthreadMutexT*) : Int
fun pthread_create(newthread : PthreadT*, attr : PthreadAttrT*, start_routine : Void* -> Void*, arg : Void*) : Int
fun pthread_detach(th : PthreadT) : Int
fun pthread_getattr_np(thread : PthreadT, attr : PthreadAttrT*) : Int
fun pthread_join(th : PthreadT, thread_return : Void**) : Int
fun pthread_mutexattr_destroy(attr : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(attr : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/i386-linux-gnu/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/i386-linux-musl/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(x0 : PthreadAttrT*) : Int
fun pthread_attr_getstack(x0 : PthreadAttrT*, x1 : Void**, x2 : SizeT*) : Int
fun pthread_condattr_destroy(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_init(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(x0 : PthreadCondattrT*, x1 : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(x0 : PthreadCondT*, x1 : PthreadMutexT*) : Int
fun pthread_create(x0 : PthreadT*, x1 : PthreadAttrT*, x2 : Void* -> Void*, x3 : Void*) : Int
fun pthread_detach(x0 : PthreadT) : Int
fun pthread_getattr_np(x0 : PthreadT, x1 : PthreadAttrT*) : Int
fun pthread_join(x0 : PthreadT, x1 : Void**) : Int
fun pthread_mutexattr_destroy(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(x0 : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/i386-linux-musl/c/sys/resource.cr

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib_c/x86_64-darwin/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ lib LibC
fun pthread_cond_wait(x0 : PthreadCondT*, x1 : PthreadMutexT*) : Int
fun pthread_create(x0 : PthreadT*, x1 : PthreadAttrT*, x2 : Void* -> Void*, x3 : Void*) : Int
fun pthread_detach(x0 : PthreadT) : Int
fun pthread_get_stackaddr_np(x0 : PthreadT) : Void*
fun pthread_get_stacksize_np(x0 : PthreadT) : SizeT
fun pthread_join(x0 : PthreadT, x1 : Void**) : Int
fun pthread_mutexattr_destroy(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(x0 : PthreadMutexattrT*) : Int
Expand Down
4 changes: 4 additions & 0 deletions src/lib_c/x86_64-freebsd/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 1

fun pthread_attr_destroy(x0 : PthreadAttrT*) : Int
fun pthread_attr_get_np(x0 : PthreadT, x1 : PthreadAttrT*) : Int
fun pthread_attr_getstack(x0 : PthreadAttrT*, x1 : Void**, x2 : SizeT*) : Int
fun pthread_attr_init(x0 : PthreadAttrT*) : Int
fun pthread_condattr_destroy(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_init(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(x0 : PthreadCondattrT*, x1 : ClockidT) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/x86_64-freebsd/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/x86_64-linux-gnu/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(attr : PthreadAttrT*) : Int
fun pthread_attr_getstack(addr : PthreadAttrT*, stackaddr : Void**, stacksize : SizeT*) : Int
fun pthread_condattr_destroy(attr : PthreadCondattrT*) : Int
fun pthread_condattr_init(attr : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(attr : PthreadCondattrT*, type : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(cond : PthreadCondT*, mutex : PthreadMutexT*) : Int
fun pthread_create(newthread : PthreadT*, attr : PthreadAttrT*, start_routine : Void* -> Void*, arg : Void*) : Int
fun pthread_detach(th : PthreadT) : Int
fun pthread_getattr_np(thread : PthreadT, attr : PthreadAttrT*) : Int
fun pthread_join(th : PthreadT, thread_return : Void**) : Int
fun pthread_mutexattr_destroy(attr : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(attr : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/x86_64-linux-gnu/c/sys/resource.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/lib_c/x86_64-linux-musl/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./sys/types"
lib LibC
PTHREAD_MUTEX_ERRORCHECK = 2

fun pthread_attr_destroy(x0 : PthreadAttrT*) : Int
fun pthread_attr_getstack(x0 : PthreadAttrT*, x1 : Void**, x2 : SizeT*) : Int
fun pthread_condattr_destroy(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_init(x0 : PthreadCondattrT*) : Int
fun pthread_condattr_setclock(x0 : PthreadCondattrT*, x1 : ClockidT) : Int
Expand All @@ -14,6 +16,7 @@ lib LibC
fun pthread_cond_wait(x0 : PthreadCondT*, x1 : PthreadMutexT*) : Int
fun pthread_create(x0 : PthreadT*, x1 : PthreadAttrT*, x2 : Void* -> Void*, x3 : Void*) : Int
fun pthread_detach(x0 : PthreadT) : Int
fun pthread_getattr_np(x0 : PthreadT, x1 : PthreadAttrT*) : Int
fun pthread_join(x0 : PthreadT, x1 : Void**) : Int
fun pthread_mutexattr_destroy(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(x0 : PthreadMutexattrT*) : Int
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/x86_64-linux-musl/c/sys/resource.cr

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib_c/x86_64-openbsd/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lib LibC
fun pthread_join(x0 : PthreadT, x1 : Void**) : Int
alias PthreadKeyDestructor = (Void*) ->
fun pthread_key_create(PthreadKeyT*, PthreadKeyDestructor) : Int
fun pthread_main_np : Int
fun pthread_mutexattr_destroy(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(x0 : PthreadMutexattrT*) : Int
fun pthread_mutexattr_settype(x0 : PthreadMutexattrT*, x1 : Int) : Int
Expand All @@ -30,4 +31,5 @@ lib LibC
fun pthread_mutex_unlock(x0 : PthreadMutexT*) : Int
fun pthread_self : PthreadT
fun pthread_setspecific(PthreadKeyT, Void*) : Int
fun pthread_stackseg_np(PthreadT, StackT*) : Int
end
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-openbsd/c/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ lib LibC
SIG_DFL = SighandlerT.new(Pointer(Void).new(0_u64), Pointer(Void).null)
SIG_IGN = SighandlerT.new(Pointer(Void).new(1_u64), Pointer(Void).null)

struct StackT
ss_sp : Void*
ss_size : SizeT
ss_flags : Int
end

fun kill(x0 : PidT, x1 : Int) : Int
fun pthread_sigmask(Int, SigsetT*, SigsetT*) : Int
fun signal(x0 : Int, x1 : Int -> Void) : Int -> Void
Expand Down
12 changes: 0 additions & 12 deletions src/lib_c/x86_64-openbsd/c/sys/resource.cr

This file was deleted.

11 changes: 6 additions & 5 deletions src/lib_c/x86_64-openbsd/c/unistd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
SC_PAGESIZE = 28

fun chroot(dirname : Char*) : Int
fun access(x0 : Char*, x1 : Int) : Int
Expand Down
Loading