Skip to content

Commit

Permalink
Fix crash when enabling debugger under Linux
Browse files Browse the repository at this point in the history
Rename buffer_init() to avoid clashing with a function with the same
name in luasocket code: under Linux, due to the of "flat" ELF
namespaces, when luasocket shared library is loaded into premake process
the existing premake function is used instead of the function defined in
luasocket code, resulting in luasocket buffer not being properly
initialized which, in turn, leads to mysterious crashes as soon as it's
used, e.g. dereferencing null timeout field:

	(gdb) bt
	#0  0x00007ffff7cce642 in timeout_markstart (tm=0x0) at ../../binmodules/luasocket/src/timeout.c:116
	premake#1  0x00007ffff7cc7618 in buffer_meth_receive (L=0x5555557882a8, buf=0x5555559044a0) at ../../binmodules/luasocket/src/buffer.c:113
	premake#2  0x00007ffff7ccd545 in meth_receive (L=0x5555557882a8) at ../../binmodules/luasocket/src/tcp.c:135
	premake#3  0x000055555558c1cc in luaD_precall (L=0x5555557882a8, func=0x5555558e36b0, nresults=1) at ../../contrib/lua/src/ldo.c:434
	premake#4  0x00005555555a971f in luaV_execute (L=0x5555557882a8) at ../../contrib/lua/src/lvm.c:1134
	premake#5  0x000055555558c555 in luaD_call (L=0x5555557882a8, func=0x5555558e3640, nResults=0) at ../../contrib/lua/src/ldo.c:499
	premake#6  0x000055555558c5b3 in luaD_callnoyield (L=0x5555557882a8, func=0x5555558e3640, nResults=0) at ../../contrib/lua/src/ldo.c:509
	premake#7  0x0000555555588af5 in lua_callk (L=0x5555557882a8, nargs=2, nresults=0, ctx=0, k=0x0) at ../../contrib/lua/src/lapi.c:925
	premake#8  0x00005555555af4f2 in hookf (L=0x5555557882a8, ar=0x7fffffffd270) at ../../contrib/lua/src/ldblib.c:316
	premake#9  0x000055555558bafb in luaD_hook (L=0x5555557882a8, event=2, line=273) at ../../contrib/lua/src/ldo.c:269
	premake#10 0x000055555558b284 in luaG_traceexec (L=0x5555557882a8) at ../../contrib/lua/src/ldebug.c:687
	premake#11 0x00005555555a6b71 in luaV_execute (L=0x5555557882a8) at ../../contrib/lua/src/lvm.c:801
	premake#12 0x000055555558c555 in luaD_call (L=0x5555557882a8, func=0x555555889360, nResults=1) at ../../contrib/lua/src/ldo.c:499
	premake#13 0x000055555558c5b3 in luaD_callnoyield (L=0x5555557882a8, func=0x555555889360, nResults=1) at ../../contrib/lua/src/ldo.c:509
	premake#14 0x0000555555588b60 in f_call (L=0x5555557882a8, ud=0x7fffffffdbb0) at ../../contrib/lua/src/lapi.c:943
	premake#15 0x000055555558b5a5 in luaD_rawrunprotected (L=0x5555557882a8, f=0x555555588b2b <f_call>, ud=0x7fffffffdbb0)
	    at ../../contrib/lua/src/ldo.c:142
	premake#16 0x000055555558cd85 in luaD_pcall (L=0x5555557882a8, func=0x555555588b2b <f_call>, u=0x7fffffffdbb0, old_top=64, ef=48)
	    at ../../contrib/lua/src/ldo.c:729
	premake#17 0x0000555555588c28 in lua_pcallk (L=0x5555557882a8, nargs=0, nresults=1, errfunc=3, ctx=0, k=0x0)
	    at ../../contrib/lua/src/lapi.c:969
	premake#18 0x0000555555584734 in premake_pcall (L=0x5555557882a8, nargs=0, nresults=1) at ../../src/host/premake.c:287
	premake#19 0x0000555555584867 in premake_execute (L=0x5555557882a8, argc=5, argv=0x7fffffffdd98,
	    script=0x555555685052 "src/_premake_main.lua") at ../../src/host/premake.c:316
	premake#20 0x000055555558535e in main (argc=5, argv=0x7fffffffdd98) at ../../src/host/premake_main.c:19

For consistency, rename all the other buffer_xxx functions too, even
though they don't conflict with anything right now.
  • Loading branch information
vadz committed Aug 27, 2023
1 parent 703ff07 commit e739d88
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/host/buffered_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
#include "premake.h"
#include "buffered_io.h"

void buffer_init(Buffer* b)
void premake_buffer_init(Buffer* b)
{
b->capacity = 0;
b->length = 0;
b->data = NULL;
}

void buffer_destroy(Buffer* b)
void premake_buffer_destroy(Buffer* b)
{
free(b->data);
b->capacity = 0;
b->length = 0;
b->data = NULL;
}

void buffer_puts(Buffer* b, const void* ptr, size_t len)
void premake_buffer_puts(Buffer* b, const void* ptr, size_t len)
{
char* data;

Expand Down Expand Up @@ -53,23 +53,23 @@ void buffer_puts(Buffer* b, const void* ptr, size_t len)
b->length += len;
}

void buffer_printf(Buffer* b, const char *fmt, ...)
void premake_buffer_printf(Buffer* b, const char *fmt, ...)
{
char text[2048];
int len;
va_list args;
va_start(args, fmt);
len = vsnprintf(text, sizeof(text) - 1, fmt, args);
va_end(args);
buffer_puts(b, text, len);
premake_buffer_puts(b, text, len);
}

// -- Lua wrappers ----------------------------------------

int buffered_new(lua_State* L)
{
Buffer* b = (Buffer*)malloc(sizeof(Buffer));
buffer_init(b);
premake_buffer_init(b);
lua_pushlightuserdata(L, b);
return 1;
}
Expand All @@ -80,7 +80,7 @@ int buffered_write(lua_State* L)
const char *s = luaL_checklstring(L, 2, &len);
Buffer* b = (Buffer*)lua_touserdata(L, 1);

buffer_puts(b, s, len);
premake_buffer_puts(b, s, len);
return 0;
}

Expand All @@ -91,15 +91,15 @@ int buffered_writeln(lua_State* L)
Buffer* b = (Buffer*)lua_touserdata(L, 1);

if (s != NULL)
buffer_puts(b, s, len);
buffer_puts(b, "\r\n", 2);
premake_buffer_puts(b, s, len);
premake_buffer_puts(b, "\r\n", 2);
return 0;
}

int buffered_close(lua_State* L)
{
Buffer* b = (Buffer*)lua_touserdata(L, 1);
buffer_destroy(b);
premake_buffer_destroy(b);
free(b);
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/host/buffered_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ typedef struct struct_Buffer
char* data;
} Buffer;

void buffer_init(Buffer* b);
void buffer_destroy(Buffer* b);
void premake_buffer_init(Buffer* b);
void premake_buffer_destroy(Buffer* b);

void buffer_puts(Buffer* b, const void* ptr, size_t len);
void buffer_printf(Buffer* b, const char* s, ...);
void premake_buffer_puts(Buffer* b, const void* ptr, size_t len);
void premake_buffer_printf(Buffer* b, const char* s, ...);

#endif
4 changes: 2 additions & 2 deletions src/host/curl_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int curlProgressCallback(curl_state* state, double dltotal, double dlnow, double
size_t curlWriteCallback(char *ptr, size_t size, size_t nmemb, curl_state* state)
{
size_t length = size * nmemb;
buffer_puts(&state->S, ptr, length);
premake_buffer_puts(&state->S, ptr, length);
return length;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ CURL* curlRequest(lua_State* L, curl_state* state, int optionsIndex, int progres
state->RefIndex = 0;
state->errorBuffer[0] = '\0';
state->headers = NULL;
buffer_init(&state->S);
premake_buffer_init(&state->S);

curl_init();
curl = curl_easy_init();
Expand Down
2 changes: 1 addition & 1 deletion src/host/http_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int http_download(lua_State* L)
lua_pushstring(L, "OK");
}

buffer_destroy(&state.S);
premake_buffer_destroy(&state.S);
lua_pushnumber(L, (lua_Number)responseCode);
return 2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/host/http_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int http_get(lua_State* L)
lua_pushstring(L, "OK");
}

buffer_destroy(&state.S);
premake_buffer_destroy(&state.S);
lua_pushnumber(L, (lua_Number)responseCode);
return 3;
}
Expand Down
2 changes: 1 addition & 1 deletion src/host/http_post.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int http_post(lua_State* L)
lua_pushstring(L, "OK");
}

buffer_destroy(&state.S);
premake_buffer_destroy(&state.S);
lua_pushnumber(L, (lua_Number)responseCode);
return 3;
}
Expand Down

0 comments on commit e739d88

Please sign in to comment.