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
|
#pragma once
#include <string>
#include <vector>
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Logging/Logging.h"
#include "WAVM/Platform/File.h"
#include "WAVM/VFS/VFS.h"
namespace WAVM {
inline bool loadFile(const char* filename, std::vector<U8>& outFileContents)
{
VFS::Result result;
VFS::VFD* vfd = nullptr;
{
result = Platform::getHostFS().open(
filename, VFS::FileAccessMode::readOnly, VFS::FileCreateMode::openExisting, vfd);
if(result != VFS::Result::success) { goto printAndReturnError; }
// Ensure that we didn't open a directory.
VFS::FileInfo fileInfo;
result = vfd->getFileInfo(fileInfo);
if(result != VFS::Result::success) { goto printAndReturnError; }
else if(fileInfo.type == VFS::FileType::directory)
{
result = VFS::Result::isDirectory;
goto printAndReturnError;
}
U64 numFileBytes64 = 0;
result = vfd->seek(0, VFS::SeekOrigin::end, &numFileBytes64);
if(result != VFS::Result::success) { goto printAndReturnError; }
else if(numFileBytes64 > UINTPTR_MAX)
{
result = VFS::Result::outOfMemory;
goto printAndReturnError;
}
const Uptr numFileBytes = Uptr(numFileBytes64);
outFileContents.resize(numFileBytes);
result = vfd->seek(0, VFS::SeekOrigin::begin);
if(result != VFS::Result::success) { goto printAndReturnError; }
result = vfd->read(const_cast<U8*>(outFileContents.data()), numFileBytes);
if(result != VFS::Result::success) { goto printAndReturnError; }
result = vfd->close();
vfd = nullptr;
if(result != VFS::Result::success) { goto printAndReturnError; }
return true;
}
printAndReturnError:
Log::printf(Log::error, "Error loading '%s': %s\n", filename, VFS::describeResult(result));
if(vfd) { WAVM_ERROR_UNLESS(vfd->close() == VFS::Result::success); }
return false;
}
inline bool saveFile(const char* filename, const void* fileBytes, Uptr numFileBytes)
{
VFS::Result result;
VFS::VFD* vfd = nullptr;
{
result = Platform::getHostFS().open(
filename, VFS::FileAccessMode::writeOnly, VFS::FileCreateMode::createAlways, vfd);
if(result != VFS::Result::success) { goto printAndReturnError; }
result = vfd->write(fileBytes, numFileBytes);
if(result != VFS::Result::success) { goto printAndReturnError; }
result = vfd->close();
if(result != VFS::Result::success)
{
vfd = nullptr;
goto printAndReturnError;
}
return true;
}
printAndReturnError:
Log::printf(Log::error, "Error saving '%s': %s\n", filename, VFS::describeResult(result));
if(vfd) { WAVM_ERROR_UNLESS(vfd->close() == VFS::Result::success); }
return false;
}
inline const char* getEnvironmentVariableHelpText()
{
return "Environment variables:\n"
" WAVM_OUTPUT=<category>(,<category>)*\n"
" Enables printing of different categories of information to stdout.\n"
" Categories:\n"
" metrics Performance and memory use metrics\n"
" debug Debug information\n"
" trace-validation Trace instructions as they are validated\n"
" trace-compilation Trace instructions as they are compiled\n"
"\n"
" WAVM_OBJECT_CACHE_DIR=<directory>\n"
" Specifies a directory that WAVM will use to cache compiled object\n"
" code for WebAssembly modules.\n"
"\n"
" WAVM_OBJECT_CACHE_MAX_MB=<n>\n"
" Specifies the maximum amount of disk space that WAVM will use to\n"
" cache compiled object code.\n";
}
inline bool initLogFromEnvironment()
{
const char* wavmOutputEnv = WAVM_SCOPED_DISABLE_SECURE_CRT_WARNINGS(getenv("WAVM_OUTPUT"));
if(wavmOutputEnv && *wavmOutputEnv)
{
std::string category;
for(Uptr charIndex = 0;; ++charIndex)
{
const char c = wavmOutputEnv[charIndex];
if(c && c != ',') { category += c; }
else
{
if(category == "metrics") { Log::setCategoryEnabled(Log::metrics, true); }
else if(category == "debug")
{
Log::setCategoryEnabled(Log::debug, true);
}
else if(category == "trace-validation")
{
Log::setCategoryEnabled(Log::traceValidation, true);
}
else if(category == "trace-compilation")
{
Log::setCategoryEnabled(Log::traceCompilation, true);
}
else
{
Log::printf(Log::error,
"Invalid category in WAVM_OUTPUT environment: %s\n",
category.c_str());
return false;
}
if(!c) { break; }
else
{
category.clear();
}
}
}
}
return true;
}
}
|