fuzz_binary_message.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. *
  5. * Copyright 2019 (c) fortiss (Author: Stefan Profanter)
  6. */
  7. #include "custom_memory_manager.h"
  8. #include <open62541/plugin/log_stdout.h>
  9. #include <open62541/server_config_default.h>
  10. #include <open62541/types.h>
  11. #include "ua_server_internal.h"
  12. #include "testing_networklayers.h"
  13. #define RECEIVE_BUFFER_SIZE 65535
  14. /*
  15. ** Main entry point. The fuzzer invokes this function with each
  16. ** fuzzed input.
  17. */
  18. extern "C" int
  19. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  20. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  21. return 0;
  22. size -= 4;
  23. UA_Connection c = createDummyConnection(RECEIVE_BUFFER_SIZE, NULL);
  24. UA_Server *server = UA_Server_new();
  25. if(!server) {
  26. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  27. "Could not create server instance using UA_Server_new");
  28. return 0;
  29. }
  30. UA_StatusCode retval = UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  31. if(retval != UA_STATUSCODE_GOOD) {
  32. UA_Server_delete(server);
  33. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  34. "Could not set the server config");
  35. return 0;
  36. }
  37. // we need to copy the message because it will be freed in the processing function
  38. UA_ByteString msg = UA_ByteString();
  39. retval = UA_ByteString_allocBuffer(&msg, size);
  40. if(retval != UA_STATUSCODE_GOOD) {
  41. UA_Server_delete(server);
  42. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  43. "Could not allocate message buffer");
  44. return 0;
  45. }
  46. memcpy(msg.data, data, size);
  47. UA_Server_processBinaryMessage(server, &c, &msg);
  48. // if we got an invalid chunk, the message is not deleted, so delete it here
  49. UA_ByteString_deleteMembers(&msg);
  50. UA_Server_run_shutdown(server);
  51. UA_Server_delete(server);
  52. c.close(&c);
  53. UA_Connection_clear(&c);
  54. return 0;
  55. }