asar coverage - build #270


src/asar/
File: src/asar/platform/generic/thread-helpers-pthread.h
Date: 2025-03-03 15:40:30
Lines:
36/39
92.3%
Functions:
10/10
100.0%
Branches:
16/36
44.4%

Line Branch Exec Source
1 #pragma once
2
3 #include <stdint.h>
4 #include <pthread.h>
5
6 struct function_pointer_wrapper /*have this struct at global level*/
7 {
8 static void* (*thread_callback)(void*);
9 158 static void* execute_thread(void* parameter) { return thread_callback(parameter); }
10 };
11
12 void* (*function_pointer_wrapper::thread_callback)(void*) = nullptr;
13
14 template <typename functor>
15 158 bool run_as_thread(functor&& callback) {
16 struct thread_wrapper {
17 functor& callback;
18 bool result;
19
20 158 void* execute() {
21 158 result = callback();
22
3/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 158 times.
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 38 times.
158 if (result)
23 120 pthread_exit(NULL);
24 else
25 38 pthread_exit(reinterpret_cast<void*>((uintptr_t)-1));
26 }
27
28 158 } wrapper{ callback, false };
29
30 158 function_pointer_wrapper::thread_callback = [](void* parameter) {
31 158 return reinterpret_cast<thread_wrapper*>(parameter)->execute();
32 };
33
34 158 pthread_attr_t settings;
35 158 pthread_t thread_id;
36 int ret;
37
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 ret = pthread_attr_init(&settings);
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (ret == -1){
40 return callback();
41 }
42
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 ret = pthread_attr_setstacksize(&settings, 16 * 1024 * 1024);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (ret == -1){
45 return callback();
46 }
47
48
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 158 times.
158 ret = pthread_create(&thread_id, &settings, &function_pointer_wrapper::execute_thread, &wrapper);
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 if (ret == -1){
50 return callback();
51 }
52
53 158 void* thread_ret;
54
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 pthread_join(thread_id, &thread_ret);
55
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 return wrapper.result;
57 }
58
59 #ifndef NO_USE_THREADS
60 void* stack_bottom = nullptr;
61 464 void init_stack_use_check() {
62 464 pthread_t self = pthread_self();
63 #ifdef __APPLE__
64 stack_bottom = (char*)pthread_get_stackaddr_np(self) - pthread_get_stacksize_np(self);
65 #else
66 464 pthread_attr_t attrs;
67 464 size_t stack_size = 0;
68 464 pthread_getattr_np(self, &attrs);
69 464 pthread_attr_getstack(&attrs, &stack_bottom, &stack_size);
70 464 pthread_attr_destroy(&attrs);
71 #endif
72 464 }
73 459 void deinit_stack_use_check() {
74 459 stack_bottom = nullptr;
75 459 }
76 64203 bool have_enough_stack_left() {
77 #if defined(__clang__) || defined(__GNUC__)
78 // this is a GCC extension, but it's necessary for this function to work in ASan
79 // (regular local variables are banished to the shadow realm or some shit,
80 // so their addresses don't tell us anything about "real" stack usage)
81 64203 void* stack_top = __builtin_frame_address(0);
82 #else
83 // using a random local as a rough estimate for current top-of-stack
84 char stackvar;
85 void* stack_top = &stackvar;
86 #endif
87 64203 ssize_t stack_left = (char*)stack_top - (char*)stack_bottom;
88
4/4
✓ Branch 0 taken 64189 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 64188 times.
✓ Branch 3 taken 1 times.
64203 return stack_bottom == nullptr || stack_left >= 32768;
89 }
90 #endif
91