fuzzing-main.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "AppMain.h"
  18. #include <app/server/Server.h>
  19. #include <CommissionableInit.h>
  20. using namespace chip;
  21. using namespace chip::DeviceLayer;
  22. namespace {
  23. LinuxCommissionableDataProvider gCommissionableDataProvider;
  24. }
  25. void CleanShutdown()
  26. {
  27. Server::GetInstance().Shutdown();
  28. PlatformMgr().Shutdown();
  29. // TODO: We don't Platform::MemoryShutdown because ~CASESessionManager calls
  30. // Dnssd::ResolverProxy::Shutdown, which starts doing Platform::Delete.
  31. // Platform::MemoryShutdown();
  32. }
  33. extern "C" int LLVMFuzzerTestOneInput(const uint8_t * aData, size_t aSize)
  34. {
  35. static bool matterStackInitialized = false;
  36. if (!matterStackInitialized)
  37. {
  38. // Might be simpler to do ChipLinuxAppInit() with argc == 0, argv set to
  39. // just a fake executable name?
  40. VerifyOrDie(Platform::MemoryInit() == CHIP_NO_ERROR);
  41. VerifyOrDie(PlatformMgr().InitChipStack() == CHIP_NO_ERROR);
  42. VerifyOrDie(chip::examples::InitCommissionableDataProvider(gCommissionableDataProvider,
  43. LinuxDeviceOptions::GetInstance()) == CHIP_NO_ERROR);
  44. SetCommissionableDataProvider(&gCommissionableDataProvider);
  45. // ChipLinuxAppMainLoop blocks, and we don't want that here.
  46. static chip::CommonCaseDeviceServerInitParams initParams;
  47. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  48. VerifyOrDie(Server::GetInstance().Init(initParams) == CHIP_NO_ERROR);
  49. ApplicationInit();
  50. // We don't start the event loop task, because we don't plan to deliver
  51. // data on a separate thread.
  52. matterStackInitialized = true;
  53. // The fuzzer does not have a way to tell us when it's done, so just
  54. // shut down things on exit.
  55. atexit(CleanShutdown);
  56. }
  57. // For now, just dump the data as a UDP payload into the session manager.
  58. // But maybe we should try to separately extract a PeerAddress and data from
  59. // the incoming data?
  60. Transport::PeerAddress peerAddr;
  61. System::PacketBufferHandle buf =
  62. System::PacketBufferHandle::NewWithData(aData, aSize, /* aAdditionalSize = */ 0, /* aReservedSize = */ 0);
  63. if (buf.IsNull())
  64. {
  65. // Too big; we couldn't represent this as a packetbuffer to start with.
  66. return 0;
  67. }
  68. // Ignoring the return value from OnMessageReceived, because we might be
  69. // passing it all sorts of garbage that will cause it to fail.
  70. Server::GetInstance().GetSecureSessionManager().OnMessageReceived(peerAddr, std::move(buf));
  71. // Now process pending events until our sentinel is reached.
  72. PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); });
  73. PlatformMgr().RunEventLoop();
  74. return 0;
  75. }