Wrap flipper client methods to avoid crash

Summary: Wraps flipper client methods to avoid crash. Also added a tests which makes sure that malcious plugin cannot cause a crash

Reviewed By: jknoxville

Differential Revision: D13153277

fbshipit-source-id: ac21731fa3c4eb447f189e61f61b9e83aad91e13
This commit is contained in:
Pritesh Nandgaonkar
2018-11-26 07:52:13 -08:00
committed by Facebook Github Bot
parent 37c973d0c9
commit bf3be3e16c
4 changed files with 248 additions and 154 deletions

View File

@@ -1,22 +1,21 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
* Copyright (c) Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#pragma once
#include <map>
#include <mutex>
#include <vector>
#include "FlipperConnectionImpl.h"
#include "FlipperConnectionManager.h"
#include "FlipperInitConfig.h"
#include "FlipperPlugin.h"
#include "FlipperState.h"
#include "FlipperConnectionManager.h"
#include <map>
#include <mutex>
#include "FlipperStep.h"
#include <vector>
namespace facebook {
namespace flipper {
@@ -24,23 +23,26 @@ namespace flipper {
class FlipperClient : public FlipperConnectionManager::Callbacks {
public:
/**
Call before accessing instance with FlipperClient::instance(). This will set up
all the state needed to establish a Flipper connection.
Call before accessing instance with FlipperClient::instance(). This will set
up all the state needed to establish a Flipper connection.
*/
static void init(FlipperInitConfig config);
/**
Standard accessor for the shared FlipperClient instance. This returns a
singleton instance to a shared FlipperClient. First call to this function will
create the shared FlipperClient. Must call FlipperClient::initDeviceData() before
first call to FlipperClient::instance().
singleton instance to a shared FlipperClient. First call to this function
will create the shared FlipperClient. Must call
FlipperClient::initDeviceData() before first call to
FlipperClient::instance().
*/
static FlipperClient* instance();
/**
Only public for testing
*/
FlipperClient(std::unique_ptr<FlipperConnectionManager> socket, std::shared_ptr<FlipperState> state)
FlipperClient(
std::unique_ptr<FlipperConnectionManager> socket,
std::shared_ptr<FlipperState> state)
: socket_(std::move(socket)), flipperState_(state) {
auto step = flipperState_->start("Create client");
socket_->setCallbacks(this);
@@ -48,15 +50,19 @@ class FlipperClient : public FlipperConnectionManager::Callbacks {
}
void start() {
auto step = flipperState_->start("Start client");
socket_->start();
step->complete();
performAndReportError([this]() {
auto step = flipperState_->start("Start client");
socket_->start();
step->complete();
});
}
void stop() {
auto step = flipperState_->start("Stop client");
socket_->stop();
step->complete();
performAndReportError([this]() {
auto step = flipperState_->start("Stop client");
socket_->stop();
step->complete();
});
}
void onConnected() override;
@@ -86,6 +92,7 @@ class FlipperClient : public FlipperConnectionManager::Callbacks {
}
bool hasPlugin(const std::string& identifier);
void performAndReportError(const std::function<void()>& func);
private:
static FlipperClient* instance_;
@@ -96,7 +103,6 @@ class FlipperClient : public FlipperConnectionManager::Callbacks {
std::mutex mutex_;
std::shared_ptr<FlipperState> flipperState_;
void performAndReportError(const std::function<void()>& func);
void disconnect(std::shared_ptr<FlipperPlugin> plugin);
void startBackgroundPlugins();
};