15
15
#include < alaska/utils.h>
16
16
#include < alaska/Runtime.hpp>
17
17
#include < alaska/Configuration.hpp>
18
+ #include " alaska/HugeObjectAllocator.hpp"
18
19
#include < alaska/liballoc.h>
19
20
#include < stdlib.h>
20
21
#include < errno.h>
35
36
static alaska::ThreadCache *tc = NULL ;
36
37
37
38
static void set_ht_addr (void *addr) {
38
- // alaska::printf("set htaddr to %p\n", addr);
39
+ alaska::printf (" set htbase to %p\n " , addr);
39
40
write_csr (0xc2 , addr);
40
41
}
41
42
@@ -72,36 +73,83 @@ static char stdout_buf[BUFSIZ];
72
73
static char stderr_buf[BUFSIZ];
73
74
74
75
76
+
77
+ static void wait_for_csr_zero (void ) {
78
+ volatile uint32_t csr_value = 0x1 ;
79
+ do {
80
+ __asm__ volatile (" csrr %0, 0xc3\n\t " : " =r" (csr_value) : : " memory" );
81
+ } while (csr_value != 0 );
82
+ }
83
+
84
+
85
+ #define BACKTRACE_SIZE 100
86
+
87
+
88
+ extern " C" void abort (void ) {
89
+ void *addr = __builtin_extract_return_addr (__builtin_return_address (0 ));
90
+
91
+ printf (" Abort from %p!\n " , addr);
92
+ printf (" going to sleep!\n " );
93
+
94
+ sleep (5000000 );
95
+ exit (-1 );
96
+ }
97
+
98
+
99
+ static void handle_sig (int sig) {
100
+ printf (" Caught signal %d (%s)\n " , sig, strsignal (sig));
101
+ void *buffer[BACKTRACE_SIZE];
102
+ // Get the backtrace
103
+ int nptrs = backtrace (buffer, BACKTRACE_SIZE);
104
+ // Print the backtrace symbols
105
+ backtrace_symbols_fd (buffer, nptrs, STDOUT_FILENO);
106
+ exit (0 );
107
+ }
108
+
109
+
110
+
75
111
static void init (void ) {
76
112
setvbuf (stdout, stdout_buf, _IOLBF, BUFSIZ);
77
113
setvbuf (stderr, stderr_buf, _IOLBF, BUFSIZ);
78
114
79
115
alaska::Configuration config;
116
+ // Use the "malloc" backend to operate cleanly w/ libc's malloc
117
+ config.huge_strategy = alaska::HugeAllocationStrategy::MALLOC_BACKED;
80
118
81
119
the_runtime = new alaska::Runtime (config);
82
120
void *handle_table_base = the_runtime->handle_table .get_base ();
83
121
// printf("Handle table at %p\n", handle_table_base);
84
- set_ht_addr (handle_table_base);
85
122
// Make sure the handle table performs mlocks
86
123
the_runtime->handle_table .enable_mlock ();
124
+
125
+ asm volatile (" fence" ::: " memory" );
126
+ set_ht_addr (handle_table_base);
127
+
128
+
129
+ signal (SIGABRT, handle_sig);
130
+ signal (SIGSEGV, handle_sig);
131
+ signal (SIGBUS, handle_sig);
132
+ signal (SIGILL, handle_sig);
87
133
}
88
134
89
135
90
136
91
137
92
138
static alaska::ThreadCache *get_tc () {
93
- if (the_runtime == NULL ) {
94
- init ();
95
- }
96
- if (tc == NULL ) {
97
- tc = the_runtime->new_threadcache ();
98
- }
139
+ if (the_runtime == NULL ) init ();
140
+ if (tc == NULL ) tc = the_runtime->new_threadcache ();
99
141
return tc;
100
142
}
101
143
102
144
void __attribute__ ((constructor(102 ))) alaska_init(void ) {
103
- unsetenv (" LD_PRELOAD" ); // make it so we don't run alaska in subprocesses!
145
+ unsetenv (" LD_PRELOAD" ); // make it so we don't run alaska in subprocesses!
104
146
get_tc ();
147
+
148
+ atexit ([]() {
149
+ printf (" Setting ht addr to zero!\n " );
150
+ set_ht_addr (0 );
151
+ printf (" set!\n " );
152
+ });
105
153
}
106
154
107
155
void __attribute__ ((destructor)) alaska_deinit(void ) {
@@ -111,48 +159,43 @@ void __attribute__((destructor)) alaska_deinit(void) {
111
159
// }
112
160
// delete the_runtime;
113
161
// }
114
- // set_ht_addr(NULL );
162
+ // set_ht_addr(0 );
115
163
}
116
164
117
-
118
- // #define malloc halloc
119
- // #define calloc hcalloc
120
- // #define realloc hrealloc
121
- // #define free hfree
122
-
123
165
static void *_halloc (size_t sz, int zero) {
124
- // print_hex("_halloc", sz);
125
- if (dead) {
126
- return alaska_internal_malloc (sz);
127
- }
128
- void *result = get_tc ()->halloc (sz, zero);
166
+ void *result = NULL ;
129
167
130
- // This seems right...
168
+ result = get_tc ()->halloc (sz, zero);
169
+ auto m = (uintptr_t )alaska::Mapping::translate (result);
170
+ printf (" halloc(%zu) -> %p (%zx-%zx)\n " , sz, result, m, m + sz);
131
171
if (result == NULL ) errno = ENOMEM;
172
+
132
173
return result;
133
174
}
134
175
135
176
136
- extern " C" void *malloc (size_t sz) noexcept { return _halloc (sz, 0 ); }
177
+ // #define halloc malloc
178
+ // #define hcalloc calloc
179
+ // #define hrealloc realloc
180
+ // #define hfree free
181
+
182
+ extern " C" void *halloc (size_t sz) noexcept { return _halloc (sz, 0 ); }
137
183
138
- extern " C" void *calloc (size_t nmemb, size_t size) { return _halloc (nmemb * size, 1 ); }
184
+ extern " C" void *hcalloc (size_t nmemb, size_t size) { return _halloc (nmemb * size, 1 ); }
139
185
140
186
// Reallocate a handle
141
- extern " C" void *realloc (void *handle, size_t new_size) {
187
+ extern " C" void *hrealloc (void *handle, size_t new_size) {
142
188
auto *tc = get_tc ();
143
189
144
- // print_hex("realloc", (uint64_t)handle);
145
- // print_hex(" newsz", (uint64_t)new_size);
146
190
// If the handle is null, then this call is equivalent to malloc(size)
147
191
if (handle == NULL ) {
148
192
return malloc (new_size);
149
193
}
150
194
auto *m = alaska::Mapping::from_handle_safe (handle);
151
195
if (m == NULL ) {
152
196
if (!alaska::Runtime::get ().heap .huge_allocator .owns (handle)) {
153
- log_debug (" realloc edge case: not a handle %p!" , handle);
197
+ log_fatal (" realloc edge case: not a handle %p!" , handle);
154
198
exit (-1 );
155
- // return ::realloc(handle, new_size);
156
199
}
157
200
}
158
201
@@ -170,8 +213,8 @@ extern "C" void *realloc(void *handle, size_t new_size) {
170
213
171
214
172
215
173
- extern " C" void free (void *ptr) {
174
- // print_hex("free ", (uint64_t) ptr);
216
+ extern " C" void hfree (void *ptr) {
217
+ printf ( " hfree %p \n " , ptr);
175
218
// no-op if NULL is passed
176
219
if (unlikely (ptr == NULL )) return ;
177
220
@@ -180,4 +223,15 @@ extern "C" void free(void *ptr) {
180
223
}
181
224
182
225
183
- extern " C" size_t malloc_usable_size (void *ptr) { return get_tc ()->get_size (ptr); }
226
+ extern " C" size_t halloc_usable_size (void *ptr) { return get_tc ()->get_size (ptr); }
227
+
228
+
229
+
230
+ void *operator new (size_t size) { return halloc (size); }
231
+
232
+ void *operator new [](size_t size) { return halloc (size); }
233
+
234
+
235
+ void operator delete (void *ptr) { hfree (ptr); }
236
+
237
+ void operator delete[] (void *ptr) { hfree (ptr); }
0 commit comments