1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <numeric>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/containers/heap_array.h"
#include "base/cpu.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/process/process_handle.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/types/expected_macros.h"
#include "build/build_config.h"
#include "components/nacl/common/nacl_host_messages.h"
#include "components/nacl/common/nacl_messages.h"
#include "components/nacl/common/nacl_switches.h"
#include "components/nacl/common/nacl_types.h"
#include "components/nacl/renderer/file_downloader.h"
#include "components/nacl/renderer/histogram.h"
#include "components/nacl/renderer/json_manifest.h"
#include "components/nacl/renderer/manifest_downloader.h"
#include "components/nacl/renderer/manifest_service_channel.h"
#include "components/nacl/renderer/nexe_load_manager.h"
#include "components/nacl/renderer/platform_info.h"
#include "components/nacl/renderer/pnacl_translation_resource_host.h"
#include "components/nacl/renderer/ppb_nacl_private.h"
#include "components/nacl/renderer/progress_event.h"
#include "components/nacl/renderer/trusted_plugin_channel.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/pepper_plugin_instance.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/renderer_ppapi_host.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "net/base/data_url.h"
#include "net/base/net_errors.h"
#include "net/http/http_util.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/private/pp_file_handle.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#include "ppapi/shared_impl/ppapi_preferences.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/shared_impl/var_tracker.h"
#include "ppapi/thunk/enter.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/platform/web_url_response.h"
#include "third_party/blink/public/web/web_associated_url_loader.h"
#include "third_party/blink/public/web/web_associated_url_loader_client.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_plugin_container.h"
namespace nacl {
namespace {
// The pseudo-architecture used to indicate portable native client.
const char* const kPortableArch = "portable";
// The base URL for resources used by the PNaCl translator processes.
const char* kPNaClTranslatorBaseUrl = "chrome://pnacl-translator/";
base::LazyInstance<scoped_refptr<PnaclTranslationResourceHost>>::
DestructorAtExit g_pnacl_resource_host = LAZY_INSTANCE_INITIALIZER;
bool InitializePnaclResourceHost() {
// Must run on the main thread.
content::RenderThread* render_thread = content::RenderThread::Get();
if (!render_thread)
return false;
if (!g_pnacl_resource_host.Get().get()) {
g_pnacl_resource_host.Get() =
new PnaclTranslationResourceHost(render_thread->GetIOTaskRunner());
render_thread->AddFilter(g_pnacl_resource_host.Get().get());
}
return true;
}
bool CanOpenViaFastPath(content::PepperPluginInstance* plugin_instance,
const GURL& gurl) {
// Fast path only works for installed file URLs.
if (!gurl.SchemeIs("chrome-extension"))
return PP_kInvalidFileHandle;
// IMPORTANT: Make sure the document can request the given URL. If we don't
// check, a malicious app could probe the extension system. This enforces a
// same-origin policy which prevents the app from requesting resources from
// another app.
blink::WebSecurityOrigin security_origin =
plugin_instance->GetContainer()->GetDocument().GetSecurityOrigin();
return security_origin.CanRequest(gurl);
}
// This contains state that is produced by LaunchSelLdr() and consumed
// by StartPpapiProxy().
struct InstanceInfo {
InstanceInfo() : plugin_pid(base::kNullProcessId), plugin_child_id(0) {}
GURL url;
ppapi::PpapiPermissions permissions;
base::ProcessId plugin_pid;
int plugin_child_id;
IPC::ChannelHandle channel_handle;
};
class NaClPluginInstance {
public:
explicit NaClPluginInstance(PP_Instance instance)
: nexe_load_manager(instance), pexe_size(0) {}
~NaClPluginInstance() {
// Make sure that we do not leak a mojo handle if the NaCl loader
// process never called ppapi_start() to initialize PPAPI.
if (instance_info) {
DCHECK(instance_info->channel_handle.is_mojo_channel_handle());
instance_info->channel_handle.mojo_handle.Close();
}
}
NexeLoadManager nexe_load_manager;
std::unique_ptr<JsonManifest> json_manifest;
std::unique_ptr<InstanceInfo> instance_info;
// When translation is complete, this records the size of the pexe in
// bytes so that it can be reported in a later load event.
uint64_t pexe_size;
};
typedef std::unordered_map<PP_Instance, std::unique_ptr<NaClPluginInstance>>
InstanceMap;
base::LazyInstance<InstanceMap>::DestructorAtExit g_instance_map =
LAZY_INSTANCE_INITIALIZER;
NaClPluginInstance* GetNaClPluginInstance(PP_Instance instance) {
InstanceMap& map = g_instance_map.Get();
auto iter = map.find(instance);
if (iter == map.end())
return NULL;
return iter->second.get();
}
NexeLoadManager* GetNexeLoadManager(PP_Instance instance) {
NaClPluginInstance* nacl_plugin_instance = GetNaClPluginInstance(instance);
if (!nacl_plugin_instance)
return NULL;
return &nacl_plugin_instance->nexe_load_manager;
}
JsonManifest* GetJsonManifest(PP_Instance instance) {
NaClPluginInstance* nacl_plugin_instance = GetNaClPluginInstance(instance);
if (!nacl_plugin_instance)
return NULL;
return nacl_plugin_instance->json_manifest.get();
}
static const PP_NaClFileInfo kInvalidNaClFileInfo = {
PP_kInvalidFileHandle,
0, // token_lo
0, // token_hi
};
int GetFrameRoutingID(PP_Instance instance) {
// Check that we are on the main renderer thread.
DCHECK(content::RenderThread::Get());
content::RendererPpapiHost* host =
content::RendererPpapiHost::GetForPPInstance(instance);
if (!host)
return 0;
auto* render_frame = host->GetRenderFrameForInstance(instance);
if (!render_frame)
return 0;
return render_frame->GetRoutingID();
}
// Returns whether the channel_handle is valid or not.
bool IsValidChannelHandle(const IPC::ChannelHandle& channel_handle) {
DCHECK(channel_handle.is_mojo_channel_handle());
return channel_handle.is_mojo_channel_handle();
}
void PostPPCompletionCallback(PP_CompletionCallback callback,
int32_t status) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data, status));
}
bool ManifestResolveKey(PP_Instance instance,
bool is_helper_process,
const std::string& key,
std::string* full_url,
PP_PNaClOptions* pnacl_options);
typedef base::OnceCallback<void(int32_t, const PP_NaClFileInfo&)>
DownloadFileCallback;
void DownloadFile(PP_Instance instance,
const std::string& url,
DownloadFileCallback callback);
PP_Bool StartPpapiProxy(PP_Instance instance);
// Thin adapter from PPP_ManifestService to ManifestServiceChannel::Delegate.
// Note that user_data is managed by the caller of LaunchSelLdr. Please see
// also PP_ManifestService's comment for more details about resource
// management.
class ManifestServiceProxy : public ManifestServiceChannel::Delegate {
public:
ManifestServiceProxy(PP_Instance pp_instance, NaClAppProcessType process_type)
: pp_instance_(pp_instance), process_type_(process_type) {}
ManifestServiceProxy(const ManifestServiceProxy&) = delete;
ManifestServiceProxy& operator=(const ManifestServiceProxy&) = delete;
~ManifestServiceProxy() override = default;
void StartupInitializationComplete() override {
if (StartPpapiProxy(pp_instance_) == PP_TRUE) {
NaClPluginInstance* nacl_plugin_instance =
GetNaClPluginInstance(pp_instance_);
JsonManifest* manifest = GetJsonManifest(pp_instance_);
if (nacl_plugin_instance && manifest) {
NexeLoadManager* load_manager =
&nacl_plugin_instance->nexe_load_manager;
std::string full_url;
PP_PNaClOptions pnacl_options;
JsonManifest::ErrorInfo error_info;
if (manifest->GetProgramURL(&full_url, &pnacl_options, &error_info)) {
int64_t exe_size = nacl_plugin_instance->pexe_size;
if (exe_size == 0)
exe_size = load_manager->nexe_size();
load_manager->ReportLoadSuccess(full_url, exe_size, exe_size);
}
}
}
}
void OpenResource(
const std::string& key,
ManifestServiceChannel::OpenResourceCallback callback) override {
DCHECK(ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->
BelongsToCurrentThread());
// For security hardening, disable open_resource() when it is isn't
// needed. PNaCl pexes can't use open_resource(), but general nexes
// and the PNaCl translator nexes may use it.
if (process_type_ != kNativeNaClProcessType &&
process_type_ != kPNaClTranslatorProcessType) {
// Return an error.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), base::File(), 0, 0));
return;
}
std::string url;
// TODO(teravest): Clean up pnacl_options logic in JsonManifest so we don't
// have to initialize it like this here.
PP_PNaClOptions pnacl_options;
pnacl_options.translate = PP_FALSE;
pnacl_options.is_debug = PP_FALSE;
pnacl_options.use_subzero = PP_FALSE;
pnacl_options.opt_level = 2;
bool is_helper_process = process_type_ == kPNaClTranslatorProcessType;
if (!ManifestResolveKey(pp_instance_, is_helper_process, key, &url,
&pnacl_options)) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), base::File(), 0, 0));
return;
}
// We have to call DidDownloadFile, even if this object is destroyed, so
// that the handle inside PP_NaClFileInfo isn't leaked. This means that the
// callback passed to this function shouldn't have a weak pointer to an
// object either.
//
// TODO(teravest): Make a type like PP_NaClFileInfo to use for DownloadFile
// that would close the file handle on destruction.
DownloadFile(pp_instance_, url,
base::BindOnce(&ManifestServiceProxy::DidDownloadFile,
std::move(callback)));
}
private:
static void DidDownloadFile(
ManifestServiceChannel::OpenResourceCallback callback,
int32_t pp_error,
const PP_NaClFileInfo& file_info) {
if (pp_error != PP_OK) {
std::move(callback).Run(base::File(), 0, 0);
return;
}
std::move(callback).Run(base::File(file_info.handle), file_info.token_lo,
file_info.token_hi);
}
PP_Instance pp_instance_;
NaClAppProcessType process_type_;
};
std::unique_ptr<blink::WebAssociatedURLLoader> CreateAssociatedURLLoader(
const blink::WebDocument& document,
const GURL& gurl) {
blink::WebAssociatedURLLoaderOptions options;
options.untrusted_http = true;
return document.GetFrame()->CreateAssociatedURLLoader(options);
}
blink::WebURLRequest CreateWebURLRequest(const blink::WebDocument& document,
const GURL& gurl) {
blink::WebURLRequest request(gurl);
request.SetSiteForCookies(document.SiteForCookies());
// Follow the original behavior in the trusted plugin and
// PepperURLLoaderHost.
if (document.GetSecurityOrigin().CanRequest(gurl)) {
request.SetMode(network::mojom::RequestMode::kSameOrigin);
request.SetCredentialsMode(network::mojom::CredentialsMode::kSameOrigin);
} else {
request.SetMode(network::mojom::RequestMode::kCors);
request.SetCredentialsMode(network::mojom::CredentialsMode::kOmit);
}
// Plug-ins should not load via service workers as plug-ins may have their own
// origin checking logic that may get confused if service workers respond with
// resources from another origin.
// https://w3c.github.io/ServiceWorker/#implementer-concerns
request.SetSkipServiceWorker(true);
return request;
}
int32_t FileDownloaderToPepperError(FileDownloader::Status status) {
switch (status) {
case FileDownloader::SUCCESS:
return PP_OK;
case FileDownloader::ACCESS_DENIED:
return PP_ERROR_NOACCESS;
case FileDownloader::FAILED:
return PP_ERROR_FAILED;
// No default case, to catch unhandled Status values.
}
return PP_ERROR_FAILED;
}
NaClAppProcessType PP_ToNaClAppProcessType(
PP_NaClAppProcessType pp_process_type) {
#define STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(pp, nonpp) \
static_assert(static_cast<int>(pp) == static_cast<int>(nonpp), \
"PP_NaClAppProcessType differs from NaClAppProcessType");
STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(PP_UNKNOWN_NACL_PROCESS_TYPE,
kUnknownNaClProcessType);
STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(PP_NATIVE_NACL_PROCESS_TYPE,
kNativeNaClProcessType);
STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(PP_PNACL_PROCESS_TYPE,
kPNaClProcessType);
STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(PP_PNACL_TRANSLATOR_PROCESS_TYPE,
kPNaClTranslatorProcessType);
STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ(PP_NUM_NACL_PROCESS_TYPES,
kNumNaClProcessTypes);
#undef STATICALLY_CHECK_NACLAPPPROCESSTYPE_EQ
DCHECK(pp_process_type > PP_UNKNOWN_NACL_PROCESS_TYPE &&
pp_process_type < PP_NUM_NACL_PROCESS_TYPES);
return static_cast<NaClAppProcessType>(pp_process_type);
}
} // namespace
// Launch NaCl's sel_ldr process.
// static
void PPBNaClPrivate::LaunchSelLdr(
PP_Instance instance,
PP_Bool main_service_runtime,
const char* alleged_url,
const PP_NaClFileInfo* nexe_file_info,
PP_NaClAppProcessType pp_process_type,
std::unique_ptr<IPC::SyncChannel>* translator_channel,
PP_CompletionCallback callback) {
CHECK(ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->
BelongsToCurrentThread());
NaClAppProcessType process_type = PP_ToNaClAppProcessType(pp_process_type);
// Create the manifest service proxy here, so on error case, it will be
// destructed (without passing it to ManifestServiceChannel).
std::unique_ptr<ManifestServiceChannel::Delegate> manifest_service_proxy(
new ManifestServiceProxy(instance, process_type));
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
DCHECK(plugin_instance);
if (!load_manager || !plugin_instance) {
if (nexe_file_info->handle != PP_kInvalidFileHandle) {
base::File closer(nexe_file_info->handle);
}
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
InstanceInfo instance_info;
instance_info.url = GURL(alleged_url);
// Keep backwards-compatible, but no other permissions.
uint32_t perm_bits = ppapi::PERMISSION_DEFAULT;
instance_info.permissions =
ppapi::PpapiPermissions::GetForCommandLine(perm_bits);
std::vector<NaClResourcePrefetchRequest> resource_prefetch_request_list;
if (process_type == kNativeNaClProcessType) {
JsonManifest* manifest = GetJsonManifest(instance);
if (manifest) {
manifest->GetPrefetchableFiles(&resource_prefetch_request_list);
for (size_t i = 0; i < resource_prefetch_request_list.size(); ++i) {
const GURL gurl(resource_prefetch_request_list[i].resource_url);
// Important security check. Do not remove.
if (!CanOpenViaFastPath(plugin_instance, gurl)) {
resource_prefetch_request_list.clear();
break;
}
}
}
}
IPC::PlatformFileForTransit nexe_for_transit =
IPC::InvalidPlatformFileForTransit();
#if BUILDFLAG(IS_POSIX)
if (nexe_file_info->handle != PP_kInvalidFileHandle)
nexe_for_transit = base::FileDescriptor(nexe_file_info->handle, true);
#else
# error Unsupported target platform.
#endif
std::string error_message_string;
NaClLaunchResult launch_result;
if (!sender->Send(new NaClHostMsg_LaunchNaCl(
NaClLaunchParams(instance_info.url.spec(), nexe_for_transit,
nexe_file_info->token_lo, nexe_file_info->token_hi,
resource_prefetch_request_list,
GetFrameRoutingID(instance), perm_bits,
process_type),
&launch_result, &error_message_string))) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
if (!error_message_string.empty()) {
// Even on error, some FDs/handles may be passed to here.
// We must release those resources.
// See also nacl_process_host.cc.
if (PP_ToBool(main_service_runtime)) {
load_manager->ReportLoadError(PP_NACL_ERROR_SEL_LDR_LAUNCH,
"ServiceRuntime: failed to start",
error_message_string);
}
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
instance_info.channel_handle = launch_result.ppapi_ipc_channel_handle;
instance_info.plugin_pid = launch_result.plugin_pid;
instance_info.plugin_child_id = launch_result.plugin_child_id;
// Don't save instance_info if channel handle is invalid.
if (IsValidChannelHandle(instance_info.channel_handle)) {
if (process_type == kPNaClTranslatorProcessType) {
// Return an IPC channel which allows communicating with a PNaCl
// translator process.
*translator_channel = IPC::SyncChannel::Create(
instance_info.channel_handle, IPC::Channel::MODE_CLIENT,
/* listener = */ nullptr,
content::RenderThread::Get()->GetIOTaskRunner(),
base::SingleThreadTaskRunner::GetCurrentDefault(), true,
content::RenderThread::Get()->GetShutdownEvent());
} else {
// Save the channel handle for when StartPpapiProxy() is called.
NaClPluginInstance* nacl_plugin_instance =
GetNaClPluginInstance(instance);
nacl_plugin_instance->instance_info =
std::make_unique<InstanceInfo>(instance_info);
}
}
// Store the crash information shared memory handle.
load_manager->set_crash_info_shmem_region(
std::move(launch_result.crash_info_shmem_region));
// Create the trusted plugin channel.
if (!IsValidChannelHandle(launch_result.trusted_ipc_channel_handle)) {
PostPPCompletionCallback(callback, PP_ERROR_FAILED);
return;
}
bool is_helper_nexe = !PP_ToBool(main_service_runtime);
std::unique_ptr<TrustedPluginChannel> trusted_plugin_channel(
new TrustedPluginChannel(
load_manager,
mojo::PendingReceiver<mojom::NaClRendererHost>(
mojo::ScopedMessagePipeHandle(
launch_result.trusted_ipc_channel_handle.mojo_handle)),
is_helper_nexe));
load_manager->set_trusted_plugin_channel(std::move(trusted_plugin_channel));
// Create the manifest service handle as well.
if (IsValidChannelHandle(launch_result.manifest_service_ipc_channel_handle)) {
std::unique_ptr<ManifestServiceChannel> manifest_service_channel(
new ManifestServiceChannel(
launch_result.manifest_service_ipc_channel_handle,
base::BindOnce(&PostPPCompletionCallback, callback),
std::move(manifest_service_proxy),
content::RenderThread::Get()->GetShutdownEvent()));
load_manager->set_manifest_service_channel(
std::move(manifest_service_channel));
}
}
namespace {
PP_Bool StartPpapiProxy(PP_Instance instance) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (!load_manager)
return PP_FALSE;
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!plugin_instance) {
DLOG(ERROR) << "GetInstance() failed";
return PP_FALSE;
}
NaClPluginInstance* nacl_plugin_instance = GetNaClPluginInstance(instance);
if (!nacl_plugin_instance->instance_info) {
DLOG(ERROR) << "Could not find instance ID";
return PP_FALSE;
}
std::unique_ptr<InstanceInfo> instance_info =
std::move(nacl_plugin_instance->instance_info);
PP_ExternalPluginResult result = plugin_instance->SwitchToOutOfProcessProxy(
base::FilePath().AppendASCII(instance_info->url.spec()),
instance_info->permissions,
instance_info->channel_handle,
instance_info->plugin_pid,
instance_info->plugin_child_id);
if (result == PP_EXTERNAL_PLUGIN_OK) {
// Log the amound of time that has passed between the trusted plugin being
// initialized and the untrusted plugin being initialized. This is
// (roughly) the cost of using NaCl, in terms of startup time.
load_manager->ReportStartupOverhead();
return PP_TRUE;
}
if (result == PP_EXTERNAL_PLUGIN_ERROR_MODULE) {
load_manager->ReportLoadError(PP_NACL_ERROR_START_PROXY_MODULE,
"could not initialize module.");
} else if (result == PP_EXTERNAL_PLUGIN_ERROR_INSTANCE) {
load_manager->ReportLoadError(PP_NACL_ERROR_START_PROXY_MODULE,
"could not create instance.");
}
return PP_FALSE;
}
// Convert a URL to a filename for GetReadonlyPnaclFd.
// Must be kept in sync with PnaclCanOpenFile() in
// components/nacl/browser/nacl_file_host.cc.
std::string PnaclComponentURLToFilename(const std::string& url) {
// PNaCl component URLs aren't arbitrary URLs; they are always either
// generated from ManifestResolveKey or PnaclResources::ReadResourceInfo.
// So, it's safe to just use string parsing operations here instead of
// URL-parsing ones.
DCHECK(base::StartsWith(url, kPNaClTranslatorBaseUrl,
base::CompareCase::SENSITIVE));
std::string r = url.substr(std::string(kPNaClTranslatorBaseUrl).length());
// Replace characters that are not allowed with '_'.
size_t replace_pos;
static const char kAllowList[] = "abcdefghijklmnopqrstuvwxyz0123456789_";
replace_pos = r.find_first_not_of(kAllowList);
while (replace_pos != std::string::npos) {
r = r.replace(replace_pos, 1, "_");
replace_pos = r.find_first_not_of(kAllowList);
}
return r;
}
PP_FileHandle GetReadonlyPnaclFd(const std::string& url,
bool is_executable,
uint64_t* nonce_lo,
uint64_t* nonce_hi) {
std::string filename = PnaclComponentURLToFilename(url);
IPC::PlatformFileForTransit out_fd = IPC::InvalidPlatformFileForTransit();
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
if (!sender->Send(new NaClHostMsg_GetReadonlyPnaclFD(
std::string(filename), is_executable,
&out_fd, nonce_lo, nonce_hi))) {
return PP_kInvalidFileHandle;
}
if (out_fd == IPC::InvalidPlatformFileForTransit()) {
return PP_kInvalidFileHandle;
}
return IPC::PlatformFileForTransitToPlatformFile(out_fd);
}
} // namespace
// static
void PPBNaClPrivate::GetReadExecPnaclFd(const char* url,
PP_NaClFileInfo* out_file_info) {
*out_file_info = kInvalidNaClFileInfo;
out_file_info->handle = GetReadonlyPnaclFd(url, true /* is_executable */,
&out_file_info->token_lo,
&out_file_info->token_hi);
}
// static
PP_FileHandle PPBNaClPrivate::CreateTemporaryFile(PP_Instance instance) {
IPC::PlatformFileForTransit transit_fd = IPC::InvalidPlatformFileForTransit();
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
if (!sender->Send(new NaClHostMsg_NaClCreateTemporaryFile(
&transit_fd))) {
return PP_kInvalidFileHandle;
}
if (transit_fd == IPC::InvalidPlatformFileForTransit()) {
return PP_kInvalidFileHandle;
}
return IPC::PlatformFileForTransitToPlatformFile(transit_fd);
}
// static
int32_t PPBNaClPrivate::GetNumberOfProcessors() {
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
int32_t num_processors = 1;
return sender->Send(new NaClHostMsg_NaClGetNumProcessors(&num_processors)) ?
num_processors : 1;
}
namespace {
void GetNexeFd(PP_Instance instance,
const std::string& pexe_url,
uint32_t opt_level,
const base::Time& last_modified_time,
const std::string& etag,
bool has_no_store_header,
bool use_subzero,
PnaclTranslationResourceHost::RequestNexeFdCallback callback) {
if (!InitializePnaclResourceHost()) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_ERROR_FAILED), false,
PP_kInvalidFileHandle));
return;
}
PnaclCacheInfo cache_info;
cache_info.pexe_url = GURL(pexe_url);
// TODO(dschuff): Get this value from the pnacl json file after it
// rolls in from NaCl.
cache_info.abi_version = 1;
cache_info.opt_level = opt_level;
cache_info.last_modified = last_modified_time;
cache_info.etag = etag;
cache_info.has_no_store_header = has_no_store_header;
cache_info.use_subzero = use_subzero;
cache_info.sandbox_isa = GetSandboxArch();
cache_info.extra_flags = GetCpuFeatures();
g_pnacl_resource_host.Get()->RequestNexeFd(instance, cache_info,
std::move(callback));
}
void LogTranslationFinishedUMA(const std::string& uma_suffix,
int32_t opt_level,
int32_t unknown_opt_level,
int64_t nexe_size,
int64_t pexe_size,
int64_t compile_time_us,
base::TimeDelta total_time) {
HistogramEnumerate("NaCl.Options.PNaCl.OptLevel" + uma_suffix, opt_level,
unknown_opt_level + 1);
HistogramKBPerSec("NaCl.Perf.PNaClLoadTime.CompileKBPerSec" + uma_suffix,
pexe_size / 1024, compile_time_us);
HistogramSizeKB("NaCl.Perf.Size.PNaClTranslatedNexe" + uma_suffix,
nexe_size / 1024);
HistogramSizeKB("NaCl.Perf.Size.Pexe" + uma_suffix, pexe_size / 1024);
HistogramRatio("NaCl.Perf.Size.PexeNexeSizePct" + uma_suffix, pexe_size,
nexe_size);
HistogramTimeTranslation(
"NaCl.Perf.PNaClLoadTime.TotalUncachedTime" + uma_suffix,
total_time.InMilliseconds());
HistogramKBPerSec(
"NaCl.Perf.PNaClLoadTime.TotalUncachedKBPerSec" + uma_suffix,
pexe_size / 1024, total_time.InMicroseconds());
}
} // namespace
// static
void PPBNaClPrivate::ReportTranslationFinished(PP_Instance instance,
PP_Bool success,
int32_t opt_level,
PP_Bool use_subzero,
int64_t nexe_size,
int64_t pexe_size,
int64_t compile_time_us) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (success == PP_TRUE && load_manager) {
base::TimeDelta total_time =
base::Time::Now() - load_manager->pnacl_start_time();
static const int32_t kUnknownOptLevel = 4;
if (opt_level < 0 || opt_level > 3)
opt_level = kUnknownOptLevel;
// Log twice: once to cover all PNaCl UMA, and then a second
// time with the more specific UMA (Subzero vs LLC).
std::string uma_suffix(use_subzero ? ".Subzero" : ".LLC");
LogTranslationFinishedUMA("", opt_level, kUnknownOptLevel, nexe_size,
pexe_size, compile_time_us, total_time);
LogTranslationFinishedUMA(uma_suffix, opt_level, kUnknownOptLevel,
nexe_size, pexe_size, compile_time_us,
total_time);
}
// If the resource host isn't initialized, don't try to do that here.
// Just return because something is already very wrong.
if (g_pnacl_resource_host.Get().get() == NULL)
return;
g_pnacl_resource_host.Get()->ReportTranslationFinished(instance, success);
// Record the pexe size for reporting in a later load event.
NaClPluginInstance* nacl_plugin_instance = GetNaClPluginInstance(instance);
if (nacl_plugin_instance) {
nacl_plugin_instance->pexe_size = pexe_size;
}
}
namespace {
PP_FileHandle OpenNaClExecutable(PP_Instance instance,
const char* file_url,
uint64_t* nonce_lo,
uint64_t* nonce_hi) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (!load_manager)
return PP_kInvalidFileHandle;
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!plugin_instance)
return PP_kInvalidFileHandle;
GURL gurl(file_url);
// Important security check. Do not remove.
if (!CanOpenViaFastPath(plugin_instance, gurl))
return PP_kInvalidFileHandle;
IPC::PlatformFileForTransit out_fd = IPC::InvalidPlatformFileForTransit();
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
*nonce_lo = 0;
*nonce_hi = 0;
base::FilePath file_path;
if (!sender->Send(new NaClHostMsg_OpenNaClExecutable(
GetFrameRoutingID(instance), GURL(file_url), &out_fd, nonce_lo,
nonce_hi))) {
return PP_kInvalidFileHandle;
}
if (out_fd == IPC::InvalidPlatformFileForTransit())
return PP_kInvalidFileHandle;
return IPC::PlatformFileForTransitToPlatformFile(out_fd);
}
} // namespace
// static
void PPBNaClPrivate::DispatchEvent(PP_Instance instance,
PP_NaClEventType event_type,
const char* resource_url,
PP_Bool length_is_computable,
uint64_t loaded_bytes,
uint64_t total_bytes) {
ProgressEvent event(event_type,
resource_url,
PP_ToBool(length_is_computable),
loaded_bytes,
total_bytes);
DispatchProgressEvent(instance, event);
}
// static
void PPBNaClPrivate::ReportLoadError(PP_Instance instance,
PP_NaClError error,
const char* error_message) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
if (load_manager)
load_manager->ReportLoadError(error, error_message);
}
// static
void PPBNaClPrivate::InstanceCreated(PP_Instance instance) {
InstanceMap& map = g_instance_map.Get();
CHECK(map.find(instance) == map.end()); // Sanity check.
std::unique_ptr<NaClPluginInstance> new_instance(
new NaClPluginInstance(instance));
map[instance] = std::move(new_instance);
}
// static
void PPBNaClPrivate::InstanceDestroyed(PP_Instance instance) {
InstanceMap& map = g_instance_map.Get();
auto iter = map.find(instance);
CHECK(iter != map.end());
// The erase may call NexeLoadManager's destructor prior to removing it from
// the map. In that case, it is possible for the trusted Plugin to re-enter
// the NexeLoadManager (e.g., by calling ReportLoadError). Passing out the
// NexeLoadManager to a local scoped_ptr just ensures that its entry is gone
// from the map prior to the destructor being invoked.
std::unique_ptr<NaClPluginInstance> temp = std::move(iter->second);
map.erase(iter);
}
// static
void PPBNaClPrivate::TerminateNaClLoader(PP_Instance instance) {
auto* load_mgr = GetNexeLoadManager(instance);
if (load_mgr)
load_mgr->CloseTrustedPluginChannel();
}
namespace {
PP_Bool NaClDebugEnabledForURL(const char* alleged_nmf_url) {
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableNaClDebug))
return PP_FALSE;
IPC::Sender* sender = content::RenderThread::Get();
DCHECK(sender);
bool should_debug = false;
return PP_FromBool(
sender->Send(new NaClHostMsg_NaClDebugEnabledForURL(GURL(alleged_nmf_url),
&should_debug)) &&
should_debug);
}
} // namespace
// static
void PPBNaClPrivate::InitializePlugin(PP_Instance instance,
uint32_t argc,
const char* argn[],
const char* argv[]) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (load_manager)
load_manager->InitializePlugin(argc, argn, argv);
}
namespace {
void DownloadManifestToBuffer(PP_Instance instance,
struct PP_CompletionCallback callback);
bool CreateJsonManifest(PP_Instance instance,
const std::string& manifest_url,
const std::string& manifest_data);
} // namespace
// static
void PPBNaClPrivate::RequestNaClManifest(PP_Instance instance,
PP_CompletionCallback callback) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (!load_manager) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
std::string url = load_manager->GetManifestURLArgument();
if (url.empty() || !load_manager->RequestNaClManifest(url)) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
const GURL& base_url = load_manager->manifest_base_url();
if (base_url.SchemeIs("data")) {
GURL gurl(base_url);
std::string mime_type;
std::string charset;
std::string data;
int32_t error = PP_ERROR_FAILED;
if (net::DataURL::Parse(gurl, &mime_type, &charset, &data)) {
if (data.size() <= ManifestDownloader::kNaClManifestMaxFileBytes) {
if (CreateJsonManifest(instance, base_url.spec(), data))
error = PP_OK;
} else {
load_manager->ReportLoadError(PP_NACL_ERROR_MANIFEST_TOO_LARGE,
"manifest file too large.");
}
} else {
load_manager->ReportLoadError(PP_NACL_ERROR_MANIFEST_LOAD_URL,
"could not load manifest url.");
}
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data, error));
} else {
DownloadManifestToBuffer(instance, callback);
}
}
// static
PP_Var PPBNaClPrivate::GetManifestBaseURL(PP_Instance instance) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (!load_manager)
return PP_MakeUndefined();
const GURL& gurl = load_manager->manifest_base_url();
if (!gurl.is_valid())
return PP_MakeUndefined();
return ppapi::StringVar::StringToPPVar(gurl.spec());
}
// static
void PPBNaClPrivate::ProcessNaClManifest(PP_Instance instance,
const char* program_url) {
nacl::NexeLoadManager* load_manager = GetNexeLoadManager(instance);
if (load_manager)
load_manager->ProcessNaClManifest(program_url);
}
namespace {
void DownloadManifestToBufferCompletion(PP_Instance instance,
struct PP_CompletionCallback callback,
base::Time start_time,
PP_NaClError pp_nacl_error,
const std::string& data);
void DownloadManifestToBuffer(PP_Instance instance,
struct PP_CompletionCallback callback) {
nacl::NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!load_manager || !plugin_instance) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
const blink::WebDocument& document =
plugin_instance->GetContainer()->GetDocument();
const GURL& gurl = load_manager->manifest_base_url();
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader(
CreateAssociatedURLLoader(document, gurl));
blink::WebURLRequest request = CreateWebURLRequest(document, gurl);
// Requests from plug-ins must skip service workers, see the comment in
// CreateWebURLRequest.
DCHECK(request.GetSkipServiceWorker());
// ManifestDownloader deletes itself after invoking the callback.
ManifestDownloader* manifest_downloader = new ManifestDownloader(
std::move(url_loader), load_manager->is_installed(),
base::BindOnce(DownloadManifestToBufferCompletion, instance, callback,
base::Time::Now()));
manifest_downloader->Load(request);
}
void DownloadManifestToBufferCompletion(PP_Instance instance,
struct PP_CompletionCallback callback,
base::Time start_time,
PP_NaClError pp_nacl_error,
const std::string& data) {
base::TimeDelta download_time = base::Time::Now() - start_time;
HistogramTimeSmall("NaCl.Perf.StartupTime.ManifestDownload",
download_time.InMilliseconds());
nacl::NexeLoadManager* load_manager = GetNexeLoadManager(instance);
if (!load_manager) {
callback.func(callback.user_data, PP_ERROR_ABORTED);
return;
}
int32_t pp_error;
switch (pp_nacl_error) {
case PP_NACL_ERROR_LOAD_SUCCESS:
pp_error = PP_OK;
break;
case PP_NACL_ERROR_MANIFEST_LOAD_URL:
pp_error = PP_ERROR_FAILED;
load_manager->ReportLoadError(PP_NACL_ERROR_MANIFEST_LOAD_URL,
"could not load manifest url.");
break;
case PP_NACL_ERROR_MANIFEST_TOO_LARGE:
pp_error = PP_ERROR_FILETOOBIG;
load_manager->ReportLoadError(PP_NACL_ERROR_MANIFEST_TOO_LARGE,
"manifest file too large.");
break;
case PP_NACL_ERROR_MANIFEST_NOACCESS_URL:
pp_error = PP_ERROR_NOACCESS;
load_manager->ReportLoadError(PP_NACL_ERROR_MANIFEST_NOACCESS_URL,
"access to manifest url was denied.");
break;
default:
NOTREACHED();
}
if (pp_error == PP_OK) {
std::string base_url = load_manager->manifest_base_url().spec();
if (!CreateJsonManifest(instance, base_url, data))
pp_error = PP_ERROR_FAILED;
}
callback.func(callback.user_data, pp_error);
}
bool CreateJsonManifest(PP_Instance instance,
const std::string& manifest_url,
const std::string& manifest_data) {
HistogramSizeKB("NaCl.Perf.Size.Manifest",
static_cast<int32_t>(manifest_data.length() / 1024));
nacl::NexeLoadManager* load_manager = GetNexeLoadManager(instance);
if (!load_manager)
return false;
const char* isa_type;
if (load_manager->IsPNaCl())
isa_type = kPortableArch;
else
isa_type = GetSandboxArch();
std::unique_ptr<nacl::JsonManifest> j(new nacl::JsonManifest(
manifest_url.c_str(), isa_type,
PP_ToBool(NaClDebugEnabledForURL(manifest_url.c_str()))));
JsonManifest::ErrorInfo error_info;
if (j->Init(manifest_data.c_str(), &error_info)) {
GetNaClPluginInstance(instance)->json_manifest = std::move(j);
return true;
}
load_manager->ReportLoadError(error_info.error, error_info.string);
return false;
}
bool ShouldUseSubzero(const PP_PNaClOptions* pnacl_options) {
// Always use Subzero if explicitly overridden on the command line.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForcePNaClSubzero))
return true;
// Otherwise, don't use Subzero for a debug pexe file since Subzero's parser
// is likely to reject an unfinalized pexe.
if (pnacl_options->is_debug)
return false;
// Only use Subzero for optlevel=0.
if (pnacl_options->opt_level != 0)
return false;
// Check a list of allowed architectures.
const char* arch = GetSandboxArch();
if (UNSAFE_TODO(strcmp(arch, "x86-32")) == 0) {
return true;
}
if (UNSAFE_TODO(strcmp(arch, "x86-64")) == 0) {
return true;
}
if (UNSAFE_TODO(strcmp(arch, "arm")) == 0) {
return true;
}
return false;
}
} // namespace
// static
PP_Bool PPBNaClPrivate::GetManifestProgramURL(PP_Instance instance,
PP_Var* pp_full_url,
PP_PNaClOptions* pnacl_options) {
nacl::NexeLoadManager* load_manager = GetNexeLoadManager(instance);
JsonManifest* manifest = GetJsonManifest(instance);
if (manifest == NULL)
return PP_FALSE;
std::string full_url;
JsonManifest::ErrorInfo error_info;
if (manifest->GetProgramURL(&full_url, pnacl_options, &error_info)) {
*pp_full_url = ppapi::StringVar::StringToPPVar(full_url);
if (ShouldUseSubzero(pnacl_options)) {
pnacl_options->use_subzero = PP_TRUE;
// Subzero -O2 is closer to LLC -O0, so indicate -O2.
pnacl_options->opt_level = 2;
}
return PP_TRUE;
}
if (load_manager)
load_manager->ReportLoadError(error_info.error, error_info.string);
return PP_FALSE;
}
namespace {
bool ManifestResolveKey(PP_Instance instance,
bool is_helper_process,
const std::string& key,
std::string* full_url,
PP_PNaClOptions* pnacl_options) {
// For "helper" processes (llc and ld, for PNaCl translation), we resolve
// keys manually as there is no existing .nmf file to parse.
if (is_helper_process) {
pnacl_options->translate = PP_FALSE;
*full_url = std::string(kPNaClTranslatorBaseUrl) + GetSandboxArch() + "/" +
key;
return true;
}
JsonManifest* manifest = GetJsonManifest(instance);
if (manifest == NULL)
return false;
return manifest->ResolveKey(key, full_url, pnacl_options);
}
} // namespace
// static
PP_Bool PPBNaClPrivate::GetPnaclResourceInfo(PP_Instance instance,
PP_Var* llc_tool_name,
PP_Var* ld_tool_name,
PP_Var* subzero_tool_name) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
CHECK(load_manager);
const auto get_info = [&]() -> base::expected<void, std::string> {
const std::string kFilename = "chrome://pnacl-translator/pnacl.json";
uint64_t nonce_lo = 0;
uint64_t nonce_hi = 0;
base::File file(GetReadonlyPnaclFd(kFilename, false /* is_executable */,
&nonce_lo, &nonce_hi));
if (!file.IsValid()) {
return base::unexpected(
"The Portable Native Client (pnacl) component is not installed. "
"Please consult chrome://components for more information.");
}
int64_t file_size = file.GetLength();
if (file_size < 0) {
return base::unexpected("GetPnaclResourceInfo, GetLength failed for: " +
kFilename);
}
if (file_size > 1 << 20) {
return base::unexpected("GetPnaclResourceInfo, file too large: " +
kFilename);
}
auto buffer = base::HeapArray<char>::Uninit(file_size + 1);
int rc = UNSAFE_TODO(file.Read(0, buffer.data(), file_size));
if (rc < 0 || rc != file_size) {
return base::unexpected("GetPnaclResourceInfo, reading failed for: " +
kFilename);
}
// Null-terminate the bytes we we read from the file.
buffer[rc] = 0;
// Expect the JSON file to contain a top-level object (dictionary).
ASSIGN_OR_RETURN(
auto parsed_json,
base::JSONReader::ReadAndReturnValueWithError(buffer.data()),
[](base::JSONReader::Error error) {
return "Parsing resource info failed: JSON parse error: " +
std::move(error).message;
});
auto* json_dict = parsed_json.GetIfDict();
if (!json_dict) {
return base::unexpected(
"Parsing resource info failed: JSON parse error: Not a "
"dictionary.");
}
if (auto* pnacl_llc_name = json_dict->FindString("pnacl-llc-name")) {
*llc_tool_name = ppapi::StringVar::StringToPPVar(*pnacl_llc_name);
}
if (auto* pnacl_ld_name = json_dict->FindString("pnacl-ld-name")) {
*ld_tool_name = ppapi::StringVar::StringToPPVar(*pnacl_ld_name);
}
if (auto* pnacl_sz_name = json_dict->FindString("pnacl-sz-name")) {
*subzero_tool_name = ppapi::StringVar::StringToPPVar(*pnacl_sz_name);
}
return base::ok();
};
RETURN_IF_ERROR(get_info(), [&](std::string error) {
load_manager->ReportLoadError(PP_NACL_ERROR_PNACL_RESOURCE_FETCH, error);
return PP_FALSE;
});
return PP_TRUE;
}
// static
const char* PPBNaClPrivate::GetSandboxArch() {
return nacl::GetSandboxArch();
}
// static
PP_Var PPBNaClPrivate::GetCpuFeatureAttrs() {
return ppapi::StringVar::StringToPPVar(GetCpuFeatures());
}
namespace {
// Encapsulates some of the state for a call to DownloadNexe to prevent
// argument lists from getting too long.
struct DownloadNexeRequest {
PP_Instance instance;
std::string url;
PP_CompletionCallback callback;
base::Time start_time;
};
// A utility class to ensure that we don't send progress events more often than
// every 10ms for a given file.
class ProgressEventRateLimiter {
public:
explicit ProgressEventRateLimiter(PP_Instance instance)
: instance_(instance) { }
void ReportProgress(const std::string& url,
int64_t total_bytes_received,
int64_t total_bytes_to_be_received) {
base::Time now = base::Time::Now();
if (now - last_event_ > base::Milliseconds(10)) {
DispatchProgressEvent(instance_,
ProgressEvent(PP_NACL_EVENT_PROGRESS,
url,
total_bytes_to_be_received >= 0,
total_bytes_received,
total_bytes_to_be_received));
last_event_ = now;
}
}
private:
PP_Instance instance_;
base::Time last_event_;
};
void DownloadNexeCompletion(const DownloadNexeRequest& request,
PP_NaClFileInfo* out_file_info,
FileDownloader::Status status,
base::File target_file,
int http_status);
} // namespace
// static
void PPBNaClPrivate::DownloadNexe(PP_Instance instance,
const char* url,
PP_NaClFileInfo* out_file_info,
PP_CompletionCallback callback) {
CHECK(url);
CHECK(out_file_info);
DownloadNexeRequest request;
request.instance = instance;
request.url = url;
request.callback = callback;
request.start_time = base::Time::Now();
// Try the fast path for retrieving the file first.
PP_FileHandle handle = OpenNaClExecutable(instance,
url,
&out_file_info->token_lo,
&out_file_info->token_hi);
if (handle != PP_kInvalidFileHandle) {
DownloadNexeCompletion(request,
out_file_info,
FileDownloader::SUCCESS,
base::File(handle),
200);
return;
}
// The fast path didn't work, we'll fetch the file using URLLoader and write
// it to local storage.
base::File target_file(PPBNaClPrivate::CreateTemporaryFile(instance));
GURL gurl(url);
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!plugin_instance) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE, base::BindOnce(callback.func, callback.user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
const blink::WebDocument& document =
plugin_instance->GetContainer()->GetDocument();
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader(
CreateAssociatedURLLoader(document, gurl));
blink::WebURLRequest url_request = CreateWebURLRequest(document, gurl);
ProgressEventRateLimiter* tracker = new ProgressEventRateLimiter(instance);
// FileDownloader deletes itself after invoking DownloadNexeCompletion.
FileDownloader* file_downloader = new FileDownloader(
std::move(url_loader), std::move(target_file),
base::BindOnce(&DownloadNexeCompletion, request, out_file_info),
base::BindRepeating(&ProgressEventRateLimiter::ReportProgress,
base::Owned(tracker), std::string(url)));
file_downloader->Load(url_request);
}
namespace {
void DownloadNexeCompletion(const DownloadNexeRequest& request,
PP_NaClFileInfo* out_file_info,
FileDownloader::Status status,
base::File target_file,
int http_status) {
int32_t pp_error = FileDownloaderToPepperError(status);
int64_t bytes_read = -1;
if (pp_error == PP_OK && target_file.IsValid()) {
base::File::Info info;
if (target_file.GetInfo(&info))
bytes_read = info.size;
}
if (bytes_read == -1) {
target_file.Close();
pp_error = PP_ERROR_FAILED;
}
base::TimeDelta download_time = base::Time::Now() - request.start_time;
NexeLoadManager* load_manager = GetNexeLoadManager(request.instance);
if (load_manager) {
load_manager->NexeFileDidOpen(pp_error,
target_file,
http_status,
bytes_read,
request.url,
download_time);
}
if (pp_error == PP_OK && target_file.IsValid())
out_file_info->handle = target_file.TakePlatformFile();
else
out_file_info->handle = PP_kInvalidFileHandle;
request.callback.func(request.callback.user_data, pp_error);
}
void DownloadFileCompletion(DownloadFileCallback callback,
FileDownloader::Status status,
base::File file,
int http_status) {
int32_t pp_error = FileDownloaderToPepperError(status);
PP_NaClFileInfo file_info;
if (pp_error == PP_OK) {
file_info.handle = file.TakePlatformFile();
file_info.token_lo = 0;
file_info.token_hi = 0;
} else {
file_info = kInvalidNaClFileInfo;
}
std::move(callback).Run(pp_error, file_info);
}
void DownloadFile(PP_Instance instance,
const std::string& url,
DownloadFileCallback callback) {
DCHECK(ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->
BelongsToCurrentThread());
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
DCHECK(load_manager);
if (!load_manager) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_ERROR_FAILED),
kInvalidNaClFileInfo));
return;
}
// Handle special PNaCl support files which are installed on the user's
// machine.
if (base::StartsWith(url, kPNaClTranslatorBaseUrl,
base::CompareCase::SENSITIVE)) {
PP_NaClFileInfo file_info = kInvalidNaClFileInfo;
PP_FileHandle handle =
GetReadonlyPnaclFd(url, false /* is_executable */, &file_info.token_lo,
&file_info.token_hi);
if (handle == PP_kInvalidFileHandle) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_ERROR_FAILED),
kInvalidNaClFileInfo));
return;
}
file_info.handle = handle;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_OK), file_info));
return;
}
// We have to ensure that this url resolves relative to the plugin base url
// before downloading it.
const GURL& test_gurl = load_manager->plugin_base_url().Resolve(url);
if (!test_gurl.is_valid()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_ERROR_FAILED),
kInvalidNaClFileInfo));
return;
}
// Try the fast path for retrieving the file first.
uint64_t file_token_lo = 0;
uint64_t file_token_hi = 0;
PP_FileHandle file_handle = OpenNaClExecutable(instance,
url.c_str(),
&file_token_lo,
&file_token_hi);
if (file_handle != PP_kInvalidFileHandle) {
PP_NaClFileInfo file_info;
file_info.handle = file_handle;
file_info.token_lo = file_token_lo;
file_info.token_hi = file_token_hi;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_OK), file_info));
return;
}
// The fast path didn't work, we'll fetch the file using URLLoader and write
// it to local storage.
base::File target_file(PPBNaClPrivate::CreateTemporaryFile(instance));
GURL gurl(url);
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!plugin_instance) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
static_cast<int32_t>(PP_ERROR_FAILED),
kInvalidNaClFileInfo));
return;
}
const blink::WebDocument& document =
plugin_instance->GetContainer()->GetDocument();
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader(
CreateAssociatedURLLoader(document, gurl));
blink::WebURLRequest url_request = CreateWebURLRequest(document, gurl);
ProgressEventRateLimiter* tracker = new ProgressEventRateLimiter(instance);
// FileDownloader deletes itself after invoking DownloadNexeCompletion.
FileDownloader* file_downloader = new FileDownloader(
std::move(url_loader), std::move(target_file),
base::BindOnce(&DownloadFileCompletion, std::move(callback)),
base::BindRepeating(&ProgressEventRateLimiter::ReportProgress,
base::Owned(tracker), std::string(url)));
file_downloader->Load(url_request);
}
} // namespace
// static
void PPBNaClPrivate::LogTranslateTime(const char* histogram_name,
int64_t time_in_us) {
ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
FROM_HERE,
base::BindOnce(&HistogramTimeTranslation, std::string(histogram_name),
time_in_us / 1000));
}
// static
void PPBNaClPrivate::LogBytesCompiledVsDownloaded(
PP_Bool use_subzero,
int64_t pexe_bytes_compiled,
int64_t pexe_bytes_downloaded) {
HistogramRatio("NaCl.Perf.PNaClLoadTime.PctCompiledWhenFullyDownloaded",
pexe_bytes_compiled, pexe_bytes_downloaded);
HistogramRatio(
use_subzero
? "NaCl.Perf.PNaClLoadTime.PctCompiledWhenFullyDownloaded.Subzero"
: "NaCl.Perf.PNaClLoadTime.PctCompiledWhenFullyDownloaded.LLC",
pexe_bytes_compiled, pexe_bytes_downloaded);
}
// static
void PPBNaClPrivate::SetPNaClStartTime(PP_Instance instance) {
NexeLoadManager* load_manager = GetNexeLoadManager(instance);
if (load_manager)
load_manager->set_pnacl_start_time(base::Time::Now());
}
namespace {
// PexeDownloader is responsible for deleting itself when the download
// finishes.
class PexeDownloader : public blink::WebAssociatedURLLoaderClient {
public:
PexeDownloader(PP_Instance instance,
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader,
const std::string& pexe_url,
int32_t pexe_opt_level,
bool use_subzero,
const PPP_PexeStreamHandler* stream_handler,
void* stream_handler_user_data)
: instance_(instance),
url_loader_(std::move(url_loader)),
pexe_url_(pexe_url),
pexe_opt_level_(pexe_opt_level),
use_subzero_(use_subzero),
stream_handler_(stream_handler),
stream_handler_user_data_(stream_handler_user_data),
success_(false),
expected_content_length_(-1) {}
void Load(const blink::WebURLRequest& request) {
url_loader_->LoadAsynchronously(request, this);
}
private:
void DidReceiveResponse(const blink::WebURLResponse& response) override {
success_ = (response.HttpStatusCode() == 200);
if (!success_)
return;
expected_content_length_ = response.ExpectedContentLength();
// Defer loading after receiving headers. This is because we may already
// have a cached translated nexe, so check for that now.
url_loader_->SetDefersLoading(true);
std::string etag = response.HttpHeaderField("etag").Utf8();
// Parse the "last-modified" date string. An invalid string will result
// in a base::Time value of 0, which is supported by the only user of
// the |CacheInfo::last_modified| field (see
// pnacl::PnaclTranslationCache::GetKey()).
std::string last_modified =
response.HttpHeaderField("last-modified").Utf8();
base::Time last_modified_time;
std::ignore =
base::Time::FromString(last_modified.c_str(), &last_modified_time);
bool has_no_store_header = false;
std::string cache_control =
response.HttpHeaderField("cache-control").Utf8();
for (const std::string& cur : base::SplitString(
cache_control, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
if (base::ToLowerASCII(cur) == "no-store")
has_no_store_header = true;
}
GetNexeFd(instance_, pexe_url_, pexe_opt_level_, last_modified_time, etag,
has_no_store_header, use_subzero_,
base::BindOnce(&PexeDownloader::didGetNexeFd,
weak_factory_.GetWeakPtr()));
}
void didGetNexeFd(int32_t pp_error,
bool cache_hit,
PP_FileHandle file_handle) {
if (!content::PepperPluginInstance::Get(instance_)) {
delete this;
return;
}
HistogramEnumerate("NaCl.Perf.PNaClCache.IsHit", cache_hit, 2);
HistogramEnumerate(use_subzero_ ? "NaCl.Perf.PNaClCache.IsHit.Subzero"
: "NaCl.Perf.PNaClCache.IsHit.LLC",
cache_hit, 2);
if (cache_hit) {
stream_handler_->DidCacheHit(stream_handler_user_data_, file_handle);
// We delete the PexeDownloader at this point since we successfully got a
// cached, translated nexe.
delete this;
return;
}
stream_handler_->DidCacheMiss(stream_handler_user_data_,
expected_content_length_,
file_handle);
// No translated nexe was found in the cache, so we should download the
// file to start streaming it.
url_loader_->SetDefersLoading(false);
}
void DidReceiveData(base::span<const char> data) override {
if (content::PepperPluginInstance::Get(instance_)) {
// Stream the data we received to the stream callback.
stream_handler_->DidStreamData(stream_handler_user_data_, data.data(),
base::checked_cast<int32_t>(data.size()));
}
}
void DidFinishLoading() override {
int32_t result = success_ ? PP_OK : PP_ERROR_FAILED;
if (content::PepperPluginInstance::Get(instance_))
stream_handler_->DidFinishStream(stream_handler_user_data_, result);
delete this;
}
void DidFail(const blink::WebURLError& error) override {
if (content::PepperPluginInstance::Get(instance_))
stream_handler_->DidFinishStream(stream_handler_user_data_,
PP_ERROR_FAILED);
delete this;
}
PP_Instance instance_;
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader_;
std::string pexe_url_;
int32_t pexe_opt_level_;
bool use_subzero_;
raw_ptr<const PPP_PexeStreamHandler> stream_handler_;
raw_ptr<void> stream_handler_user_data_;
bool success_;
int64_t expected_content_length_;
base::WeakPtrFactory<PexeDownloader> weak_factory_{this};
};
} // namespace
// static
void PPBNaClPrivate::StreamPexe(PP_Instance instance,
const char* pexe_url,
int32_t opt_level,
PP_Bool use_subzero,
const PPP_PexeStreamHandler* handler,
void* handler_user_data) {
content::PepperPluginInstance* plugin_instance =
content::PepperPluginInstance::Get(instance);
if (!plugin_instance) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(handler->DidFinishStream, handler_user_data,
static_cast<int32_t>(PP_ERROR_FAILED)));
return;
}
GURL gurl(pexe_url);
const blink::WebDocument& document =
plugin_instance->GetContainer()->GetDocument();
std::unique_ptr<blink::WebAssociatedURLLoader> url_loader(
CreateAssociatedURLLoader(document, gurl));
PexeDownloader* downloader =
new PexeDownloader(instance, std::move(url_loader), pexe_url, opt_level,
PP_ToBool(use_subzero), handler, handler_user_data);
blink::WebURLRequest url_request = CreateWebURLRequest(document, gurl);
// Mark the request as requesting a PNaCl bitcode file,
// so that component updater can detect this user action.
url_request.AddHttpHeaderField(
blink::WebString::FromUTF8("Accept"),
blink::WebString::FromUTF8("application/x-pnacl, */*"));
url_request.SetRequestContext(blink::mojom::RequestContextType::OBJECT);
downloader->Load(url_request);
}
} // namespace nacl
|