1 /*
2 * Copyright (C) 2023 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 "sigevent_emulation.h"
18
19 #include <cstring>
20
21 #include "berberis/runtime_primitives/guest_function_wrapper_impl.h"
22 #include "berberis/runtime_primitives/runtime_library.h"
23
24 namespace berberis {
25
26 namespace {
27
28 using NotifyFunc = void (*)(sigval);
29
30 } // namespace
31
ConvertGuestSigeventToHost(sigevent * guest_sigevent,sigevent * host_sigevent)32 sigevent* ConvertGuestSigeventToHost(sigevent* guest_sigevent, sigevent* host_sigevent) {
33 if (guest_sigevent == nullptr) {
34 return nullptr;
35 }
36 if (guest_sigevent->sigev_notify != SIGEV_THREAD) {
37 return guest_sigevent;
38 }
39 // Even though sigevent data structure is low-level and includes unions,
40 // it can be safely copied with memcpy.
41 memcpy(host_sigevent, guest_sigevent, sizeof(*host_sigevent));
42 GuestAddr func = reinterpret_cast<GuestAddr>(guest_sigevent->sigev_notify_function);
43 host_sigevent->sigev_notify_function = AsFuncPtr<NotifyFunc>(
44 WrapGuestFunctionImpl(func, "vp", RunGuestCall, "sigev_notify_function"));
45 return host_sigevent;
46 }
47
48 } // namespace berberis
49