Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 1 | /* |
| 2 | * JSON streaming support |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2009 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 15 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/json-lexer.h" |
| 17 | #include "qapi/qmp/json-streamer.h" |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 18 | |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 19 | #define MAX_TOKEN_SIZE (64ULL << 20) |
Markus Armbruster | df64983 | 2015-11-25 22:23:33 +0100 | [diff] [blame] | 20 | #define MAX_TOKEN_COUNT (2ULL << 20) |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 21 | #define MAX_NESTING (1ULL << 10) |
| 22 | |
Eric Blake | ba4dba5 | 2016-05-18 15:46:52 -0600 | [diff] [blame] | 23 | static void json_message_free_token(void *token, void *opaque) |
| 24 | { |
| 25 | g_free(token); |
| 26 | } |
| 27 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 28 | static void json_message_free_tokens(JSONMessageParser *parser) |
| 29 | { |
| 30 | if (parser->tokens) { |
Eric Blake | ba4dba5 | 2016-05-18 15:46:52 -0600 | [diff] [blame] | 31 | g_queue_foreach(parser->tokens, json_message_free_token, NULL); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 32 | g_queue_free(parser->tokens); |
| 33 | parser->tokens = NULL; |
| 34 | } |
| 35 | } |
| 36 | |
Paolo Bonzini | d2ca7c0 | 2015-11-25 22:23:29 +0100 | [diff] [blame] | 37 | static void json_message_process_token(JSONLexer *lexer, GString *input, |
| 38 | JSONTokenType type, int x, int y) |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 39 | { |
| 40 | JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 41 | JSONToken *token; |
Paolo Bonzini | a942d8f | 2016-07-04 14:40:59 +0200 | [diff] [blame] | 42 | GQueue *tokens; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 43 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 44 | switch (type) { |
| 45 | case JSON_LCURLY: |
| 46 | parser->brace_count++; |
| 47 | break; |
| 48 | case JSON_RCURLY: |
| 49 | parser->brace_count--; |
| 50 | break; |
| 51 | case JSON_LSQUARE: |
| 52 | parser->bracket_count++; |
| 53 | break; |
| 54 | case JSON_RSQUARE: |
| 55 | parser->bracket_count--; |
| 56 | break; |
| 57 | default: |
| 58 | break; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 59 | } |
| 60 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 61 | token = g_malloc(sizeof(JSONToken) + input->len + 1); |
| 62 | token->type = type; |
| 63 | memcpy(token->str, input->str, input->len); |
| 64 | token->str[input->len] = 0; |
| 65 | token->x = x; |
| 66 | token->y = y; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 67 | |
Paolo Bonzini | d2ca7c0 | 2015-11-25 22:23:29 +0100 | [diff] [blame] | 68 | parser->token_size += input->len; |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 69 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 70 | g_queue_push_tail(parser->tokens, token); |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 71 | |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 72 | if (type == JSON_ERROR) { |
| 73 | goto out_emit_bad; |
| 74 | } else if (parser->brace_count < 0 || |
Anthony Liguori | 55f8301 | 2011-06-01 12:14:51 -0500 | [diff] [blame] | 75 | parser->bracket_count < 0 || |
| 76 | (parser->brace_count == 0 && |
| 77 | parser->bracket_count == 0)) { |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 78 | goto out_emit; |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 79 | } else if (parser->token_size > MAX_TOKEN_SIZE || |
Markus Armbruster | df64983 | 2015-11-25 22:23:33 +0100 | [diff] [blame] | 80 | g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT || |
Markus Armbruster | 4f2d31f | 2015-11-25 22:23:22 +0100 | [diff] [blame] | 81 | parser->bracket_count + parser->brace_count > MAX_NESTING) { |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 82 | /* Security consideration, we limit total memory allocated per object |
| 83 | * and the maximum recursion depth that a message can force. |
| 84 | */ |
Markus Armbruster | 0753113 | 2015-11-25 22:23:23 +0100 | [diff] [blame] | 85 | goto out_emit_bad; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 86 | } |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 87 | |
| 88 | return; |
| 89 | |
| 90 | out_emit_bad: |
Markus Armbruster | 0753113 | 2015-11-25 22:23:23 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Clear out token list and tell the parser to emit an error |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 93 | * indication by passing it a NULL list |
| 94 | */ |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 95 | json_message_free_tokens(parser); |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 96 | out_emit: |
| 97 | /* send current list of tokens to parser and reset tokenizer */ |
| 98 | parser->brace_count = 0; |
| 99 | parser->bracket_count = 0; |
Paolo Bonzini | a942d8f | 2016-07-04 14:40:59 +0200 | [diff] [blame] | 100 | /* parser->emit takes ownership of parser->tokens. Remove our own |
| 101 | * reference to parser->tokens before handing it out to parser->emit. |
| 102 | */ |
| 103 | tokens = parser->tokens; |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 104 | parser->tokens = g_queue_new(); |
Paolo Bonzini | a942d8f | 2016-07-04 14:40:59 +0200 | [diff] [blame] | 105 | parser->emit(parser, tokens); |
Michael Roth | 5e2dafe | 2011-06-01 12:14:59 -0500 | [diff] [blame] | 106 | parser->token_size = 0; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void json_message_parser_init(JSONMessageParser *parser, |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 110 | void (*func)(JSONMessageParser *, GQueue *)) |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 111 | { |
| 112 | parser->emit = func; |
| 113 | parser->brace_count = 0; |
| 114 | parser->bracket_count = 0; |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 115 | parser->tokens = g_queue_new(); |
Anthony Liguori | 29c75dd | 2011-06-01 12:14:53 -0500 | [diff] [blame] | 116 | parser->token_size = 0; |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 117 | |
| 118 | json_lexer_init(&parser->lexer, json_message_process_token); |
| 119 | } |
| 120 | |
| 121 | int json_message_parser_feed(JSONMessageParser *parser, |
| 122 | const char *buffer, size_t size) |
| 123 | { |
| 124 | return json_lexer_feed(&parser->lexer, buffer, size); |
| 125 | } |
| 126 | |
| 127 | int json_message_parser_flush(JSONMessageParser *parser) |
| 128 | { |
| 129 | return json_lexer_flush(&parser->lexer); |
| 130 | } |
| 131 | |
| 132 | void json_message_parser_destroy(JSONMessageParser *parser) |
| 133 | { |
| 134 | json_lexer_destroy(&parser->lexer); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 135 | json_message_free_tokens(parser); |
Anthony Liguori | d7ff3ac | 2009-11-11 10:38:59 -0600 | [diff] [blame] | 136 | } |