unsafe

Writing to multiple bytes efficiently in Rust

旧城冷巷雨未停 提交于 2019-12-10 15:35:43
问题 I'm working with raw pointers in Rust and am trying to copy an area of memory from one place to another. I've got it successfully copying memory over, but only using a for loop and copying each byte individually using an offset of the pointer. I can't figure out how to do this more efficiently, i.e. as a single copy of a string of bytes, can anyone point me in the right direction? fn copy_block_memory<T>(src: *const T, dst: *mut u8) { let src = src as *const u8; let size = mem::size_of::<T>()

What are the possible consequences of using unsafe conversion from []byte to string in go?

霸气de小男生 提交于 2019-12-10 10:47:06
问题 The preferred way of converting []byte to string is this: var b []byte // fill b s := string(b) In this code byte slice is copied, which can be a problem in situations where performance is important. When performance is critical, one can consider performing the unsafe conversion: var b []byte // fill b s := *(*string)(unsafe.Pointer(&b)) My question is: what can go wrong when using the unsafe conversion? I known that string should be immutable and if we change b , s will also be changed. And

Where to set unsafe mode in Safari 10 to made java plugin access local file

会有一股神秘感。 提交于 2019-12-10 10:11:26
问题 I just updated my Safari from Safari 9 to safari 10 beta version. My client is a java applet runs from a page. In Safari 9 and before version, it runs OK. But in Safari 10 beta, it fails. I found that safari 10 blocks access to local files from Java applet. So I think it is because the java plugin runs under safe mode. But in Safari ->preferences->security->Plug-in Settings. there is no unsafe mode option to check. there is only three option :Ask,Off,On. So my client can not access local file

What are the differences between `*const T` and *mut T` raw pointers?

不羁岁月 提交于 2019-12-10 03:56:14
问题 I'm writing some unsafe Rust code so I need to know the exact differences between *const T and *mut T . I assumed that it's like &T and &mut T (i.e. you just can't mutate T through &T , period), but that doesn't seem to be the case! For example, the pointer wrapper NonNull<T> is defined as follows (source): pub struct NonNull<T: ?Sized> { pointer: *const T, } However, it's possible to obtain a *mut T from this wrapper via as_ptr, which is just defined as: pub const fn as_ptr(self) -> *mut T {

Is mem::forget(mem::uninitialized()) defined behavior?

ε祈祈猫儿з 提交于 2019-12-10 01:26:58
问题 In mutagen, I'm injecting various mutations in the code. One thing I'd like to mutate is the pattern if let Ok(x) = y { .. } . However, this poses quite the challenge, as I cannot know the type of y – the user could have built their own enum with an unary Ok variant. I can still opportunistically mutate it for cases where we actually have a Result whose error type implements Default using a trait that looks like the following simplified: #![feature(specialization)] pub trait Errorer { fn err

Unsafe C# trick to improve speed

空扰寡人 提交于 2019-12-09 14:40:29
问题 I am not used to code with pointers (e.g. C++), nor with unsafe islands: only "safe" C#. Now I'd like to implement a function in C# for the .Net Micro Framework, where the compactness and the performance are very important. Basically, I would to collect 4-ples of shorts and thus fill a buffer (e.g. byte-array). Let's say that every sample is such: struct MyStruct { public short An1; public short An2; public short An3; public short An4; } Each sample is collected via a timer-event, so that I

How do I get the instance of sun.misc.Unsafe?

百般思念 提交于 2019-12-09 14:21:54
问题 How do I get the instance of the unsafe class? I always get the security exception. I listed the code of the OpenJDK 6 implementation. I would like to mess around with the function sun.misc.Unsafe offers to me, but I always end up getting the SecurityException("Unsafe") . public static Unsafe getUnsafe() { Class cc = sun.reflect.Reflection.getCallerClass(2); if (cc.getClassLoader() != null) throw new SecurityException("Unsafe"); return theUnsafe; } (Please don't try to tell me how unsafe it

Unsafe Code Compilation error in .Net Core even after setting allowunsafe flag to true in project.json

白昼怎懂夜的黑 提交于 2019-12-09 08:23:30
问题 I am using some unsafe code in my .Net core App. For that i had made this change in the project.json file "compilationOptions": { "allowUnsafe": true, } Still this error comes error CS0227: Unsafe code may only appear if compiling with /unsafe I had already gone through this already Unsafe code won't compile on Visual Studio 2015 How to call unsafe code from ASP.NET xproj 回答1: Change compilationOptions to buildOptions : "buildOptions": { "allowUnsafe": true } 回答2: In the newer *.csproj files,

Does unsafe code have any effect on safe code?

a 夏天 提交于 2019-12-08 14:47:39
问题 As I understand it, marking an method as unsafe will disable some of the CLR checks on that code, but does this have any effect on the rest of the system which is safe, other than the fact that the DLL/EXE can not run in a untrusted environment. In particular, Are they are any safety checks that will not work on the complete dll because it is marked as unsafe? If a DLL is marked as unsafe, but the methods marked as unsafe are not actually called, is this the same as if the DLL is marked as

Is it possible to call constructor on existing instance?

非 Y 不嫁゛ 提交于 2019-12-08 10:52:45
问题 It is known that using sun.misc.Unsafe#allocateInstance one can create an object without calling any class constructors. Is it possible to do the opposite: given an existing instance, invoke a constructor on it? Clarification : this is not the question about something I'd do in production code. I'm curious about JVM internals and crazy things that can still be done. Answers specific to some JVM version are welcome. 回答1: JVMS §2.9 forbids invocation of constructor on already initialized