OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifdef _MSC_VER | 5 #ifdef _MSC_VER |
6 // Do not warn about use of std::copy with raw pointers. | 6 // Do not warn about use of std::copy with raw pointers. |
7 #pragma warning(disable : 4996) | 7 #pragma warning(disable : 4996) |
8 #endif | 8 #endif |
9 | 9 |
10 #include "native_client/src/trusted/plugin/plugin.h" | 10 #include "native_client/src/trusted/plugin/plugin.h" |
11 | 11 |
12 #include <fcntl.h> | |
13 #include <stdio.h> | |
14 #include <stdlib.h> | |
15 #include <string.h> | |
16 | |
17 #include <sys/stat.h> | 12 #include <sys/stat.h> |
18 #include <sys/types.h> | 13 #include <sys/types.h> |
19 | 14 |
20 #include <algorithm> | 15 #include <algorithm> |
21 #include <deque> | 16 #include <deque> |
22 #include <string> | 17 #include <string> |
23 #include <vector> | 18 #include <vector> |
24 | 19 |
25 #include "native_client/src/include/nacl_base.h" | 20 #include "native_client/src/include/nacl_base.h" |
26 #include "native_client/src/include/nacl_macros.h" | 21 #include "native_client/src/include/nacl_macros.h" |
27 #include "native_client/src/include/nacl_scoped_ptr.h" | 22 #include "native_client/src/include/nacl_scoped_ptr.h" |
28 #include "native_client/src/include/nacl_string.h" | 23 #include "native_client/src/include/nacl_string.h" |
29 #include "native_client/src/include/portability.h" | 24 #include "native_client/src/include/portability.h" |
30 #include "native_client/src/include/portability_io.h" | 25 #include "native_client/src/include/portability_io.h" |
31 #include "native_client/src/include/portability_string.h" | 26 #include "native_client/src/include/portability_string.h" |
32 #include "native_client/src/shared/platform/nacl_check.h" | 27 #include "native_client/src/shared/platform/nacl_check.h" |
33 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" | 28 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
34 #include "native_client/src/trusted/nonnacl_util/sel_ldr_launcher.h" | 29 #include "native_client/src/trusted/nonnacl_util/sel_ldr_launcher.h" |
| 30 #include "native_client/src/trusted/plugin/file_utils.h" |
35 #include "native_client/src/trusted/plugin/json_manifest.h" | 31 #include "native_client/src/trusted/plugin/json_manifest.h" |
36 #include "native_client/src/trusted/plugin/nacl_entry_points.h" | 32 #include "native_client/src/trusted/plugin/nacl_entry_points.h" |
37 #include "native_client/src/trusted/plugin/nacl_subprocess.h" | 33 #include "native_client/src/trusted/plugin/nacl_subprocess.h" |
38 #include "native_client/src/trusted/plugin/nexe_arch.h" | 34 #include "native_client/src/trusted/plugin/nexe_arch.h" |
39 #include "native_client/src/trusted/plugin/plugin_error.h" | 35 #include "native_client/src/trusted/plugin/plugin_error.h" |
40 #include "native_client/src/trusted/plugin/scriptable_plugin.h" | 36 #include "native_client/src/trusted/plugin/scriptable_plugin.h" |
41 #include "native_client/src/trusted/plugin/service_runtime.h" | 37 #include "native_client/src/trusted/plugin/service_runtime.h" |
42 #include "native_client/src/trusted/plugin/utility.h" | 38 #include "native_client/src/trusted/plugin/utility.h" |
43 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" | 39 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" |
44 | 40 |
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 error_info.SetReport(ERROR_MANIFEST_NOACCESS_URL, | 1093 error_info.SetReport(ERROR_MANIFEST_NOACCESS_URL, |
1098 "access to manifest url was denied."); | 1094 "access to manifest url was denied."); |
1099 ReportLoadError(error_info); | 1095 ReportLoadError(error_info); |
1100 } else { | 1096 } else { |
1101 error_info.SetReport(ERROR_MANIFEST_LOAD_URL, | 1097 error_info.SetReport(ERROR_MANIFEST_LOAD_URL, |
1102 "could not load manifest url."); | 1098 "could not load manifest url."); |
1103 ReportLoadError(error_info); | 1099 ReportLoadError(error_info); |
1104 } | 1100 } |
1105 return; | 1101 return; |
1106 } | 1102 } |
1107 // Duplicate the file descriptor in order to create a FILE stream with it | 1103 // SlurpFile closes the file descriptor after reading (or on error). |
1108 // that can later be closed without closing the original descriptor. The | 1104 // Duplicate our file descriptor since it will be handled by the browser. |
1109 // browser will take care of the original descriptor. | |
1110 int dup_file_desc = DUP(file_desc); | 1105 int dup_file_desc = DUP(file_desc); |
1111 struct stat stat_buf; | 1106 nacl::string json_buffer; |
1112 if (0 != fstat(dup_file_desc, &stat_buf)) { | 1107 file_utils::StatusCode status = file_utils::SlurpFile( |
1113 CLOSE(dup_file_desc); | 1108 dup_file_desc, json_buffer, kNaClManifestMaxFileBytes); |
1114 error_info.SetReport(ERROR_MANIFEST_STAT, | 1109 |
1115 "could not stat manifest file."); | 1110 if (status != file_utils::PLUGIN_FILE_SUCCESS) { |
| 1111 switch (status) { |
| 1112 case file_utils::PLUGIN_FILE_SUCCESS: |
| 1113 CHECK(0); |
| 1114 break; |
| 1115 case file_utils::PLUGIN_FILE_ERROR_MEM_ALLOC: |
| 1116 error_info.SetReport(ERROR_MANIFEST_MEMORY_ALLOC, |
| 1117 "could not allocate manifest memory."); |
| 1118 break; |
| 1119 case file_utils::PLUGIN_FILE_ERROR_OPEN: |
| 1120 error_info.SetReport(ERROR_MANIFEST_OPEN, |
| 1121 "could not open manifest file."); |
| 1122 break; |
| 1123 case file_utils::PLUGIN_FILE_ERROR_FILE_TOO_LARGE: |
| 1124 error_info.SetReport(ERROR_MANIFEST_TOO_LARGE, |
| 1125 "manifest file too large."); |
| 1126 break; |
| 1127 case file_utils::PLUGIN_FILE_ERROR_STAT: |
| 1128 error_info.SetReport(ERROR_MANIFEST_STAT, |
| 1129 "could not stat manifest file."); |
| 1130 break; |
| 1131 case file_utils::PLUGIN_FILE_ERROR_READ: |
| 1132 error_info.SetReport(ERROR_MANIFEST_READ, |
| 1133 "could not read manifest file."); |
| 1134 break; |
| 1135 } |
1116 ReportLoadError(error_info); | 1136 ReportLoadError(error_info); |
1117 return; | 1137 return; |
1118 } | 1138 } |
1119 size_t bytes_to_read = static_cast<size_t>(stat_buf.st_size); | |
1120 if (bytes_to_read > kNaClManifestMaxFileBytes) { | |
1121 CLOSE(dup_file_desc); | |
1122 error_info.SetReport(ERROR_MANIFEST_TOO_LARGE, | |
1123 "manifest file too large."); | |
1124 ReportLoadError(error_info); | |
1125 return; | |
1126 } | |
1127 FILE* json_file = fdopen(dup_file_desc, "rb"); | |
1128 PLUGIN_PRINTF(("Plugin::NaClManifestFileDidOpen " | |
1129 "(dup_file_desc=%"NACL_PRId32", json_file=%p)\n", | |
1130 dup_file_desc, static_cast<void*>(json_file))); | |
1131 if (json_file == NULL) { | |
1132 CLOSE(dup_file_desc); | |
1133 error_info.SetReport(ERROR_MANIFEST_OPEN, | |
1134 "could not open manifest file."); | |
1135 ReportLoadError(error_info); | |
1136 return; | |
1137 } | |
1138 nacl::scoped_array<char> json_buffer(new char[bytes_to_read + 1]); | |
1139 if (json_buffer == NULL) { | |
1140 fclose(json_file); | |
1141 error_info.SetReport(ERROR_MANIFEST_MEMORY_ALLOC, | |
1142 "could not allocate manifest memory."); | |
1143 ReportLoadError(error_info); | |
1144 return; | |
1145 } | |
1146 // json_buffer could hold a large enough buffer that the system might need | |
1147 // multiple reads to fill it, so iterate through reads. | |
1148 size_t total_bytes_read = 0; | |
1149 while (0 < bytes_to_read) { | |
1150 size_t bytes_this_read = fread(&json_buffer[total_bytes_read], | |
1151 sizeof(char), | |
1152 bytes_to_read, | |
1153 json_file); | |
1154 if (bytes_this_read < bytes_to_read && | |
1155 (feof(json_file) || ferror(json_file))) { | |
1156 PLUGIN_PRINTF(("Plugin::NaClManifestFileDidOpen failed: " | |
1157 "total_bytes_read=%"NACL_PRIuS" " | |
1158 "bytes_to_read=%"NACL_PRIuS"\n", | |
1159 total_bytes_read, bytes_to_read)); | |
1160 fclose(json_file); | |
1161 error_info.SetReport(ERROR_MANIFEST_READ, | |
1162 "could not read manifest file."); | |
1163 ReportLoadError(error_info); | |
1164 return; | |
1165 } | |
1166 total_bytes_read += bytes_this_read; | |
1167 bytes_to_read -= bytes_this_read; | |
1168 } | |
1169 // Once the bytes are read, the FILE is no longer needed, so close it. This | |
1170 // allows for early returns without leaking the |json_file| FILE object. | |
1171 fclose(json_file); | |
1172 // No need to close |file_desc|, that is handled by |nexe_downloader_|. | |
1173 json_buffer[total_bytes_read] = '\0'; // Force null termination. | |
1174 | 1139 |
1175 ProcessNaClManifest(json_buffer.get()); | 1140 ProcessNaClManifest(json_buffer); |
1176 } | 1141 } |
1177 | 1142 |
1178 void Plugin::ProcessNaClManifest(const nacl::string& manifest_json) { | 1143 void Plugin::ProcessNaClManifest(const nacl::string& manifest_json) { |
1179 HistogramSizeKB("NaCl.Perf.Size.Manifest", | 1144 HistogramSizeKB("NaCl.Perf.Size.Manifest", |
1180 static_cast<int32_t>(manifest_json.length() / 1024)); | 1145 static_cast<int32_t>(manifest_json.length() / 1024)); |
1181 nacl::string program_url; | 1146 nacl::string program_url; |
1182 PnaclOptions pnacl_options; | 1147 PnaclOptions pnacl_options; |
1183 ErrorInfo error_info; | 1148 ErrorInfo error_info; |
1184 if (!SetManifestObject(manifest_json, &error_info)) { | 1149 if (!SetManifestObject(manifest_json, &error_info)) { |
1185 ReportLoadError(error_info); | 1150 ReportLoadError(error_info); |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1675 static_cast<uint32_t>(text.size())); | 1640 static_cast<uint32_t>(text.size())); |
1676 const PPB_Console* console_interface = | 1641 const PPB_Console* console_interface = |
1677 static_cast<const PPB_Console*>( | 1642 static_cast<const PPB_Console*>( |
1678 module->GetBrowserInterface(PPB_CONSOLE_INTERFACE)); | 1643 module->GetBrowserInterface(PPB_CONSOLE_INTERFACE)); |
1679 console_interface->LogWithSource(pp_instance(), PP_LOGLEVEL_LOG, prefix, str); | 1644 console_interface->LogWithSource(pp_instance(), PP_LOGLEVEL_LOG, prefix, str); |
1680 var_interface->Release(prefix); | 1645 var_interface->Release(prefix); |
1681 var_interface->Release(str); | 1646 var_interface->Release(str); |
1682 } | 1647 } |
1683 | 1648 |
1684 } // namespace plugin | 1649 } // namespace plugin |
OLD | NEW |