[lldb][windows] add unit tests for the StripConPTYSequences method (#194654)

Co-authored-by: Nerixyz <nero.9@hotmail.de>
This commit is contained in:
Charles Zablit
2026-04-29 12:05:25 +01:00
committed by GitHub
parent 7b58716d96
commit 6617aac292
7 changed files with 245 additions and 81 deletions

View File

@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_HOST_WINDOWS_CONPTYUTILS_H
#define LLDB_HOST_WINDOWS_CONPTYUTILS_H
#include <cstddef>
namespace lldb_private {
/// Remove ConPTY management sequences from a buffer in-place.
///
/// ConPTY injects several VT sequences into its output pipe that are not part
/// of the inferior's output: a cursor-position query (\x1b[6n), Win32 Input
/// Mode toggles (\x1b[?9001h/l), focus-event toggles (\x1b[?1004h/l), and a
/// window-title OSC sequence (\x1b]0;...\x07).
///
/// \param[in,out] data Buffer containing raw ConPTY output.
/// \param[in,out] len On entry, the number of valid bytes in \p data.
/// Updated to the number of bytes after stripping.
/// \param[in] strip_init If true, also strip init-only sequences (\x1b[m,
/// \x1b[?25h) that ConPTY emits at startup.
void StripConPTYSequences(void *data, size_t &len, bool strip_init);
} // namespace lldb_private
#endif // LLDB_HOST_WINDOWS_CONPTYUTILS_H

View File

@@ -9,6 +9,7 @@
#ifndef LLDB_HOST_WINDOWS_CONNECTIONCONPTYWINDOWS_H
#define LLDB_HOST_WINDOWS_CONNECTIONCONPTYWINDOWS_H
#include "lldb/Host/windows/ConPTYUtils.h"
#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
#include "lldb/Host/windows/PseudoConsole.h"
#include "lldb/Host/windows/windows.h"

View File

@@ -17,6 +17,7 @@ macro(add_host_subdirectory group)
endmacro()
add_host_subdirectory(common
common/ConPTYUtils.cpp
common/DiagnosticsRendering.cpp
common/FileAction.cpp
common/FileCache.cpp

View File

@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/windows/ConPTYUtils.h"
#include <cstring>
using namespace lldb_private;
void lldb_private::StripConPTYSequences(void *data, size_t &len,
bool strip_init) {
auto *buf = static_cast<char *>(data);
char *out = buf;
const char *in = buf;
const char *end = buf + len;
while (in < end) {
if (*in != '\x1b') {
*out++ = *in++;
continue;
}
size_t remaining = end - in;
// \x1b[6n - cursor-position query (PSEUDOCONSOLE_INHERIT_CURSOR init)
if (remaining >= 4 && memcmp(in, "\x1b[6n", 4) == 0) {
in += 4;
continue;
}
if (strip_init) {
// \x1b[m - SGR reset (ConPTY init)
if (remaining >= 3 && memcmp(in, "\x1b[m", 3) == 0) {
in += 3;
continue;
}
// \x1b[?25h - show cursor (ConPTY init)
if (remaining >= 6 && memcmp(in, "\x1b[?25h", 6) == 0) {
in += 6;
continue;
}
}
// \x1b[?9001h / \x1b[?9001l - Win32 Input Mode enable/disable
if (remaining >= 8 && memcmp(in, "\x1b[?9001", 7) == 0 &&
(in[7] == 'h' || in[7] == 'l')) {
in += 8;
continue;
}
// \x1b[?1004h / \x1b[?1004l - focus-event reporting enable/disable
if (remaining >= 8 && memcmp(in, "\x1b[?1004", 7) == 0 &&
(in[7] == 'h' || in[7] == 'l')) {
in += 8;
continue;
}
// \x1b]0;...\x07 - ConPTY window-title OSC sequence
if (remaining >= 4 && in[1] == ']' && in[2] == '0' && in[3] == ';') {
const char *bel =
static_cast<const char *>(memchr(in + 4, '\x07', end - in - 4));
if (bel)
in = bel + 1;
else
in = end;
continue;
}
*out++ = *in++;
}
len = static_cast<size_t>(out - buf);
}

View File

@@ -15,87 +15,6 @@
using namespace lldb;
using namespace lldb_private;
/// Remove ConPTY management sequences from a buffer in-place.
///
/// ConPTY injects several VT sequences into its output pipe that are not part
/// of the inferior's output: a cursor-position query (\x1b[6n) emitted during
/// PSEUDOCONSOLE_INHERIT_CURSOR initialisation, Win32 Input Mode toggles
/// (\x1b[?9001h/l), focus-event toggles (\x1b[?1004h/l), and a window-title
/// OSC sequence (\x1b]0;...\x07). These sequences must not reach the outer
/// terminal.
///
/// \param[in,out] data Buffer containing raw ConPTY output.
/// \param[in,out] len On entry, the number of valid bytes in \p data.
/// Updated to the number of bytes after stripping.
/// \param[in] strip_init If true, also strip init-only sequences (\x1b[m,
/// \x1b[?25h) that ConPTY emits at startup.
static void StripConPTYSequences(void *data, size_t &len, bool strip_init) {
auto *buf = static_cast<char *>(data);
char *out = buf;
const char *in = buf;
const char *end = buf + len;
while (in < end) {
if (*in != '\x1b') {
*out++ = *in++;
continue;
}
size_t remaining = end - in;
// \x1b[6n - cursor-position query (PSEUDOCONSOLE_INHERIT_CURSOR init)
// This query is always replied to in OpenPseudoConsole.
if (remaining >= 4 && memcmp(in, "\x1b[6n", 4) == 0) {
in += 4;
continue;
}
if (strip_init) {
// \x1b[m - SGR reset (ConPTY init)
if (remaining >= 3 && memcmp(in, "\x1b[m", 3) == 0) {
in += 3;
continue;
}
// \x1b[?25h - show cursor (ConPTY init)
if (remaining >= 6 && memcmp(in, "\x1b[?25h", 6) == 0) {
in += 6;
continue;
}
}
// \x1b[?9001h / \x1b[?9001l - Win32 Input Mode enable/disable
if (remaining >= 8 && memcmp(in, "\x1b[?9001", 7) == 0 &&
(in[7] == 'h' || in[7] == 'l')) {
in += 8;
continue;
}
// \x1b[?1004h / \x1b[?1004l - focus-event reporting enable/disable
if (remaining >= 8 && memcmp(in, "\x1b[?1004", 7) == 0 &&
(in[7] == 'h' || in[7] == 'l')) {
in += 8;
continue;
}
// \x1b]0;...\x07 - ConPTY window-title OSC sequence
if (remaining >= 4 && in[1] == ']' && in[2] == '0' && in[3] == ';') {
const char *bel =
static_cast<const char *>(memchr(in + 4, '\x07', end - in - 4));
// We assume a sequence is not split accross multiple chunks.
if (bel)
in = bel + 1;
else
in = end;
continue;
}
*out++ = *in++;
}
len = static_cast<size_t>(out - buf);
}
ConnectionConPTY::ConnectionConPTY(std::shared_ptr<PseudoConsole> pty)
: ConnectionGenericFile(pty->GetSTDOUTHandle(), false), m_pty(pty) {}

View File

@@ -12,6 +12,7 @@ set (FILES
ProcessLaunchInfoTest.cpp
SocketAddressTest.cpp
SocketTest.cpp
StripConPTYSequencesTest.cpp
ThreadLauncherTest.cpp
XMLTest.cpp
)

View File

@@ -0,0 +1,132 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/windows/ConPTYUtils.h"
#include "gtest/gtest.h"
#include <cstring>
#include <string>
using namespace lldb_private;
namespace {
std::string Strip(const std::string &input, bool strip_init) {
std::string buf(input);
size_t len = buf.size();
StripConPTYSequences(buf.data(), len, strip_init);
return buf.substr(0, len);
}
} // namespace
TEST(StripConPTYSequencesTest, PassthroughPlainText) {
EXPECT_EQ("Hello World!\r\n", Strip("Hello World!\r\n", true));
}
TEST(StripConPTYSequencesTest, EmptyInput) { EXPECT_EQ("", Strip("", true)); }
TEST(StripConPTYSequencesTest, StripCursorQuery) {
EXPECT_EQ("", Strip("\x1b[6n", true));
EXPECT_EQ("abc", Strip("abc\x1b[6n", true));
EXPECT_EQ("abc", Strip("\x1b[6nabc", true));
}
TEST(StripConPTYSequencesTest, StripWin32InputMode) {
EXPECT_EQ("", Strip("\x1b[?9001h", true));
EXPECT_EQ("", Strip("\x1b[?9001l", true));
EXPECT_EQ("abc", Strip("\x1b[?9001habc", true));
EXPECT_EQ("abc", Strip("abc\x1b[?9001l", true));
}
TEST(StripConPTYSequencesTest, StripFocusEvents) {
EXPECT_EQ("", Strip("\x1b[?1004h", true));
EXPECT_EQ("", Strip("\x1b[?1004l", true));
EXPECT_EQ("text", Strip("\x1b[?1004htext\x1b[?1004l", true));
}
TEST(StripConPTYSequencesTest, StripWindowTitle) {
EXPECT_EQ("", Strip("\x1b]0;My Title\x07", true));
EXPECT_EQ("abc", Strip("abc\x1b]0;window\x07", true));
EXPECT_EQ("abc", Strip("\x1b]0;title\x07"
"abc",
true));
}
TEST(StripConPTYSequencesTest, WindowTitleWithoutBEL) {
// If BEL is missing, strip to end of buffer.
EXPECT_EQ("abc", Strip("abc\x1b]0;unterminated title", true));
}
TEST(StripConPTYSequencesTest, StripSGRResetOnlyWhenInit) {
EXPECT_EQ("", Strip("\x1b[m", true));
// With strip_init=false, \x1b[m is preserved.
EXPECT_EQ("\x1b[m", Strip("\x1b[m", false));
}
TEST(StripConPTYSequencesTest, StripShowCursorOnlyWhenInit) {
EXPECT_EQ("", Strip("\x1b[?25h", true));
// With strip_init=false, \x1b[?25h is preserved.
EXPECT_EQ("\x1b[?25h", Strip("\x1b[?25h", false));
}
TEST(StripConPTYSequencesTest, AlwaysStripNonInitSequences) {
// Cursor query, Win32 Input Mode, focus events, and window title are
// stripped regardless of strip_init.
EXPECT_EQ("", Strip("\x1b[6n", false));
EXPECT_EQ("", Strip("\x1b[?9001h", false));
EXPECT_EQ("", Strip("\x1b[?1004l", false));
EXPECT_EQ("", Strip("\x1b]0;title\x07", false));
}
TEST(StripConPTYSequencesTest, PreserveUnrecognizedEscape) {
// An ESC sequence we don't recognize should be passed through.
EXPECT_EQ("\x1b[31m", Strip("\x1b[31m", true));
EXPECT_EQ("\x1b[H", Strip("\x1b[H", true));
}
TEST(StripConPTYSequencesTest, TypicalConPTYInitBurst) {
// Simulates the first read from ConPTY with PSEUDOCONSOLE_INHERIT_CURSOR.
std::string init = "\x1b[6n" // cursor query
"\x1b[?9001h" // Win32 Input Mode on
"\x1b[?1004h" // focus events on
"\x1b[m" // SGR reset
"\x1b]0;C:\\app\x07" // window title
"\x1b[?25h"; // show cursor
EXPECT_EQ("", Strip(init, true));
}
TEST(StripConPTYSequencesTest, InitBurstWithInferiorOutput) {
std::string input = "\x1b[6n\x1b[?9001h\x1b[?1004h\x1b[m"
"\x1b]0;app\x07\x1b[?25h"
"Hello World!\r\n";
EXPECT_EQ("Hello World!\r\n", Strip(input, true));
}
TEST(StripConPTYSequencesTest, ShutdownSequences) {
// ConPTY emits these when the process exits.
std::string shutdown = "\x1b[?9001l\x1b[?1004l\r\n";
EXPECT_EQ("\r\n", Strip(shutdown, false));
}
TEST(StripConPTYSequencesTest, MultipleSequencesInterleaved) {
std::string input = "aaa\x1b[6nbbb\x1b[?9001hccc";
EXPECT_EQ("aaabbbccc", Strip(input, true));
}
TEST(StripConPTYSequencesTest, PartialEscapeAtEnd) {
// A lone ESC at the end of buffer - not a recognized sequence, pass through.
std::string input = "text\x1b";
EXPECT_EQ("text\x1b", Strip(input, true));
}
TEST(StripConPTYSequencesTest, LenUpdatedToZero) {
std::string buf = "\x1b[6n";
size_t len = buf.size();
StripConPTYSequences(buf.data(), len, true);
EXPECT_EQ(0u, len);
}