ByteCode.News
Submit RSS Atom Sign in

Back to the channels

#java

  1. pr3d4t0r :: yawn ::
  2. pr3d4t0r Good morning.
  3. pr3d4t0r dreamreal!! Come to #cime or the other channel when you have a moment?
  4. dreamreal aaaaand nevet should be back. Still finding transaction leaks. :/
  5. dreamreal still muted HERE, though. :D
  6. dreamreal https://bytecode.news/posts/2026/04/boilerplate-three-ways
  7. javabot dreamreal's title: "Boilerplate Three Ways | bytecode.news"
  8. mawk hi
  9. mawk is this calling 'new' under the hood? byte[] a = {1, 2, 3};
  10. mawk i.e. is this equivalent to byte[] a = new byte[3]; a[0] = 1; a[1] = 2; a[2] = 3;
  11. DoofusCanadensis doubt it
  12. DoofusCanadensis byte != Byte
  13. mawk I'm writing JavaCard and need to avoid 'new' as much as possible
  14. jbosmans if implementation details like that matter a lot i'd suggest investigating how to validate answers to such questions, i'm thinking it'll lead to reading bytecode (aka jvm assembly)
  15. jbosmans which i never do because i don't work at that level of detail
  16. Chronos mawk: Yes, as far as I know, it's identical to: byte[] a = new byte[]{1, 2, 3};
  17. Chronos So I believe it does indeed call "new".
  18. Chronos (Corrections welcome.)
  19. ernimril Chronos, compile it and then check with javap
  20. Chronos In Java, every array is a full-fledged object.
  21. mawk hmmm
  22. jbosmans what ernimril said
  23. mawk thanks
  24. sonOfRa mawk: byte[] a = {1, 2, 3};
  25. sonOfRa oops
  26. sonOfRa mawk: https://javap.yawk.at/#lnBjVV
  27. nevet javap pastebin
  28. jbosmans also, https://godbolt.org ftw
  29. nevet Compiler Explorer
  30. sonOfRa true, but javap.yawk.at is made by (former) channel regular yawkat!
  31. jbosmans kudos for that ^
  32. mawk ah yes indeed sonOfRa , thanks!
  33. jbosmans as far as i can see creating a new array doesn't create a new object
  34. jbosmans static int[] square() { return new int[]{9999}; }
  35. ernimril jbosmans, what about the newarray ?
  36. jbosmans 0: iconst_1
  37. jbosmans 1: newarray int
  38. jbosmans 3: dup
  39. jbosmans 4: iconst_0
  40. jbosmans 5: sipush 9999
  41. mawk well it calls "newarray byte" so it must be allocating it on the heap
  42. mawk so I should do that only in the constructor of my applet so it's only called once
  43. mawk I can technically trigger the garbage collector but it has to be done manually and it's not portable, not every card has it
  44. jbosmans thanks for unbanning :)
  45. jbosmans mawk, there's the epsilon gc which doesn't do anything iirc, might be useful for measuring/.. to accomplish your goals
  46. mawk ah yeah, I can run this on a simulator on a pc
  47. jbosmans should you want to invest in that approach
  48. mawk thanks
  49. jbosmans mawk, assuming sarcasm, understand what you mean (i think). You could make it runnable on a pc if you can separate the dependencies from the code that's actually doing the work (or the work you care about)
  50. jbosmans overlapping msgs:)
  51. jreicher mawk: are you really assigning the byte array something known at compile time? Or is that just how you wrote the example?
  52. mawk yes it's known at compile time
  53. mawk maybe if I set it as "final" it won't make an allocation
  54. jbosmans afaik object instantiations are extremely cheap on jvm
  55. ernimril it is on all modern jvms, yes, has been so for a long time. Also constants are quite often inlined by the compiler
  56. jbosmans ~jep 519
  57. javabot 'JEP 519: Compact Object Headers' can be found at http://openjdk.java.net/jeps/519
  58. nevet JEP 519: Compact Object Headers
  59. jbosmans also, it was pretty cheap before, too
  60. jbosmans (before java 25)
  61. jbosmans also, up to recently, i compiled some tools using docker to an ubuntu 18/debian 10 compatible binary
  62. jbosmans which really made a difference for cmdline tools
  63. jbosmans fwiw it was compatible by compiling in dedicated docker containers, i just upgraded the container OS versions
  64. jbosmans iirc i got java+spring+jdbc+postgres+code down to ~60mb, which was nice
  65. jreicher mawk: if it's known at compile time then declare it static and don't worry about the new. At worst it will only be done once, but perhaps not at all.
  66. jbosmans UPX compression could drive that down substantially, but that would take longer to execute (because of decompression)
  67. jreicher A good rule of thumb IMO is to always tell the compiler everything you know.
  68. mawk jbosmans: this is running on a smartcard, it's a a very small and slow microcontroller
  69. mawk with just a couple hundred bytes of RAM to spare, maybe a thousand
  70. jbosmans mawk, great to be working on that :-)
  71. jbosmans i actually recognized JavaCard
  72. jreicher Is it possible Java is not the best language for that kind of target? (Honest question; I have no experience in this area)
  73. mawk well there's no choice, java is all they run; in your credit card, your epassport, your SIM card
  74. mawk you can sometimes run C but you need to sell your soul for a NDA
  75. jbosmans mawk, which context? eid or alike?
  76. mawk I'm working on a PKI smartcard
  77. jbosmans sweet, iirc our eid runs JavaCard too
  78. mawk java is actually a sane choice for the application, the JVM is fully controlled by the OS and can apply all the static checks and security mitigations they want
  79. jbosmans crazy stuff
  80. mawk they can do stuff like rolling back transactions in case of tearing, skewing the clock inbetween instructions or running instructions several times to throw off side channel attacks; and also exceptions are an integral part of how the card communicates with the reader, that's how you set the APDU status codes
  81. jbosmans mawk, what jdk version are you working in?
  82. mawk it's javacard 3.0.5
  83. mawk compiling with jdk 17
  84. jbosmans that's not too bad for such a restricted context
  85. jbosmans iirc records are already supported
  86. jbosmans best use those wherever they make sense
  87. jbosmans because both the compiler and jvm will know they're mean to be (shallowly) immutable
  88. jbosmans alas way post midnight, sweet dreams
  89. dreamreal for javacard, eh
  90. dreamreal That's a little surprising
  91. mawk why?
  92. dreamreal javacard's not very common in the wild. What are you deploying it to?
  93. mawk I'm currently trying to add remote management capabilities with public key crypto
  94. mawk like SIM cards have
  95. mawk then you can open a secure channel with the card even through an untrusted network
  96. mawk then the card will serve as a secure element to hold keys for mTLS or other things, in an embedded product, in SIM format
  97. mawk and it's running on a JCOP 4 smartcard from NXP
  98. dreamreal *nod* nice!
  99. mawk I have half of the secure channel thing working, I can request AES session keys from the card and then continue with AES and CMAC
  100. mawk but now I need to wrap the AES keys with a public key instead of transmitting them in cleartext
  101. dreamreal so I have VERY VERY VERY little experience with that profile, but in general you avoid allocations like the plague: new is fine *but* you reuse *everything* you can, so if you allocate an array, you reuse that array for the lifetime of the application
  102. mawk yeah indeed
  103. dreamreal It's like you're writing in MUMPS except with java syntax, wooo
  104. mawk I can request transient arrays which get cleared either on deselect or on reset (like if the application is selected several times on different logical channels or different interfaces like contact vs contactless)
  105. mawk but then it's not with new
  106. dreamreal I don't know if there's a GC on that profile at all: you'd want to allocate and preserve
  107. dreamreal unless things have changed! Like I said, I've got VERY little experience here, like "ooo I tried it once"
  108. mawk yeah some cards do have a GC but not all of them
  109. mawk some cards even have ints and not just bytes and shorts
  110. mawk luxurious
  111. mawk and you need to invoke the GC manually, and it will maybe run next time a command is received
  112. dreamreal I think I last did javacard on a lark for javaone 2007? maybe 2008?
  113. mawk ah yeah the standards were quite different then
  114. mawk I think the standard was still to use the VISA platform manager instead of being the globalplatform thing of now
  115. dreamreal Gosh, I don't remember that at all - we had this physical connection to the board, I don't even remember what it was called to install the application
  116. dreamreal I remember having a GPS available, which I thought was going to be great for basically building the apple tags using java
  117. dreamreal like how train car locators work: when connected to an open network, send an identifier ("package XYZ is at X:Y at 23:01")