Skip to content

Commit 46a0be7

Browse files
committed
runtime: provide my own printf wrapper
this is so the runtime doesn't call libc's printf, which could potentially cause infinitely recursive calls to `malloc`
1 parent 9cc13ae commit 46a0be7

13 files changed

+94
-74
lines changed

runtime/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ endif(ALASKA_HTLB_SIM)
151151
set_target_properties(alaska PROPERTIES SOVERSION ${ALASKA_VERSION})
152152
target_link_libraries(alaska alaska_core dl pthread ${LIBUNWIND_LIBRARIES})
153153

154-
target_compile_options(alaska PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-nostdlib++ -nostdinc++>)
154+
# target_compile_options(alaska PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-nostdlib++ -nostdinc++>)
155+
target_compile_options(alaska PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
155156

156157
if(ALASKA_HTLB_SIM)
157158
target_link_libraries(alaska alaska_sim)

runtime/core/HandleTable.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ namespace alaska {
3838

3939
uintptr_t table_start = config.handle_table_location;
4040
m_capacity = HandleTable::initial_capacity;
41-
printf("Allocate handle table to %zx\n", table_start);
4241

4342
// Attempt to allocate the initial memory for the table.
4443
m_table = (Mapping *)mmap((void *)table_start, m_capacity * HandleTable::slab_size,
@@ -152,8 +151,8 @@ namespace alaska {
152151
ck::scoped_lock lk(this->lock);
153152

154153
// Dump the handle table in a nice debug output
155-
fprintf(stream, "Handle Table:\n");
156-
fprintf(stream, " - Size: %zu bytes\n", m_capacity * HandleTable::slab_size);
154+
log_info("Handle Table:\n");
155+
log_info(" - Size: %zu bytes\n", m_capacity * HandleTable::slab_size);
157156
for (auto *slab : m_slabs) {
158157
slab->dump(stream);
159158
}
@@ -275,13 +274,12 @@ namespace alaska {
275274

276275

277276
void HandleSlab::dump(FILE *stream) {
278-
fprintf(stream, "Slab %4zu | ", idx);
279-
fprintf(stream, "st %d | ", state);
277+
log_info("Slab %4zu | ", idx);
278+
log_info("st %d | ", state);
280279

281280
auto owner = this->get_owner();
282-
fprintf(stream, "owner: %4d | ", owner ? owner->get_id() : -1);
283-
fprintf(stream, "free %4zu | ", allocator.num_free());
284-
285-
fprintf(stream, "\n");
281+
log_info("owner: %4d | ", owner ? owner->get_id() : -1);
282+
log_info("free %4zu | ", allocator.num_free());
283+
log_info("\n");
286284
}
287285
} // namespace alaska

runtime/core/Heap.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ namespace alaska {
249249
return true;
250250
});
251251
}
252-
printf("Heap compaction moved %ld objects, recovering %zu bytes\n", c, bytes_saved);
252+
alaska::printf("Heap compaction moved %ld objects, recovering %zu bytes\n", c, bytes_saved);
253253
return c;
254254
}
255255

runtime/core/Logger.cpp

+53-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <stdarg.h>
1717
#include <stdlib.h>
1818
#include <unistd.h>
19-
19+
#include <string.h>
2020

2121
static const char *level_strings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
2222

@@ -54,31 +54,69 @@ static void __attribute__((constructor(102))) alaska_logger_init(void) {
5454

5555

5656

57+
#define BUFFER_SIZE 1024
58+
59+
static int log_vemitf(const char *format, va_list args) {
60+
char buffer[BUFFER_SIZE];
61+
int printed_length;
62+
// Format the string into the buffer using snprintf
63+
printed_length = vsnprintf(buffer, BUFFER_SIZE, format, args);
64+
65+
// Clean up variadic arguments
66+
va_end(args);
67+
68+
// Check for truncation
69+
if (printed_length < 0 || printed_length >= BUFFER_SIZE) {
70+
// Handle buffer overflow or other snprintf errors if needed
71+
return -1;
72+
}
73+
74+
// Write the formatted string to stdout
75+
if (write(STDOUT_FILENO, buffer, printed_length) != printed_length) {
76+
// Handle write error
77+
return -1;
78+
}
79+
80+
return printed_length; // Return the number of characters written
81+
}
82+
83+
static int log_emitf(const char *format, ...) {
84+
va_list args;
85+
86+
// Initialize variadic arguments
87+
va_start(args, format);
88+
89+
return log_vemitf(format, args);
90+
}
91+
92+
93+
5794

5895
namespace alaska {
5996
void log(int level, const char *file, int line, const char *fmt, ...) {
6097
if (level >= log_level) {
6198
va_list args;
6299
va_start(args, fmt);
63100

101+
64102
// Grab the lock
65103
pthread_mutex_lock(&log_mutex);
66104

67-
68105
time_t t = time(NULL);
69106
auto time = localtime(&t);
70107

71108
char buf[16];
72109
buf[strftime(buf, sizeof(buf), "%H:%M:%S", time)] = '\0';
73110

74111
if (enable_colors) {
75-
fprintf(stderr, "%s %s%-5s\x1b[0m \x1b[90m%s:%d\x1b[0m | ", buf, level_colors[level],
112+
log_emitf("%s %s%-5s\x1b[0m \x1b[90m%s:%d\x1b[0m | ", buf, level_colors[level],
76113
level_strings[level], file, line);
77114
} else {
78-
fprintf(stderr, "%s %-5s %s:%d | ", buf, level_strings[level], file, line);
115+
log_emitf("%s %-5s %s:%d | ", buf, level_strings[level], file, line);
79116
}
80-
vfprintf(stderr, fmt, args);
81-
fprintf(stderr, "\n");
117+
118+
log_vemitf(fmt, args);
119+
log_emitf("\n");
82120

83121
// release the lock
84122
pthread_mutex_unlock(&log_mutex);
@@ -92,4 +130,12 @@ namespace alaska {
92130
log_level = level;
93131
}
94132
}
95-
} // namespace alaska
133+
134+
135+
int printf(const char *fmt, ...) {
136+
va_list args;
137+
va_start(args, fmt);
138+
log_vemitf(fmt, args);
139+
return 0;
140+
}
141+
} // namespace alaska

runtime/core/Runtime.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace alaska {
5454

5555
void Runtime::dump(FILE *stream) {
5656
//
57-
fprintf(stream, "Alaska Runtime Information:\n");
57+
alaska::printf("Alaska Runtime Information:\n");
5858
heap.dump(stream);
5959
handle_table.dump(stream);
6060
}
@@ -78,11 +78,11 @@ namespace alaska {
7878

7979

8080
void wait_for_initialization(void) {
81-
printf("waiting for initialization!\n");
81+
log_debug("waiting for initialization!\n");
8282
while (not is_initialized()) {
8383
sched_yield();
8484
}
85-
printf("Initialized!\n");
85+
log_debug("Initialized!\n");
8686
}
8787

8888

runtime/core/SizedPage.cpp

-31
Original file line numberDiff line numberDiff line change
@@ -140,33 +140,6 @@ namespace alaska {
140140
Header *right = headers + capacity - 1;
141141

142142

143-
auto dump = [&]() {
144-
if (capacity > 32) return;
145-
printf("[");
146-
for (int i = 0; i < capacity; i++) {
147-
auto *cur = &headers[i];
148-
149-
if (cur->is_free()) {
150-
printf("_");
151-
} else {
152-
printf("#");
153-
}
154-
}
155-
printf("]\n");
156-
157-
printf(" ");
158-
for (int i = 0; i < capacity; i++) {
159-
auto *cur = &headers[i];
160-
if (cur == left or cur == right) {
161-
printf("^");
162-
} else if (cur == last_object) {
163-
printf("X");
164-
} else {
165-
printf(" ");
166-
}
167-
}
168-
printf("\n");
169-
};
170143

171144

172145
while (right > left) {
@@ -219,7 +192,6 @@ namespace alaska {
219192
handle_mapping->set_pointer(free_slot);
220193
// make sure the headers make sense
221194
*left = *right;
222-
dump();
223195

224196
ALASKA_ASSERT(left->get_mapping() == right->get_mapping(), ".");
225197
right->set_mapping(0);
@@ -231,7 +203,6 @@ namespace alaska {
231203
// left++;
232204
}
233205

234-
dump();
235206
// If we were lucky, and no pinned object were found, we need to
236207
// point last_object to the end of the heap, which at this point
237208
// is `right`
@@ -262,8 +233,6 @@ namespace alaska {
262233
auto *lo = ind_to_header(left);
263234
auto *ro = ind_to_header(right);
264235

265-
// printf("%ld %ld %p %p\n", left, right, lo->get_mapping(), ro->get_mapping());
266-
267236
auto *rm = ro->get_mapping();
268237
auto *lm = lo->get_mapping();
269238

runtime/core/ThreadCache.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,13 @@ namespace alaska {
187187
return_value = m->to_handle();
188188
}
189189

190-
// printf("hrealloc %p %p %8zu -> %p %p %8zu\n", handle, original_data, original_size,
191-
// return_value, new_data, new_size);
192-
193190
return return_value;
194191
}
195192

196193

197194
void ThreadCache::hfree(void *handle) {
198195
alaska::Mapping *m = alaska::Mapping::from_handle_safe(handle);
199196
if (unlikely(m == nullptr)) {
200-
// printf("attempt to free non handle %p\n", handle);
201197
bool worked = this->runtime.heap.huge_allocator.free(handle);
202198
(void)worked;
203199
// ALASKA_ASSERT(worked, "huge free failed");
@@ -240,7 +236,6 @@ namespace alaska {
240236
bool ThreadCache::localize(void *handle, uint64_t epoch) {
241237
alaska::Mapping *m = alaska::Mapping::from_handle_safe(handle);
242238
if (unlikely(m == nullptr)) {
243-
printf("%p No because not handle\n", handle);
244239
return false;
245240
}
246241

@@ -256,12 +251,10 @@ namespace alaska {
256251
auto *source_page = this->runtime.heap.pt.get_unaligned(ptr);
257252
// if there wasn't a page for some reason, we can't localize.
258253
if (unlikely(source_page == nullptr)) {
259-
// printf("no because no page for %p\n", (void*)m.encode());
260254
return false;
261255
}
262256
// if the page has recently been localized into, don't try again
263257
if (unlikely(!source_page->should_localize_from(epoch))) {
264-
// printf("no because shouldnt localize\n");
265258
return false;
266259
}
267260

runtime/core/Utils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212

1313
#include <alaska/utils.h>
14+
#include <alaska/Logger.hpp>
1415
#include <stdio.h>
1516
#include <string.h>
1617

1718
void alaska_dump_backtrace() {
1819
FILE *stream = fopen("/proc/self/maps", "r");
1920

20-
21-
printf("Memory Map:\n");
21+
alaska::printf("Memory Map:\n");
2222
char line[1024];
2323
while (fgets(line, sizeof(line), stream) != NULL) {
2424
fwrite(line, strlen(line), 1, stderr);

runtime/core/translate.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <alaska/alaska.hpp>
2222
#include <alaska/config.h>
2323
#include <alaska/utils.h>
24+
#include <alaska/Logger.hpp>
2425
#include <dlfcn.h>
2526

2627
/**
@@ -104,7 +105,7 @@ void print_backtrace() {
104105
if (end > base_start) base_start = end;
105106
rbp = *(void **)rbp; // Follow the chain of rbp values
106107
}
107-
printf(" (%zd)\n", end - start);
108+
alaska::printf(" (%zd)\n", end - start);
108109
}
109110

110111
extern bool alaska_should_safepoint;

runtime/include/alaska/Logger.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace alaska {
2222

2323

2424
void set_log_level(int level);
25+
26+
int printf(const char *format, ...);
2527
}; // namespace alaska
2628

2729

runtime/rt/init.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void __attribute__((constructor(102))) alaska_init(void) {
6767
}
6868

6969
void __attribute__((destructor)) alaska_deinit(void) {
70-
pthread_kill(barrier_thread, SIGKILL);
71-
pthread_join(barrier_thread, NULL);
70+
// pthread_kill(barrier_thread, SIGKILL);
71+
// pthread_join(barrier_thread, NULL);
7272

7373
// Note: we don't currently care about deinitializing the runtime for now, since the application
7474
// is about to die and all it's memory is going to be cleaned up.

runtime/yukon/yukon.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
static alaska::ThreadCache *tc = NULL;
3636

3737
static void set_ht_addr(void *addr) {
38-
printf("set htaddr to %p\n", addr);
39-
write_csr(0xc2, addr);
38+
// printf("set htaddr to %p\n", addr);
39+
// write_csr(0xc2, addr);
4040
}
4141

4242
static alaska::Runtime *the_runtime = NULL;
@@ -82,12 +82,20 @@ void segfault_handler(int sig) {
8282
char **ns = backtrace_symbols(array, size);
8383
}
8484

85+
86+
static char stdout_buf[BUFSIZ];
87+
static char stderr_buf[BUFSIZ];
88+
89+
8590
static void init(void) {
91+
setvbuf(stdout, stdout_buf, _IOLBF, BUFSIZ);
92+
setvbuf(stderr, stderr_buf, _IOLBF, BUFSIZ);
93+
8694
alaska::Configuration config;
8795

8896
the_runtime = new alaska::Runtime(config);
8997
void *handle_table_base = the_runtime->handle_table.get_base();
90-
printf("Handle table at %p\n", handle_table_base);
98+
// printf("Handle table at %p\n", handle_table_base);
9199
set_ht_addr(handle_table_base);
92100
// Make sure the handle table performs mlocks
93101
the_runtime->handle_table.enable_mlock();
@@ -106,7 +114,9 @@ static alaska::ThreadCache *get_tc() {
106114
return tc;
107115
}
108116

109-
void __attribute__((constructor(102))) alaska_init(void) {}
117+
void __attribute__((constructor(102))) alaska_init(void) {
118+
init();
119+
}
110120

111121
void __attribute__((destructor)) alaska_deinit(void) {
112122
if (the_runtime != NULL) {
@@ -119,13 +129,13 @@ void __attribute__((destructor)) alaska_deinit(void) {
119129
}
120130

121131

122-
#define malloc halloc
123-
#define calloc hcalloc
124-
#define realloc hrealloc
125-
#define free hfree
132+
// #define malloc halloc
133+
// #define calloc hcalloc
134+
// #define realloc hrealloc
135+
// #define free hfree
126136

127137
static void *_halloc(size_t sz, int zero) {
128-
// print_hex("_halloc", sz);
138+
print_hex("_halloc", sz);
129139
if (dead) {
130140
return alaska_internal_malloc(sz);
131141
}

0 commit comments

Comments
 (0)