Repair ESP32 3.3.11 BLE service cleanup in build-local SDK sources
This commit is contained in:
1 parent
a4afc9c54f
commit
669107c3db
7 files changed
+356
-15
No files matched your search
+17
-2
@@ -127,11 +127,22 @@ public:
|
||||
class BLEService {
|
||||
public:
|
||||
BLEService() { ++fake_bt::services; }
|
||||
#ifdef TEST_BLE_OWNED_SERVER
|
||||
private:
|
||||
friend class BLEDevice;
|
||||
~BLEService() { --fake_bt::services; }
|
||||
public:
|
||||
#else
|
||||
~BLEService() { --fake_bt::services; }
|
||||
#endif
|
||||
std::vector<BLECharacteristic*> children;
|
||||
unsigned made=0;
|
||||
BLECharacteristic* createCharacteristic(const char*,uint32_t) {
|
||||
const char* phase = made++ ? "rx" : "tx";
|
||||
return fake_bt::failure==phase ? nullptr : new BLECharacteristic;
|
||||
if (fake_bt::failure==phase) return nullptr;
|
||||
auto* child = new BLECharacteristic;
|
||||
children.push_back(child);
|
||||
return child;
|
||||
}
|
||||
void start() {}
|
||||
};
|
||||
@@ -145,7 +156,10 @@ public:
|
||||
assert(fake_bt::host!=ESP_BLUEDROID_STATUS_ENABLED || fake_bt::server!=this);
|
||||
--fake_bt::servers;
|
||||
}
|
||||
BLEService* createService(const char*) { return fake_bt::failure=="service" ? nullptr : new BLEService; }
|
||||
BLEService* service = nullptr;
|
||||
BLEService* createService(const char*) {
|
||||
return service = fake_bt::failure=="service" ? nullptr : new BLEService;
|
||||
}
|
||||
BLEAdvertising* getAdvertising() { if(!fake_advertising) fake_advertising=new BLEAdvertising; return fake_advertising; }
|
||||
void setCallbacks(BLEServerCallbacks* cb) { callbacks=cb; }
|
||||
unsigned getConnectedCount() const { return connected; }
|
||||
@@ -175,6 +189,7 @@ public:
|
||||
if(!fake_bt::initialized) return;
|
||||
#ifdef TEST_BLE_OWNED_SERVER
|
||||
assert(fake_bt::host!=ESP_BLUEDROID_STATUS_ENABLED);
|
||||
if (fake_bt::server) delete fake_bt::server->service;
|
||||
delete fake_bt::server; fake_bt::server=nullptr;
|
||||
delete fake_advertising; fake_advertising=nullptr;
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify pinned SDK ownership, private destruction, and build-local patching."""
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SPEC = importlib.util.spec_from_file_location("esp32_ble_lifecycle_fix", ROOT / "scripts/esp32_ble_lifecycle_fix.py")
|
||||
FIX = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(FIX)
|
||||
|
||||
HARNESS = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
static bool host_running = false;
|
||||
static unsigned objects = 0;
|
||||
struct Lifetime {
|
||||
Lifetime() { ++objects; }
|
||||
~Lifetime() { assert(!host_running); --objects; }
|
||||
};
|
||||
struct DescriptorDefinition { int permissions = 0; };
|
||||
struct CharacteristicDefinition { const void* uuid = nullptr; DescriptorDefinition* descriptors = nullptr; };
|
||||
struct ServiceDefinition { CharacteristicDefinition* characteristics = nullptr; };
|
||||
class BLEDevice;
|
||||
class BLEService : Lifetime {
|
||||
friend class BLEDevice;
|
||||
ServiceDefinition* m_pSvcDef = nullptr;
|
||||
~BLEService(); // Matches the actual SDK's private destructor.
|
||||
public:
|
||||
BLEService() {
|
||||
#if defined(CONFIG_NIMBLE_ENABLED)
|
||||
m_pSvcDef = new ServiceDefinition[2]{};
|
||||
auto* chars = new CharacteristicDefinition[3]{};
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
chars[i].uuid = this;
|
||||
chars[i].descriptors = new DescriptorDefinition[2]{};
|
||||
}
|
||||
m_pSvcDef[0].characteristics = chars;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
@SERVICE_DESTRUCTOR@
|
||||
struct ServiceMap {
|
||||
std::vector<BLEService*> pointers;
|
||||
unsigned next = 0;
|
||||
BLEService* getFirst() { next = 0; return getNext(); }
|
||||
BLEService* getNext() { return next < pointers.size() ? pointers[next++] : nullptr; }
|
||||
};
|
||||
class BLEServer : Lifetime {
|
||||
friend class BLEDevice;
|
||||
ServiceMap m_serviceMap; // Raw-pointer map, as in the SDK.
|
||||
public:
|
||||
void createService() { m_serviceMap.pointers.push_back(new BLEService); }
|
||||
};
|
||||
class BLEDevice {
|
||||
public:
|
||||
inline static BLEServer* m_pServer = nullptr;
|
||||
static void deinit() {
|
||||
assert(!host_running);
|
||||
@SERVER_DELETE@
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
BLEDevice::deinit(); // Init failed before any server existed.
|
||||
for (unsigned cycle = 0; cycle < 100; ++cycle) {
|
||||
BLEDevice::m_pServer = new BLEServer;
|
||||
if (cycle % 3 != 0) {
|
||||
BLEDevice::m_pServer->createService();
|
||||
BLEDevice::m_pServer->createService();
|
||||
}
|
||||
// The SDK service destructor owns its private definition arrays, but
|
||||
// NUS TX/RX/2902 objects remain app-owned and must not be deleted twice.
|
||||
auto* tx = new Lifetime;
|
||||
auto* rx = new Lifetime;
|
||||
auto* cccd = new Lifetime;
|
||||
host_running = true;
|
||||
host_running = false;
|
||||
BLEDevice::deinit();
|
||||
assert(BLEDevice::m_pServer == nullptr && objects == 3);
|
||||
delete tx; delete rx; delete cccd;
|
||||
assert(objects == 0);
|
||||
}
|
||||
puts("private service ownership cycles passed");
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class FakeNode:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def srcnode(self):
|
||||
return self
|
||||
|
||||
def get_abspath(self):
|
||||
return str(self.path)
|
||||
|
||||
|
||||
class FakeEnvironment:
|
||||
def __init__(self, build):
|
||||
self.build = build
|
||||
self.paths = []
|
||||
|
||||
def subst(self, expression):
|
||||
assert expression == "$BUILD_DIR"
|
||||
return str(self.build)
|
||||
|
||||
def AppendUnique(self, **kwargs):
|
||||
self.paths.extend(kwargs["CPPPATH"])
|
||||
|
||||
def File(self, path):
|
||||
return Path(path)
|
||||
|
||||
|
||||
class BleLifecycleFixTests(unittest.TestCase):
|
||||
def test_private_service_destruction_and_native_arrays_under_sanitizers(self):
|
||||
for native in (False, True):
|
||||
with self.subTest(native=native), tempfile.TemporaryDirectory(prefix="ble-private-service-") as directory:
|
||||
root = Path(directory)
|
||||
cpp, exe = root / "test.cpp", root / "test"
|
||||
text = HARNESS.replace("@SERVER_DELETE@", FIX.REPAIRED).replace(
|
||||
"@SERVICE_DESTRUCTOR@", FIX.SERVICE_REPAIRED)
|
||||
cpp.write_text(text)
|
||||
command = [os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", *( ["-DCONFIG_NIMBLE_ENABLED=1"] if native else []),
|
||||
str(cpp), "-o", str(exe)]
|
||||
built = subprocess.run(command, text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
run = subprocess.run([str(exe)], text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
if native:
|
||||
# The original SDK destructor used scalar delete for
|
||||
# arrays. Ensure ASan catches that precise mismatch.
|
||||
cpp.write_text(text.replace(FIX.SERVICE_REPAIRED, FIX.SERVICE_ORIGINAL))
|
||||
built = subprocess.run(command, text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
run = subprocess.run([str(exe)], text=True, capture_output=True, timeout=10)
|
||||
self.assertNotEqual(run.returncode, 0)
|
||||
self.assertIn("alloc-dealloc-mismatch", run.stderr)
|
||||
|
||||
def test_hash_guards_idempotence_and_old_sdk_unchanged(self):
|
||||
for name, (_, _, original, repaired) in FIX.SOURCE_PINS.items():
|
||||
with self.subTest(name=name):
|
||||
source = "// reviewed source fixture\n" + original
|
||||
expected = source.replace(original, repaired)
|
||||
pins = {name: (hashlib.sha256(source.encode()).hexdigest(),
|
||||
hashlib.sha256(expected.encode()).hexdigest(), original, repaired)}
|
||||
with patch.object(FIX, "SOURCE_PINS", pins):
|
||||
self.assertEqual(FIX.patched_ble_source(source, (3, 3, 11), name), expected)
|
||||
self.assertEqual(FIX.patched_ble_source(expected, (3, 3, 11), name), expected)
|
||||
self.assertEqual(FIX.patched_ble_source(source.replace("\n", "\r\n"), (3, 3, 11), name), expected)
|
||||
for version in ((2, 0, 17), (3, 1, 3)):
|
||||
self.assertEqual(FIX.patched_ble_source(source, version, name), source)
|
||||
with self.assertRaisesRegex(RuntimeError, "changed pinned source"):
|
||||
FIX.patched_ble_source(source + "// changed\n", (3, 3, 11), name)
|
||||
with self.assertRaisesRegex(RuntimeError, "unreviewed framework"):
|
||||
FIX.patched_ble_source(source, (3, 3, 12), name)
|
||||
with self.assertRaisesRegex(RuntimeError, "unreviewed source"):
|
||||
FIX.patched_ble_source(source, (3, 3, 11), "BLEOther.cpp")
|
||||
|
||||
def test_middleware_copies_only_pinned_sources_without_touching_shared_sdk(self):
|
||||
with tempfile.TemporaryDirectory(prefix="ble-sdk-middleware-") as directory:
|
||||
root = Path(directory)
|
||||
sdk = root / "framework"
|
||||
source = sdk / "libraries/BLE/src/BLEDevice.cpp"
|
||||
version = sdk / "cores/esp32/esp_arduino_version.h"
|
||||
source.parent.mkdir(parents=True)
|
||||
version.parent.mkdir(parents=True)
|
||||
version.write_text("#define ESP_ARDUINO_VERSION_MAJOR 3\n#define ESP_ARDUINO_VERSION_MINOR 3\n#define ESP_ARDUINO_VERSION_PATCH 11\n")
|
||||
original = "// fixture\n" + FIX.ORIGINAL
|
||||
expected = original.replace(FIX.ORIGINAL, FIX.REPAIRED)
|
||||
source.write_text(original)
|
||||
pins = {"BLEDevice.cpp": (hashlib.sha256(original.encode()).hexdigest(),
|
||||
hashlib.sha256(expected.encode()).hexdigest(), FIX.ORIGINAL, FIX.REPAIRED)}
|
||||
node, env = FakeNode(source), FakeEnvironment(root / "build")
|
||||
with patch.object(FIX, "SOURCE_PINS", pins):
|
||||
destination = FIX.replace_ble_source(env, node)
|
||||
self.assertEqual(source.read_text(), original)
|
||||
self.assertEqual(destination.read_text(), expected)
|
||||
self.assertEqual(env.paths, [str(source.parent)])
|
||||
timestamp = destination.stat().st_mtime_ns
|
||||
self.assertEqual(FIX.replace_ble_source(env, node), destination)
|
||||
self.assertEqual(destination.stat().st_mtime_ns, timestamp)
|
||||
version.write_text(version.read_text().replace("PATCH 11", "PATCH 12"))
|
||||
with self.assertRaisesRegex(RuntimeError, "unreviewed framework"):
|
||||
FIX.replace_ble_source(env, node)
|
||||
|
||||
def test_installed_exact_sdk_pins_when_available(self):
|
||||
packages = Path.home() / ".platformio/packages"
|
||||
candidates = list(packages.glob("framework-arduinoespressif32*"))
|
||||
sdk = None
|
||||
for candidate in candidates:
|
||||
source = candidate / "libraries/BLE/src/BLEDevice.cpp"
|
||||
if source.exists() and FIX.framework_version(source) == (3, 3, 11):
|
||||
sdk = source.parent
|
||||
break
|
||||
if sdk is None:
|
||||
self.skipTest("Pinned Arduino3.3.11 SDK is not installed")
|
||||
for name in FIX.SOURCE_PINS:
|
||||
source = (sdk / name).read_text()
|
||||
patched = FIX.patched_ble_source(source, (3, 3, 11), name)
|
||||
self.assertNotEqual(source, patched)
|
||||
self.assertEqual(FIX.patched_ble_source(patched, (3, 3, 11), name), patched)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -29,6 +29,7 @@ HARNESS = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
using esp_err_t = int;
|
||||
constexpr int ESP_OK = 0;
|
||||
enum esp_bt_controller_status_t { ESP_BT_CONTROLLER_STATUS_IDLE,
|
||||
@@ -71,6 +72,7 @@ using BLEService = GattObject;
|
||||
using BLECharacteristic = GattObject;
|
||||
using BLE2902 = GattObject;
|
||||
static BLEServer* library_server = nullptr;
|
||||
static std::vector<GattObject*> library_children;
|
||||
struct BLEDevice {
|
||||
static bool getInitialized() { return initialized; }
|
||||
static BLEServer* getServer() { return library_server; }
|
||||
@@ -80,6 +82,8 @@ struct BLEDevice {
|
||||
++wrapper_deinits;
|
||||
// The current native wrapper deletes the server before stopping its port.
|
||||
// The adapter must have stopped callback dispatch before entering here.
|
||||
for (auto* child : library_children) delete child;
|
||||
library_children.clear();
|
||||
delete library_server; library_server = nullptr;
|
||||
(void)nimble_port_stop();
|
||||
initialized = host_task_created = false;
|
||||
@@ -110,6 +114,7 @@ public:
|
||||
pTxCharacteristic = new BLECharacteristic;
|
||||
pRxCharacteristic = new BLECharacteristic;
|
||||
pTxDescriptor = new BLE2902;
|
||||
library_children = {pService};
|
||||
}
|
||||
};
|
||||
@STACK_STOP@
|
||||
@@ -166,7 +171,7 @@ int main() {
|
||||
class NativeNimbleStopTests(unittest.TestCase):
|
||||
def test_production_stop_handles_absent_and_started_host_tasks(self):
|
||||
source = (ROOT / "src/helpers/esp32/SerialBLEInterface.cpp").read_text()
|
||||
ownership_start = source.index("template <typename Device>")
|
||||
ownership_start = source.index("template <typename Device,")
|
||||
ownership = source[ownership_start:source.index("\n#endif", ownership_start)]
|
||||
harness = HARNESS.replace("@OWNERSHIP@", ownership).replace(
|
||||
"@CONTROLLER_STOP@", function(source, "static bool stopBluetoothController()"))
|
||||
|
||||
Reference in new issue
Block a user