comparison mrjunejune/latex_renderer.c @ 247:70f2a3dafc1c

[build] Bundle offline Tectonic runtime Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Mon, 03 Aug 2026 20:20:06 -0700
parents b8aa08503378
children b8b6e726964a
comparison
equal deleted inserted replaced
246:4f2b50bc78e7 247:70f2a3dafc1c
42 #endif 42 #endif
43 43
44 #define LATEX_DIAGNOSTICS_MAX_BYTES (16 * 1024) 44 #define LATEX_DIAGNOSTICS_MAX_BYTES (16 * 1024)
45 #define LATEX_CPU_TIMEOUT_SECONDS 5 45 #define LATEX_CPU_TIMEOUT_SECONDS 5
46 #define LATEX_WALL_TIMEOUT_SECONDS 8 46 #define LATEX_WALL_TIMEOUT_SECONDS 8
47 #define TECTONIC_BUNDLE_URL "https://data1.fullyjustified.net/tlextras-2022.0r0.tar"
48 #define TECTONIC_CACHE_PATH "third_party/tectonic/runtime/cache"
49 #define TECTONIC_COMPILER_PATH "third_party/tectonic/runtime/bin/tectonic"
50 #define TECTONIC_FONTCONFIG_SYSROOT_PATH "third_party/tectonic/runtime/sysroot"
47 51
48 static char *duplicate_message(const char *message) 52 static char *duplicate_message(const char *message)
49 { 53 {
50 size_t length = strlen(message); 54 size_t length = strlen(message);
51 char *copy = malloc(length + 1); 55 char *copy = malloc(length + 1);
164 0) == 0; 168 0) == 0;
165 close(path_fd); 169 close(path_fd);
166 return ok; 170 return ok;
167 } 171 }
168 172
169 static bool apply_filesystem_sandbox(const char *job_directory) 173 static bool add_landlock_tree_rules(
174 int ruleset_fd,
175 const char *path,
176 uint64_t access,
177 unsigned depth)
178 {
179 if (depth > 32 ||
180 !add_landlock_path_rule(ruleset_fd, path, access, true))
181 return false;
182
183 struct stat status;
184 if (lstat(path, &status) != 0)
185 return false;
186 if (!S_ISDIR(status.st_mode))
187 return true;
188
189 DIR *directory = opendir(path);
190 if (!directory)
191 return false;
192 bool ok = true;
193 struct dirent *entry;
194 while (ok && (entry = readdir(directory)) != NULL)
195 {
196 if (strcmp(entry->d_name, ".") == 0 ||
197 strcmp(entry->d_name, "..") == 0)
198 continue;
199 char child[PATH_MAX];
200 int length = snprintf(
201 child,
202 sizeof(child),
203 "%s/%s",
204 path,
205 entry->d_name);
206 ok = length >= 0 &&
207 (size_t)length < sizeof(child) &&
208 add_landlock_tree_rules(
209 ruleset_fd,
210 child,
211 access,
212 depth + 1);
213 }
214 closedir(directory);
215 return ok;
216 }
217
218 static bool apply_filesystem_sandbox(
219 const char *job_directory,
220 const char *compiler,
221 const char *cache_directory,
222 const char *fontconfig_sysroot)
170 { 223 {
171 int abi = syscall( 224 int abi = syscall(
172 SYS_landlock_create_ruleset, 225 SYS_landlock_create_ruleset,
173 NULL, 226 NULL,
174 0, 227 0,
205 sizeof(ruleset), 258 sizeof(ruleset),
206 0); 259 0);
207 if (ruleset_fd < 0) 260 if (ruleset_fd < 0)
208 return false; 261 return false;
209 262
210 const char *read_only_directories[] = {
211 "/usr",
212 "/lib",
213 "/lib64",
214 "/bin",
215 "/var/lib/texmf",
216 "/etc/texmf",
217 "/etc/fonts",
218 };
219 bool ok = true; 263 bool ok = true;
220 for (size_t i = 0; 264 if (ok)
221 ok && i < sizeof(read_only_directories) / sizeof(read_only_directories[0]);
222 i++)
223 {
224 ok = add_landlock_path_rule( 265 ok = add_landlock_path_rule(
225 ruleset_fd, 266 ruleset_fd,
226 read_only_directories[i], 267 compiler,
268 LANDLOCK_ACCESS_FS_EXECUTE | LANDLOCK_ACCESS_FS_READ_FILE,
269 true);
270 if (ok)
271 ok = add_landlock_tree_rules(
272 ruleset_fd,
273 cache_directory,
274 read_access | LANDLOCK_ACCESS_FS_MAKE_DIR,
275 0);
276 if (ok)
277 ok = add_landlock_tree_rules(
278 ruleset_fd,
279 fontconfig_sysroot,
227 read_access, 280 read_access,
228 false); 281 0);
229 }
230 if (ok)
231 ok = add_landlock_path_rule(
232 ruleset_fd,
233 "/etc/ld.so.cache",
234 LANDLOCK_ACCESS_FS_READ_FILE,
235 false);
236 if (ok) 282 if (ok)
237 ok = add_landlock_path_rule( 283 ok = add_landlock_path_rule(
238 ruleset_fd, 284 ruleset_fd,
239 "/dev/null", 285 "/dev/null",
240 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 286 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE,
308 #undef DENY_SYSCALL 354 #undef DENY_SYSCALL
309 #undef ADD_FILTER 355 #undef ADD_FILTER
310 return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &program) == 0; 356 return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &program) == 0;
311 } 357 }
312 358
313 static void configure_child_environment(const char *job_directory) 359 static void configure_child_environment(
360 const char *job_directory,
361 const char *cache_directory,
362 const char *fontconfig_sysroot)
314 { 363 {
315 clearenv(); 364 clearenv();
316 setenv("HOME", job_directory, 1); 365 setenv("HOME", job_directory, 1);
317 setenv("TMPDIR", job_directory, 1); 366 setenv("TMPDIR", job_directory, 1);
318 setenv("TEXMFVAR", job_directory, 1); 367 setenv("XDG_CACHE_HOME", job_directory, 1);
319 setenv("TEXMFCONFIG", job_directory, 1); 368 char tectonic_cache[PATH_MAX];
320 setenv("openin_any", "p", 1); 369 snprintf(
321 setenv("openout_any", "p", 1); 370 tectonic_cache,
322 setenv("shell_escape", "0", 1); 371 sizeof(tectonic_cache),
372 "%s/tectonic",
373 cache_directory);
374 setenv("TECTONIC_CACHE_DIR", tectonic_cache, 1);
375 setenv("FONTCONFIG_FILE", "fonts.conf", 1);
376 setenv("FONTCONFIG_PATH", "/etc/fonts", 1);
377 setenv("FONTCONFIG_SYSROOT", fontconfig_sysroot, 1);
378 setenv("TECTONIC_UNTRUSTED_MODE", "1", 1);
323 setenv("SOURCE_DATE_EPOCH", "0", 1); 379 setenv("SOURCE_DATE_EPOCH", "0", 1);
380 setenv("TZ", "UTC", 1);
324 setenv("LANG", "C.UTF-8", 1); 381 setenv("LANG", "C.UTF-8", 1);
325 } 382 }
326 383
327 static void close_inherited_descriptors(void) 384 static void close_inherited_descriptors(void)
328 { 385 {
340 close(fd); 397 close(fd);
341 } 398 }
342 399
343 static void run_compiler_child( 400 static void run_compiler_child(
344 const char *compiler, 401 const char *compiler,
402 const char *cache_directory,
403 const char *fontconfig_sysroot,
345 const char *job_directory, 404 const char *job_directory,
346 int output_fd) 405 int output_fd)
347 { 406 {
348 if (dup2(output_fd, STDOUT_FILENO) < 0 || 407 if (dup2(output_fd, STDOUT_FILENO) < 0 ||
349 dup2(output_fd, STDERR_FILENO) < 0) 408 dup2(output_fd, STDERR_FILENO) < 0)
356 getppid() == 1) 415 getppid() == 1)
357 _exit(119); 416 _exit(119);
358 if (chdir(job_directory) != 0) 417 if (chdir(job_directory) != 0)
359 _exit(121); 418 _exit(121);
360 419
361 configure_child_environment(job_directory); 420 configure_child_environment(
421 job_directory,
422 cache_directory,
423 fontconfig_sysroot);
362 if (!set_limit(RLIMIT_CPU, LATEX_CPU_TIMEOUT_SECONDS) || 424 if (!set_limit(RLIMIT_CPU, LATEX_CPU_TIMEOUT_SECONDS) ||
363 !set_limit(RLIMIT_AS, 768 * 1024 * 1024) || 425 !set_limit(RLIMIT_AS, 768 * 1024 * 1024) ||
364 !set_limit(RLIMIT_FSIZE, LATEX_PDF_MAX_BYTES) || 426 !set_limit(RLIMIT_FSIZE, LATEX_PDF_MAX_BYTES) ||
365 !set_limit(RLIMIT_NOFILE, 64) || 427 !set_limit(RLIMIT_NOFILE, 64) ||
366 !set_limit(RLIMIT_NPROC, 1)) 428 !set_limit(RLIMIT_NPROC, 1))
367 _exit(122); 429 _exit(122);
368 if (!apply_filesystem_sandbox(job_directory) || 430 if (!apply_filesystem_sandbox(
431 job_directory,
432 compiler,
433 cache_directory,
434 fontconfig_sysroot) ||
369 !apply_network_sandbox()) 435 !apply_network_sandbox())
370 _exit(123); 436 _exit(123);
371 437
372 char *const arguments[] = { 438 char *const arguments[] = {
373 (char *)compiler, 439 (char *)compiler,
374 "-interaction=nonstopmode", 440 "--untrusted",
375 "-halt-on-error", 441 "--only-cached",
376 "-no-shell-escape", 442 "--chatter",
377 "-file-line-error", 443 "minimal",
378 "-output-directory=.", 444 "--color",
445 "never",
446 "--reruns",
447 "1",
448 "--outdir",
449 ".",
450 "--bundle",
451 TECTONIC_BUNDLE_URL,
379 "document.tex", 452 "document.tex",
380 NULL, 453 NULL,
381 }; 454 };
382 execv(compiler, arguments); 455 execv(compiler, arguments);
383 _exit(errno == ENOENT ? 127 : 126); 456 _exit(errno == ENOENT ? 127 : 126);
514 return false; 587 return false;
515 } 588 }
516 *data = content; 589 *data = content;
517 *size = offset; 590 *size = offset;
518 return true; 591 return true;
592 }
593
594 static bool resolve_runtime_path(
595 const char *relative_path,
596 char resolved[PATH_MAX])
597 {
598 if (realpath(relative_path, resolved))
599 return true;
600
601 const char *runfiles = getenv("RUNFILES_DIR");
602 const char *workspace = getenv("TEST_WORKSPACE");
603 char candidate[PATH_MAX];
604 if (runfiles && workspace)
605 {
606 int length = snprintf(
607 candidate,
608 sizeof(candidate),
609 "%s/%s/%s",
610 runfiles,
611 workspace,
612 relative_path);
613 if (length >= 0 &&
614 (size_t)length < sizeof(candidate) &&
615 realpath(candidate, resolved))
616 return true;
617 }
618
619 char executable[PATH_MAX];
620 ssize_t executable_length = readlink(
621 "/proc/self/exe",
622 executable,
623 sizeof(executable) - 1);
624 if (executable_length <= 0)
625 return false;
626 executable[executable_length] = '\0';
627
628 char *last_slash = strrchr(executable, '/');
629 if (last_slash)
630 {
631 *last_slash = '\0';
632 int length = snprintf(
633 candidate,
634 sizeof(candidate),
635 "%s/%s",
636 executable,
637 relative_path);
638 if (length >= 0 &&
639 (size_t)length < sizeof(candidate) &&
640 realpath(candidate, resolved))
641 return true;
642 }
643
644 char *package = strstr(executable, "/mrjunejune/");
645 if (!package)
646 return false;
647 *package = '\0';
648 int length = snprintf(
649 candidate,
650 sizeof(candidate),
651 "%s/%s",
652 executable,
653 relative_path);
654 return length >= 0 &&
655 (size_t)length < sizeof(candidate) &&
656 realpath(candidate, resolved) != NULL;
519 } 657 }
520 658
521 Latex_Render_Result Latex_Render(const uint8_t *source, size_t source_size) 659 Latex_Render_Result Latex_Render(const uint8_t *source, size_t source_size)
522 { 660 {
523 if (!source || source_size == 0) 661 if (!source || source_size == 0)
531 if (memchr(source, '\0', source_size)) 669 if (memchr(source, '\0', source_size))
532 return result_with_message( 670 return result_with_message(
533 LATEX_RENDER_INVALID_INPUT, 671 LATEX_RENDER_INVALID_INPUT,
534 "LaTeX source cannot contain NUL bytes."); 672 "LaTeX source cannot contain NUL bytes.");
535 673
536 const char *compiler = "/usr/bin/pdflatex"; 674 char compiler[PATH_MAX];
537 if (access(compiler, X_OK) != 0) 675 char cache_directory[PATH_MAX];
676 char fontconfig_sysroot[PATH_MAX];
677 if (!resolve_runtime_path(TECTONIC_COMPILER_PATH, compiler) ||
678 !resolve_runtime_path(TECTONIC_CACHE_PATH, cache_directory) ||
679 !resolve_runtime_path(
680 TECTONIC_FONTCONFIG_SYSROOT_PATH,
681 fontconfig_sysroot) ||
682 access(compiler, X_OK) != 0)
538 return result_with_message( 683 return result_with_message(
539 LATEX_RENDER_COMPILER_UNAVAILABLE, 684 LATEX_RENDER_COMPILER_UNAVAILABLE,
540 "The server LaTeX compiler is unavailable."); 685 "The Bazel Tectonic runtime is unavailable.");
541 686
542 char job_directory[] = "/tmp/mrjunejune-latex-XXXXXX"; 687 char job_directory[] = "/tmp/mrjunejune-latex-XXXXXX";
543 if (!mkdtemp(job_directory)) 688 if (!mkdtemp(job_directory))
544 return result_with_message( 689 return result_with_message(
545 LATEX_RENDER_INTERNAL_ERROR, 690 LATEX_RENDER_INTERNAL_ERROR,
586 "Unable to start the LaTeX compiler."); 731 "Unable to start the LaTeX compiler.");
587 } 732 }
588 if (child == 0) 733 if (child == 0)
589 { 734 {
590 close(output_pipe[0]); 735 close(output_pipe[0]);
591 run_compiler_child(compiler, job_directory, output_pipe[1]); 736 run_compiler_child(
737 compiler,
738 cache_directory,
739 fontconfig_sysroot,
740 job_directory,
741 output_pipe[1]);
592 } 742 }
593 setpgid(child, child); 743 setpgid(child, child);
594 744
595 close(output_pipe[1]); 745 close(output_pipe[1]);
596 char *diagnostics = calloc(1, LATEX_DIAGNOSTICS_MAX_BYTES + 1); 746 char *diagnostics = calloc(1, LATEX_DIAGNOSTICS_MAX_BYTES + 1);