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: refactor core CJIT code #90

Merged
merged 8 commits into from
Dec 26, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: cjit_exec now does relocation
cleanup a bit the portable code
jaromil committed Dec 25, 2024
commit 3b082a36656780efdfd02d15b7ff997e11df413d
62 changes: 29 additions & 33 deletions src/cjit.c
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@
#if defined(WINDOWS)
char tempPath[MAX_PATH];
char filename [64];
snprintf(filename,63,"CJIT-%s",VERSION);

Check warning on line 51 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L51

If you can, use sizeof(filename) instead of 63 as the 2nd arg to snprintf. [runtime/printf] [3]
Raw output
src/cjit.c:51:  If you can, use sizeof(filename) instead of 63 as the 2nd arg to snprintf.  [runtime/printf] [3]
// Get the temporary path
if (GetTempPath(MAX_PATH, tempPath) == 0) {
_err("Failed to get temporary path");
@@ -76,7 +76,7 @@
}
}
#else // POSIX
snprintf(tempDir,259,"/tmp/cjit-%s",VERSION);

Check warning on line 79 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L79

If you can, use sizeof(tempDir) instead of 259 as the 2nd arg to snprintf. [runtime/printf] [3]
Raw output
src/cjit.c:79:  If you can, use sizeof(tempDir) instead of 259 as the 2nd arg to snprintf.  [runtime/printf] [3]
struct stat info;
if (stat(tempDir, &info) != 0) {
// stat() failed; the path does not exist
@@ -92,7 +92,7 @@
if(cjit->fresh) mkdir(tempDir,0755);
#endif
cjit->tmpdir = malloc(strlen(tempDir)+1);
strcpy(cjit->tmpdir, tempDir);

Check warning on line 95 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L95

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/cjit.c:95:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
return(true);
}

@@ -151,7 +151,7 @@
tcc_define_symbol(cjit->TCC,"DMON_IMPL",NULL);
#if defined(CJIT_BUILD_LINUX)
tcc_define_symbol(cjit->TCC,"DMON_OS_LINUX",NULL);
// TODO: test dmon on OSX (missing library frameworks)

Check warning on line 154 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L154

Missing username in TODO; it should look like "// TODO(my_username): Stuff." [readability/todo] [2]
Raw output
src/cjit.c:154:  Missing username in TODO; it should look like "// TODO(my_username): Stuff."  [readability/todo] [2]
// #elif defined(CJIT_BUILD_OSX)
// tcc_define_symbol(cjit->TCC,"DMON_OS_MACOS",NULL);
#elif defined(CJIT_BUILD_WIN)
@@ -182,17 +182,17 @@
tcc_add_library_path(cjit->TCC, "C:\\Windows\\SysWOW64");
// tinycc win32 headers
char *tpath = malloc(strlen(cjit->tmpdir)+32);
strcpy(tpath,cjit->tmpdir);

Check warning on line 185 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L185

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/cjit.c:185:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
strcat(tpath,"/tinycc_win32/winapi");

Check warning on line 186 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L186

Almost always, snprintf is better than strcat [runtime/printf] [4]
Raw output
src/cjit.c:186:  Almost always, snprintf is better than strcat  [runtime/printf] [4]
tcc_add_sysinclude_path(cjit->TCC, tpath);
free(tpath);
// windows SDK headers
char *sdkpath = malloc(512);
if( get_winsdkpath(sdkpath,511) ) {
int pathend = strlen(sdkpath);
strcpy(&sdkpath[pathend],"\\um"); // um/GL

Check warning on line 193 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L193

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/cjit.c:193:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
tcc_add_sysinclude_path(cjit->TCC, sdkpath);
strcpy(&sdkpath[pathend],"\\shared"); // winapifamili.h etc.

Check warning on line 195 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L195

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/cjit.c:195:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
tcc_add_sysinclude_path(cjit->TCC, sdkpath);
}
free(sdkpath);
@@ -203,14 +203,19 @@
}

int cjit_exec(CJITState *cjit, int argc, char **argv) {
#if defined(WINDOWS)
int res = 1;
int (*_ep)(int, char**);
// relocate the code (link symbols)
if (tcc_relocate(cjit->TCC) < 0) {
_err("TCC symbol relocation error (some library missing?)");
return -1;
}
_ep = tcc_get_symbol(cjit->TCC, cjit->entry?cjit->entry:"main");
if (!_ep) {
_err("Symbol not found in source: %s",cjit->entry?cjit->entry:"main");
return -1;
}
#if defined(WINDOWS)
if(cjit->write_pid) {
pid_t pid = getpid();
FILE *fd = fopen(cjit->write_pid, "w");
@@ -224,21 +229,12 @@
}
res = _ep(argc, argv);
return(res);

#else // we assume anything else but WINDOWS has fork()

pid_t pid;
int res = 1;
int (*_ep)(int, char**);
_ep = tcc_get_symbol(cjit->TCC, cjit->entry?cjit->entry:"main");
if (!_ep) {
_err("Symbol not found in source: %s",cjit->entry?cjit->entry:"main");
return -1;
}
pid = fork();
if (pid == 0) {
res = _ep(argc, argv);
exit(res);
res = _ep(argc, argv);
exit(res);
} else {
if(cjit->write_pid) {
// pid_t pid = getpid();
@@ -251,27 +247,27 @@
fprintf(fd,"%d\n",pid);
fclose(fd);
}
int status;
int ret;
ret = waitpid(pid, &status, WUNTRACED | WCONTINUED);
if (ret != pid){
_err("Wait error in source: %s","main");
}
if (WIFEXITED(status)) {
res = WEXITSTATUS(status);
//_err("Process has returned %d", res);
} else if (WIFSIGNALED(status)) {
res = WTERMSIG(status);
_err("Process terminated with signal %d", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
res = WSTOPSIG(status);
//_err("Process has returned %d", WSTOPSIG(status));
} else if (WIFSTOPPED(status)) {
res = WSTOPSIG(status);
_err("Process stopped with signal", WSTOPSIG(status));
} else {
_err("wait: unknown status: %d", status);
}
int status;
int ret;
ret = waitpid(pid, &status, WUNTRACED | WCONTINUED);
if (ret != pid){
_err("Wait error in source: %s","main");
}
if (WIFEXITED(status)) {
res = WEXITSTATUS(status);
//_err("Process has returned %d", res);
} else if (WIFSIGNALED(status)) {
res = WTERMSIG(status);
_err("Process terminated with signal %d", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
res = WSTOPSIG(status);
//_err("Process has returned %d", WSTOPSIG(status));
} else if (WIFSTOPPED(status)) {
res = WSTOPSIG(status);
_err("Process stopped with signal", WSTOPSIG(status));
} else {
_err("wait: unknown status: %d", status);
}
}
return res;
#endif // cjit_exec with fork()
@@ -308,7 +304,7 @@
int len;
va_list args;
va_start(args, fmt);
len = vsnprintf(msg, MAX_STRING, fmt, args);

Check warning on line 307 in src/cjit.c

GitHub Actions / cpplint

[cpplint] src/cjit.c#L307

Add #include <cstdio> for vsnprintf [build/include_what_you_use] [4]
Raw output
src/cjit.c:307:  Add #include <cstdio> for vsnprintf  [build/include_what_you_use] [4]
va_end(args);
msg[len] = '\n';
msg[len+1] = 0x0;
6 changes: 0 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -144,12 +144,12 @@
if(!CJIT->quiet)_err("entry: %s",opt.arg);
if(CJIT->entry) free(CJIT->entry);
CJIT->entry = malloc(strlen(opt.arg)+1);
strcpy(CJIT->entry,opt.arg);

Check warning on line 147 in src/main.c

GitHub Actions / cpplint

[cpplint] src/main.c#L147

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/main.c:147:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
} else if (c == 'p') { // write pid to file
if(!CJIT->quiet)_err("pid file: %s",opt.arg);
if(CJIT->write_pid) free(CJIT->write_pid);
CJIT->write_pid = malloc(strlen(opt.arg)+1);
strcpy(CJIT->write_pid,opt.arg);

Check warning on line 152 in src/main.c

GitHub Actions / cpplint

[cpplint] src/main.c#L152

Almost always, snprintf is better than strcpy [runtime/printf] [4]
Raw output
src/main.c:152:  Almost always, snprintf is better than strcpy  [runtime/printf] [4]
} else if (c == 301) {
#if defined(_WIN32)
_err("Live mode not supported in Windows");
@@ -158,7 +158,7 @@
CJIT->live = true;
#endif
} else if (c == 401) { // --temp
fprintf(stdout,"%s\n",CJIT->tmpdir);

Check warning on line 161 in src/main.c

GitHub Actions / cpplint

[cpplint] src/main.c#L161

Add #include <cstdio> for fprintf [build/include_what_you_use] [4]
Raw output
src/main.c:161:  Add #include <cstdio> for fprintf  [build/include_what_you_use] [4]
cjit_free(CJIT);
exit(0);
} else if (c == 501) { // --utar
@@ -259,12 +259,6 @@
}
}

// relocate the code (link symbols)
if (tcc_relocate(CJIT->TCC) < 0) {
_err("TCC symbol relocation error (some library missing?)");
goto endgame;
}

// number of args at the left hand of arg separator, or all of them
int right_args = argc-left_args+1;//arg_separator? argc-arg_separator : 0;
char **right_argv = &argv[left_args-1];//arg_separator?&argv[arg_separator]:0