xref: /aosp_15_r20/external/perfetto/src/trace_processor/rpc/wasm_bridge.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <emscripten/emscripten.h>
18 
19 #include <cstdint>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <new>
23 
24 #include "perfetto/base/compiler.h"
25 #include "src/trace_processor/rpc/rpc.h"
26 
27 namespace perfetto::trace_processor {
28 
29 namespace {
30 using RpcResponseFn = void(const void*, uint32_t);
31 
32 Rpc* g_trace_processor_rpc;
33 
34 // The buffer used to pass the request arguments. The caller (JS) decides how
35 // big this buffer should be in the Initialize() call.
36 uint8_t* g_req_buf;
37 
OutOfMemoryHandler()38 PERFETTO_NO_INLINE void OutOfMemoryHandler() {
39   fprintf(stderr, "\nCannot enlarge memory\n");
40   abort();
41 }
42 
43 }  // namespace
44 
45 // +---------------------------------------------------------------------------+
46 // | Exported functions called by the JS/TS running in the worker.             |
47 // +---------------------------------------------------------------------------+
48 extern "C" {
49 
50 // Returns the address of the allocated request buffer.
51 uint8_t* EMSCRIPTEN_KEEPALIVE
52 trace_processor_rpc_init(RpcResponseFn* RpcResponseFn, uint32_t);
trace_processor_rpc_init(RpcResponseFn * resp_function,uint32_t req_buffer_size)53 uint8_t* trace_processor_rpc_init(RpcResponseFn* resp_function,
54                                   uint32_t req_buffer_size) {
55   // Usually OOMs manifest as a failure in dlmalloc() -> sbrk() ->
56   //_emscripten_resize_heap() which aborts itself. However in some rare cases
57   // sbrk() can fail outside of _emscripten_resize_heap and just return null.
58   // When that happens, just abort with the same message that
59   // _emscripten_resize_heap uses, so error_dialog.ts shows a OOM message.
60   std::set_new_handler(&OutOfMemoryHandler);
61 
62   g_trace_processor_rpc = new Rpc();
63 
64   // |resp_function| is a JS-bound function passed by wasm_bridge.ts. It will
65   // call back into JavaScript. There the JS code will copy the passed
66   // buffer with the response (a proto-encoded TraceProcessorRpc message) and
67   // postMessage() it to the controller. See the comment in wasm_bridge.ts for
68   // an overview of the JS<>Wasm callstack.
69   g_trace_processor_rpc->SetRpcResponseFunction(resp_function);
70 
71   g_req_buf = new uint8_t[req_buffer_size];
72   return g_req_buf;
73 }
74 
75 void EMSCRIPTEN_KEEPALIVE trace_processor_on_rpc_request(uint32_t);
trace_processor_on_rpc_request(uint32_t size)76 void trace_processor_on_rpc_request(uint32_t size) {
77   g_trace_processor_rpc->OnRpcRequest(g_req_buf, size);
78 }
79 
80 }  // extern "C"
81 }  // namespace perfetto::trace_processor
82 
main(int,char **)83 int main(int, char**) {
84   // This is unused but is needed for the following reasons:
85   // - We need the callMain() Emscripten JS helper function for traceconv (but
86   //   not for trace_processor).
87   // - Newer versions of emscripten require that callMain is explicitly exported
88   //   via EXTRA_EXPORTED_RUNTIME_METHODS = ['callMain'].
89   // - We have one set of EXTRA_EXPORTED_RUNTIME_METHODS for both
90   //   trace_processor.wasm (which does not need a main()) and traceconv (which
91   //   does).
92   // - Without this main(), the Wasm bootstrap code will cause a JS error at
93   //   runtime when trying to load trace_processor.js.
94   return 0;
95 }
96