Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "native_client/src/nonsfi/irt/irt_interfaces.h" | 7 #include "native_client/src/nonsfi/irt/irt_interfaces.h" |
| 8 | 8 |
| 9 #include <assert.h> | 9 #include <assert.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 #include <fcntl.h> | 11 #include <fcntl.h> |
| 12 #include <limits.h> | 12 #include <limits.h> |
| 13 #include <pthread.h> | 13 #include <pthread.h> |
| 14 #include <stdio.h> | 14 #include <stdio.h> |
| 15 #include <stdlib.h> | 15 #include <stdlib.h> |
| 16 #include <string.h> | 16 #include <string.h> |
| 17 #include <sys/mman.h> | 17 #include <sys/mman.h> |
| 18 #include <sys/stat.h> | 18 #include <sys/stat.h> |
| 19 #include <unistd.h> | 19 #include <unistd.h> |
| 20 | 20 |
| 21 #if defined(__linux__) | 21 #if defined(__linux__) |
| 22 # include <linux/futex.h> | 22 # include <linux/futex.h> |
| 23 # include <sys/syscall.h> | 23 # include <sys/syscall.h> |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #include "native_client/src/include/elf32.h" | 26 #include "native_client/src/include/elf32.h" |
| 27 #include "native_client/src/include/elf_auxv.h" | 27 #include "native_client/src/include/elf_auxv.h" |
| 28 #include "native_client/src/include/nacl/nacl_exception.h" | 28 #include "native_client/src/include/nacl/nacl_exception.h" |
| 29 #include "native_client/src/include/nacl/nacl_signal.h" | |
| 29 #include "native_client/src/include/nacl_macros.h" | 30 #include "native_client/src/include/nacl_macros.h" |
| 30 #include "native_client/src/public/irt_core.h" | 31 #include "native_client/src/public/irt_core.h" |
| 31 #include "native_client/src/public/nonsfi/irt_exception_handling.h" | 32 #include "native_client/src/public/nonsfi/irt_signal_handling.h" |
| 32 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" | 33 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" |
| 33 #include "native_client/src/trusted/service_runtime/include/sys/mman.h" | 34 #include "native_client/src/trusted/service_runtime/include/sys/mman.h" |
| 34 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" | 35 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" |
| 35 #include "native_client/src/trusted/service_runtime/include/sys/time.h" | 36 #include "native_client/src/trusted/service_runtime/include/sys/time.h" |
| 36 #include "native_client/src/trusted/service_runtime/include/sys/unistd.h" | 37 #include "native_client/src/trusted/service_runtime/include/sys/unistd.h" |
| 37 #include "native_client/src/untrusted/irt/irt.h" | 38 #include "native_client/src/untrusted/irt/irt.h" |
| 38 #include "native_client/src/untrusted/irt/irt_dev.h" | 39 #include "native_client/src/untrusted/irt/irt_dev.h" |
| 39 #include "native_client/src/untrusted/irt/irt_interfaces.h" | 40 #include "native_client/src/untrusted/irt/irt_interfaces.h" |
| 40 #include "native_client/src/untrusted/nacl/nacl_random.h" | 41 #include "native_client/src/untrusted/nacl/nacl_random.h" |
| 42 #include "native_client/src/untrusted/pthread/pthread_types.h" | |
|
Mark Seaborn
2015/06/26 18:31:59
Not used? irt_interfaces.c shouldn't depend on sr
Luis Héctor Chávez
2015/07/06 23:44:59
Removed.
| |
| 41 | 43 |
| 42 #if defined(__native_client__) && defined(__arm__) | 44 #if defined(__native_client__) && defined(__arm__) |
| 43 #include "native_client/src/nonsfi/irt/irt_icache.h" | 45 #include "native_client/src/nonsfi/irt/irt_icache.h" |
| 44 #endif | 46 #endif |
| 45 | 47 |
| 46 /* | 48 /* |
| 47 * This is an implementation of NaCl's IRT interfaces that runs | 49 * This is an implementation of NaCl's IRT interfaces that runs |
| 48 * outside of the NaCl sandbox. | 50 * outside of the NaCl sandbox. |
| 49 * | 51 * |
| 50 * This allows PNaCl to be used as a portability layer without the | 52 * This allows PNaCl to be used as a portability layer without the |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 | 348 |
| 347 static void *start_thread(void *arg) { | 349 static void *start_thread(void *arg) { |
| 348 struct thread_args args = *(struct thread_args *) arg; | 350 struct thread_args args = *(struct thread_args *) arg; |
| 349 free(arg); | 351 free(arg); |
| 350 g_tls_value = args.thread_ptr; | 352 g_tls_value = args.thread_ptr; |
| 351 args.start_func(); | 353 args.start_func(); |
| 352 abort(); | 354 abort(); |
| 353 } | 355 } |
| 354 | 356 |
| 355 static int thread_create(void (*start_func)(void), void *stack, | 357 static int thread_create(void (*start_func)(void), void *stack, |
| 356 void *thread_ptr) { | 358 void *thread_ptr, nacl_irt_tid_t *child_tid) { |
| 357 /* | 359 /* |
| 358 * For now, we ignore the stack that user code provides and just use | 360 * For now, we ignore the stack that user code provides and just use |
| 359 * the stack that the host libpthread allocates. | 361 * the stack that the host libpthread allocates. |
| 360 */ | 362 */ |
| 361 pthread_attr_t attr; | 363 pthread_attr_t attr; |
| 362 int error = pthread_attr_init(&attr); | 364 int error = pthread_attr_init(&attr); |
| 363 if (error != 0) | 365 if (error != 0) |
| 364 return error; | 366 return error; |
| 365 error = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 367 error = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 366 if (error != 0) | 368 if (error != 0) |
| 367 return error; | 369 return error; |
| 368 struct thread_args *args = malloc(sizeof(struct thread_args)); | 370 struct thread_args *args = malloc(sizeof(struct thread_args)); |
| 369 if (args == NULL) { | 371 if (args == NULL) { |
| 370 error = ENOMEM; | 372 error = ENOMEM; |
| 371 goto cleanup; | 373 goto cleanup; |
| 372 } | 374 } |
| 373 args->start_func = start_func; | 375 args->start_func = start_func; |
| 374 args->thread_ptr = thread_ptr; | 376 args->thread_ptr = thread_ptr; |
| 375 pthread_t tid; | 377 pthread_t tid; |
| 376 error = pthread_create(&tid, &attr, start_thread, args); | 378 error = pthread_create(&tid, &attr, start_thread, args); |
| 377 if (error != 0) | 379 if (error != 0) { |
| 378 free(args); | 380 free(args); |
| 381 } else if (child_tid != NULL) { | |
| 382 *child_tid = tid->native_tid; | |
|
Mark Seaborn
2015/06/26 18:31:59
This creates an unfortunate dependency on libpthre
Luis Héctor Chávez
2015/07/06 23:44:59
Split that off into https://codereview.chromium.or
| |
| 383 } | |
| 379 cleanup: | 384 cleanup: |
| 380 pthread_attr_destroy(&attr); | 385 pthread_attr_destroy(&attr); |
| 381 return error; | 386 return error; |
| 382 } | 387 } |
| 383 | 388 |
| 389 static int thread_create_v0_1(void (*start_func)(void), void *stack, | |
| 390 void *thread_ptr) { | |
| 391 return thread_create(start_func, stack, thread_ptr, NULL); | |
| 392 } | |
| 393 | |
| 384 static void thread_exit(int32_t *stack_flag) { | 394 static void thread_exit(int32_t *stack_flag) { |
| 385 *stack_flag = 0; /* Indicate that the user code's stack can be freed. */ | 395 *stack_flag = 0; /* Indicate that the user code's stack can be freed. */ |
| 386 pthread_exit(NULL); | 396 pthread_exit(NULL); |
| 387 } | 397 } |
| 388 | 398 |
| 389 static int thread_nice(const int nice) { | 399 static int thread_nice(const int nice) { |
| 390 return 0; | 400 return 0; |
| 391 } | 401 } |
| 392 | 402 |
| 393 /* | 403 /* |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 irt_munmap, | 588 irt_munmap, |
| 579 irt_mprotect, | 589 irt_mprotect, |
| 580 }; | 590 }; |
| 581 | 591 |
| 582 const struct nacl_irt_tls nacl_irt_tls = { | 592 const struct nacl_irt_tls nacl_irt_tls = { |
| 583 tls_init, | 593 tls_init, |
| 584 tls_get, | 594 tls_get, |
| 585 }; | 595 }; |
| 586 | 596 |
| 587 const struct nacl_irt_thread nacl_irt_thread = { | 597 const struct nacl_irt_thread nacl_irt_thread = { |
| 598 thread_create_v0_1, | |
| 599 thread_exit, | |
| 600 thread_nice, | |
| 601 }; | |
| 602 | |
| 603 const struct nacl_irt_thread_v0_2 nacl_irt_thread_v0_2 = { | |
| 588 thread_create, | 604 thread_create, |
| 589 thread_exit, | 605 thread_exit, |
| 590 thread_nice, | 606 thread_nice, |
| 591 }; | 607 }; |
| 592 | 608 |
| 593 #if defined(__linux__) | 609 #if defined(__linux__) |
| 594 const struct nacl_irt_futex nacl_irt_futex = { | 610 const struct nacl_irt_futex nacl_irt_futex = { |
| 595 futex_wait_abs, | 611 futex_wait_abs, |
| 596 futex_wake, | 612 futex_wake, |
| 597 }; | 613 }; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 * The following condition is true when building for Non-SFI Mode, | 659 * The following condition is true when building for Non-SFI Mode, |
| 644 * when we're calling Linux syscalls directly. (Counter-intuitively, | 660 * when we're calling Linux syscalls directly. (Counter-intuitively, |
| 645 * "__linux__" is not #defined in this case.) | 661 * "__linux__" is not #defined in this case.) |
| 646 */ | 662 */ |
| 647 #if defined(__native_client__) | 663 #if defined(__native_client__) |
| 648 const struct nacl_irt_exception_handling nacl_irt_exception_handling = { | 664 const struct nacl_irt_exception_handling nacl_irt_exception_handling = { |
| 649 nacl_exception_get_and_set_handler, | 665 nacl_exception_get_and_set_handler, |
| 650 nacl_exception_set_stack, | 666 nacl_exception_set_stack, |
| 651 nacl_exception_clear_flag, | 667 nacl_exception_clear_flag, |
| 652 }; | 668 }; |
| 669 | |
| 670 const struct nacl_irt_signal_handling nacl_irt_signal_handling = { | |
| 671 nacl_signal_set_handler, | |
| 672 nacl_signal_send_async_signal, | |
| 673 }; | |
| 653 #endif | 674 #endif |
| 654 | 675 |
| 655 #if defined(__native_client__) && defined(__arm__) | 676 #if defined(__native_client__) && defined(__arm__) |
| 656 const struct nacl_irt_icache nacl_irt_icache = { | 677 const struct nacl_irt_icache nacl_irt_icache = { |
| 657 irt_clear_cache, | 678 irt_clear_cache, |
| 658 }; | 679 }; |
| 659 #endif | 680 #endif |
| 660 | 681 |
| 661 static int g_allow_dev_interfaces = 0; | 682 static int g_allow_dev_interfaces = 0; |
| 662 | 683 |
| 663 void nacl_irt_nonsfi_allow_dev_interfaces() { | 684 void nacl_irt_nonsfi_allow_dev_interfaces() { |
| 664 g_allow_dev_interfaces = 1; | 685 g_allow_dev_interfaces = 1; |
| 665 } | 686 } |
| 666 | 687 |
| 667 static int irt_dev_filter(void) { | 688 static int irt_dev_filter(void) { |
| 668 return g_allow_dev_interfaces; | 689 return g_allow_dev_interfaces; |
| 669 } | 690 } |
| 670 | 691 |
| 671 static const struct nacl_irt_interface irt_interfaces[] = { | 692 static const struct nacl_irt_interface irt_interfaces[] = { |
| 672 { NACL_IRT_BASIC_v0_1, &nacl_irt_basic, sizeof(nacl_irt_basic), NULL }, | 693 { NACL_IRT_BASIC_v0_1, &nacl_irt_basic, sizeof(nacl_irt_basic), NULL }, |
| 673 { NACL_IRT_FDIO_v0_1, &nacl_irt_fdio, sizeof(nacl_irt_fdio), NULL }, | 694 { NACL_IRT_FDIO_v0_1, &nacl_irt_fdio, sizeof(nacl_irt_fdio), NULL }, |
| 674 { NACL_IRT_MEMORY_v0_3, &nacl_irt_memory, sizeof(nacl_irt_memory), NULL }, | 695 { NACL_IRT_MEMORY_v0_3, &nacl_irt_memory, sizeof(nacl_irt_memory), NULL }, |
| 675 { NACL_IRT_TLS_v0_1, &nacl_irt_tls, sizeof(nacl_irt_tls), NULL }, | 696 { NACL_IRT_TLS_v0_1, &nacl_irt_tls, sizeof(nacl_irt_tls), NULL }, |
| 676 { NACL_IRT_THREAD_v0_1, &nacl_irt_thread, sizeof(nacl_irt_thread), NULL }, | 697 { NACL_IRT_THREAD_v0_1, &nacl_irt_thread, sizeof(nacl_irt_thread), NULL }, |
| 698 { NACL_IRT_THREAD_v0_2, &nacl_irt_thread_v0_2, | |
| 699 sizeof(nacl_irt_thread_v0_2), NULL }, | |
| 677 { NACL_IRT_FUTEX_v0_1, &nacl_irt_futex, sizeof(nacl_irt_futex), NULL }, | 700 { NACL_IRT_FUTEX_v0_1, &nacl_irt_futex, sizeof(nacl_irt_futex), NULL }, |
| 678 { NACL_IRT_RANDOM_v0_1, &nacl_irt_random, sizeof(nacl_irt_random), NULL }, | 701 { NACL_IRT_RANDOM_v0_1, &nacl_irt_random, sizeof(nacl_irt_random), NULL }, |
| 679 #if defined(__linux__) || defined(__native_client__) | 702 #if defined(__linux__) || defined(__native_client__) |
| 680 { NACL_IRT_CLOCK_v0_1, &nacl_irt_clock, sizeof(nacl_irt_clock), NULL }, | 703 { NACL_IRT_CLOCK_v0_1, &nacl_irt_clock, sizeof(nacl_irt_clock), NULL }, |
| 681 #endif | 704 #endif |
| 682 { NACL_IRT_DEV_FILENAME_v0_3, &nacl_irt_dev_filename, | 705 { NACL_IRT_DEV_FILENAME_v0_3, &nacl_irt_dev_filename, |
| 683 sizeof(nacl_irt_dev_filename), irt_dev_filter }, | 706 sizeof(nacl_irt_dev_filename), irt_dev_filter }, |
| 684 { NACL_IRT_DEV_GETPID_v0_1, &nacl_irt_dev_getpid, | 707 { NACL_IRT_DEV_GETPID_v0_1, &nacl_irt_dev_getpid, |
| 685 sizeof(nacl_irt_dev_getpid), irt_dev_filter }, | 708 sizeof(nacl_irt_dev_getpid), irt_dev_filter }, |
| 686 #if defined(__native_client__) | 709 #if defined(__native_client__) |
| 687 { NACL_IRT_EXCEPTION_HANDLING_v0_1, &nacl_irt_exception_handling, | 710 { NACL_IRT_EXCEPTION_HANDLING_v0_1, &nacl_irt_exception_handling, |
| 688 sizeof(nacl_irt_exception_handling), NULL }, | 711 sizeof(nacl_irt_exception_handling), NULL }, |
| 712 { NACL_IRT_SIGNAL_HANDLING_v0_1, &nacl_irt_signal_handling, | |
| 713 sizeof(nacl_irt_signal_handling), NULL }, | |
| 689 #endif | 714 #endif |
| 690 #if defined(__native_client__) && defined(__arm__) | 715 #if defined(__native_client__) && defined(__arm__) |
| 691 { NACL_IRT_ICACHE_v0_1, &nacl_irt_icache, sizeof(nacl_irt_icache), NULL }, | 716 { NACL_IRT_ICACHE_v0_1, &nacl_irt_icache, sizeof(nacl_irt_icache), NULL }, |
| 692 #endif | 717 #endif |
| 693 }; | 718 }; |
| 694 | 719 |
| 695 size_t nacl_irt_query_core(const char *interface_ident, | 720 size_t nacl_irt_query_core(const char *interface_ident, |
| 696 void *table, size_t tablesize) { | 721 void *table, size_t tablesize) { |
| 697 return nacl_irt_query_list(interface_ident, table, tablesize, | 722 return nacl_irt_query_list(interface_ident, table, tablesize, |
| 698 irt_interfaces, sizeof(irt_interfaces)); | 723 irt_interfaces, sizeof(irt_interfaces)); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 753 nacl_entry_func_t entry_func = | 778 nacl_entry_func_t entry_func = |
| 754 #if defined(__APPLE__) | 779 #if defined(__APPLE__) |
| 755 _start; | 780 _start; |
| 756 #else | 781 #else |
| 757 _user_start; | 782 _user_start; |
| 758 #endif | 783 #endif |
| 759 | 784 |
| 760 return nacl_irt_nonsfi_entry(argc, argv, environ, entry_func); | 785 return nacl_irt_nonsfi_entry(argc, argv, environ, entry_func); |
| 761 } | 786 } |
| 762 #endif | 787 #endif |
| OLD | NEW |