[zircon][utest][pager] Reland "Exclude root-resource tests at runtime, not compile"

This is a reland of fd4a750800aeabfe01beb0062a1eee7c873c3608

Original change's description:
> [zircon][utest][pager] Exclude root-resource tests at runtime, not compile
>
> Rather than using #ifdef BUILD_COMBINED_TESTS to decide whether the
> root resource handle will be available for tests that need it, instead
> use a weak symbol for get_root_resource and check it at runtime.
> This obviates the need to compile the test sourcce file differently for
> the integrated and standalone cases.
>
> Bug: BLD-325
> Test: CQ
> Change-Id: I48e3f192af87da354f05cd9b75c6e1124d4e2106

Bug: BLD-325
Change-Id: I05613f4756a5f573e681e540c09da07f02d25e98
diff --git a/zircon/system/utest/core/pager/pager.cpp b/zircon/system/utest/core/pager/pager.cpp
index 24ffbd7..7c32e74 100644
--- a/zircon/system/utest/core/pager/pager.cpp
+++ b/zircon/system/utest/core/pager/pager.cpp
@@ -15,7 +15,7 @@
 #include "userpager.h"
 
 __BEGIN_CDECLS
-extern zx_handle_t get_root_resource(void);
+__WEAK extern zx_handle_t get_root_resource(void);
 __END_CDECLS
 
 namespace pager_tests {
@@ -1573,17 +1573,23 @@
     ASSERT_EQ(zx_pager_supply_pages(pager.get(), vmo.get(),
                                     0, 0, aux_vmo.get(), 1), ZX_ERR_INVALID_ARGS);
 
-#ifdef BUILD_COMBINED_TESTS
-    // unsupported aux vmo type
-    zx::vmo physical_vmo;
-    // We're not actually going to do anything with this vmo, and since the kernel doesn't
-    // do any checks with the address if you're using the root resource, just use addr 0.
-    ASSERT_EQ(zx_vmo_create_physical(get_root_resource(), 0, ZX_PAGE_SIZE,
-                                     physical_vmo.reset_and_get_address()),
-              ZX_OK);
-    ASSERT_EQ(zx_pager_supply_pages(pager.get(), vmo.get(),
-                                    0, ZX_PAGE_SIZE, physical_vmo.get(), 0), ZX_ERR_NOT_SUPPORTED);
-#endif // BUILD_COMBINED_TESTS
+    // The get_root_resource() function is a weak reference here.  In the
+    // standalone pager-test program, it's not defined because the root
+    // resource handle is not available to to the test.  In the unified
+    // standalone core-tests program, get_root_resource() is available.
+    if (&get_root_resource) {
+        // unsupported aux vmo type
+        zx::vmo physical_vmo;
+        // We're not actually going to do anything with this vmo, and since the
+        // kernel doesn't do any checks with the address if you're using the
+        // root resource, just use addr 0.
+        ASSERT_EQ(zx_vmo_create_physical(get_root_resource(), 0, ZX_PAGE_SIZE,
+                                         physical_vmo.reset_and_get_address()),
+                  ZX_OK);
+        ASSERT_EQ(zx_pager_supply_pages(pager.get(), vmo.get(),
+                                        0, ZX_PAGE_SIZE, physical_vmo.get(), 0),
+                  ZX_ERR_NOT_SUPPORTED);
+    }
 
     // violations of conditions for taking pages from a vmo
     enum PagerViolation {
@@ -1592,12 +1598,14 @@
         kHasMapping,
         kHasClone,
         kNotCommitted,
-#ifdef BUILD_COMBINED_TESTS
         kHasPinned,
-#endif // BUILD_COMBINED_TESTS
         kViolationCount,
     };
     for (uint32_t i = 0; i < kViolationCount; i++) {
+        if (i == kHasPinned && ! &get_root_resource) {
+            continue;
+        }
+
         zx::vmo aux_vmo; // aux vmo given to supply pages
         zx::vmo alt_vmo; // alt vmo if clones are involved
 
@@ -1631,7 +1639,6 @@
             }
         }
 
-#ifdef BUILD_COMBINED_TESTS
         zx::iommu iommu;
         zx::bti bti;
         zx::pmt pmt;
@@ -1644,16 +1651,14 @@
             zx_paddr_t addr;
             ASSERT_EQ(bti.pin(ZX_BTI_PERM_READ, aux_vmo, 0, ZX_PAGE_SIZE, &addr, 1, &pmt), ZX_OK);
         }
-#endif // BUILD_COMBINED_TESTS
 
         ASSERT_EQ(zx_pager_supply_pages(pager.get(), vmo.get(),
-                                        0, ZX_PAGE_SIZE, aux_vmo.get(), 0), ZX_ERR_BAD_STATE);
+                                        0, ZX_PAGE_SIZE, aux_vmo.get(), 0),
+                  ZX_ERR_BAD_STATE);
 
-#ifdef BUILD_COMBINED_TESTS
         if (pmt) {
             pmt.unpin();
         }
-#endif // BUILD_COMBINED_TESTS
     }
 
     // out of range pager_vmo region
diff --git a/zircon/system/utest/core/pager/test_thread.cpp b/zircon/system/utest/core/pager/test_thread.cpp
index 633d44f..40c4868 100644
--- a/zircon/system/utest/core/pager/test_thread.cpp
+++ b/zircon/system/utest/core/pager/test_thread.cpp
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include <fbl/function.h>
+#include <inspector/inspector.h>
 #include <string.h>
 #include <threads.h>
 #include <zircon/process.h>
@@ -12,10 +13,6 @@
 #include <zircon/syscalls/port.h>
 #include <zircon/threads.h>
 
-#ifndef BUILD_COMBINED_TESTS
-#include <inspector/inspector.h>
-#endif
-
 #include "test_thread.h"
 
 namespace pager_tests {
@@ -131,8 +128,6 @@
 }
 
 void TestThread::PrintDebugInfo(const zx_exception_report_t& report) {
-    // The crash library isn't available when running as part of core-tests
-#ifndef BUILD_COMBINED_TESTS
     printf("\nCrash info:\n");
 
     zx_thread_state_general_regs_t regs;
@@ -155,7 +150,6 @@
     inspector_dso_print_list(stdout, dso_list);
     inspector_print_backtrace(stdout, zx_process_self(), zx_thread_.get(), dso_list,
                               pc, sp, fp, true);
-#endif
 }
 
 bool TestThread::WaitForBlocked() {
diff --git a/zircon/system/utest/core/rules.mk b/zircon/system/utest/core/rules.mk
index 42845eb..ab40bf1 100644
--- a/zircon/system/utest/core/rules.mk
+++ b/zircon/system/utest/core/rules.mk
@@ -19,8 +19,10 @@
 
 MODULE_STATIC_LIBS := \
     system/ulib/ddk \
+    system/ulib/elf-search \
     system/ulib/fbl \
     system/ulib/fzl \
+    system/ulib/inspector \
     system/ulib/runtime \
     system/ulib/sync \
     system/ulib/zx \
@@ -31,13 +33,13 @@
 MODULE_STATIC_LIBS += system/utest/core/threads/thread-functions
 
 MODULE_LIBS := \
+    third_party/ulib/backtrace \
+    third_party/ulib/ngunwind \
     system/ulib/unittest \
     system/ulib/mini-process \
     system/ulib/zircon \
     system/ulib/c
 
-MODULE_DEFINES := BUILD_COMBINED_TESTS=1
-
 # core/channel needs a header file generated by kernel/lib/vdso/rules.mk.
 MODULE_COMPILEFLAGS += -I$(BUILDDIR)/kernel/lib/vdso
 MODULE_SRCDEPS += $(BUILDDIR)/kernel/lib/vdso/vdso-code.h