update_engine: Make SystemState pointer static only

The style guide does not allow a global object with non-trivial dtor. It
can cause hidden problems and it has caused issue this CL is
fixing (look at the attached bug). Instead of keeping the ownership of
the SystemState in global, we can keep the ownership in the high level
object DaemonChromeOS and keep a global static pointer to it so it can
easily be accessed by SystemState::Get().

BUG=b:174212887
TEST=cros_workon_make --board reef --test update_engine
TEST=cros deploy + stop update-engine -> The update_engine did not crash anymore.

Change-Id: I442f4220bfd8586c59fcdfd7d699776362143467
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2566875
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
diff --git a/cros/real_system_state.h b/cros/real_system_state.h
index 348c31b..81a5e0e 100644
--- a/cros/real_system_state.h
+++ b/cros/real_system_state.h
@@ -47,13 +47,16 @@
 // used by the actual product code.
 class RealSystemState : public SystemState {
  public:
+  // Constructs all system objects that do not require separate initialization;
+  // see Initialize() below for the remaining ones.
+  RealSystemState() = default;
   ~RealSystemState() = default;
 
-  static void CreateInstance() {
-    CHECK(!g_instance_) << "SystemState has been previously created.";
-    RealSystemState* rss = new RealSystemState();
-    g_instance_.reset(rss);
-    LOG_IF(FATAL, !rss->Initialize()) << "Failed to initialize system state.";
+  static void SetInstance(RealSystemState* system_state) {
+    CHECK(g_pointer_ == nullptr) << "SystemState has been previously set.";
+    g_pointer_ = system_state;
+    LOG_IF(FATAL, !system_state->Initialize())
+        << "Failed to initialize system state.";
   }
 
   // SystemState overrides.
@@ -108,10 +111,6 @@
   DlcServiceInterface* dlcservice() override { return dlcservice_.get(); }
 
  private:
-  // Constructs all system objects that do not require separate initialization;
-  // see Initialize() below for the remaining ones.
-  RealSystemState() = default;
-
   // Initializes and sets systems objects that require an initialization
   // separately from construction. Returns |true| on success.
   bool Initialize();