diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2024-04-06 20:40:20 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2024-04-06 20:40:20 -0700 |
commit | 2a6b1e134982847f1b6d90fd1e163a16821a4a6e (patch) | |
tree | 8e144266abbd24b6447c1ebc07d73449fb440630 | |
parent | 61a87d12aebbcbd0b70eaa9cb0244b2faf989c91 (diff) |
sendfile not great
-rw-r--r-- | c_version/fakeobj.c | 24 | ||||
-rw-r--r-- | c_version/main.c | 8 | ||||
-rw-r--r-- | gamma_version/fakeobj.c | 24 | ||||
-rw-r--r-- | gamma_version/main.c | 8 |
4 files changed, 36 insertions, 28 deletions
diff --git a/c_version/fakeobj.c b/c_version/fakeobj.c index 0840d3c..cc23d7c 100644 --- a/c_version/fakeobj.c +++ b/c_version/fakeobj.c @@ -88,17 +88,21 @@ fake_obj: int fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); assert(fd >= 0); uint32_t start_word = 0x7F474D41; - write(fd, &start_word, sizeof(start_word)); - write(fd, &new_argc, sizeof(new_argc)); + assert(-1 != write(fd, &start_word, sizeof(start_word))); + assert(-1 != write(fd, &new_argc, sizeof(new_argc))); for (uint32_t j = 0; j < new_argc; j++) { uint32_t len = strlen(new_argv[j]); - write(fd, &len, sizeof(len)); - write(fd, new_argv[j], len * sizeof(new_argv[j][0])); + assert(-1 != write(fd, &len, sizeof(len))); + assert(-1 != write(fd, new_argv[j], len * sizeof(new_argv[j][0]))); } int in_fd = open(preproc_dst, O_RDONLY); assert(in_fd >= 0); struct stat statbuf; fstat(in_fd, &statbuf); - sendfile(fd, in_fd, NULL, statbuf.st_size); + + char *data = malloc(statbuf.st_size); + assert(statbuf.st_size == read(in_fd, data, statbuf.st_size)); + assert(statbuf.st_size == write(fd, data, statbuf.st_size)); + free(data); close(in_fd); close(fd); return; @@ -108,7 +112,7 @@ char **read_fakeobj(char *path, char *c_dst) { int fd = open(path, O_RDONLY); assert(fd >= 0); uint32_t start_word = 0; - read(fd, &start_word, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &start_word, sizeof(uint32_t))); // It's an ELF file! if (start_word == 0x464C457F) { @@ -119,18 +123,18 @@ char **read_fakeobj(char *path, char *c_dst) { // It's a GMA file! if (start_word != 0x7F474D41) { printf("Couldn't read object file '%s'\n", path); - printf("Got start word: %lu\n", start_word); + printf("Got start word: %u\n", start_word); } assert(start_word == 0x7F474D41); uint32_t n_args = 0; - read(fd, &n_args, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &n_args, sizeof(uint32_t))); char **argv = calloc(n_args + 8, sizeof(argv[0])); for (size_t i = 0; i < n_args; i++) { uint32_t len = 0; - read(fd, &len, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &len, sizeof(uint32_t))); argv[i] = calloc(len + 1, sizeof(char)); - read(fd, argv[i], len); + assert(len == read(fd, argv[i], len)); } struct stat statbuf; diff --git a/c_version/main.c b/c_version/main.c index 75a3a9c..5f60673 100644 --- a/c_version/main.c +++ b/c_version/main.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) { // ======= Make a temporary directory... ======= TMPDIR = calloc(128, sizeof(char)); strcpy(TMPDIR, "/tmp/gamma.XXXXXX"); - mkdtemp(TMPDIR); + assert(mkdtemp(TMPDIR)); // ======= Handle -c ... ======= // If "-c" in args, then check if there are any templates/etc. and compile @@ -113,7 +113,7 @@ int main(int argc, char **argv) { for (int i = 0; i < ARGC; i++) { if (!str_ends_with(ARGV[i], ".o")) continue; char *fakeobj_c_dst = calloc(256, sizeof(char)); - sprintf(fakeobj_c_dst, "%s/%lu.unfake.c", TMPDIR, i); + sprintf(fakeobj_c_dst, "%s/%d.unfake.c", TMPDIR, i); fake_argvs[i] = read_fakeobj(ARGV[i], fakeobj_c_dst); if (fake_argvs[i]) { // it's a fake obj... int fake_argc = 0; @@ -121,7 +121,7 @@ int main(int argc, char **argv) { fake_argvs[i][fake_argc++] = "-c"; fake_argvs[i][fake_argc++] = "-o"; char *fakeobj_o_dst = calloc(256, sizeof(char)); - sprintf(fakeobj_o_dst, "%s/%lu.unfake.o", TMPDIR, i); + sprintf(fakeobj_o_dst, "%s/%d.unfake.o", TMPDIR, i); fake_argvs[i][fake_argc++] = fakeobj_o_dst; // NOTE: must match with below! @@ -143,7 +143,7 @@ int main(int argc, char **argv) { // NOTE: every character in the file *should* be contained in a chunk. for (struct chunked_file *file = files; file; file = file->next) { char *c_path = calloc(256, sizeof(char)); - sprintf(c_path, "%s/%d.mangled.c", TMPDIR, file->id); + sprintf(c_path, "%s/%ld.mangled.c", TMPDIR, file->id); FILE *c_file = fopen(c_path, "w"); assert(c_file); diff --git a/gamma_version/fakeobj.c b/gamma_version/fakeobj.c index 0840d3c..cc23d7c 100644 --- a/gamma_version/fakeobj.c +++ b/gamma_version/fakeobj.c @@ -88,17 +88,21 @@ fake_obj: int fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); assert(fd >= 0); uint32_t start_word = 0x7F474D41; - write(fd, &start_word, sizeof(start_word)); - write(fd, &new_argc, sizeof(new_argc)); + assert(-1 != write(fd, &start_word, sizeof(start_word))); + assert(-1 != write(fd, &new_argc, sizeof(new_argc))); for (uint32_t j = 0; j < new_argc; j++) { uint32_t len = strlen(new_argv[j]); - write(fd, &len, sizeof(len)); - write(fd, new_argv[j], len * sizeof(new_argv[j][0])); + assert(-1 != write(fd, &len, sizeof(len))); + assert(-1 != write(fd, new_argv[j], len * sizeof(new_argv[j][0]))); } int in_fd = open(preproc_dst, O_RDONLY); assert(in_fd >= 0); struct stat statbuf; fstat(in_fd, &statbuf); - sendfile(fd, in_fd, NULL, statbuf.st_size); + + char *data = malloc(statbuf.st_size); + assert(statbuf.st_size == read(in_fd, data, statbuf.st_size)); + assert(statbuf.st_size == write(fd, data, statbuf.st_size)); + free(data); close(in_fd); close(fd); return; @@ -108,7 +112,7 @@ char **read_fakeobj(char *path, char *c_dst) { int fd = open(path, O_RDONLY); assert(fd >= 0); uint32_t start_word = 0; - read(fd, &start_word, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &start_word, sizeof(uint32_t))); // It's an ELF file! if (start_word == 0x464C457F) { @@ -119,18 +123,18 @@ char **read_fakeobj(char *path, char *c_dst) { // It's a GMA file! if (start_word != 0x7F474D41) { printf("Couldn't read object file '%s'\n", path); - printf("Got start word: %lu\n", start_word); + printf("Got start word: %u\n", start_word); } assert(start_word == 0x7F474D41); uint32_t n_args = 0; - read(fd, &n_args, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &n_args, sizeof(uint32_t))); char **argv = calloc(n_args + 8, sizeof(argv[0])); for (size_t i = 0; i < n_args; i++) { uint32_t len = 0; - read(fd, &len, sizeof(uint32_t)); + assert(sizeof(uint32_t) == read(fd, &len, sizeof(uint32_t))); argv[i] = calloc(len + 1, sizeof(char)); - read(fd, argv[i], len); + assert(len == read(fd, argv[i], len)); } struct stat statbuf; diff --git a/gamma_version/main.c b/gamma_version/main.c index fcb1374..a402e1b 100644 --- a/gamma_version/main.c +++ b/gamma_version/main.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) { // ======= Make a temporary directory... ======= TMPDIR = calloc(128, sizeof(char)); strcpy(TMPDIR, "/tmp/gamma.XXXXXX"); - mkdtemp(TMPDIR); + assert(mkdtemp(TMPDIR)); // ======= Handle -c ... ======= // If "-c" in args, then check if there are any templates/etc. and compile @@ -113,7 +113,7 @@ int main(int argc, char **argv) { for (int i = 0; i < ARGC; i++) { if (!str_ends_with(ARGV[i], ".o")) continue; char *fakeobj_c_dst = calloc(256, sizeof(char)); - sprintf(fakeobj_c_dst, "%s/%lu.unfake.c", TMPDIR, i); + sprintf(fakeobj_c_dst, "%s/%u.unfake.c", TMPDIR, i); fake_argvs[i] = read_fakeobj(ARGV[i], fakeobj_c_dst); if (fake_argvs[i]) { // it's a fake obj... int fake_argc = 0; @@ -121,7 +121,7 @@ int main(int argc, char **argv) { fake_argvs[i][fake_argc++] = "-c"; fake_argvs[i][fake_argc++] = "-o"; char *fakeobj_o_dst = calloc(256, sizeof(char)); - sprintf(fakeobj_o_dst, "%s/%lu.unfake.o", TMPDIR, i); + sprintf(fakeobj_o_dst, "%s/%u.unfake.o", TMPDIR, i); fake_argvs[i][fake_argc++] = fakeobj_o_dst; // NOTE: must match with below! @@ -143,7 +143,7 @@ int main(int argc, char **argv) { // NOTE: every character in the file *should* be contained in a chunk. for (struct chunked_file *file = files; file; file = file->next) { char *c_path = calloc(256, sizeof(char)); - sprintf(c_path, "%s/%d.mangled.c", TMPDIR, file->id); + sprintf(c_path, "%s/%ld.mangled.c", TMPDIR, file->id); FILE *c_file = fopen(c_path, "w"); assert(c_file); |