OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 /* | 9 /* |
10 * NaCl Simple/secure ELF loader (NaCl SEL). | 10 * NaCl Simple/secure ELF loader (NaCl SEL). |
(...skipping 728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 * Environment not set or redirect failed -- handle default inheritance. | 739 * Environment not set or redirect failed -- handle default inheritance. |
740 */ | 740 */ |
741 NaClAddHostDescriptor(nap, DUP(g_nacl_redir_control[ix].d), | 741 NaClAddHostDescriptor(nap, DUP(g_nacl_redir_control[ix].d), |
742 g_nacl_redir_control[ix].nacl_flags, (int) ix); | 742 g_nacl_redir_control[ix].nacl_flags, (int) ix); |
743 } | 743 } |
744 } | 744 } |
745 | 745 |
746 NaClLog(4, "... done.\n"); | 746 NaClLog(4, "... done.\n"); |
747 } | 747 } |
748 | 748 |
749 void NaClCreateServiceSocket(struct NaClApp *nap) { | |
750 struct NaClDesc *secure_pair[2]; | |
751 struct NaClDesc *pair[2]; | |
752 | |
753 NaClLog(3, "Entered NaClCreateServiceSocket\n"); | |
754 | |
755 if (NACL_FI_ERROR_COND("NaClCreateServiceSocket__secure_boundsock", | |
756 0 != NaClCommonDescMakeBoundSock(secure_pair))) { | |
757 NaClLog(LOG_FATAL, "Cound not create secure service socket\n"); | |
758 } | |
759 NaClLog(4, | |
760 "got bound socket at 0x%08"NACL_PRIxPTR", " | |
761 "addr at 0x%08"NACL_PRIxPTR"\n", | |
762 (uintptr_t) secure_pair[0], | |
763 (uintptr_t) secure_pair[1]); | |
764 | |
765 NaClDescSafeUnref(nap->secure_service_port); | |
766 nap->secure_service_port = secure_pair[0]; | |
767 | |
768 NaClDescSafeUnref(nap->secure_service_address); | |
769 nap->secure_service_address = secure_pair[1]; | |
770 | |
771 if (NACL_FI_ERROR_COND("NaClCreateServiceSocket__boundsock", | |
772 0 != NaClCommonDescMakeBoundSock(pair))) { | |
773 NaClLog(LOG_FATAL, "Cound not create service socket\n"); | |
774 } | |
775 NaClLog(4, | |
776 "got bound socket at 0x%08"NACL_PRIxPTR", " | |
777 "addr at 0x%08"NACL_PRIxPTR"\n", | |
778 (uintptr_t) pair[0], | |
779 (uintptr_t) pair[1]); | |
780 NaClAppSetDesc(nap, NACL_SERVICE_PORT_DESCRIPTOR, pair[0]); | |
781 NaClAppSetDesc(nap, NACL_SERVICE_ADDRESS_DESCRIPTOR, pair[1]); | |
782 | |
783 NaClDescSafeUnref(nap->service_port); | |
784 | |
785 nap->service_port = pair[0]; | |
786 NaClDescRef(nap->service_port); | |
787 | |
788 NaClDescSafeUnref(nap->service_address); | |
789 | |
790 nap->service_address = pair[1]; | |
791 NaClDescRef(nap->service_address); | |
792 | |
793 NaClLog(4, "Leaving NaClCreateServiceSocket\n"); | |
794 } | |
795 | |
796 /* | |
797 * Import the |inherited_desc| descriptor as an IMC handle, save a | |
798 * reference to it at nap->bootstrap_channel, then send the | |
799 * service_address over that channel. | |
800 */ | |
801 void NaClSetUpBootstrapChannel(struct NaClApp *nap, | |
802 NaClHandle inherited_desc) { | |
803 struct NaClDescImcDesc *channel; | |
804 struct NaClImcTypedMsgHdr hdr; | |
805 struct NaClDesc *descs[2]; | |
806 ssize_t rv; | |
807 | |
808 NaClLog(4, | |
809 "NaClSetUpBootstrapChannel(0x%08"NACL_PRIxPTR", %"NACL_PRIdPTR")\n", | |
810 (uintptr_t) nap, | |
811 (uintptr_t) inherited_desc); | |
812 | |
813 channel = (struct NaClDescImcDesc *) malloc(sizeof *channel); | |
814 if (NULL == channel) { | |
815 NaClLog(LOG_FATAL, "NaClSetUpBootstrapChannel: no memory\n"); | |
816 } | |
817 if (!NaClDescImcDescCtor(channel, inherited_desc)) { | |
818 NaClLog(LOG_FATAL, | |
819 ("NaClSetUpBootstrapChannel: cannot construct IMC descriptor" | |
820 " object for inherited descriptor %"NACL_PRIdPTR"\n"), | |
821 (uintptr_t) inherited_desc); | |
822 return; | |
823 } | |
824 if (NULL == nap->secure_service_address) { | |
825 NaClLog(LOG_FATAL, | |
826 "NaClSetUpBootstrapChannel: secure service address not set\n"); | |
827 return; | |
828 } | |
829 if (NULL == nap->service_address) { | |
830 NaClLog(LOG_FATAL, | |
831 "NaClSetUpBootstrapChannel: service address not set\n"); | |
832 return; | |
833 } | |
834 /* | |
835 * service_address and service_port are set together. | |
836 */ | |
837 descs[0] = nap->secure_service_address; | |
838 descs[1] = nap->service_address; | |
839 | |
840 hdr.iov = (struct NaClImcMsgIoVec *) NULL; | |
841 hdr.iov_length = 0; | |
842 hdr.ndescv = descs; | |
843 hdr.ndesc_length = NACL_ARRAY_SIZE(descs); | |
844 | |
845 rv = (*NACL_VTBL(NaClDesc, channel)->SendMsg)((struct NaClDesc *) channel, | |
846 &hdr, 0); | |
847 NaClXMutexLock(&nap->mu); | |
848 if (NULL != nap->bootstrap_channel) { | |
849 NaClLog(LOG_FATAL, | |
850 "NaClSetUpBootstrapChannel: cannot have two bootstrap channels\n"); | |
851 } | |
852 nap->bootstrap_channel = (struct NaClDesc *) channel; | |
853 channel = NULL; | |
854 NaClXMutexUnlock(&nap->mu); | |
855 | |
856 NaClLog(1, | |
857 ("NaClSetUpBootstrapChannel: descriptor %"NACL_PRIdPTR | |
858 ", error %"NACL_PRIdS"\n"), | |
859 (uintptr_t) inherited_desc, | |
860 rv); | |
861 if (NACL_FI_ERROR_COND("NaClSetUpBootstrapChannel__SendMsg", 0 != rv)) { | |
862 NaClLog(LOG_FATAL, | |
863 "NaClSetUpBootstrapChannel: SendMsg failed, rv = %"NACL_PRIdS"\n", | |
864 rv); | |
865 } | |
866 } | |
867 | |
868 enum NaClModuleInitializationState NaClGetInitState(struct NaClApp *nap) { | 749 enum NaClModuleInitializationState NaClGetInitState(struct NaClApp *nap) { |
869 enum NaClModuleInitializationState state; | 750 enum NaClModuleInitializationState state; |
870 NaClXMutexLock(&nap->mu); | 751 NaClXMutexLock(&nap->mu); |
871 state = nap->module_initialization_state; | 752 state = nap->module_initialization_state; |
872 NaClXMutexUnlock(&nap->mu); | 753 NaClXMutexUnlock(&nap->mu); |
873 return state; | 754 return state; |
874 } | 755 } |
875 | 756 |
876 void NaClSetInitState(struct NaClApp *nap, | 757 void NaClSetInitState(struct NaClApp *nap, |
877 enum NaClModuleInitializationState state) { | 758 enum NaClModuleInitializationState state) { |
878 NaClXMutexLock(&nap->mu); | 759 NaClXMutexLock(&nap->mu); |
879 /* The initialization state should be increasing monotonically. */ | 760 /* The initialization state should be increasing monotonically. */ |
880 CHECK(state > nap->module_initialization_state); | 761 CHECK(state > nap->module_initialization_state); |
881 nap->module_initialization_state = state; | 762 nap->module_initialization_state = state; |
882 NaClXCondVarBroadcast(&nap->cv); | 763 NaClXCondVarBroadcast(&nap->cv); |
883 NaClXMutexUnlock(&nap->mu); | 764 NaClXMutexUnlock(&nap->mu); |
884 } | 765 } |
885 | 766 |
886 NaClErrorCode NaClWaitForLoadModuleCommand(struct NaClApp *nap) { | |
887 NaClErrorCode status; | |
888 | |
889 NaClLog(4, "NaClWaitForLoadModuleCommand started\n"); | |
890 NaClXMutexLock(&nap->mu); | |
891 while (nap->module_initialization_state < NACL_MODULE_LOADED) { | |
892 NaClXCondVarWait(&nap->cv, &nap->mu); | |
893 } | |
894 status = nap->module_load_status; | |
895 NaClXMutexUnlock(&nap->mu); | |
896 NaClLog(4, "NaClWaitForLoadModuleCommand finished\n"); | |
897 | |
898 return status; | |
899 } | |
900 | |
901 void NaClRememberLoadStatus(struct NaClApp *nap, NaClErrorCode status) { | 767 void NaClRememberLoadStatus(struct NaClApp *nap, NaClErrorCode status) { |
902 NaClXMutexLock(&nap->mu); | 768 NaClXMutexLock(&nap->mu); |
903 /* Remember the first error we encountered. */ | 769 /* Remember the first error we encountered. */ |
904 if (nap->module_load_status == LOAD_OK) { | 770 if (nap->module_load_status == LOAD_OK) { |
905 nap->module_load_status = status; | 771 nap->module_load_status = status; |
906 } | 772 } |
907 NaClXMutexUnlock(&nap->mu); | 773 NaClXMutexUnlock(&nap->mu); |
908 } | 774 } |
909 | 775 |
910 | 776 |
911 NaClErrorCode NaClGetLoadStatus(struct NaClApp *nap) { | 777 NaClErrorCode NaClGetLoadStatus(struct NaClApp *nap) { |
912 NaClErrorCode status; | 778 NaClErrorCode status; |
913 NaClXMutexLock(&nap->mu); | 779 NaClXMutexLock(&nap->mu); |
914 status = nap->module_load_status; | 780 status = nap->module_load_status; |
915 NaClXMutexUnlock(&nap->mu); | 781 NaClXMutexUnlock(&nap->mu); |
916 return status; | 782 return status; |
917 } | 783 } |
918 | 784 |
919 NaClErrorCode NaClWaitForStartModuleCommand(struct NaClApp *nap) { | |
920 NaClErrorCode status; | |
921 | |
922 NaClLog(4, "NaClWaitForStartModuleCommand started\n"); | |
923 NaClXMutexLock(&nap->mu); | |
924 while (nap->module_initialization_state < NACL_MODULE_STARTED) { | |
925 NaClXCondVarWait(&nap->cv, &nap->mu); | |
926 } | |
927 status = nap->module_load_status; | |
928 NaClXMutexUnlock(&nap->mu); | |
929 NaClLog(4, "NaClWaitForStartModuleCommand finished\n"); | |
930 | |
931 return status; | |
932 } | |
933 | |
934 void NaClBlockIfCommandChannelExists(struct NaClApp *nap) { | |
935 if (NULL != nap->secure_service) { | |
936 for (;;) { | |
937 struct nacl_abi_timespec req; | |
938 req.tv_sec = 1000; | |
939 req.tv_nsec = 0; | |
940 NaClNanosleep(&req, (struct nacl_abi_timespec *) NULL); | |
941 } | |
942 } | |
943 } | |
944 | |
945 void NaClSecureCommandChannel(struct NaClApp *nap) { | |
946 struct NaClSecureService *secure_command_server; | |
947 | |
948 NaClLog(4, "Entered NaClSecureCommandChannel\n"); | |
949 | |
950 secure_command_server = (struct NaClSecureService *) malloc( | |
951 sizeof *secure_command_server); | |
952 if (NACL_FI_ERROR_COND("NaClSecureCommandChannel__malloc", | |
953 NULL == secure_command_server)) { | |
954 NaClLog(LOG_FATAL, "Out of memory for secure command channel\n"); | |
955 } | |
956 if (NACL_FI_ERROR_COND("NaClSecureCommandChannel__NaClSecureServiceCtor", | |
957 !NaClSecureServiceCtor(secure_command_server, | |
958 nap, | |
959 nap->secure_service_port, | |
960 nap->secure_service_address))) { | |
961 NaClLog(LOG_FATAL, "NaClSecureServiceCtor failed\n"); | |
962 } | |
963 nap->secure_service = secure_command_server; | |
964 | |
965 NaClLog(4, "NaClSecureCommandChannel: starting service thread\n"); | |
966 if (NACL_FI_ERROR_COND( | |
967 "NaClSecureCommandChannel__NaClSimpleServiceStartServiceThread", | |
968 !NaClSimpleServiceStartServiceThread((struct NaClSimpleService *) | |
969 secure_command_server))) { | |
970 NaClLog(LOG_FATAL, | |
971 "Could not start secure command channel service thread\n"); | |
972 } | |
973 | |
974 NaClLog(4, "Leaving NaClSecureCommandChannel\n"); | |
975 } | |
976 | |
977 | |
978 void NaClAppLoadModule(struct NaClApp *nap, | 785 void NaClAppLoadModule(struct NaClApp *nap, |
979 struct NaClDesc *nexe, | 786 struct NaClDesc *nexe, |
980 void (*load_cb)(void *instance_data, | 787 void (*load_cb)(void *instance_data, |
981 NaClErrorCode status), | 788 NaClErrorCode status), |
982 void *instance_data) { | 789 void *instance_data) { |
983 NaClErrorCode status = LOAD_OK; | 790 NaClErrorCode status = LOAD_OK; |
984 int is_double_init = NaClGetInitState(nap) != NACL_MODULE_UNINITIALIZED; | 791 int is_double_init = NaClGetInitState(nap) != NACL_MODULE_UNINITIALIZED; |
985 | 792 |
986 NaClLog(4, | 793 NaClLog(4, |
987 ("Entered NaClAppLoadModule: nap 0x%"NACL_PRIxPTR"," | 794 ("Entered NaClAppLoadModule: nap 0x%"NACL_PRIxPTR"," |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1197 nacl_global_xlate_base = mem_start; | 1004 nacl_global_xlate_base = mem_start; |
1198 | 1005 |
1199 NaClSandboxMemoryStartForValgrind(mem_start); | 1006 NaClSandboxMemoryStartForValgrind(mem_start); |
1200 | 1007 |
1201 _ovly_debug_event(); | 1008 _ovly_debug_event(); |
1202 } | 1009 } |
1203 | 1010 |
1204 void NaClGdbHook(struct NaClApp const *nap) { | 1011 void NaClGdbHook(struct NaClApp const *nap) { |
1205 StopForDebuggerInit(nap->mem_start); | 1012 StopForDebuggerInit(nap->mem_start); |
1206 } | 1013 } |
OLD | NEW |