fuzz_binary_message.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "custom_memory_manager.h"
  5. #include <open62541/plugin/log_stdout.h>
  6. #include <open62541/server_config_default.h>
  7. #include <open62541/types.h>
  8. #include "ua_server_internal.h"
  9. #include "testing_networklayers.h"
  10. #define RECEIVE_BUFFER_SIZE 65535
  11. /*
  12. ** Main entry point. The fuzzer invokes this function with each
  13. ** fuzzed input.
  14. */
  15. extern "C" int
  16. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  17. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  18. return 0;
  19. size -= 4;
  20. UA_Connection c = createDummyConnection(RECEIVE_BUFFER_SIZE, NULL);
  21. UA_Server *server = UA_Server_new();
  22. if(!server) {
  23. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  24. "Could not create server instance using UA_Server_new");
  25. return 0;
  26. }
  27. UA_StatusCode retval = UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  28. if(retval != UA_STATUSCODE_GOOD) {
  29. UA_Server_delete(server);
  30. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  31. "Could not set the server config");
  32. return 0;
  33. }
  34. // we need to copy the message because it will be freed in the processing function
  35. UA_ByteString msg = UA_ByteString();
  36. retval = UA_ByteString_allocBuffer(&msg, size);
  37. if(retval != UA_STATUSCODE_GOOD) {
  38. UA_Server_delete(server);
  39. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  40. "Could not allocate message buffer");
  41. return 0;
  42. }
  43. memcpy(msg.data, data, size);
  44. UA_Server_processBinaryMessage(server, &c, &msg);
  45. // if we got an invalid chunk, the message is not deleted, so delete it here
  46. UA_ByteString_deleteMembers(&msg);
  47. UA_Server_run_shutdown(server);
  48. UA_Server_delete(server);
  49. c.close(&c);
  50. UA_Connection_deleteMembers(&c);
  51. return 0;
  52. }