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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <algorithm>
#include "mozilla/IOInterposer.h"
#include "mozilla/PoisonIOInterposer.h"
#include "mozilla/ProcessedStack.h"
#include "mozilla/SHA1.h"
#include "mozilla/Scoped.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Unused.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsLocalFile.h"
#include "nsPrintfCString.h"
#include "mozilla/StackWalk.h"
#include "plstr.h"
#include "prio.h"
#ifdef XP_WIN
# define NS_SLASH "\\"
# include <fcntl.h>
# include <io.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/stat.h>
# include <windows.h>
#else
# define NS_SLASH "/"
#endif
#include "LateWriteChecks.h"
/*************************** Auxiliary Declarations ***************************/
static MOZ_THREAD_LOCAL(int) tlsSuspendLateWriteChecks;
bool SuspendingLateWriteChecksForCurrentThread() {
if (!tlsSuspendLateWriteChecks.init()) {
return true;
}
return tlsSuspendLateWriteChecks.get() > 0;
}
// This a wrapper over a file descriptor that provides a Printf method and
// computes the sha1 of the data that passes through it.
class SHA1Stream {
public:
explicit SHA1Stream(FILE* aStream) : mFile(aStream) {
MozillaRegisterDebugFILE(mFile);
}
void Printf(const char* aFormat, ...) MOZ_FORMAT_PRINTF(2, 3) {
MOZ_ASSERT(mFile);
va_list list;
va_start(list, aFormat);
nsAutoCString str;
str.AppendVprintf(aFormat, list);
va_end(list);
mSHA1.update(str.get(), str.Length());
mozilla::Unused << fwrite(str.get(), 1, str.Length(), mFile);
}
void Finish(mozilla::SHA1Sum::Hash& aHash) {
int fd = fileno(mFile);
fflush(mFile);
MozillaUnRegisterDebugFD(fd);
fclose(mFile);
mSHA1.finish(aHash);
mFile = nullptr;
}
private:
FILE* mFile;
mozilla::SHA1Sum mSHA1;
};
static void RecordStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP,
void* aClosure) {
std::vector<uintptr_t>* stack =
static_cast<std::vector<uintptr_t>*>(aClosure);
stack->push_back(reinterpret_cast<uintptr_t>(aPC));
}
/**************************** Late-Write Observer ****************************/
/**
* An implementation of IOInterposeObserver to be registered with IOInterposer.
* This observer logs all writes as late writes.
*/
class LateWriteObserver final : public mozilla::IOInterposeObserver {
using char_type = mozilla::filesystem::Path::value_type;
public:
explicit LateWriteObserver(const char_type* aProfileDirectory)
: mProfileDirectory(NS_xstrdup(aProfileDirectory)) {}
~LateWriteObserver() {
free(mProfileDirectory);
mProfileDirectory = nullptr;
}
void Observe(
mozilla::IOInterposeObserver::Observation& aObservation) override;
private:
char_type* mProfileDirectory;
};
void LateWriteObserver::Observe(
mozilla::IOInterposeObserver::Observation& aOb) {
if (SuspendingLateWriteChecksForCurrentThread()) {
return;
}
#ifdef DEBUG
MOZ_CRASH();
#endif
// If we can't record then abort
if (!mozilla::Telemetry::CanRecordExtended()) {
return;
}
// Write the stack and loaded libraries to a file. We can get here
// concurrently from many writes, so we use multiple temporary files.
std::vector<uintptr_t> rawStack;
MozStackWalk(RecordStackWalker, nullptr, /* maxFrames */ 0, &rawStack);
mozilla::Telemetry::ProcessedStack stack =
mozilla::Telemetry::GetStackAndModules(rawStack);
nsTAutoString<char_type> nameAux(mProfileDirectory);
nameAux.AppendLiteral(NS_SLASH "Telemetry.LateWriteTmpXXXXXX");
char_type* name = nameAux.BeginWriting();
// We want the sha1 of the entire file, so please don't write to fd
// directly; use sha1Stream.
FILE* stream;
#ifdef XP_WIN
HANDLE hFile;
do {
// mkstemp isn't supported so keep trying until we get a file
_wmktemp_s(char16ptr_t(name), NS_strlen(name) + 1);
hFile = CreateFileW(char16ptr_t(name), GENERIC_WRITE, 0, nullptr,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
} while (GetLastError() == ERROR_FILE_EXISTS);
if (hFile == INVALID_HANDLE_VALUE) {
MOZ_CRASH("Um, how did we get here?");
}
// http://support.microsoft.com/kb/139640
int fd = _open_osfhandle((intptr_t)hFile, _O_APPEND);
if (fd == -1) {
MOZ_CRASH("Um, how did we get here?");
}
stream = _fdopen(fd, "w");
#else
int fd = mkstemp(name);
if (fd == -1) {
MOZ_CRASH("mkstemp failed");
}
stream = fdopen(fd, "w");
#endif
SHA1Stream sha1Stream(stream);
size_t numModules = stack.GetNumModules();
sha1Stream.Printf("%u\n", (unsigned)numModules);
for (size_t i = 0; i < numModules; ++i) {
mozilla::Telemetry::ProcessedStack::Module module = stack.GetModule(i);
sha1Stream.Printf("%s %s\n", module.mBreakpadId.get(),
NS_ConvertUTF16toUTF8(module.mName).get());
}
size_t numFrames = stack.GetStackSize();
sha1Stream.Printf("%u\n", (unsigned)numFrames);
for (size_t i = 0; i < numFrames; ++i) {
const mozilla::Telemetry::ProcessedStack::Frame& frame = stack.GetFrame(i);
// NOTE: We write the offsets, while the atos tool expects a value with
// the virtual address added. For example, running otool -l on the the
// firefox binary shows
// cmd LC_SEGMENT_64
// cmdsize 632
// segname __TEXT
// vmaddr 0x0000000100000000
// so to print the line matching the offset 123 one has to run
// atos -o firefox 0x100000123.
sha1Stream.Printf("%d %x\n", frame.mModIndex, (unsigned)frame.mOffset);
}
mozilla::SHA1Sum::Hash sha1;
sha1Stream.Finish(sha1);
// Note: These files should be deleted by telemetry once it reads them. If
// there were no telemetry runs by the time we shut down, we just add files
// to the existing ones instead of replacing them. Given that each of these
// files is a bug to be fixed, that is probably the right thing to do.
// We append the sha1 of the contents to the file name. This provides a simple
// client side deduplication.
nsAutoString finalName(u"Telemetry.LateWriteFinal-"_ns);
for (int i = 0; i < 20; ++i) {
finalName.AppendPrintf("%02x", sha1[i]);
}
RefPtr<nsLocalFile> file = new nsLocalFile(nameAux);
file->RenameTo(nullptr, finalName);
}
/******************************* Setup/Teardown *******************************/
static mozilla::StaticAutoPtr<LateWriteObserver> sLateWriteObserver;
namespace mozilla {
void InitLateWriteChecks() {
nsCOMPtr<nsIFile> mozFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mozFile));
if (mozFile) {
PathString nativePath = mozFile->NativePath();
if (nativePath.get()) {
sLateWriteObserver = new LateWriteObserver(nativePath.get());
}
}
}
void BeginLateWriteChecks() {
if (sLateWriteObserver) {
IOInterposer::Register(IOInterposeObserver::OpWriteFSync,
sLateWriteObserver);
}
}
void StopLateWriteChecks() {
if (sLateWriteObserver) {
IOInterposer::Unregister(IOInterposeObserver::OpAll, sLateWriteObserver);
// Deallocation would not be thread-safe, and StopLateWriteChecks() is
// called at shutdown and only in special cases.
// sLateWriteObserver = nullptr;
}
}
void PushSuspendLateWriteChecks() {
if (!tlsSuspendLateWriteChecks.init()) {
return;
}
tlsSuspendLateWriteChecks.set(tlsSuspendLateWriteChecks.get() + 1);
}
void PopSuspendLateWriteChecks() {
if (!tlsSuspendLateWriteChecks.init()) {
return;
}
int current = tlsSuspendLateWriteChecks.get();
MOZ_ASSERT(current > 0);
tlsSuspendLateWriteChecks.set(current - 1);
}
} // namespace mozilla
|