blob: 954bf9d4682bf65636f39e49d8a0a1da333d742c [file] [log] [blame]
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -06001/*
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 Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060015#include "qemu-common.h"
Markus Armbruster84a56f32018-08-23 18:40:06 +020016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/json-lexer.h"
Markus Armbruster62815d82018-08-23 18:40:01 +020018#include "qapi/qmp/json-parser.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060020
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_TOKEN_SIZE (64ULL << 20)
Markus Armbrusterdf649832015-11-25 22:23:33 +010022#define MAX_TOKEN_COUNT (2ULL << 20)
Markus Armbrusterda09cfb2018-08-23 18:40:15 +020023#define MAX_NESTING (1 << 10)
Anthony Liguori29c75dd2011-06-01 12:14:53 -050024
Eric Blakeba4dba52016-05-18 15:46:52 -060025static void json_message_free_token(void *token, void *opaque)
26{
27 g_free(token);
28}
29
Paolo Bonzini95385fe2015-11-25 22:23:31 +010030static void json_message_free_tokens(JSONMessageParser *parser)
31{
32 if (parser->tokens) {
Eric Blakeba4dba52016-05-18 15:46:52 -060033 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010034 g_queue_free(parser->tokens);
35 parser->tokens = NULL;
36 }
37}
38
Markus Armbruster037f2442018-08-23 18:40:00 +020039void json_message_process_token(JSONLexer *lexer, GString *input,
40 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060041{
42 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Markus Armbrusterff281a22018-08-23 18:40:02 +020043 QObject *json = NULL;
Markus Armbruster62815d82018-08-23 18:40:01 +020044 Error *err = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +010045 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060046
Markus Armbrusterc5461662015-11-25 22:23:26 +010047 switch (type) {
48 case JSON_LCURLY:
49 parser->brace_count++;
50 break;
51 case JSON_RCURLY:
52 parser->brace_count--;
53 break;
54 case JSON_LSQUARE:
55 parser->bracket_count++;
56 break;
57 case JSON_RSQUARE:
58 parser->bracket_count--;
59 break;
Markus Armbruster269e57a2018-08-23 18:40:03 +020060 case JSON_ERROR:
Markus Armbruster84a56f32018-08-23 18:40:06 +020061 error_setg(&err, "JSON parse error, stray '%s'", input->str);
Markus Armbruster269e57a2018-08-23 18:40:03 +020062 goto out_emit;
Markus Armbrusterf9277912018-08-23 18:40:12 +020063 case JSON_END_OF_INPUT:
64 if (g_queue_is_empty(parser->tokens)) {
65 return;
66 }
67 json = json_parser_parse(parser->tokens, parser->ap, &err);
68 parser->tokens = NULL;
69 goto out_emit;
Markus Armbrusterc5461662015-11-25 22:23:26 +010070 default:
71 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060072 }
73
Markus Armbrusterda09cfb2018-08-23 18:40:15 +020074 /*
75 * Security consideration, we limit total memory allocated per object
76 * and the maximum recursion depth that a message can force.
77 */
78 if (parser->token_size + input->len + 1 > MAX_TOKEN_SIZE) {
79 error_setg(&err, "JSON token size limit exceeded");
80 goto out_emit;
81 }
82 if (g_queue_get_length(parser->tokens) + 1 > MAX_TOKEN_COUNT) {
83 error_setg(&err, "JSON token count limit exceeded");
84 goto out_emit;
85 }
86 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
87 error_setg(&err, "JSON nesting depth limit exceeded");
88 goto out_emit;
89 }
90
Paolo Bonzini9bada892015-11-25 22:23:32 +010091 token = g_malloc(sizeof(JSONToken) + input->len + 1);
92 token->type = type;
93 memcpy(token->str, input->str, input->len);
94 token->str[input->len] = 0;
95 token->x = x;
96 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060097
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010098 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050099
Paolo Bonzini9bada892015-11-25 22:23:32 +0100100 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600101
Markus Armbruster8d3265b2018-08-23 18:40:16 +0200102 if ((parser->brace_count > 0 || parser->bracket_count > 0)
103 && parser->bracket_count >= 0 && parser->bracket_count >= 0) {
104 return;
Markus Armbrusterff281a22018-08-23 18:40:02 +0200105 }
106
Markus Armbruster8d3265b2018-08-23 18:40:16 +0200107 json = json_parser_parse(parser->tokens, parser->ap, &err);
108 parser->tokens = NULL;
Michael Roth5e2dafe2011-06-01 12:14:59 -0500109
Michael Roth5e2dafe2011-06-01 12:14:59 -0500110out_emit:
Michael Roth5e2dafe2011-06-01 12:14:59 -0500111 parser->brace_count = 0;
112 parser->bracket_count = 0;
Markus Armbrusterff281a22018-08-23 18:40:02 +0200113 json_message_free_tokens(parser);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100114 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -0500115 parser->token_size = 0;
Markus Armbruster62815d82018-08-23 18:40:01 +0200116 parser->emit(parser->opaque, json, err);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600117}
118
119void json_message_parser_init(JSONMessageParser *parser,
Markus Armbruster62815d82018-08-23 18:40:01 +0200120 void (*emit)(void *opaque, QObject *json,
121 Error *err),
122 void *opaque, va_list *ap)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600123{
Markus Armbruster62815d82018-08-23 18:40:01 +0200124 parser->emit = emit;
125 parser->opaque = opaque;
126 parser->ap = ap;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600127 parser->brace_count = 0;
128 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100129 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500130 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600131
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200132 json_lexer_init(&parser->lexer, !!ap);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600133}
134
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200135void json_message_parser_feed(JSONMessageParser *parser,
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600136 const char *buffer, size_t size)
137{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200138 json_lexer_feed(&parser->lexer, buffer, size);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600139}
140
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200141void json_message_parser_flush(JSONMessageParser *parser)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600142{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200143 json_lexer_flush(&parser->lexer);
Markus Armbrusterf9277912018-08-23 18:40:12 +0200144 assert(g_queue_is_empty(parser->tokens));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600145}
146
147void json_message_parser_destroy(JSONMessageParser *parser)
148{
149 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100150 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600151}