I have a clang-tooling application that traverses an AST and prints it out with mangled names. It works fine in clang10, but after the (simple) port to clang11 it is segfaulting and I can't pinpoint the issue. From the backtrace, it seems that I've somehow managed to get an AST node that isn't castable.
#0 getKind () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/DeclBase.h:433
#1 classof () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/Decl.h:4242
#2 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:58
#3 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:105
#4 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:131
#5 doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:121
#6 isa<clang::BlockDecl, clang::Decl const*> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:142
#7 dyn_cast<clang::BlockDecl, clang::Decl const> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:345
#8 getEffectiveDeclContext () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:59
#9 0x00007fb7ea08865a in mangleNameWithAbiTags () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:867
#10 0x00007fb7ea087b1b in mangleName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:855
#11 0x00007fb7ea084374 in mangleCXXName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:5144
...my application code...
The code that I'm running is
mangleContext_->mangleName(decl, llvm::errs());
where decl is a const NamedDecl* that corresponds to the following union:
union U { int x; int y; };
and I'm effectively trying to print the mangling of U (I'm expecting _Z1U).
I am constructing the mangle context using the following code.
ClangPrinter::ClangPrinter(clang::CompilerInstance *compiler,
clang::ASTContext *context)
: compiler_(compiler), context_(context) {
mangleContext_ = ItaniumMangleContext::create(*context, compiler->getDiagnostics());
}
Does anyone have any suggestions as to where to look to debug this issue? My guess is that I'm somehow violating an invariant of the underlying system, but since I'm only consuming the AST in a read-only manner, I don't understand how I could end up with an uncastable object.
Related
I have downloaded a Wikipedia dump and I am trying to read it line by line. But when doing the utf8-decode I get the following error
12633: FormatException: Unfinished UTF-8 octet sequence (at offset 65536)
Stacktrace :#0 _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1789:7)
#1 Utf8Decoder.convert (dart:convert/utf.dart:351:42)
#2 Utf8Codec.decode (dart:convert/utf.dart:63:20)
#3 _MapStream._handleData (dart:async/stream_pipe.dart:213:31)
#4 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1618:10)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#8 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
#9 _StreamController._add (dart:async/stream_controller.dart:648:7)
#10 _StreamController.add (dart:async/stream_controller.dart:596:5)
#11 _FileStream._readBlock.<anonymous closure> (dart:io/file_impl.dart:98:19)
<asynchronous suspension>
That is this line
ar جزر_غالاباغوس 1 0
So I tried saving the file utf-8 encoded with this button
But that does not seem to work
This is my code
final filePath = p.join(
Directory.current.path,
'bin\\migrate_most_views\\data\\pageviews-20220416-170000',
);
final file = File(filePath);
logger.stderr('exporting pageviews...');
StreamSubscription? reader;
int lineNumer = 0;
reader = file.openRead().map(utf8.decode).transform(LineSplitter()).listen(
(line) {
final page = MostViewedPageDaily.fromLine(line);
db.collection('page_views').insert(page.toMap());
lineNumer++;
if (lineNumer % 1000 == 0) {
logger.stdout('inserting at line $lineNumer');
}
},
onDone: () {
logger.stdout('Reader read $lineNumer lines');
reader?.cancel();
exit(0);
},
onError: (error, stackTrace) {
final message = '$lineNumer: $error\n\nStacktrace :$stackTrace';
logger.stdout(logger.ansi.error(message));
exit(1);
},
cancelOnError: true,
);
What can I do?
I downloaded the file from here
https://dumps.wikimedia.org/other/pageviews/2022/2022-04/pageviews-20220417-010000.gz
You should use file.openRead().transform(utf8.decoder) instead of file.openRead().map(utf8.decode). (Also note the argument difference: utf8.decoder is a Utf8Decoder object, and utf8.decode is a method tear-off.)
The Stream.map documentation specifically discusses this:
Unlike transform, this method does not treat the stream as chunks of a single value. Instead each event is converted independently of the previous and following events, which may not always be correct. For example, UTF-8 encoding, or decoding, will give wrong results if a surrogate pair, or a multibyte UTF-8 encoding, is split into separate events, and those events are attempted encoded or decoded independently.
Is it possible to throw a fake stack trace in dart?
For example, I have this code:
final FakeStartContext startContext = FakeStartContext(
runOverride: () async => throw Exception(fakeError),
);
It is overriding a function by throwing a fake exceptionMsg to it. Is there a way to throw a fake stack trace in addition to the fake error as well? Like something similar to this:
final FakeStartContext startContext = FakeStartContext(
runOverride: () async => throw Exception(fakeError, fakeStackTrace),
);
Such that it could be caught by catch(err, stacktrace)?
You can, because your code is asynchronous:
final FakeStartContext startContext = FakeStartContext(
runOverride: () async =>
await Future.error(Exception(fakeError), fakeStackTrace),
);
or just
final FakeStartContext startContext = FakeStartContext(
runOverride: () => Future.error(Exception(fakeError), fakeStackTrace),
);
It's not currently possible to do the same thing in a synchronous setting.
It doesn't help to to create a future containing your fake stack trace, because you can't wait for that future synchronously.
As mentioned, an Error.throwWithStackTrace(error, stack) function is scheduled to be added in Dart 2.16, which can synchronously throw an object and a stack trace of your choice.
Why use a fake stacktrace when you can easily get a real one?
For example:
thrower() {
ghi() => throw Exception('error');
def() => ghi();
abc() => def();
abc();
}
void main() {
try {
thrower();
} catch (e, st) {
print('Error $e, StackTrace: $st');
}
}
This will print something like this on Dart VM:
Error Exception: error, StackTrace: #0 thrower.ghi (file:///Users/renato/example.dart:2:12)
#1 thrower.def (file:///Users/renato/example.dart:3:15)
#2 thrower.abc (file:///Users/renato/example.dart:4:15)
#3 thrower (file:///Users/renato/example.dart:5:6)
#4 main (file:///Users/renato/example.dart:10:5)
#5 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
#6 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
On dartpad.dev (JS) it prints this instead:
Error Exception: error, StackTrace: wrapException#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:363:17
throwExpression#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:377:15
call$0#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3040:16
call$0#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3046:23
call$0#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3052:23
thrower#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:272:65
main#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:277:11
#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3217:15
#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3200:15
dartProgram#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3211:5
#https://dartpad.dev/scripts/frame_dark.html line 19 > injectedScript:3219:3
replaceJavaScript#https://dartpad.dev/scripts/frame.js:19:19
messageHandler#https://dartpad.dev/scripts/frame.js:140:30
I want to generate a random BigInt with dynamik length Bits. I am using the pointycastle package to get a SecureRandom BigInt.
import 'package:pointycastle/pointycastle.dart';
void main(List<String> arguments) {
print(gen(500));
}
BigInt gen(int Bits) {
var n = BigInt.from(1);
var ran = SecureRandom('Fortuna');
n = ran.nextBigInteger(Bits);
return n;
}
This line throws an exception:
n = ran.nextBigInteger(Bits);
StateError (Bad state: AES engine not initialised)
This is the complete error in the console:
Unhandled exception:
Bad state: AES engine not initialised
#0 AESFastEngine.processBlock
package:pointycastle/block/aes_fast.dart:109
#1 BlockCtrRandom.nextUint8
package:pointycastle/random/block_ctr_random.dart:55
#2 SecureRandomBase._randomBits
package:pointycastle/…/impl/secure_random_base.dart:55
#3 SecureRandomBase.nextBigInteger
package:pointycastle/…/impl/secure_random_base.dart:33
#4 AutoSeedBlockCtrRandom.nextBigInteger.<anonymous closure>
package:pointycastle/random/auto_seed_block_ctr_random.dart:69
#5 AutoSeedBlockCtrRandom._autoReseedIfNeededAfter
package:pointycastle/random/auto_seed_block_ctr_random.dart:81
#6 AutoSeedBlockCtrRandom.nextBigInteger
package:pointycastle/random/auto_seed_block_ctr_random.dart:68
#7 FortunaRandom.nextBigInteger
package:pointycastle/random/fortuna_random.dart:46
#8 gen
bin\encrypt.dart:10
#9 main
bin\encrypt.dart:4
#10 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
I can't seem to find a solution to this error message anywhere else. I hope u guys can help me. :D
It is not clear but if I look at the examples from the project it seems like you need to call the seed method. The following works for me:
import 'dart:math';
import 'dart:typed_data';
import 'package:pointycastle/pointycastle.dart';
void main(List<String> arguments) {
print(gen(500));
}
BigInt gen(int bits) {
final _sGen = Random.secure();
var n = BigInt.from(1);
var ran = SecureRandom('Fortuna');
ran.seed(KeyParameter(
Uint8List.fromList(List.generate(32, (_) => _sGen.nextInt(255)))));
n = ran.nextBigInteger(bits);
return n;
}
The example I was inspired by: https://github.com/PointyCastle/pointycastle/blob/master/tutorials/examples/import-demo/import-demo-1.dart
Welcome to Stackoverflow.
When using any part of Pointycastle you do need to instantiate the implementation objects.
In your code you are using
var ran = SecureRandom('Fortuna');
that uses the SecureRandom class.
Simply add
final rnd = new SecureRandom("AES/CTR/PRNG");
and kindly see the PointCastle SecureRandom example for further questions:
https://github.com/PointyCastle/pointycastle/blob/master/test/random/block_ctr_random_test.dart
I am getting this exception when I close the pool very soon after closing a query:
Uncaught Error: Bad state: Cannot write to socket, it is closed
Stack Trace:
#0 BufferedSocket.writeBufferPart (package:sqljocky/src/buffered_socket.dart:114:7)
#1 BufferedSocket.writeBuffer (package:sqljocky/src/buffered_socket.dart:108:27)
#2 _Connection._sendBufferPart (package:sqljocky/src/connection.dart:261:31)
#3 _Connection._sendBuffer (package:sqljocky/src/connection.dart:249:29)
#4 _Connection.processHandler (package:sqljocky/src/connection.dart:289:16)
#5 ConnectionPool._closeQuery.<anonymous closure> (package:sqljocky/src/connection_pool.dart:220:29)
#6 _rootRunUnary (dart:async/zone.dart:730)
#7 _RootZone.runUnary (dart:async/zone.dart:864)
#8 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488)
#9 _Future._propagateToListeners (dart:async/future_impl.dart:571)
#10 _Future._completeWithValue (dart:async/future_impl.dart:331)
#11 _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:393)
#12 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#13 _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128)
Unhandled exception:
Bad state: Cannot write to socket, it is closed
#0 _rootHandleUncaughtError.<anonymous closure>.<anonymous closure> (dart:async/zone.dart:713)
#1 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#2 _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#3 _asyncRunCallback (dart:async/schedule_microtask.dart:36)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128)
the issue seems to be that the query close fires of a Future internally, so the close() function returns before the close is actually finished:
void _closeQuery(Query q, bool retain) {
_log.finest("Closing query: ${q.sql}");
for (var cnx in _pool) {
var preparedQuery = cnx.removePreparedQueryFromCache(q.sql);
if (preparedQuery != null) {
_waitUntilReady(cnx).then((_) {
_log.finest("Connection ready - closing query: ${q.sql}");
var handler = new _CloseStatementHandler(preparedQuery.statementHandlerId);
cnx.autoRelease = !retain;
cnx.processHandler(handler, noResponse: true);
});
}
}
}
The pool close happens immediately, it closes the socket right away. This means the query close (which is delayed till after the pool close due to the Future) fails, unable to send whatever information it needs to through the socket. I've opened a ticket to sqljocky at https://github.com/jamesots/sqljocky/issues/44 but I've received no replies, and I need a workaround if it's going to take a while to get a response.
This code has allowed me to replicate the issue 100% of the time:
Future _putMethod(RestRequest request) {
return new Future.sync(() {
mysql.ConnectionPool pool = getConnectionPool();
return pool.prepare("SELECT * FROM files").then((mysql.Query query) {
return query.execute().then((result) {
// Do something?
}).then((_) {
this._log.info("Closing");
query.close();
});
}).then((_) {
pool.close();
});
});
}
This is yet more a question than an answer but I can't put this code in a comment in a usable way.
You should ensure that you return the Future returned from every async invocation.
I don't know if the lines where I added the comment // added return are async invocations.
Can you please try and give feedback if this changes anything.
Future _putMethod(RestRequest request) {
return new Future.sync(() {
mysql.ConnectionPool pool = getConnectionPool();
return pool.prepare("SELECT * FROM files").then((mysql.Query query) {
return query.execute().then((result) {
// Do something? // also ensure that a Future of an async invocation is returned
}).then((_) {
this._log.info("Closing");
return query.close(); // added return
});
}).then((_) {
return pool.close(); // added return
});
});
}
I made a simple web server but it crashes every time I am refreshing page many times in a short time. I just enter 127.0.0.1:8080 in my browser and then spam with F5. Here is the code to reproduce this issue:
void main()
{
HttpServer server = new HttpServer();
server.addRequestHandler((req) => true, handleGET);
server.listen('127.0.0.1', 8080);
}
void handleGET(HttpRequest req, HttpResponse res)
{
var requestedFile = ".${req.path}";
if(req.path == "/")
{
requestedFile = requestedFile.concat("index.html");
}
File file = new File(requestedFile);
file.exists().then((bool found) {
if(found)
{
file.openInputStream().pipe(res.outputStream);
}
else
{
res.statusCode = HttpStatus.NOT_FOUND;
res.outputStream.close();
}
});
}
The error I get is following:
Unhandled exception:
StreamException: Stream closed
#0 _SocketOutputStream._write (dart:io:6017:30)
#1 _HttpResponse._writeHeader (dart:io:5981:18)
#2 _HttpRequestResponseBase._ensureHeadersSent (dart:io:2696:19)
#3 _HttpResponse._streamClose (dart:io:2921:23)
#4 _HttpOutputStream.close (dart:io:3078:36)
#5 _pipe.<anonymous closure> (dart:io:6271:28)
#6 _BaseDataInputStream._checkScheduleCallbacks.issueCloseCallback (dart:io:6231:59)
#7 _Timer._createTimerHandler._handleTimeout (dart:io:6804:28)
#8 _Timer._createTimerHandler._handleTimeout (dart:io:6812:7)
#9 _Timer._createTimerHandler.<anonymous closure> (dart:io:6820:23)
#10 _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
Often before this mayor exception I receive a bunch of warnings like WSASend failed: 10053 but those don't crash the server. I work on Windows if this problem is related to some specific implementation.
Because you are hitting reload very quickly, your code ends up trying to write to a socket that has already been closed. Hence, you should probably catch StreamException and just ignore it. It could be argued that the io library should help you out a bit more. I just filed this bug:
http://code.google.com/p/dart/issues/detail?id=7334&thanks=7334&ts=1355280746