summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Lib/Platform/POSIX/DiagnosticsPOSIX.cpp
blob: 9975de2fa6f1550fbff8f84b7509c64512f85335 (plain) (blame)
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
#include <cxxabi.h>
#include <dlfcn.h>
#include <stdio.h>
#include <atomic>
#include <string>
#include "POSIXPrivate.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Platform/Diagnostics.h"

#if WAVM_ENABLE_UNWIND
#define UNW_LOCAL_ONLY
#include "libunwind.h"
#endif

#if WAVM_ENABLE_ASAN                                                                               \
	&& (defined(HAS_SANITIZER_PRINT_MEMORY_PROFILE_1)                                              \
		|| defined(HAS_SANITIZER_PRINT_MEMORY_PROFILE_2))
#include <sanitizer/common_interface_defs.h>
#endif

using namespace WAVM;
using namespace WAVM::Platform;

extern "C" const char* __asan_default_options()
{
	return "handle_segv=false"
		   ":handle_sigbus=false"
		   ":handle_sigfpe=false"
		   ":replace_intrin=false";
}

CallStack Platform::captureCallStack(Uptr numOmittedFramesFromTop)
{
	CallStack result;

#if WAVM_ENABLE_UNWIND
	unw_context_t context;
	WAVM_ERROR_UNLESS(!unw_getcontext(&context));

	unw_cursor_t cursor;

	WAVM_ERROR_UNLESS(!unw_init_local(&cursor, &context));
	for(Uptr frameIndex = 0; !result.frames.isFull() && unw_step(&cursor) > 0; ++frameIndex)
	{
		if(frameIndex >= numOmittedFramesFromTop)
		{
			unw_word_t ip;
			WAVM_ERROR_UNLESS(!unw_get_reg(&cursor, UNW_REG_IP, &ip));
			result.frames.push_back(CallStack::Frame{frameIndex == 0 ? ip : (ip - 1)});
		}
	}
#endif

	return result;
}

bool Platform::getInstructionSourceByAddress(Uptr ip, InstructionSource& outSource)
{
#if defined(__linux__) || defined(__APPLE__)
	// Look up static symbol information for the address.
	Dl_info symbolInfo;
	if(dladdr((void*)ip, &symbolInfo))
	{
		WAVM_ASSERT(symbolInfo.dli_fname);
		outSource.module = symbolInfo.dli_fname;
		if(!symbolInfo.dli_sname)
		{
			outSource.function = std::string();
			outSource.instructionOffset = ip - reinterpret_cast<Uptr>(symbolInfo.dli_fbase);
		}
		else
		{
			if(symbolInfo.dli_sname[0] == '_')
			{
				int demangleStatus = 0;
				if(char* demangledBuffer
				   = abi::__cxa_demangle(symbolInfo.dli_sname, nullptr, nullptr, &demangleStatus))
				{
					outSource.function = demangledBuffer;
					free(demangledBuffer);
				}
			}
			else
			{
				outSource.function = symbolInfo.dli_sname;
			}
			outSource.instructionOffset = ip - reinterpret_cast<Uptr>(symbolInfo.dli_saddr);
		}
		return true;
	}
#endif
	return false;
}

static std::atomic<Uptr> numCommittedPageBytes{0};

void Platform::printMemoryProfile()
{
#if WAVM_ENABLE_ASAN
#if defined(HAS_SANITIZER_PRINT_MEMORY_PROFILE_1)
	__sanitizer_print_memory_profile(100);
#elif defined(HAS_SANITIZER_PRINT_MEMORY_PROFILE_2)
	__sanitizer_print_memory_profile(100, 20);
#endif
#endif
	printf("Committed virtual pages: %" PRIuPTR " KB\n",
		   uintptr_t(numCommittedPageBytes.load(std::memory_order_seq_cst) / 1024));
	fflush(stdout);
}

void Platform::registerVirtualAllocation(Uptr numBytes) { numCommittedPageBytes += numBytes; }

void Platform::deregisterVirtualAllocation(Uptr numBytes) { numCommittedPageBytes -= numBytes; }