#include <tuple>
#include "base/base_switches.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "third_party/blink/public/common/switches.h"
namespace {
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && \
(defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
constexpr bool kIsTrapHandlerSupported = true;
#elif BUILDFLAG(IS_WIN) && defined(ARCH_CPU_X86_64)
constexpr bool kIsTrapHandlerSupported = true;
#elif BUILDFLAG(IS_MAC) && (defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
constexpr bool kIsTrapHandlerSupported = true;
#else
constexpr bool kIsTrapHandlerSupported = false;
#endif
class WasmTrapHandlerBrowserTest : public InProcessBrowserTest {
public:
WasmTrapHandlerBrowserTest() = default;
WasmTrapHandlerBrowserTest(const WasmTrapHandlerBrowserTest&) = delete;
WasmTrapHandlerBrowserTest& operator=(const WasmTrapHandlerBrowserTest&) =
delete;
~WasmTrapHandlerBrowserTest() override = default;
protected:
void RunJSTest(const std::string& js) const {
auto* const tab = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_EQ(true, content::EvalJs(tab, js));
}
void RunJSTestAndEnsureTrapHandlerRan(const std::string& js,
int num_oob_loads,
int num_oob_stores) const {
if (IsTrapHandlerEnabled()) {
int original_count = GetRecoveredTrapCount();
ASSERT_NO_FATAL_FAILURE(RunJSTest(js));
int new_count = GetRecoveredTrapCount();
int expected_count = IsPartialOOBWriteNoop()
? original_count + num_oob_loads + num_oob_stores
: original_count + num_oob_loads;
ASSERT_EQ(new_count, expected_count);
} else {
ASSERT_NO_FATAL_FAILURE(RunJSTest(js));
}
}
int GetRecoveredTrapCount() const {
const char* script = "%GetWasmRecoveredTrapCount()";
auto* const tab = browser()->tab_strip_model()->GetActiveWebContents();
return content::EvalJs(tab, script).ExtractInt();
}
bool IsPartialOOBWriteNoop() const {
const char* script = "%IsWasmPartialOOBWriteNoop()";
auto* const tab = browser()->tab_strip_model()->GetActiveWebContents();
return content::EvalJs(tab, script).ExtractBool();
}
bool IsTrapHandlerEnabled() const {
const char* script = "%IsWasmTrapHandlerEnabled()";
auto* const tab = browser()->tab_strip_model()->GetActiveWebContents();
return content::EvalJs(tab, script).ExtractBool();
}
private:
void SetUpCommandLine(base::CommandLine* command_line) override {
#if BUILDFLAG(IS_POSIX)
command_line->AppendSwitch(switches::kEnableCrashReporterForTesting);
#endif
command_line->AppendSwitchASCII(blink::switches::kJavaScriptFlags,
"--allow-natives-syntax");
}
};
IN_PROC_BROWSER_TEST_F(WasmTrapHandlerBrowserTest, OutOfBounds) {
ASSERT_TRUE(embedded_test_server()->Start());
const auto& url = embedded_test_server()->GetURL("/wasm/out_of_bounds.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_NO_FATAL_FAILURE(RunJSTest("peek_in_bounds()"));
ASSERT_NO_FATAL_FAILURE(
RunJSTestAndEnsureTrapHandlerRan("peek_out_of_bounds()", 6, 0));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"peek_out_of_bounds_grow_memory_from_zero_js()", 1, 0));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"peek_out_of_bounds_grow_memory_js()", 1, 0));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"peek_out_of_bounds_grow_memory_from_zero_wasm()", 1, 0));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"peek_out_of_bounds_grow_memory_wasm()", 1, 0));
ASSERT_NO_FATAL_FAILURE(RunJSTest("poke_in_bounds()"));
ASSERT_NO_FATAL_FAILURE(
RunJSTestAndEnsureTrapHandlerRan("poke_out_of_bounds()", 0, 6));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"poke_out_of_bounds_grow_memory_from_zero_js()", 0, 1));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"poke_out_of_bounds_grow_memory_js()", 0, 1));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"poke_out_of_bounds_grow_memory_from_zero_wasm()", 0, 1));
ASSERT_NO_FATAL_FAILURE(RunJSTestAndEnsureTrapHandlerRan(
"poke_out_of_bounds_grow_memory_wasm()", 0, 1));
}
IN_PROC_BROWSER_TEST_F(WasmTrapHandlerBrowserTest,
TrapHandlerCorrectlyConfigured) {
const bool is_trap_handler_enabled = IsTrapHandlerEnabled();
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
defined(UNDEFINED_SANITIZER)
std::ignore = is_trap_handler_enabled;
return;
#endif
ASSERT_EQ(is_trap_handler_enabled,
kIsTrapHandlerSupported && base::FeatureList::IsEnabled(
features::kWebAssemblyTrapHandler));
}
}