Ghidra is a software reverse engineering (SRE) framework
GP-1105 improve HttpSymbolServer download timeouts Use async method for HTTP GET, improve various error messages whentimeout or cancel occurs.
dev747368ab6ab5696274
diff --git a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/DisabledSymbolServer.java b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/DisabledSymbolServer.javaindex dea6f2d56a..26514b5f6b 100644--- a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/DisabledSymbolServer.java+++ b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/DisabledSymbolServer.java@@ -15,11 +15,11 @@*/package pdb.symbolserver;+import java.io.IOException;import java.util.List;import java.util.Set;-import java.io.IOException;-+import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;/**@@ -105,7 +105,7 @@ public class DisabledSymbolServer implements SymbolServer {@Overridepublic SymbolServerInputStream getFileStream(String filename, TaskMonitor monitor)- throws IOException {+ throws IOException, CancelledException {return delegate.getFileStream(filename, monitor);}diff --git a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/HttpSymbolServer.java b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/HttpSymbolServer.javaindex a9d6980c4a..3f16813217 100644--- a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/HttpSymbolServer.java+++ b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/HttpSymbolServer.java@@ -24,9 +24,12 @@ import java.net.http.HttpRequest.BodyPublishers;import java.net.http.HttpResponse;import java.net.http.HttpResponse.BodyHandlers;import java.time.Duration;+import java.util.concurrent.*;import ghidra.net.HttpClients;import ghidra.util.Msg;+import ghidra.util.exception.CancelledException;+import ghidra.util.task.CancelledListener;import ghidra.util.task.TaskMonitor;/**@@ -37,7 +40,7 @@ import ghidra.util.task.TaskMonitor;public class HttpSymbolServer extends AbstractSymbolServer {private static final String GHIDRA_USER_AGENT = "Ghidra_HttpSymbolServer_client";private static final int HTTP_STATUS_OK = HttpURLConnection.HTTP_OK;- private static final int HTTP_REQUEST_TIMEOUT_MS = 10000; // 10 seconds+ private static final int HTTP_REQUEST_TIMEOUT_MS = 10 * 1000; // 10 seconds/*** Predicate that tests if the location string is an instance of a HttpSymbolServer location.@@ -81,14 +84,16 @@ public class HttpSymbolServer extends AbstractSymbolServer {private HttpRequest.Builder request(String str) {return HttpRequest.newBuilder(serverURI.resolve(str))- .timeout(Duration.ofMillis(HTTP_REQUEST_TIMEOUT_MS)).setHeader("User-Agent", GHIDRA_USER_AGENT);}@Overridepublic boolean exists(String filename, TaskMonitor monitor) {try {- HttpRequest request = request(filename).method("HEAD", BodyPublishers.noBody()).build();+ HttpRequest request = request(filename)+ .timeout(Duration.ofMillis(HTTP_REQUEST_TIMEOUT_MS))+ .method("HEAD", BodyPublishers.noBody())+ .build();Msg.debug(this,logPrefix() + ": Checking exist for [" + filename + "]: " + request.toString());@@ -107,23 +112,50 @@ public class HttpSymbolServer extends AbstractSymbolServer {@Overridepublic SymbolServerInputStream getFileStream(String filename, TaskMonitor monitor)- throws IOException {+ throws IOException, CancelledException {+ monitor.setIndeterminate(true);+ monitor.setMessage("Connecting to " + serverURI);++ HttpRequest request = request(filename).GET().build();+ Msg.debug(this,+ logPrefix() + ": Getting file [" + filename + "]: " + request.toString());+ CompletableFuture<HttpResponse<InputStream>> futureResponse =+ HttpClients.getHttpClient().sendAsync(request, BodyHandlers.ofInputStream());+ CancelledListener l = () -> futureResponse.cancel(true);+ monitor.addCancelledListener(l);+try {- HttpRequest request = request(filename).GET().build();- Msg.debug(this,- logPrefix() + ": Getting file [" + filename + "]: " + request.toString());HttpResponse<InputStream> response =- HttpClients.getHttpClient().send(request, BodyHandlers.ofInputStream());+ futureResponse.get(HTTP_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);+int statusCode = response.statusCode();+ monitor.setMessage(statusCode == HTTP_STATUS_OK ? "Success" : "Failed");Msg.debug(this, logPrefix() + ": Http response: " + response.statusCode());- if (statusCode == HTTP_STATUS_OK) {- long contentLen = response.headers().firstValueAsLong("Content-Length").orElse(-1);- return new SymbolServerInputStream(response.body(), contentLen);+ InputStream bodyIS = response.body();+ if (statusCode != HTTP_STATUS_OK) {+ // clean up the body inputstream since its just an error message+ uncheckedClose(bodyIS);+ throw new IOException("Unable to get file: " + statusCode);}- throw new IOException("Unable to get file: " + statusCode);+ long contentLen = response.headers().firstValueAsLong("Content-Length").orElse(-1);+ return new SymbolServerInputStream(bodyIS, contentLen);}catch (InterruptedException e) {- throw new IOException("Http get interrupted");+ throw new CancelledException("Download canceled");+ }+ catch (TimeoutException e) {+ throw new IOException("Connection timed out");+ }+ catch (ExecutionException e) {+ // if possible, unwrap the exception that happened inside the future+ Throwable cause = e.getCause();+ Msg.error(this, "Error during HTTP get", cause);+ throw (cause instanceof IOException)+ ? (IOException) cause+ : new IOException("Error during HTTP get", cause);+ }+ finally {+ monitor.removeCancelledListener(l);}}@@ -147,4 +179,13 @@ public class HttpSymbolServer extends AbstractSymbolServer {return getClass().getSimpleName() + "[" + serverURI + "]";}+ private static void uncheckedClose(InputStream is) {+ try {+ is.close();+ }+ catch (IOException e) {+ // ignore it+ }+ }+}diff --git a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/LocalSymbolStore.java b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/LocalSymbolStore.javaindex 49c66c8f69..525013fcb8 100644--- a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/LocalSymbolStore.java+++ b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/LocalSymbolStore.java@@ -339,14 +339,15 @@ public class LocalSymbolStore extends AbstractSymbolServer implements SymbolStorFile destinationFileTmp = new File(rootDir, relativeDestinationFilename + ".tmp");destinationFileTmp.delete();- monitor.setMessage("Storing " + filename + " in local symbol store ");-- if (symbolServerInputStream.getExpectedLength() >= 0) {- monitor.initialize(symbolServerInputStream.getExpectedLength());- }- try {- long bytesCopied = FileUtilities.copyStreamToFile(- symbolServerInputStream.getInputStream(), destinationFileTmp, false, monitor);+ long expectedLength = symbolServerInputStream.getExpectedLength();+ String expectedLenMsg =+ expectedLength >= 0 ? (" (" + FileUtilities.formatLength(expectedLength) + ")") : "";+ monitor.setIndeterminate(expectedLength < 0);+ monitor.initialize(expectedLength);+ monitor.setMessage("Storing " + filename + " in local symbol store" + expectedLenMsg);+ try (InputStream is = symbolServerInputStream.getInputStream()) {+ long bytesCopied =+ FileUtilities.copyStreamToFile(is, destinationFileTmp, false, monitor);if (symbolServerInputStream.getExpectedLength() >= 0 &&bytesCopied != symbolServerInputStream.getExpectedLength()) {throw new IOException("Copy length mismatch, expected " +@@ -361,7 +362,6 @@ public class LocalSymbolStore extends AbstractSymbolServer implements SymbolStorfinally {destinationFileTmp.delete();}-}@Overridediff --git a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/SymbolServer.java b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/SymbolServer.javaindex e958183a75..48c6ba4ecf 100644--- a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/SymbolServer.java+++ b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/SymbolServer.java@@ -15,12 +15,12 @@*/package pdb.symbolserver;-import java.util.List;-import java.util.Set;-import java.io.IOException;import java.io.InputStream;+import java.util.List;+import java.util.Set;+import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;/**@@ -87,8 +87,10 @@ public interface SymbolServer {* @param monitor {@link TaskMonitor}* @return {@link SymbolServerInputStream} wrapped {@link InputStream}, never null* @throws IOException if error or not found+ * @throws CancelledException if cancelled*/- SymbolServerInputStream getFileStream(String filename, TaskMonitor monitor) throws IOException;+ SymbolServerInputStream getFileStream(String filename, TaskMonitor monitor)+ throws IOException, CancelledException;/*** Returns a location description string of a specific file contained in this symbol server.diff --git a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/ui/LoadPdbDialog.java b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/ui/LoadPdbDialog.javaindex 516effa84e..98e680a1c4 100644--- a/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/ui/LoadPdbDialog.java+++ b/Ghidra/Features/PDB/src/main/java/pdb/symbolserver/ui/LoadPdbDialog.java@@ -46,6 +46,7 @@ import ghidra.framework.preferences.Preferences;import ghidra.program.model.listing.Program;import ghidra.util.*;import ghidra.util.exception.CancelledException;+import ghidra.util.exception.IOCancelledException;import ghidra.util.filechooser.ExtensionFileFilter;import ghidra.util.filechooser.GhidraFileFilter;import ghidra.util.layout.PairLayout;@@ -552,7 +553,8 @@ public class LoadPdbDialog extends DialogComponentProvider {(!selectedSymbolFile.isExactMatch(programSymbolFileInfo) &&OptionDialog.showYesNoDialog(loadPdbButton, "Mismatched Pdb File Warning","<html>The selected file is not an exact match for the current program.<br>" +- "Note: <b>Invalid disassembly may be produced!</b><br>Continue anyway?") != OptionDialog.YES_OPTION)) {+ "Note: <b>Invalid disassembly may be produced!</b><br>" ++ "Continue anyway?") != OptionDialog.YES_OPTION)) {return;}executeMonitoredRunnable("Prepare Selected Symbol File",@@ -588,12 +590,13 @@ public class LoadPdbDialog extends DialogComponentProvider {Swing.runLater(() -> close());return;}+ catch (CancelledException | IOCancelledException ce) {+ setStatusText("Operation cancelled");+ monitor.clearCanceled();+ }catch (IOException ioe) {Msg.showError(this, getComponent(), "Error Getting Symbol File", ioe);}- catch (CancelledException ce) {- // ignore- }}private StatusText getAllowRemoteWarning() {