-
Notifications
You must be signed in to change notification settings - Fork 145
/
start_ui.cc
176 lines (154 loc) · 5.18 KB
/
start_ui.cc
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
// Copyright 2011-2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/zynamics/bindiff/start_ui.h"
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#include <windows.h> // NOLINT
#else
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef __APPLE__
#include <sys/sysctl.h>
#else
#include <sys/sysinfo.h> // For sysinfo struct
#endif
#endif
#include <cstdint>
#include <cstdlib>
#include "third_party/absl/status/status.h"
#include "third_party/absl/strings/str_cat.h"
#include "third_party/absl/strings/str_join.h"
#include "third_party/absl/strings/str_split.h"
#include "third_party/zynamics/binexport/util/filesystem.h"
#include "third_party/zynamics/binexport/util/process.h"
namespace security::bindiff {
using ::security::binexport::SpawnProcess;
std::string GetJavaHomeDir() {
#ifdef _WIN32
char buffer[MAX_PATH] = {0};
// Try environment variable first.
int size = GetEnvironmentVariable("JAVA_HOME", &buffer[0], MAX_PATH);
if (size != 0 && size < MAX_PATH) {
return buffer;
}
#else
const char* java_home = getenv("JAVA_HOME");
if (java_home) {
return java_home;
}
#endif
return "";
}
uint64_t GetPhysicalMemSize() {
#if defined(_WIN32)
MEMORYSTATUSEX mi;
mi.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&mi);
return mi.ullTotalPhys;
#elif defined(__APPLE__)
uint64_t result;
int param[2];
param[0] = CTL_HW;
param[1] = HW_MEMSIZE;
size_t length = sizeof(uint64_t);
sysctl(param, 2, &result, &length, nullptr, 0);
return result;
#else
struct sysinfo mi;
sysinfo(&mi);
return static_cast<uint64_t>(mi.totalram) * mi.mem_unit;
#endif
}
absl::Status StartUiWithOptions(const std::vector<std::string>& extra_args,
const StartUiOptions& options) {
std::vector<std::string> argv;
// Set max heap size to 75% of available physical memory if unset.
const int max_heap_mb(
options.max_heap_size_mb > 0
? options.max_heap_size_mb
: std::max(static_cast<uint64_t>(512),
(GetPhysicalMemSize() / 1024 / 1024) * 3 / 4));
#ifdef __APPLE__
using ::security::binexport::SetEnvironmentVariable;
// On macOS, try to use the launcher binary first if java-binary is not set
// (the default). This improves overall UX: the dock icon will show correctly
// for example.
if (options.java_binary.empty()) {
// Directory layout:
// <prefix>/BinDiff.app/Contents/app (bindiff.jar)
// <prefix>/BinDiff.app/Contents/MacOS ("bindiff_dir")
// <prefix>/BinDiff.app/Contents/MacOS/bin (BinDiff binaries)
// <prefix>/BinDiff.app/Contents/MacOS/BinDiff (Launcher)
argv = {"/usr/bin/open", JoinPath(options.bindiff_dir, "../..")};
// The launcher does not take any JVM arguments, so they have to be set via
// environment variable.
std::string tool_options =
absl::StrCat("-Xms128m -Xmx", max_heap_mb, "m",
absl::StrJoin(options.java_vm_options, " "));
SetEnvironmentVariable("JAVA_TOOL_OPTIONS", tool_options);
argv.insert(argv.end(), extra_args.begin(), extra_args.end());
if (SpawnProcess(argv).ok()) {
return absl::OkStatus();
}
// Try again using the regular process below.
}
#endif
argv = {options.java_binary};
std::string& java_exe = argv.front();
if (java_exe.empty()) {
java_exe = GetJavaHomeDir();
if (!java_exe.empty()) {
absl::StrAppend(&java_exe, kPathSeparator, "bin", kPathSeparator);
}
#ifdef _WIN32
absl::StrAppend(&java_exe, "javaw.exe");
#else
absl::StrAppend(&java_exe, "java");
#endif
}
// Command-line takes precedence over JAVA_TOOL_OPTIONS, which may be set on
// macOS.
argv.push_back("-Xms128m");
argv.push_back(absl::StrCat("-Xmx", max_heap_mb, "m"));
argv.insert(argv.end(), options.java_vm_options.begin(),
options.java_vm_options.end());
#ifdef __APPLE__
argv.push_back("-Xdock:name=BinDiff");
#endif
argv.push_back("-jar");
bool found_jar = false;
// Try to find the UI JAR file in several locations (b/63617055).
for (const auto& relative_path : {"bin",
#ifdef __APPLE__
"../app",
#endif
""}) {
const std::string jar_file =
JoinPath(options.bindiff_dir, relative_path, "bindiff.jar");
found_jar = FileExists(jar_file);
if (found_jar) {
argv.push_back(jar_file);
break;
}
}
if (!found_jar) {
return absl::NotFoundError(
absl::StrCat("Missing jar file in prefix: ", options.bindiff_dir));
}
argv.insert(argv.end(), extra_args.begin(), extra_args.end());
return SpawnProcess(argv);
}
} // namespace security::bindiff